-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
260 lines (232 loc) · 7.06 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
package main
import (
"context"
_ "embed"
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"text/template"
"time"
"github.com/google/go-github/github"
"github.com/kelseyhightower/envconfig"
"golang.org/x/oauth2"
)
type env struct {
GithubToken string `envconfig:"GITHUB_TOKEN"`
Owner string `envconfig:"OWNER"`
Repo string `envconfig:"REPO"`
PRNumber int `envconfig:"PR_NUMBER"`
Comment string `envconfig:"COMMENT"`
MergeMethod string `envconfig:"MERGE_METHOD" default:"merge"`
Mergers []string `envconfig:"MERGERS"`
Actor string `envconfig:"GITHUB_ACTOR"` // github user who initiated the workflow.
EnableAutoMerge bool `envconfig:"ENABLE_AUTO_MERGE" default:"false"`
GitTrailers []string `envconfig:"GIT_TRAILERS"`
}
const (
mergeComment = "/merge"
jobTimeout = 10 * 60 * time.Second
)
func main() {
var e env
err := envconfig.Process("INPUT", &e)
if err != nil {
fmt.Printf("failed to load inputs: %s\n", err.Error())
panic(err.Error())
}
err = os.Setenv("GITHUB_TOKEN", e.GithubToken)
if err != nil {
fmt.Printf("failed to set env: %s\n", err.Error())
panic(err.Error())
}
ctx, f := context.WithTimeout(context.Background(), jobTimeout)
defer f()
client := newGHClient(e.GithubToken)
if err := validateEnv(e); err != nil {
if serr := client.sendMsg(ctx, e.Owner, e.Repo, e.PRNumber, errMsg(err)); serr != nil {
fmt.Printf("failed to send message: %v original: %v", serr, err)
panic(serr.Error())
}
fmt.Printf("failed to validate env: %v", err)
panic(err.Error())
}
if err := client.merge(ctx, e.Owner, e.Repo, e.PRNumber, e.MergeMethod, e.EnableAutoMerge, e.GitTrailers); err != nil {
if serr := client.sendMsg(ctx, e.Owner, e.Repo, e.PRNumber, errMsg(err)); serr != nil {
fmt.Printf("failed to send message: %v original: %v", serr, err)
panic(serr.Error())
}
fmt.Printf("failed to merge: %v", err)
panic(err.Error())
}
var successMsg string
if e.EnableAutoMerge {
successMsg = "Enabled auto merge #" + fmt.Sprintf("%d", e.PRNumber) + " \nIf CI fails, fix problems and retry."
} else {
successMsg = "Merged PR #" + fmt.Sprintf("%d", e.PRNumber) + " successfully!"
}
if err := client.sendMsg(ctx, e.Owner, e.Repo, e.PRNumber, successMsg); err != nil {
fmt.Printf("failed to send message: %v", err)
panic(err.Error())
}
fmt.Printf(successMsg)
}
func validateEnv(e env) error {
if e.Comment != mergeComment {
return fmt.Errorf("comment must be %s, got %s", mergeComment, e.Comment)
}
if len(e.Mergers) == 0 {
return nil
}
for _, m := range e.Mergers {
if e.Actor == m {
// if actor matches specified mergers, then valid workflow run.
return nil
}
}
return fmt.Errorf("actor %s is not in mergers list", e.Actor)
}
type ghClient struct {
client *github.Client
}
func newGHClient(token string) *ghClient {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
return &ghClient{
client: client,
}
}
func (gh *ghClient) merge(
ctx context.Context, owner, repo string, prNumber int, mergeMethod string, enableAutoMerge bool, gitTrailers []string,
) error {
pr, _, err := gh.client.PullRequests.Get(ctx, owner, repo, prNumber)
if err != nil {
return fmt.Errorf("failed to get pull request: %w", err)
}
commitMsg, err := generateCommitBody(pr, gitTrailers)
if err != nil {
return fmt.Errorf("failed to generate template: %w", err)
}
if enableAutoMerge {
// GitHub API docs: https://cli.github.com/manual/gh_pr_merge
err = exec.Command("gh", "pr", "merge", strconv.Itoa(prNumber), fmt.Sprintf("--%s", mergeMethod), "--auto", "--subject", generateCommitSubject(pr), "--body", commitMsg, "--repo", fmt.Sprintf("%s/%s", owner, repo)).Run()
} else {
_, _, err = gh.client.PullRequests.Merge(ctx, owner, repo, prNumber, commitMsg, &github.PullRequestOptions{
CommitTitle: generateCommitSubject(pr),
MergeMethod: mergeMethod,
})
}
if err != nil {
return fmt.Errorf("failed to merge pull request: %w", err)
}
return nil
}
func generateCommitSubject(pr *github.PullRequest) string {
return fmt.Sprintf("%s (#%d)", pr.GetTitle(), pr.GetNumber())
}
func generateCommitBody(pr *github.PullRequest, gitTrailers []string) (string, error) {
body := newCommitBody(pr, gitTrailers)
t, err := getTemplate(body)
if err != nil {
return "", err
}
return t, nil
}
func (gh *ghClient) sendMsg(ctx context.Context, owner, repo string, prNumber int, msg string) error {
_, _, err := gh.client.Issues.CreateComment(ctx, owner, repo, prNumber, &github.IssueComment{
Body: &msg,
})
if err != nil {
return fmt.Errorf("failed to send message: %w", err)
}
return nil
}
func newCommitBody(pr *github.PullRequest, gitTrailersInput []string) commitBody {
labels := make([]string, 0, len(pr.Labels))
for _, l := range pr.Labels {
labels = append(labels, l.GetName())
}
description, releaseNote := splitReleaseNote(pr.GetBody())
gitTrailers := make([]gitTrailer, 0, len(gitTrailersInput))
for _, i := range gitTrailersInput {
kv := strings.SplitN(i, "=", 2)
if len(kv) != 2 {
continue
}
gitTrailers = append(gitTrailers, gitTrailer{Key: kv[0], Value: kv[1]})
}
return commitBody{
Message: description,
Labels: labels,
ReleaseNote: releaseNote,
GitTrailers: gitTrailers,
}
}
type commitBody struct {
Labels []string
Message string
ReleaseNote string
GitTrailers []gitTrailer
}
type gitTrailer struct {
Key string
Value string
}
//go:embed template/commit.tmpl
var commitTemplateFile string
func getTemplate(commitBody commitBody) (string, error) {
tmpl, err := template.New("commit").Parse(commitTemplateFile)
if err != nil {
return "", err
}
data := struct {
Labels []string
Message string
ReleaseNote string
GitTrailers []gitTrailer
}{
Labels: commitBody.Labels,
Message: commitBody.Message,
ReleaseNote: commitBody.ReleaseNote,
GitTrailers: commitBody.GitTrailers,
}
var b strings.Builder
if err := tmpl.Execute(&b, data); err != nil {
return "", err
}
return b.String(), nil
}
var (
needApproveRegexp = regexp.MustCompile("At least ([0-9]+) approving review is required by reviewers with write access")
releaseNoteRegexp = regexp.MustCompile("```release-note\n(.+?)\n```")
)
// errMsg returns error message to post from error.
// Especially handing error from github. go-github does not have error type for some cases.
func errMsg(err error) string {
if err == nil {
return "Succeeded!"
}
ss := needApproveRegexp.FindStringSubmatch(err.Error())
if len(ss) == 2 {
return fmt.Sprintf("Need %s approving review", ss[1])
}
return err.Error()
}
// splitReleaseNote returns description and release note from commit body.
// if release note is empty, return whole body and "NONE"
func splitReleaseNote(body string) (description, releaseNote string) {
ss := releaseNoteRegexp.FindStringSubmatch(body)
if len(ss) != 2 {
return body, "NONE"
}
if rn := strings.TrimSpace(ss[1]); rn != "" {
return strings.ReplaceAll(body, ss[0], ""), rn
}
return body, "NONE"
}