-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
237 lines (210 loc) · 5.78 KB
/
main.go
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"context"
"fmt"
"log"
"os"
"slices"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
type Label struct {
Name string
}
type LabelConnection struct {
Nodes []Label
}
type Repository struct {
Name string
}
type Actor struct {
Login string
}
type IssueTimelineItems struct {
CrossReferencedEvent struct {
ReferencedSubject struct {
PullRequest struct {
Title string
Author Actor
Number int
Merged bool
Repository Repository
} `graphql:"... on PullRequest"`
} `graphql:"source"`
} `graphql:"... on CrossReferencedEvent"`
}
type IssueTimelineItemsConnection struct {
Nodes []IssueTimelineItems
}
type ProjectV2Item struct {
Content struct {
Issue struct {
Title string
Number int
Labels LabelConnection `graphql:"labels(first: 10)"`
TimelineItems IssueTimelineItemsConnection `graphql:"timelineItems(first: 100, itemTypes: $timelineItemsTypes)"`
} `graphql:"... on Issue"`
}
}
type ProjectV2ItemConnection struct {
Nodes []ProjectV2Item
}
type ProjectPullRequestsQuery struct {
Organization struct {
ProjectV2 struct {
Items ProjectV2ItemConnection `graphql:"items(first: $maxItemCount)"`
} `graphql:"projectV2(number: $projectId)"`
} `graphql:"organization(login: $login)"`
}
type ReleaseNoteItem struct {
issueTitle string
prShortLinks []string
authors []string
}
var cmd = &cobra.Command{
Use: "./gh-release-note-generator -o 'organization' -r 'repository' -p 'project' -l 'label name'",
Version: "0.0.1",
Run: func(cmd *cobra.Command, args []string) {
token, err := cmd.Flags().GetString("token")
if err != nil {
log.Fatal(err)
}
if len(token) == 0 {
token = os.Getenv("GITHUB_ACCESS_TOKEN")
}
if len(token) == 0 {
log.Fatal(fmt.Errorf("missing GitHub Access Token"))
}
project, err := cmd.Flags().GetInt("project")
if err != nil {
log.Fatal(err)
}
orginization, err := cmd.Flags().GetString("organization")
if err != nil {
log.Fatal(err)
}
repository, err := cmd.Flags().GetString("repository")
if err != nil {
log.Fatal(err)
}
labels, err := cmd.Flags().GetStringArray("labels")
if err != nil {
log.Fatal(err)
}
maxItemCount, err := cmd.Flags().GetInt("max-item-count")
if err != nil {
log.Fatal(err)
}
excludeLabels, err := cmd.Flags().GetStringArray("exclude-labels")
if err != nil {
log.Fatal(err)
}
generateGitHubReleaseNote(token, project, orginization, repository, labels, excludeLabels, maxItemCount)
},
}
func init() {
cobra.OnInitialize()
cmd.PersistentFlags().StringP("token", "t", "", "GitHub access token")
cmd.PersistentFlags().StringP("organization", "o", "", "Organization")
cmd.MarkPersistentFlagRequired("organization")
cmd.PersistentFlags().IntP("project", "p", 1, "Target GitHub Project ID")
cmd.MarkPersistentFlagRequired("project")
cmd.PersistentFlags().StringP("repository", "r", "", "Target repository")
cmd.MarkPersistentFlagRequired("repository")
cmd.PersistentFlags().StringArrayP("labels", "l", []string{}, "Target issue labels")
cmd.MarkPersistentFlagRequired("labels")
cmd.PersistentFlags().IntP("max-item-count", "i", 100, "Max item count in target Project")
cmd.PersistentFlags().StringArray("exclude-labels", []string{}, "Exclude issue labels")
}
func main() {
cmd.Execute()
}
func generateGitHubReleaseNote(
token string,
projectId int,
organization string,
repository string,
labels []string,
excludeLabels []string,
maxItemCount int,
) {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
httpClient := oauth2.NewClient(context.Background(), src)
graphQLClient := githubv4.NewClient(httpClient)
variables := map[string]interface{}{
"login": githubv4.String(organization),
"projectId": githubv4.Int(projectId),
"maxItemCount": githubv4.Int(maxItemCount),
"timelineItemsTypes": []githubv4.IssueTimelineItemsItemType{githubv4.IssueTimelineItemsItemTypeCrossReferencedEvent},
}
var query ProjectPullRequestsQuery
err := graphQLClient.Query(context.Background(), &query, variables)
if err != nil {
log.Fatal(err)
}
releaseNoteItems := make(map[string][]ReleaseNoteItem)
for _, node := range query.Organization.ProjectV2.Items.Nodes {
label := ""
skip := false
for _, l := range node.Content.Issue.Labels.Nodes {
if slices.Contains(excludeLabels, l.Name) {
skip = true
break
}
if slices.Contains(labels, l.Name) {
label = l.Name
break
}
}
if skip || len(label) == 0 {
continue
}
prLinks := []string{}
authors := []string{}
for _, node := range node.Content.Issue.TimelineItems.Nodes {
pr := node.CrossReferencedEvent.ReferencedSubject.PullRequest
repo := pr.Repository
if repo.Name == repository && pr.Merged {
number := pr.Number
shortLink := "#" + strconv.Itoa(number)
prLinks = append(prLinks, shortLink)
author := "@" + pr.Author.Login
if !slices.Contains(authors, author) {
authors = append(authors, author)
}
}
}
if len(prLinks) == 0 {
continue
}
item := ReleaseNoteItem{
issueTitle: node.Content.Issue.Title,
prShortLinks: prLinks,
authors: authors,
}
if _, ok := releaseNoteItems[label]; !ok {
releaseNoteItems[label] = []ReleaseNoteItem{}
}
releaseNoteItems[label] = append(releaseNoteItems[label], item)
}
releaseNote := ""
for _, label := range labels {
items := releaseNoteItems[label]
if len(items) == 0 {
continue
}
releaseNote += "## " + label + "\n"
for _, item := range items {
authors := strings.Join(item.authors, ", ")
links := strings.Join(item.prShortLinks, ", ")
releaseNote += fmt.Sprintf("* %s by %s in %s\n", item.issueTitle, authors, links)
}
releaseNote += "\n"
}
fmt.Println(releaseNote)
}