Skip to content

Commit b4d37f6

Browse files
zakiskchmouel
authored andcommitted
feat: Exclude tag push events from skip-pr-commits logic
When the skip-pr-commits setting is enabled, the system skips push events for commits that are part of an open pull request to avoid duplicate pipeline runs. However, this should not apply to tag push events, as tags represent important release points that should always trigger pipelines regardless of PR status. Changes: - Detect tag push events by checking if ref starts with "refs/tags/" - Exclude tag push events from skip logic even when skip-pr-commits setting is enabled - Add test case verifying tag push events are not skipped - Add test case verifying regular push events are still skipped when commit is part of a PR This ensures that tagging a commit (e.g., for releases) always triggers the associated pipeline runs, while still avoiding duplicate runs for regular branch commits that are part of pull requests. Signed-off-by: Zaki Shaikh <[email protected]>
1 parent fefe731 commit b4d37f6

File tree

7 files changed

+103
-5
lines changed

7 files changed

+103
-5
lines changed

docs/content/docs/install/settings.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ There are a few things you can configure through the ConfigMap
164164

165165
Default: `true`
166166

167+
**Note:** This setting does not apply to git tag push events. Tag push events will always trigger
168+
pipeline runs regardless of whether the tagged commit is part of an open pull request.
169+
167170
{{< support_matrix github_app="true" github_webhook="true" gitea="false" gitlab="false" bitbucket_cloud="false" bitbucket_datacenter="false" >}}
168171

169172
### Global Cancel In Progress Settings

pkg/provider/github/parse_payload.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,14 @@ func (v *Provider) processEvent(ctx context.Context, event *info.Event, eventInt
346346
v.Logger.Warnf("Error getting pull requests associated with the commit in this push event: %v", err)
347347
}
348348

349-
// Only check if the flag is enabled and there are pull requests associated with this commit.
350-
if v.pacInfo.SkipPushEventForPRCommits && len(prs) > 0 {
349+
isGitTagEvent := strings.HasPrefix(gitEvent.GetRef(), "refs/tags/")
350+
351+
if v.pacInfo.SkipPushEventForPRCommits && isGitTagEvent {
352+
v.Logger.Infof("Processing tag push event for commit %s despite skip-push-events-for-pr-commits being enabled (tag events are excluded from this setting)", sha)
353+
}
354+
355+
// Only check if the flag is enabled, and there are pull requests associated with this commit, and it's not a tag push event.
356+
if v.pacInfo.SkipPushEventForPRCommits && len(prs) > 0 && !isGitTagEvent {
351357
isPartOfPR, prNumber := v.isCommitPartOfPullRequest(sha, org, repoName, prs)
352358

353359
// If the commit is part of a PR, skip processing the push event

pkg/provider/github/parse_payload_test.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,18 @@ func TestParsePayLoad(t *testing.T) {
356356
samplePRNoRepo.Repo = nil
357357
samplePrEventClosed := samplePRevent
358358
samplePrEventClosed.Action = github.Ptr("closed")
359+
360+
sampleGhPRs := []*github.PullRequest{
361+
{
362+
Number: github.Ptr(41),
363+
State: github.Ptr("closed"),
364+
},
365+
{
366+
Number: github.Ptr(42),
367+
State: github.Ptr("open"),
368+
},
369+
}
370+
359371
tests := []struct {
360372
name string
361373
wantErrString string
@@ -371,6 +383,7 @@ func TestParsePayLoad(t *testing.T) {
371383
wantedBranchName string
372384
wantedTagName string
373385
isCancelPipelineRunEnabled bool
386+
skipPushEventForPRCommits bool
374387
}{
375388
{
376389
name: "bad/unknown event",
@@ -897,6 +910,41 @@ func TestParsePayLoad(t *testing.T) {
897910
wantedBranchName: "main",
898911
wantErrString: "provided SHA samplePRshanew is not the HEAD commit of the branch main",
899912
},
913+
{
914+
name: "good/skip push event for skip-pr-commits setting",
915+
eventType: "push",
916+
triggerTarget: "push",
917+
githubClient: true,
918+
payloadEventStruct: github.PushEvent{
919+
Ref: github.Ptr("refs/heads/main"),
920+
Repo: &github.PushEventRepository{
921+
Owner: &github.User{Login: github.Ptr("owner")},
922+
Name: github.Ptr("pushRepo"),
923+
},
924+
HeadCommit: &github.HeadCommit{ID: github.Ptr("SHAPush")},
925+
},
926+
shaRet: "SHAPush",
927+
skipPushEventForPRCommits: true,
928+
muxReplies: map[string]any{"/repos/owner/pushRepo/commits/SHAPush/pulls": sampleGhPRs},
929+
wantErrString: "commit SHAPush is part of pull request #42, skipping push event",
930+
},
931+
{
932+
name: "good/skip tag push event for skip-pr-commits setting",
933+
eventType: "push",
934+
triggerTarget: "push",
935+
githubClient: true,
936+
payloadEventStruct: github.PushEvent{
937+
Ref: github.Ptr("refs/tags/v1.0.0"),
938+
Repo: &github.PushEventRepository{
939+
Owner: &github.User{Login: github.Ptr("owner")},
940+
Name: github.Ptr("pushRepo"),
941+
},
942+
HeadCommit: &github.HeadCommit{ID: github.Ptr("SHAPush")},
943+
},
944+
shaRet: "SHAPush",
945+
skipPushEventForPRCommits: true,
946+
muxReplies: map[string]any{"/repos/owner/pushRepo/commits/SHAPush/pulls": sampleGhPRs},
947+
},
900948
}
901949
for _, tt := range tests {
902950
t.Run(tt.name, func(t *testing.T) {
@@ -969,7 +1017,7 @@ func TestParsePayLoad(t *testing.T) {
9691017
ghClient: ghClient,
9701018
Logger: logger,
9711019
pacInfo: &info.PacOpts{
972-
Settings: settings.Settings{},
1020+
Settings: settings.Settings{SkipPushEventForPRCommits: tt.skipPushEventForPRCommits},
9731021
},
9741022
}
9751023
request := &http.Request{Header: map[string][]string{}}

test/gitea_params_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build e2e
2+
13
package test
24

35
import (

test/github_pullrequest_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build e2e
2+
13
package test
24

35
import (
@@ -23,6 +25,7 @@ import (
2325

2426
"github.com/google/go-github/v74/github"
2527
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
28+
"github.com/tektoncd/pipeline/pkg/names"
2629
"gotest.tools/v3/assert"
2730
"gotest.tools/v3/assert/cmp"
2831
"gotest.tools/v3/golden"
@@ -651,6 +654,37 @@ func TestGithubDisableCommentsOnPR(t *testing.T) {
651654
assert.Equal(t, 0, successCommentsPost)
652655
}
653656

657+
func TestGithubIgnoreTagPushCommitsFromSkipPushEventsSetting(t *testing.T) {
658+
ctx := context.Background()
659+
g := &tgithub.PRTest{
660+
Label: "Github PullRequest",
661+
YamlFiles: []string{"testdata/pipelinerun.yaml"},
662+
NoStatusCheck: true,
663+
}
664+
g.RunPullRequest(ctx, t)
665+
defer g.TearDown(ctx, t)
666+
667+
tag := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("v1.0")
668+
669+
_, err := tgithub.CreateTag(ctx, t, g.Cnx, g.Provider, g.Options, g.SHA, tag)
670+
assert.NilError(t, err)
671+
defer tgithub.DeleteTag(ctx, g.Provider, g.Options, tag) //nolint:errcheck
672+
673+
globalNs, _, err := params.GetInstallLocation(ctx, g.Cnx)
674+
assert.NilError(t, err)
675+
ctx = info.StoreNS(ctx, globalNs)
676+
677+
reg := regexp.MustCompile(fmt.Sprintf("Processing tag push event for commit %s despite skip-push-events-for-pr-commits being enabled.*", g.SHA))
678+
maxLines := int64(100)
679+
err = twait.RegexpMatchingInControllerLog(ctx, g.Cnx, *reg, 20, "controller", &maxLines)
680+
assert.NilError(t, err)
681+
682+
g.Cnx.Clients.Log.Infof("Deleting tag %s", tag)
683+
err = tgithub.DeleteTag(ctx, g.Provider, g.Options, tag)
684+
assert.NilError(t, err)
685+
g.Cnx.Clients.Log.Infof("Tag %s has been deleted", tag)
686+
}
687+
654688
// Local Variables:
655689
// compile-command: "go test -tags=e2e -v -info TestGithubPullRequest$ ."
656690
// End:

test/github_tag_gitops_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build e2e
2+
13
package test
24

35
import (

test/pkg/github/tag.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ func CreateTag(ctx context.Context, t *testing.T, runcnx *params.Run, ghcnx *pac
2929
tag, _, err := ghcnx.Client().Git.CreateTag(ctx, opts.Organization, opts.Repo, tag)
3030
assert.NilError(t, err)
3131

32-
runcnx.Clients.Log.Infof("Tag %s has been created successfully", *tag.SHA)
33-
3432
refToCreate := &github.Reference{
3533
Ref: github.Ptr("refs/tags/" + tagName),
3634
Object: &github.GitObject{SHA: tag.SHA},
@@ -41,3 +39,8 @@ func CreateTag(ctx context.Context, t *testing.T, runcnx *params.Run, ghcnx *pac
4139

4240
return tag, nil
4341
}
42+
43+
func DeleteTag(ctx context.Context, ghcnx *pacgithub.Provider, opts options.E2E, tagName string) error {
44+
_, err := ghcnx.Client().Git.DeleteRef(ctx, opts.Organization, opts.Repo, "refs/tags/"+tagName)
45+
return err
46+
}

0 commit comments

Comments
 (0)