-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
498 lines (408 loc) · 14.1 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/MateSousa/create-release-bot/initializers"
"github.com/google/go-github/v33/github"
"golang.org/x/oauth2"
"gorm.io/gorm/utils"
)
const (
prLabelPending string = "createrelease:pending"
prLabelmerged string = "createrelease:merged"
releaseCommit string = "Release is at:"
releaseName string = "Release "
mergeCommand string = "/merge"
mergeComment string = "Merged by Create Release Bot"
)
func PREvent(client *github.Client, env initializers.Env, event *github.PullRequestEvent) error {
switch *event.Action {
case "closed":
if !HasPendingLabel(event.PullRequest, nil) {
return nil
}
// Remove label "createrelease:pending" from PR
err := RemovePendingLabel(client, env, event.PullRequest, nil)
if err != nil {
fmt.Printf("error removing label: %v", err)
return err
}
}
return nil
}
func IssueEvent(client *github.Client, env initializers.Env, event *github.IssueCommentEvent) error {
switch *event.Action {
case "created":
// Check if PR has label "createrelease:pending"
if !HasPendingLabel(nil, event) {
return nil
}
// Check if comment is "/merge"
if !CheckIfCommentIsForMerge(*event.Comment.Body) {
return nil
}
// Create/Update Changelog
changelogCommit, err := CreateOrUpdateChangelog(client, env, event)
if err != nil {
return err
}
// sleep for 15 seconds to wait for changelog commit to be created
time.Sleep(15 * time.Second)
// Aprove Merge
err = ApproveMerge(client, env, event)
if err != nil {
return err
}
// Remove label "createrelease:pending" and add "createrelease:merged" to PR
err = AddMergedLabel(client, env, event)
if err != nil {
return err
}
// Create a new latest release tag and increment the minor version
newReleaseTag, err := CreateNewLatestReleaseTag(client, env, *changelogCommit.SHA)
if err != nil {
return err
}
// Create a new release
newRelease, err := CreateNewRelease(client, env, newReleaseTag)
if err != nil {
return err
}
// Create a new comment in PR with commit message
commit := "Release is at: " + newRelease.GetHTMLURL()
err = CreateNewComment(client, env, event, commit)
if err != nil {
return err
}
}
return nil
}
func main() {
env, err := initializers.LoadEnv()
if err != nil {
fmt.Printf("error loading env: %v", err)
os.Exit(1)
}
client, err := CreateGithubClient(env)
if err != nil {
fmt.Printf("error creating github client: %v", err)
os.Exit(1)
}
prEvent, _, err := ParsePullRequestEvent(env.GithubEvent, false)
if err != nil {
fmt.Printf("error parsing event: %v", err)
os.Exit(1)
}
err = PREvent(client, env, prEvent)
if err != nil {
fmt.Printf("error handling pr event: %v", err)
os.Exit(1)
}
_, issueEvent, err := ParsePullRequestEvent(env.GithubEvent, true)
if err != nil {
fmt.Printf("error parsing event: %v", err)
os.Exit(1)
}
err = IssueEvent(client, env, issueEvent)
if err != nil {
fmt.Printf("error handling issue event: %v", err)
os.Exit(1)
}
os.Exit(0)
}
// Create a github client with a token
func CreateGithubClient(env initializers.Env) (*github.Client, error) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: env.Token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
return client, nil
}
// Create a label "createrelease:pending" and add to PR
func AddPendingLabel(client *github.Client, env initializers.Env, pr *github.PullRequest) error {
_, _, err := client.Issues.AddLabelsToIssue(context.Background(), env.RepoOwner, env.RepoName, *pr.Number, []string{prLabelPending})
if err != nil {
return err
}
return nil
}
func RemovePendingLabel(client *github.Client, env initializers.Env, pr *github.PullRequest, issueEvent *github.IssueCommentEvent) error {
var prNumber int
if pr != nil {
prNumber = *pr.Number
} else {
prNumber = *issueEvent.Issue.Number
}
_, err := client.Issues.RemoveLabelForIssue(context.Background(), env.RepoOwner, env.RepoName, prNumber, prLabelPending)
if err != nil {
fmt.Printf("error removing label: %v", err)
return err
}
return nil
}
// Remove label "createrelease:pending" and add "createrelease:merged" to PR
func AddMergedLabel(client *github.Client, env initializers.Env, pr *github.IssueCommentEvent) error {
err := RemovePendingLabel(client, env, nil, pr)
if err != nil {
fmt.Printf("error removing label: %v", err)
return err
}
_, _, err = client.Issues.AddLabelsToIssue(context.Background(), env.RepoOwner, env.RepoName, *pr.Issue.Number, []string{prLabelmerged})
if err != nil {
return err
}
return nil
}
// Create a new latest release tag and increment the minor version
func CreateNewLatestReleaseTag(client *github.Client, env initializers.Env, lastCommitSHA string) (string, error) {
var releaseTag string
releaseList, _, err := client.Repositories.ListReleases(context.Background(), env.RepoOwner, env.RepoName, nil)
if err != nil {
return "", err
}
noReleaseTag := len(releaseList) == 0
if noReleaseTag {
releaseTag = "v0.0.1"
} else {
latestReleaseTag := releaseList[0].GetTagName()
latestReleaseTagSplit := strings.Split(latestReleaseTag, ".")
latestReleaseTagMajorVersion, err := strconv.Atoi(latestReleaseTagSplit[0][1:])
if err != nil {
return "", err
}
latestReleaseTagMinorVersion, err := strconv.Atoi(latestReleaseTagSplit[1])
if err != nil {
return "", err
}
latestReleaseTagPatchVersion, err := strconv.Atoi(latestReleaseTagSplit[2])
if err != nil {
return "", err
}
if latestReleaseTagPatchVersion == 9 {
latestReleaseTagMinorVersion = latestReleaseTagMinorVersion + 1
latestReleaseTagPatchVersion = 0
} else {
latestReleaseTagPatchVersion = latestReleaseTagPatchVersion + 1
}
if latestReleaseTagMinorVersion == 9 && latestReleaseTagPatchVersion == 9 {
latestReleaseTagMajorVersion = latestReleaseTagMajorVersion + 1
latestReleaseTagMinorVersion = 0
latestReleaseTagPatchVersion = 0
}
releaseTag = fmt.Sprintf("v%d.%d.%d", latestReleaseTagMajorVersion, latestReleaseTagMinorVersion, latestReleaseTagPatchVersion)
}
// Create a new tag
now := time.Now()
newReleaseTag, _, err := client.Git.CreateTag(context.Background(), env.RepoOwner, env.RepoName, &github.Tag{
Tag: &releaseTag,
Message: &releaseTag,
Object: &github.GitObject{
Type: github.String("commit"),
SHA: github.String(lastCommitSHA),
},
Tagger: &github.CommitAuthor{
Name: github.String("Create Release Bot"),
Email: github.String("githubaction@github.com"),
Date: &now,
},
})
if err != nil {
fmt.Printf("error creating tag: %v", err)
return "", err
}
return *newReleaseTag.Tag, nil
}
// Create a new release
func CreateNewRelease(client *github.Client, env initializers.Env, newReleaseTag string) (*github.RepositoryRelease, error) {
name := fmt.Sprintf("%s %s", releaseName, newReleaseTag)
newRelease, _, err := client.Repositories.CreateRelease(context.Background(), env.RepoOwner, env.RepoName, &github.RepositoryRelease{
TagName: &newReleaseTag,
Name: &name,
TargetCommitish: github.String("master"),
})
if err != nil {
return nil, fmt.Errorf("error creating release mate: %v", err)
}
return newRelease, nil
}
// Create a new comment in PR with commit message
func CreateNewComment(client *github.Client, env initializers.Env, pr *github.IssueCommentEvent, commitMessage string) error {
_, _, err := client.Issues.CreateComment(context.Background(), env.RepoOwner, env.RepoName, *pr.Issue.Number, &github.IssueComment{
Body: &commitMessage,
})
if err != nil {
return err
}
return nil
}
// Check if PR has label "createrelease:pending"
func HasPendingLabel(pr *github.PullRequest, issue *github.IssueCommentEvent) bool {
// if pr is nil then it's a comment event
if pr == nil {
for _, label := range issue.Issue.Labels {
if label.GetName() == prLabelPending {
return true
}
}
} else {
for _, label := range pr.Labels {
if label.GetName() == prLabelPending {
return true
}
}
}
return false
}
func ParsePullRequestEvent(pullRequestEvent string, isIssueCommentEvent bool) (*github.PullRequestEvent, *github.IssueCommentEvent, error) {
// Read the event payload from the env vars
payloadEnv := pullRequestEvent
if payloadEnv == "" {
return nil, nil, fmt.Errorf("no payload found for event %s", pullRequestEvent)
}
// if it's a comment event, parse it as a comment event
if isIssueCommentEvent {
var issueCommentEvent github.IssueCommentEvent
err := json.Unmarshal([]byte(payloadEnv), &issueCommentEvent)
if err != nil {
return nil, nil, err
}
return nil, &issueCommentEvent, nil
} else {
var pullRequestEvent github.PullRequestEvent
err := json.Unmarshal([]byte(payloadEnv), &pullRequestEvent)
if err != nil {
return nil, nil, err
}
return &pullRequestEvent, nil, nil
}
}
// Aprove merge create/update changelog and merge PR
func ApproveMerge(client *github.Client, env initializers.Env, pr *github.IssueCommentEvent) error {
// Merge PR
_, _, err := client.PullRequests.Merge(context.Background(), env.RepoOwner, env.RepoName, *pr.Issue.Number, mergeComment, nil)
if err != nil {
return err
}
return nil
}
// Get commits from PR
func GetPRCommits(client *github.Client, env initializers.Env, pr *github.IssueCommentEvent) []*github.RepositoryCommit {
commits, _, err := client.PullRequests.ListCommits(context.Background(), env.RepoOwner, env.RepoName, *pr.Issue.Number, nil)
if err != nil {
return nil
}
return commits
}
// Get all commits from PR and create/update a CHANGELOG.md file with the changes from PR
func CreateOrUpdateChangelog(client *github.Client, env initializers.Env, pr *github.IssueCommentEvent) (*github.RepositoryContentResponse, error) {
commits := GetPRCommits(client, env, pr)
categorizedCommits, err := CategorizeCommits(commits)
if err != nil {
return nil, err
}
// Check if CHANGELOG.md file exists
changelogFile, _, _, err := client.Repositories.GetContents(context.Background(), env.RepoOwner, env.RepoName, "CHANGELOG.md", nil)
if err != nil {
// If CHANGELOG.md file does not exist, create it
if strings.Contains(err.Error(), "404") {
commitChangelog, err := CreateChangelog(client, env, categorizedCommits, env.BaseBranch, *pr.Issue.Title)
if err != nil {
return nil, err
}
return commitChangelog, nil
} else {
return nil, err
}
} else {
// If CHANGELOG.md file exists, update it
commitChangelog, err := UpdateChangelog(client, env, categorizedCommits, env.BaseBranch, *changelogFile.SHA, *pr.Issue.Title)
if err != nil {
return nil, err
}
return commitChangelog, nil
}
return nil, nil
}
// Create CHANGELOG.md file
func CreateChangelog(client *github.Client, env initializers.Env, categorizedCommits map[string][]*github.RepositoryCommit, baseBranch string, releaseTag string) (*github.RepositoryContentResponse, error) {
now := time.Now()
var changelogContent string
changelogContent = "# Changelog " + releaseTag + " (" + now.Format("2006-01-02") + ")\n\n"
for commitType, commits := range categorizedCommits {
changelogContent = changelogContent + "\n\n" + "## " + commitType + "\n\n"
for _, commit := range commits {
changelogContent = changelogContent + "- " + *commit.Commit.Message + "\n"
}
}
commitMessage := "chore: create CHANGELOG.md file"
commitContent := &github.RepositoryContentFileOptions{
Message: github.String(commitMessage),
Content: []byte(changelogContent),
Branch: github.String(baseBranch),
}
commitChangelog, _, err := client.Repositories.CreateFile(context.Background(), env.RepoOwner, env.RepoName, "CHANGELOG.md", commitContent)
if err != nil {
return nil, err
}
return commitChangelog, nil
}
// Update CHANGELOG.md file
func UpdateChangelog(client *github.Client, env initializers.Env, categorizedCommits map[string][]*github.RepositoryCommit, baseBranch string, lastCommitSHA string, releaseTag string) (*github.RepositoryContentResponse, error) {
now := time.Now()
var changelogContent string
changelogContent = "# Changelog " + releaseTag + " (" + now.Format("2006-01-02") + ")\n\n"
for commitType, commits := range categorizedCommits {
changelogContent = changelogContent + "\n\n" + "## " + commitType + "\n\n"
for _, commit := range commits {
changelogContent = changelogContent + "- " + *commit.Commit.Message + "\n"
}
}
commitMessage := "chore: update CHANGELOG.md file"
commitContent := &github.RepositoryContentFileOptions{
Message: github.String(commitMessage),
Content: []byte(changelogContent),
Branch: github.String(baseBranch),
SHA: github.String(lastCommitSHA),
}
commitChangelog, _, err := client.Repositories.UpdateFile(context.Background(), env.RepoOwner, env.RepoName, "CHANGELOG.md", commitContent)
if err != nil {
return nil, err
}
return commitChangelog, nil
}
// Category each commits from PR using conventional commits
func CategorizeCommits(commits []*github.RepositoryCommit) (map[string][]*github.RepositoryCommit, error) {
var conventionalCommitsType = []string{"feat", "fix", "perf", "refactor", "docs", "test", "chore", "style", "ci", "build"}
categorizedCommits := make(map[string][]*github.RepositoryCommit)
for _, commit := range commits {
commitMessage := *commit.Commit.Message
commitMessageSplit := strings.Split(commitMessage, ":")
commitType := commitMessageSplit[0]
// if commit type is contain any letter of conventional commits type then use it as commit type becasue some commits its like feat(main.go) or fix(main.go)
for _, conventionalCommitsTypeLetter := range conventionalCommitsType {
if strings.Contains(commitType, conventionalCommitsTypeLetter) {
commitType = conventionalCommitsTypeLetter
break
}
}
// if commit type is not contain any letter of conventional commits type then use others as commit type
if !utils.Contains(conventionalCommitsType, commitType) {
commitType = "others"
}
categorizedCommits[commitType] = append(categorizedCommits[commitType], commit)
}
return categorizedCommits, nil
}
func CheckIfCommentIsForMerge(commentBody string) bool {
if strings.Contains(commentBody, "/merge") {
return true
}
return false
}