~sungo/ghch

4d0ccf4b5b19d773b98802b77c9f9e0e878b671c — sungo 3 months ago 8783878
allow the user to provide a custom template
1 files changed, 64 insertions(+), 18 deletions(-)

M main.go
M main.go => main.go +64 -18
@@ 7,6 7,7 @@ import (
	_ "embed"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
	"text/template"


@@ 28,21 29,26 @@ type TemplatePayload struct {
}

var Flags struct {
	Token  string
	Owner  string
	Repo   string
	API    string
	Labels string
	Token        string
	Owner        string
	Repo         string
	API          string
	Labels       string
	TemplateFile string
}

func main() {
	var (
		gh     GqlResponse
		token  = os.Getenv("GITHUB_API_TOKEN")
		owner  = os.Getenv("OWNER")
		repo   = os.Getenv("REPO")
		api    = os.Getenv("API")
		labels = os.Getenv("LABELS")
		gh       GqlResponse
		fullTemp *template.Template
		err      error

		token        = os.Getenv("GITHUB_API_TOKEN")
		owner        = os.Getenv("OWNER")
		repo         = os.Getenv("REPO")
		api          = os.Getenv("API")
		labels       = os.Getenv("LABELS")
		templateFile = os.Getenv("TEMPLATE_FILE")
	)

	if Flags.Token == "" {


@@ 83,19 89,48 @@ func main() {
		}
	}

	if Flags.TemplateFile == "" {
		if templateFile != "" {
			Flags.TemplateFile = templateFile
		}
	}

	if Flags.API == "" {
		if api != "" {
			Flags.API = api
		}
	}

	///////
	if Flags.TemplateFile != "" {
		temp, err := readFile(Flags.TemplateFile)
		if err != nil {
			fmt.Fprintf(
				os.Stderr,
				"Could not process %s: %s",
				Flags.TemplateFile,
				err,
			)
		}
		fullTemp, err = template.New("full").Parse(temp)
	} else {
		fullTemp, err = template.New("full").Parse(defaultTemplate)
	}

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

	///////

	query := fmt.Sprintf(
		GqlQuery,
		Flags.Owner,
		Flags.Repo,
	)

	_, err := sling.New().Post(API).
	_, err = sling.New().Post(API).
		Set("Authorization", "bearer "+Flags.Token).
		BodyJSON(GqlRequest{Query: query}).
		ReceiveSuccess(&gh)


@@ 105,16 140,12 @@ func main() {
		os.Exit(1)
	}

	///////

	buckets := bucketize(
		gh.Data.Repo.PullRequests.Nodes,
		strings.Split(Flags.Labels, ","),
	)

	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,


@@ 170,5 201,20 @@ func init() {
	flag.StringVar(&Flags.Repo, "repo", "", "Github repo name")
	flag.StringVar(&Flags.API, "api", API, "Url to Github API")
	flag.StringVar(&Flags.Labels, "labels", "bug,feature,docs,tech-debt", "Comma-separated list of labels to call out")
	flag.StringVar(&Flags.TemplateFile, "template", "", "Custom template")
	flag.Parse()
}

func readFile(fileName string) (string, error) {
	file, err := os.Open(fileName)
	if err != nil {
		return "", err
	}
	defer file.Close()

	b, err := ioutil.ReadAll(file)
	if err != nil {
		return "", err
	}
	return string(b), nil
}