Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/content/docs/install/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ There are a few things you can configure through the ConfigMap

Default: `true`

**Note:** This setting does not apply to git tag push events. Tag push events will always trigger
pipeline runs regardless of whether the tagged commit is part of an open pull request.

{{< support_matrix github_app="true" github_webhook="true" gitea="false" gitlab="false" bitbucket_cloud="false" bitbucket_datacenter="false" >}}

### Global Cancel In Progress Settings
Expand Down
10 changes: 8 additions & 2 deletions pkg/provider/github/parse_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,14 @@ func (v *Provider) processEvent(ctx context.Context, event *info.Event, eventInt
v.Logger.Warnf("Error getting pull requests associated with the commit in this push event: %v", err)
}

// Only check if the flag is enabled and there are pull requests associated with this commit.
if v.pacInfo.SkipPushEventForPRCommits && len(prs) > 0 {
isGitTagEvent := strings.HasPrefix(gitEvent.GetRef(), "refs/tags/")

if v.pacInfo.SkipPushEventForPRCommits && isGitTagEvent {
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)
}

// Only check if the flag is enabled, and there are pull requests associated with this commit, and it's not a tag push event.
if v.pacInfo.SkipPushEventForPRCommits && len(prs) > 0 && !isGitTagEvent {
isPartOfPR, prNumber := v.isCommitPartOfPullRequest(sha, org, repoName, prs)

// If the commit is part of a PR, skip processing the push event
Expand Down
50 changes: 49 additions & 1 deletion pkg/provider/github/parse_payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,18 @@ func TestParsePayLoad(t *testing.T) {
samplePRNoRepo.Repo = nil
samplePrEventClosed := samplePRevent
samplePrEventClosed.Action = github.Ptr("closed")

sampleGhPRs := []*github.PullRequest{
{
Number: github.Ptr(41),
State: github.Ptr("closed"),
},
{
Number: github.Ptr(42),
State: github.Ptr("open"),
},
}

tests := []struct {
name string
wantErrString string
Expand All @@ -371,6 +383,7 @@ func TestParsePayLoad(t *testing.T) {
wantedBranchName string
wantedTagName string
isCancelPipelineRunEnabled bool
skipPushEventForPRCommits bool
}{
{
name: "bad/unknown event",
Expand Down Expand Up @@ -897,6 +910,41 @@ func TestParsePayLoad(t *testing.T) {
wantedBranchName: "main",
wantErrString: "provided SHA samplePRshanew is not the HEAD commit of the branch main",
},
{
name: "good/skip push event for skip-pr-commits setting",
eventType: "push",
triggerTarget: "push",
githubClient: true,
payloadEventStruct: github.PushEvent{
Ref: github.Ptr("refs/heads/main"),
Repo: &github.PushEventRepository{
Owner: &github.User{Login: github.Ptr("owner")},
Name: github.Ptr("pushRepo"),
},
HeadCommit: &github.HeadCommit{ID: github.Ptr("SHAPush")},
},
shaRet: "SHAPush",
skipPushEventForPRCommits: true,
muxReplies: map[string]any{"/repos/owner/pushRepo/commits/SHAPush/pulls": sampleGhPRs},
wantErrString: "commit SHAPush is part of pull request #42, skipping push event",
},
{
name: "good/skip tag push event for skip-pr-commits setting",
eventType: "push",
triggerTarget: "push",
githubClient: true,
payloadEventStruct: github.PushEvent{
Ref: github.Ptr("refs/tags/v1.0.0"),
Repo: &github.PushEventRepository{
Owner: &github.User{Login: github.Ptr("owner")},
Name: github.Ptr("pushRepo"),
},
HeadCommit: &github.HeadCommit{ID: github.Ptr("SHAPush")},
},
shaRet: "SHAPush",
skipPushEventForPRCommits: true,
muxReplies: map[string]any{"/repos/owner/pushRepo/commits/SHAPush/pulls": sampleGhPRs},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -969,7 +1017,7 @@ func TestParsePayLoad(t *testing.T) {
ghClient: ghClient,
Logger: logger,
pacInfo: &info.PacOpts{
Settings: settings.Settings{},
Settings: settings.Settings{SkipPushEventForPRCommits: tt.skipPushEventForPRCommits},
},
}
request := &http.Request{Header: map[string][]string{}}
Expand Down
2 changes: 2 additions & 0 deletions test/gitea_params_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build e2e

package test

import (
Expand Down
34 changes: 34 additions & 0 deletions test/github_pullrequest_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build e2e

package test

import (
Expand All @@ -23,6 +25,7 @@ import (

"github.com/google/go-github/v74/github"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/names"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
"gotest.tools/v3/golden"
Expand Down Expand Up @@ -651,6 +654,37 @@ func TestGithubDisableCommentsOnPR(t *testing.T) {
assert.Equal(t, 0, successCommentsPost)
}

func TestGithubIgnoreTagPushCommitsFromSkipPushEventsSetting(t *testing.T) {
ctx := context.Background()
g := &tgithub.PRTest{
Label: "Github PullRequest",
YamlFiles: []string{"testdata/pipelinerun.yaml"},
NoStatusCheck: true,
}
g.RunPullRequest(ctx, t)
defer g.TearDown(ctx, t)

tag := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("v1.0")

_, err := tgithub.CreateTag(ctx, t, g.Cnx, g.Provider, g.Options, g.SHA, tag)
assert.NilError(t, err)
defer tgithub.DeleteTag(ctx, g.Provider, g.Options, tag) //nolint:errcheck

globalNs, _, err := params.GetInstallLocation(ctx, g.Cnx)
assert.NilError(t, err)
ctx = info.StoreNS(ctx, globalNs)

reg := regexp.MustCompile(fmt.Sprintf("Processing tag push event for commit %s despite skip-push-events-for-pr-commits being enabled.*", g.SHA))
maxLines := int64(100)
err = twait.RegexpMatchingInControllerLog(ctx, g.Cnx, *reg, 20, "controller", &maxLines)
assert.NilError(t, err)

g.Cnx.Clients.Log.Infof("Deleting tag %s", tag)
err = tgithub.DeleteTag(ctx, g.Provider, g.Options, tag)
assert.NilError(t, err)
g.Cnx.Clients.Log.Infof("Tag %s has been deleted", tag)
}

// Local Variables:
// compile-command: "go test -tags=e2e -v -info TestGithubPullRequest$ ."
// End:
2 changes: 2 additions & 0 deletions test/github_tag_gitops_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build e2e

package test

import (
Expand Down
7 changes: 5 additions & 2 deletions test/pkg/github/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ func CreateTag(ctx context.Context, t *testing.T, runcnx *params.Run, ghcnx *pac
tag, _, err := ghcnx.Client().Git.CreateTag(ctx, opts.Organization, opts.Repo, tag)
assert.NilError(t, err)

runcnx.Clients.Log.Infof("Tag %s has been created successfully", *tag.SHA)

refToCreate := &github.Reference{
Ref: github.Ptr("refs/tags/" + tagName),
Object: &github.GitObject{SHA: tag.SHA},
Expand All @@ -41,3 +39,8 @@ func CreateTag(ctx context.Context, t *testing.T, runcnx *params.Run, ghcnx *pac

return tag, nil
}

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