-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathentfork_sync.go
381 lines (331 loc) · 10.9 KB
/
entfork_sync.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
package main
import (
"bytes"
"context"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"text/template"
"time"
"github.com/google/go-github/v28/github"
"github.com/sirupsen/logrus"
"github.com/mendersoftware/integration-test-runner/git"
)
// syncIfOSHasEnterpriseRepo detects whether a commit has been merged to
// the Open Source edition of a repo, and then creates a PR-branch
// in the Enterprise edition, which is then used in order to open
// a PR to the Enterprise repo with the OS changes.
func syncIfOSHasEnterpriseRepo(
log *logrus.Entry,
conf *config,
gpr *github.PullRequestEvent,
) error {
repo := gpr.GetRepo()
if repo == nil {
return fmt.Errorf("syncIfOSHasEnterpriseRepo: Failed to get the repository information")
}
// Enterprise repo sentinel
switch repo.GetName() {
case "deployments":
case "inventory":
case "useradm":
case "workflows":
case "deviceauth":
default:
log.Debugf(
"syncIfOSHasEnterpriseRepo: Repository without Enterprise fork detected: (%s). "+
"Not syncing",
repo.GetName(),
)
return nil
}
pr := gpr.GetPullRequest()
if pr == nil {
return errors.New("syncIfOSHasEnterpriseRepo: Failed to get the pull request")
}
// If the action is "closed" and the "merged" key is "true", the pull request was merged.
// While webhooks are also triggered when a pull request is synchronized, Events API timelines
// don't include pull request events with the "synchronize" action.
if gpr.GetAction() == "closed" && pr.GetMerged() {
// Only sync on Merges to master, release and feature branches.
// Verify release branches.
branch := pr.GetBase()
if branch == nil {
return fmt.Errorf(
"syncIfOSHasEnterpriseRepo: Failed to get the base-branch of the PR: %v",
branch,
)
}
syncBranches := regexp.MustCompile(
`(main|master|[0-9]+\.[0-9]+\.x|` + featureBranchPrefix + `.+)`,
)
branchRef := branch.GetRef()
if branchRef == "" {
return fmt.Errorf("Failed to get the branch-ref from the PR: %v", pr)
}
if !syncBranches.MatchString(branchRef) {
log.Debugf(
"syncIfOSHasEnterpriseRepo: Detected a merge into another branch than master or "+
"a release branch: (%s), no syncing done",
branchRef,
)
} else {
log.Infof("syncIfOSHasEnterpriseRepo: Merge to (%s) in an OS repository detected. "+
"Syncing the repositories...", branchRef)
PRNumber := strconv.Itoa(pr.GetNumber())
PRBranchName := "mergeostoent_" + PRNumber
merged, err := createPRBranchOnEnterprise(
log,
repo.GetName(),
branchRef,
PRNumber,
PRBranchName,
conf,
)
if err != nil {
return fmt.Errorf("syncIfOSHasEnterpriseRepo: Failed to create the PR branch on "+
"the Enterprise repo due to error: %v", err)
}
// Get the link to the original PR, so that it can be linked to
// in the commit body
PRURL := pr.GetHTMLURL()
var assignees []string
if issuer := pr.GetUser().GetLogin(); issuer != "" {
assignees = append(assignees, issuer)
}
enterprisePR, err := createPullRequestFromTestBotFork(createPRArgs{
conf: conf,
repo: repo.GetName() + "-enterprise",
prBranch: githubBotName + ":" + PRBranchName,
baseBranch: branchRef,
message: fmt.Sprintf("[Bot] %s", pr.GetTitle()),
messageBody: fmt.Sprintf("Original PR: %s\n\n%s", PRURL, pr.GetBody()),
assignees: assignees,
})
if err != nil {
return fmt.Errorf(
"syncIfOSHasEnterpriseRepo: Failed to create a PR with error: %v",
err,
)
}
log.Infof("syncIfOSHasEnterpriseRepo: Created PR: %d on Enterprise/%s/%s",
enterprisePR.GetNumber(), repo.GetName(), branchRef)
log.Debugf("syncIfOSHasEnterpriseRepo: Created PR: id=%d,number=%d,title=%s",
pr.GetID(), pr.GetNumber(), pr.GetTitle())
log.Debug("Trying to @mention the user in the newly created PR")
userName := pr.GetMergedBy().GetLogin()
log.Debugf("userName: %s", userName)
if userName != "" {
err = commentToNotifyUser(log, commentArgs{
pr: enterprisePR,
conf: conf,
mergeConflicts: !merged,
repo: repo.GetName() + "-enterprise",
userName: userName,
prBranchName: PRBranchName,
branchName: branchRef,
})
if err != nil {
log.Errorf("syncIfOSHasEnterpriseRepo: %s", err.Error())
}
}
}
}
return nil
}
// createPRBranchOnEnterprise creates a new branch in the Enterprise repository
// starting at the branch in which to sync, with the name 'PRBranchName'
// and merges this with the OS equivalent of 'branchName'.
func createPRBranchOnEnterprise(
log *logrus.Entry,
repo, branchName, PRNumber, PRBranchName string,
conf *config,
) (merged bool, err error) {
state, err := git.Commands(
git.Command("init", "."),
git.Command("remote", "add", "opensource",
getRemoteURLGitHub(conf.githubProtocol, conf.githubOrganization, repo)),
git.Command("remote", "add", "enterprise",
getRemoteURLGitHub(conf.githubProtocol, conf.githubOrganization, repo+"-enterprise")),
git.Command("remote", "add", githubBotName,
getRemoteURLGitHub(conf.githubProtocol, githubBotName, repo+"-enterprise")),
git.Command("config", "--add", "user.name", githubBotName),
git.Command("config", "--add", "user.email", "[email protected]"),
git.Command("fetch", "opensource", branchName),
git.Command("fetch", "enterprise", branchName+":"+PRBranchName),
git.Command("checkout", PRBranchName),
)
defer state.Cleanup()
if err != nil {
return false, err
}
// Merge the OS branch into the PR branch
mergeMsg := fmt.Sprintf(
"Merge OS base branch: (%s) including PR: (%s) into Enterprise: (%[1]s)",
branchName,
PRNumber,
)
log.Debug("Trying to " + mergeMsg)
gitcmd := git.Command("merge", "-m", mergeMsg, "opensource/"+branchName)
gitcmd.Dir = state.Dir
out, err := gitcmd.CombinedOutput()
merged = true
if err != nil {
merged = false
if strings.Contains(string(out), "Automatic merge failed") {
msg := "Merge conflict detected. Still pushing the Enterprise PR branch, " +
"and creating the PR, so that the user can manually resolve, " +
"and re-push to the PR once these are fixed"
log.Warn(msg)
} else {
return false, fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
}
}
if !merged {
// In case of a failed merge, reset PRBranchName to opensource/branchName
// and push this branch to enterprise
gitcmd = git.Command("reset", "--hard", "opensource/"+branchName)
gitcmd.Dir = state.Dir
out, err = gitcmd.CombinedOutput()
if err != nil {
return merged, fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
}
}
// Push the branch to the bot's own fork
gitcmd = git.Command("push", "--set-upstream", githubBotName, PRBranchName)
gitcmd.Dir = state.Dir
out, err = gitcmd.CombinedOutput()
if err != nil {
return merged, fmt.Errorf("%v returned error: %s: %s", gitcmd.Args, out, err.Error())
}
if merged {
log.Infof("Merged branch: opensource/%s/%s into enterprise/%[1]s/%s in the Enterprise repo",
repo, branchName, PRBranchName)
} else {
msg := "Failed to merge opensource/%s/%s into enterprise/%[1]s/%s in the Enterprise " +
"repo. Therefore pushed opensource/%[1]s/%[2]s to %s so that " +
"merging can be done by a human locally"
log.Infof(msg, repo, branchName, PRBranchName, PRBranchName)
}
return merged, nil
}
type createPRArgs struct {
conf *config
repo string
prBranch string
baseBranch string
message string
messageBody string
assignees []string
}
func createPullRequestFromTestBotFork(args createPRArgs) (*github.PullRequest, error) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*30)
defer cancel()
newPR := &github.NewPullRequest{
Title: github.String(args.message),
Head: github.String(args.prBranch),
Base: github.String(args.baseBranch),
Body: github.String(args.messageBody),
MaintainerCanModify: github.Bool(true),
}
pr, err := githubClient.CreatePullRequest(
ctx,
args.conf.githubOrganization,
args.repo,
newPR,
)
if err != nil {
return nil, fmt.Errorf("Failed to create the PR for: (%s) %v", args.repo, err)
}
if len(args.assignees) > 0 {
err = githubClient.AssignPullRequest(
ctx, args.conf.githubOrganization,
args.repo, pr.GetNumber(),
args.assignees,
)
if err != nil {
logrus.Warnf("failed to assign users %v to PR: %s", args.assignees, err)
}
}
return pr, nil
}
type commentArgs struct {
pr *github.PullRequest
conf *config
mergeConflicts bool
repo string
userName string
prBranchName string
branchName string
}
func commentToNotifyUser(log *logrus.Entry, args commentArgs) error {
// Post a comment, and @mention the user
var commentBody string
if !args.mergeConflicts {
commentBody = fmt.Sprintf(
"@%s I have created a PR for you, ready to merge as soon as tests are passed",
args.userName,
)
} else {
// nolint:lll
tmplString := `
@{{.UserName}} I have created a PR for you.
Unfortunately, a merge conflict was detected. This means that the conflict will have to be resolved manually by you, human. Then pushed to the PR-branch, once all conflicts are resolved.
This can be done by following:
<details>
<summary><small>this</small> recipe</summary><p>
1. Make sure that the '{{.GitHubBotName}}' remote is present in your repository, or else add it with:
1. {{.BackQuote}}git remote add {{.GitHubBotName}} [email protected]:{{.GitHubBotName}}/{{.Repo}}.git{{.BackQuote}}
2. Fetch the remote branches
1. {{.BackQuote}}git fetch origin {{.BranchName}}:localtmp{{.BackQuote}}
2. {{.BackQuote}}git fetch {{.GitHubBotName}} {{.PRBranchName}}{{.BackQuote}}
3. Checkout the localtmp branch
1. {{.BackQuote}}git checkout localtmp{{.BackQuote}}
4. Merge the branch into the PR branch
1. {{.BackQuote}}git merge {{.GitHubBotName}}/{{.PRBranchName}}{{.BackQuote}}
5. Resolve all conflicts
6. Commit the merged changes
7. Push to the PR branch
1. {{.BackQuote}}git push {{.GitHubBotName}} localtmp:{{.PRBranchName}}{{.BackQuote}}
</p></details>
`
tmpl, err := template.New("Main").Parse(tmplString)
if err != nil {
log.Error("The text template should never fail to render!")
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, struct {
UserName string
Repo string
PRBranchName string
BranchName string
BackQuote string
GitHubBotName string
}{
UserName: args.userName,
Repo: args.repo,
PRBranchName: args.prBranchName,
BranchName: args.branchName,
BackQuote: "`",
GitHubBotName: githubBotName,
}); err != nil {
log.Errorf(
"Failed to execute the merge-conflict PR template string. Error: %s",
err.Error(),
)
}
commentBody = buf.String()
}
comment := github.IssueComment{
Body: &commentBody,
}
return githubClient.CreateComment(
context.Background(),
args.conf.githubOrganization,
args.repo,
args.pr.GetNumber(),
&comment,
)
}