~sungo/ghch

8783878d3803ef4d005d472e7b5f431d371ed249 — sungo 3 months ago 9d11253
move the default template content to a single external file
3 files changed, 86 insertions(+), 60 deletions(-)

A default.tmpl
M main.go
D templates.go
A default.tmpl => default.tmpl +57 -0
@@ 0,0 1,57 @@
{{- define "TLDR"}}
{{- range . }}
* [PR #{{.Number}}]({{.URL}}) - {{.Title}}
{{- end}}
{{- end}}
{{- /* *** */}}
{{- $bugs := index .Buckets "bug" }}
{{- $features := index .Buckets "features" }}
{{- $docs := index .Buckets "docs" }}
{{- $techdebt := index .Buckets "tech-debt" }}
{{- $unmatched := index .Buckets "__unmatched" }}
# Summary

{{- if len $bugs }}

## Bugs Fixed
{{ template "TLDR" $bugs}}
{{- end }}
{{- if len $features }}

## New Features
{{ template "TLDR" $features}}
{{- end }}
{{- if len $docs }}

## Documentation Updates
{{ template "TLDR" $docs}}
{{- end }}
{{- if len $techdebt }}

## Technical Debt
{{ template "TLDR" $techdebt}}
{{- end }}

{{- if len $unmatched }}

## Everything Else
{{ template "TLDR" $unmatched}}
{{- end }}

- - -

# Details
{{ range .PRs }}
## [PR #{{.Number}}]({{.URL}}) - {{.Title}}

* Labels: {{ range .Labels.Nodes }}{{.Name}} {{- end}}
* Created At {{.CreatedAt}}
* Merged At {{.MergedAt}}

* Author: {{.Author.Login}}
* Merged By: {{.MergedBy.Login}}

{{.Body}}
{{ end }}



M main.go => main.go +29 -39
@@ 4,6 4,7 @@
package main

import (
	_ "embed"
	"flag"
	"fmt"
	"os"


@@ 18,6 19,14 @@ const (
	API = "https://api.github.com/graphql"
)

//go:embed "default.tmpl"
var defaultTemplate string

type TemplatePayload struct {
	Buckets map[string][]GqlPR
	PRs     []GqlPR
}

var Flags struct {
	Token  string
	Owner  string


@@ 96,24 105,30 @@ func main() {
		os.Exit(1)
	}

	fmt.Println("# Summary")
	tldr(gh.Data.Repo.PullRequests.Nodes, strings.Split(Flags.Labels, ","))
	fmt.Println()
	fmt.Println("- - -")
	fmt.Println()
	fmt.Println("# Details")
	longForm(gh.Data.Repo.PullRequests.Nodes)
}
	buckets := bucketize(
		gh.Data.Repo.PullRequests.Nodes,
		strings.Split(Flags.Labels, ","),
	)

func longForm(prs []GqlPR) {
	prTemp, _ := template.New("pr long").Parse(prLongTemplate)
	for _, pr := range prs {
		prTemp.Execute(os.Stdout, pr)
	fullTemp, err := template.New("full").Parse(defaultTemplate)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to parse template: %s", err)
		os.Exit(1)
	}
	err = fullTemp.Execute(os.Stdout, TemplatePayload{
		Buckets: buckets,
		PRs:     gh.Data.Repo.PullRequests.Nodes,
	})

	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to process template: %s", err)
		os.Exit(1)
	}

}

////////////////////
func tldr(prs []GqlPR, labels []string) {
func bucketize(prs []GqlPR, labels []string) map[string][]GqlPR {

	tags := make(map[string][]GqlPR)
	tags["__unmatched"] = make([]GqlPR, 0)


@@ 137,32 152,7 @@ func tldr(prs []GqlPR, labels []string) {
			)
		}
	}

	temp, _ := template.New("tldr section").Parse(tldrSectionTemplate)
	if len(tags["bug"]) > 0 {
		fmt.Println("## Bugs Fixed")
		temp.Execute(os.Stdout, tags["bug"])
	}

	if len(tags["feature"]) > 0 {
		fmt.Println("\n## Features")
		temp.Execute(os.Stdout, tags["feature"])
	}

	if len(tags["docs"]) > 0 {
		fmt.Println("\n## Documentation Updates")
		temp.Execute(os.Stdout, tags["docs"])
	}

	if len(tags["tech-debt"]) > 0 {
		fmt.Println("\n## Tech Debt")
		temp.Execute(os.Stdout, tags["tech-debt"])
	}

	if len(tags["__unmatched"]) > 0 {
		fmt.Println("\n## Everything Else")
		temp.Execute(os.Stdout, tags["__unmatched"])
	}
	return tags
}

func contains(labelNodes []GqlLabel, str string) bool {

D templates.go => templates.go +0 -21
@@ 1,21 0,0 @@
package main

const tldrSectionTemplate = `
{{- range . }}
* [PR #{{.Number}}]({{.URL}}) - {{.Title}}
{{- end}}
`

const prLongTemplate = `
## [PR #{{.Number}}]({{.URL}}) - {{.Title}}

* Labels: {{- range .Labels.Nodes }}{{.Name}} {{- end}}
* Created At {{.CreatedAt}}
* Merged At {{.MergedAt}}

* Author: {{.Author.Login}}
* Merged By: {{.MergedBy.Login}}

{{.Body}}

`