Skip to content

Commit 2217f5d

Browse files
committed
Resolve GitOps comment problem with push requests
When a user adds a GitOps comment to a push request targeting a branch other than the main branch, the pipelinerun should not execute for those instances where the target branch is set to 'main.' Signed-off-by: Savita Ashture <[email protected]>
1 parent e90fcef commit 2217f5d

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

pkg/provider/github/parse_payload.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -390,48 +390,49 @@ func (v *Provider) handleCommitCommentEvent(ctx context.Context, event *github.C
390390
runevent.EventType = "push"
391391
runevent.TriggerTarget = "push"
392392

393-
// by default head and base branch is main
394-
runevent.HeadBranch = "main"
395-
runevent.BaseBranch = "main"
396-
393+
// Set main as default branch to runevent.HeadBranch, runevent.BaseBranch
394+
runevent.HeadBranch, runevent.BaseBranch = "main", "main"
397395
var (
398396
branchName string
399397
prName string
400398
err error
401399
)
402400

403-
// if it is a /test or /retest comment with pipelinerun name figure out the pipelinerun name
401+
// If it is a /test or /retest comment with pipelinerun name figure out the pipelinerun name
404402
if provider.IsTestRetestComment(event.GetComment().GetBody()) {
405403
prName, branchName, err = provider.GetPipelineRunAndBranchNameFromTestComment(event.GetComment().GetBody())
406404
if err != nil {
407405
return runevent, err
408406
}
409407
runevent.TargetTestPipelineRun = prName
410408
}
409+
// Check for /cancel comment
411410
if provider.IsCancelComment(event.GetComment().GetBody()) {
412411
action = "cancellation"
413412
prName, branchName, err = provider.GetPipelineRunAndBranchNameFromCancelComment(event.GetComment().GetBody())
414413
if err != nil {
415414
return runevent, err
416415
}
416+
runevent.CancelPipelineRuns = true
417417
runevent.TargetCancelPipelineRun = prName
418418
}
419419

420-
if branchName != "" {
421-
if err = v.isBranchContainsCommit(ctx, runevent, branchName); err != nil {
422-
return runevent, err
423-
}
424-
runevent.HeadBranch = branchName
425-
runevent.BaseBranch = branchName
420+
// If no branch is specified in GitOps comments, use runevent.HeadBranch
421+
if branchName == "" {
422+
branchName = runevent.HeadBranch
426423
}
427424

428-
if provider.IsCancelComment(event.GetComment().GetBody()) {
429-
if err = v.isBranchContainsCommit(ctx, runevent, runevent.HeadBranch); err != nil {
425+
// Check if the specified branch contains the commit
426+
if err = v.isBranchContainsCommit(ctx, runevent, branchName); err != nil {
427+
if provider.IsCancelComment(event.GetComment().GetBody()) {
430428
runevent.CancelPipelineRuns = false
431-
return runevent, err
432429
}
433-
runevent.CancelPipelineRuns = true
430+
return runevent, err
434431
}
432+
// Finally update branch information to runevent.HeadBranch and runevent.BaseBranch
433+
runevent.HeadBranch = branchName
434+
runevent.BaseBranch = branchName
435+
435436
v.Logger.Infof("commit_comment: pipelinerun %s on %s/%s#%s has been requested", action, runevent.Organization, runevent.Repository, runevent.SHA)
436437
return runevent, nil
437438
}

pkg/provider/github/parse_payload_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ var samplePR = github.PullRequest{
7777
},
7878
}
7979

80+
var samplePRAnother = github.PullRequest{
81+
Number: github.Int(54321),
82+
Head: &github.PullRequestBranch{
83+
SHA: github.String("samplePRshanew"),
84+
Repo: sampleRepo,
85+
},
86+
}
87+
8088
func TestParsePayLoad(t *testing.T) {
8189
tests := []struct {
8290
name string
@@ -474,6 +482,25 @@ func TestParsePayLoad(t *testing.T) {
474482
isCancelPipelineRunEnabled: false,
475483
wantErrString: "404 Not Found",
476484
},
485+
{
486+
name: "commit comment to retest a pr with a SHA that does not exist in the main branch",
487+
eventType: "commit_comment",
488+
triggerTarget: "push",
489+
githubClient: true,
490+
payloadEventStruct: github.CommitCommentEvent{
491+
Repo: sampleRepo,
492+
Comment: &github.RepositoryComment{
493+
CommitID: github.String("samplePRshanew"),
494+
HTMLURL: github.String("/777"),
495+
Body: github.String("/retest dummy"),
496+
},
497+
},
498+
muxReplies: map[string]interface{}{"/repos/owner/reponame/pulls/777": samplePRAnother},
499+
shaRet: "samplePRshanew",
500+
targetPipelinerun: "dummy",
501+
wantedBranchName: "main",
502+
wantErrString: "provided branch main does not contains sha samplePRshanew",
503+
},
477504
}
478505
for _, tt := range tests {
479506
t.Run(tt.name, func(t *testing.T) {
@@ -498,6 +525,16 @@ func TestParsePayLoad(t *testing.T) {
498525
"commit": {
499526
"sha": "samplePRsha"
500527
}
528+
}`)
529+
assert.NilError(t, err)
530+
})
531+
mux.HandleFunc(fmt.Sprintf("/repos/%s/%s/branches/testnew",
532+
"owner", "reponame"), func(rw http.ResponseWriter, r *http.Request) {
533+
_, err := fmt.Fprintf(rw, `{
534+
"name": "testnew",
535+
"commit": {
536+
"sha": "samplePRshanew"
537+
}
501538
}`)
502539
assert.NilError(t, err)
503540
})

0 commit comments

Comments
 (0)