~sungo/ghch

ghch/main.go -rw-r--r-- 2.7 KiB
18942019 — sungo include the license in the readme 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Original code by sungo ( https://sungo.io ).
// Licensed under the 0BSD License. See the included LICENSE file.

package main

import (
	"fmt"
	"os"
	"text/template"

	"github.com/dghubble/sling"
	_ "github.com/joho/godotenv/autoload"
)

const (
	API = "https://api.github.com/graphql"
)

func main() {
	var (
		gh    GqlResponse
		token = os.Getenv("GITHUB_API_TOKEN")
		owner = os.Getenv("OWNER")
		repo  = os.Getenv("REPO")
		api   = os.Getenv("API")
	)

	if token == "" {
		fmt.Println("Please set GITHUB_API_TOKEN in your env")
		os.Exit(1)
	}

	if owner == "" {
		fmt.Println("Please set OWNER in your env")
		os.Exit(1)
	}

	if repo == "" {
		fmt.Println("Please set REPO in your env")
		os.Exit(1)

	}

	if api == "" {
		api = API
	}

	query := fmt.Sprintf(
		GqlQuery,
		owner,
		repo,
	)

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

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Println("# Summary")
	tldr(gh.Data.Repo.PullRequests.Nodes)
	fmt.Println()
	fmt.Println("- - -")
	fmt.Println()
	fmt.Println("# Details")
	longForm(gh.Data.Repo.PullRequests.Nodes)
}

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}}

`

func longForm(prs []GqlPR) {
	prTemp, _ := template.New("pr long").Parse(prLongTemplate)
	for _, pr := range prs {
		prTemp.Execute(os.Stdout, pr)
	}
}

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

func tldr(prs []GqlPR) {
	var (
		bugs     = make([]GqlPR, 0)
		techDebt = make([]GqlPR, 0)
		features = make([]GqlPR, 0)
		misc     = make([]GqlPR, 0)
	)

	for _, pr := range prs {
		if contains(pr.Labels.Nodes, "bug") {
			bugs = append(bugs, pr)
		} else if contains(pr.Labels.Nodes, "feature") {
			features = append(features, pr)
		} else if contains(pr.Labels.Nodes, "tech-debt") {
			techDebt = append(techDebt, pr)
		} else {
			misc = append(misc, pr)
		}
	}

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

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

	if len(techDebt) > 0 {
		fmt.Println("\n## Tech Debt")
		temp.Execute(os.Stdout, techDebt)
	}

	if len(misc) > 0 {
		fmt.Println("\n## Everything Else")
		temp.Execute(os.Stdout, misc)
	}
}

func contains(labelNodes []GqlLabel, str string) bool {
	for _, node := range labelNodes {
		if node.Name == str {
			return true
		}
	}
	return false
}