Skip to content

Commit

Permalink
Addressing the event title matching issue
Browse files Browse the repository at this point in the history
When event_title.startsWith() is used
for pull_request, the CEL expressions
do not behave as anticipated due to the
lack of handling for pull_request events.

Signed-off-by: Savita Ashture <[email protected]>
  • Loading branch information
savitaashture authored and chmouel committed Dec 8, 2023
1 parent 5c0708a commit 3053676
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/provider/github/parse_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func (v *Provider) processEvent(ctx context.Context, event *info.Event, eventInt
processedEvent.Sender = gitEvent.GetPullRequest().GetUser().GetLogin()
processedEvent.EventType = event.EventType
processedEvent.PullRequestNumber = gitEvent.GetPullRequest().GetNumber()
processedEvent.PullRequestTitle = gitEvent.GetPullRequest().GetTitle()
// getting the repository ids of the base and head of the pull request
// to scope the token to
v.RepositoryIDs = []int64{
Expand Down
4 changes: 4 additions & 0 deletions pkg/provider/github/parse_payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var samplePRevent = github.PullRequestEvent{
User: &github.User{
Login: github.String("user"),
},
Title: github.String("my first PR"),
},
Repo: sampleRepo,
}
Expand Down Expand Up @@ -577,6 +578,9 @@ func TestParsePayLoad(t *testing.T) {
assert.NilError(t, err)
assert.Assert(t, ret != nil)
assert.Equal(t, tt.shaRet, ret.SHA)
if tt.eventType == "pull_request" {
assert.Equal(t, "my first PR", ret.PullRequestTitle)
}
if tt.eventType == "commit_comment" {
assert.Equal(t, tt.wantedBranchName, ret.HeadBranch)
assert.Equal(t, tt.wantedBranchName, ret.BaseBranch)
Expand Down
7 changes: 7 additions & 0 deletions test/github_pullrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ func TestGithubPullRequestMatchOnCEL(t *testing.T) {
defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts)
}

func TestGithubPullRequestCELMatchOnTitle(t *testing.T) {
ctx := context.Background()
runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPullRequest(ctx, t, "Github PullRequest",
[]string{"testdata/pipelinerun-cel-annotation-for-title-match.yaml"}, false)
defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts)
}

func TestGithubPullRequestWebhook(t *testing.T) {
if os.Getenv("NIGHTLY_E2E_TEST") != "true" {
t.Skip("Skipping test since only enabled for nightly")
Expand Down
13 changes: 13 additions & 0 deletions test/github_push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,16 @@ func TestGithubPush(t *testing.T) {
defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts)
}
}

func TestGithubPushRequestCELMatchOnTitle(t *testing.T) {
ctx := context.Background()
for _, onWebhook := range []bool{false, true} {
if onWebhook && os.Getenv("TEST_GITHUB_REPO_OWNER_WEBHOOK") == "" {
t.Skip("TEST_GITHUB_REPO_OWNER_WEBHOOK is not set")
continue
}
runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, _ := tgithub.RunPushRequest(ctx, t,
"Github Push Request", []string{"testdata/pipelinerun-cel-annotation-for-title-match.yaml"}, onWebhook)
defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts)
}
}
18 changes: 18 additions & 0 deletions test/testdata/pipelinerun-cel-annotation-for-title-match.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: "\\ .PipelineName //"
annotations:
pipelinesascode.tekton.dev/target-namespace: "\\ .TargetNamespace //"
pipelinesascode.tekton.dev/on-cel-expression: |
event == "\\ .TargetEvent //" && event_title.startsWith("Testing Github")
spec:
pipelineSpec:
tasks:
- name: task
taskSpec:
steps:
- name: task
image: registry.access.redhat.com/ubi9/ubi-micro
command: ["/bin/echo", "HELLOMOTO"]

0 comments on commit 3053676

Please sign in to comment.