-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
320 lines (273 loc) · 7.16 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/google/go-github/github"
ga "github.com/sethvargo/go-githubactions"
"golang.org/x/oauth2"
)
type tagData struct {
tagName string
link string
publishedAt *github.Timestamp
}
type issuesData struct {
title string
issueNumber int
link string
}
type pullsData struct {
title string
prNumber int
link string
assigneeUser string
assigneeLink string
}
type changelog struct {
owner string
repoName string
previousRelease tagData
nextRelease tagData
closedIssues []issuesData
mergedPulls []pullsData
}
type filterData struct {
client *github.Client
previousRelease tagData
nextRelease tagData
owner string
repoName string
}
const (
fileName = "CHANGELOG.md"
closedIssues = "\n\n**Closed issues:**\n"
mergedPR = "\n\n**Merged pull requests:**\n"
)
var (
changelogTitle = "# Changelog\n"
title = "\n## [%s](%s) (%s)"
fullChangelog = "\n\n[Full Changelog](https://github.com/%v/%v/compare/%v...%v)"
issueTemplate = "\n- %s [#%v](%s)"
prTemplate = "\n- %s [#%v](%s) ([%s](%s))"
token = ga.GetInput("token")
repo = ga.GetInput("repo")
ctx = context.Background()
)
func main() {
client := setupClient()
if token == "" || repo == "" {
ga.Fatalf("missing required inputs")
}
ga.AddMask(token)
splitRepo := strings.Split(repo, "/")
owner := splitRepo[0]
repoName := splitRepo[1]
previousRelease := getPreviousRelease(client, owner, repoName)
nextRelease := getNextRelease(client, owner, repoName)
reqData := filterData{
client,
previousRelease,
nextRelease,
owner,
repoName,
}
closedIssues := filterIssues(reqData)
mergedPulls := filterPulls(reqData)
changelogData := changelog{
owner,
repoName,
previousRelease,
nextRelease,
closedIssues,
mergedPulls,
}
generateChangelog(changelogData)
fmt.Println("Process completed successfully!")
}
func generateChangelog(c changelog) {
file := filepath.Join(fileName)
// Cria o arquivo apenas se houver issue ou pr na release gerada
if !fileExists(file) || (len(c.closedIssues) > 0 || len(c.mergedPulls) > 0) {
err := ioutil.WriteFile(file, []byte(changelogTitle), os.ModePerm)
if err != nil {
log.Fatalf("Unable to write file: %v", err)
}
}
fileRead, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalf("error: %v", err)
}
lines := strings.Split(string(fileRead), "\n")
// Lógica: https: //stackoverflow.com/questions/46128016/insert-a-value-in-a-slice-at-a-given-index
lines = append(lines[:1+1], lines[1:]...)
formatTitle := fmt.Sprintf(
title,
c.nextRelease.tagName,
c.nextRelease.link,
c.nextRelease.publishedAt.Format("2006-01-04"),
)
formatFullChangelog := fmt.Sprintf(
fullChangelog,
c.owner,
c.repoName,
c.previousRelease.tagName,
c.nextRelease.tagName,
)
lines[1] = formatTitle + formatFullChangelog
// Valida e formata a parte das issues
if len(c.closedIssues) > 0 {
lines[1] = lines[1] + closedIssues
for _, issue := range c.closedIssues {
lines[1] = lines[1] + fmt.Sprintf(issueTemplate, issue.title, issue.issueNumber, issue.link)
}
}
// Valida e formata a parte das prs
if len(c.mergedPulls) > 0 {
lines[1] = lines[1] + mergedPR
for _, pr := range c.mergedPulls {
lines[1] = lines[1] + fmt.Sprintf(
prTemplate,
pr.title,
pr.prNumber,
pr.link,
pr.assigneeUser,
pr.assigneeLink,
)
}
}
if len(c.closedIssues) > 0 || len(c.mergedPulls) > 0 {
// Escreve no arquivo o changelog gerado
newFile := strings.Join(lines, "\n")
ioutil.WriteFile(file, []byte(newFile), os.ModePerm)
}
}
func filterIssues(d filterData) []issuesData {
if d.previousRelease.tagName != "" {
// Seleciona todas as issues fechadas depois da data de criação da tag
issues, _, err := d.client.Issues.ListByRepo(
context.Background(),
d.owner,
d.repoName,
&github.IssueListByRepoOptions{State: "closed", Since: d.previousRelease.publishedAt.Time},
)
if err != nil {
log.Fatalf("error listing issues: %v", err)
}
// Coloca todos os títulos das issues elegíveis dentro do slice para uso posterior
var filteredIssues []issuesData
for _, issue := range issues {
if issue.ClosedAt.After(d.previousRelease.publishedAt.Time) && issue.PullRequestLinks == nil && issue.ClosedAt.Before(d.nextRelease.publishedAt.Time) {
filterIssue := issuesData{
title: *issue.Title,
issueNumber: *issue.Number,
link: *issue.HTMLURL,
}
filteredIssues = append(filteredIssues, filterIssue)
}
}
return filteredIssues
}
return []issuesData{}
}
func filterPulls(d filterData) []pullsData {
prs := fetchMergedPulls(d)
if len(prs) == 0 {
return []pullsData{}
}
// Filtra as prs mergeadas após a data de criação da tag
// TODO: abrir issue no repo go-github, pois retorna erro ao usar o campo name da struct de user
var mergedPulls []pullsData
if d.previousRelease.link != "" {
for _, pr := range prs {
if pr.MergedAt.After(d.previousRelease.publishedAt.Time) && pr.MergedAt.Before(d.nextRelease.publishedAt.Time) {
filterPull := pullsData{
title: *pr.Title,
prNumber: *pr.Number,
link: *pr.HTMLURL,
assigneeUser: *pr.User.Login,
assigneeLink: *pr.User.HTMLURL,
}
mergedPulls = append(mergedPulls, filterPull)
}
}
}
return mergedPulls
}
func fetchMergedPulls(d filterData) []*github.PullRequest {
// Seleciona todas as pr fechadas
prs, _, err := d.client.PullRequests.List(
ctx,
d.owner,
d.repoName,
&github.PullRequestListOptions{State: "closed"},
)
if err != nil {
log.Fatalf("error listing prs: %v", err)
}
var mergedPulls []*github.PullRequest
for _, pr := range prs {
if pr.MergedAt != nil {
mergedPulls = append(mergedPulls, pr)
}
}
return mergedPulls
}
func getNextRelease(client *github.Client, owner, repoName string) tagData {
// Pega a última tag do repositório
lastTag, _, err := client.Repositories.GetLatestRelease(
context.Background(),
owner,
repoName,
)
if err != nil {
log.Fatalf("error getting the last tag: %v", err)
}
nrData := tagData{
tagName: *lastTag.TagName,
link: *lastTag.HTMLURL,
publishedAt: lastTag.PublishedAt,
}
return nrData
}
func getPreviousRelease(client *github.Client, owner, repoName string) tagData {
// Pega a última tag do repositório
tags, _, err := client.Repositories.ListReleases(
context.Background(),
owner,
repoName,
&github.ListOptions{},
)
if err != nil {
log.Fatalf("error getting the previous tag: %v", err)
}
if len(tags) <= 1 {
return tagData{}
}
previousRelease := tags[1]
prData := tagData{
tagName: *previousRelease.TagName,
link: *previousRelease.HTMLURL,
publishedAt: previousRelease.PublishedAt,
}
return prData
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func setupClient() *github.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
return client
}