From 3053676e2149403d8e033a42f2607533ca425cf0 Mon Sep 17 00:00:00 2001 From: savitaashture Date: Tue, 5 Dec 2023 19:05:22 +0530 Subject: [PATCH] Addressing the event title matching issue 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 --- pkg/provider/github/parse_payload.go | 1 + pkg/provider/github/parse_payload_test.go | 4 ++++ test/github_pullrequest_test.go | 7 +++++++ test/github_push_test.go | 13 +++++++++++++ ...linerun-cel-annotation-for-title-match.yaml | 18 ++++++++++++++++++ 5 files changed, 43 insertions(+) create mode 100644 test/testdata/pipelinerun-cel-annotation-for-title-match.yaml diff --git a/pkg/provider/github/parse_payload.go b/pkg/provider/github/parse_payload.go index 2d980a7e4..8f34b3b45 100644 --- a/pkg/provider/github/parse_payload.go +++ b/pkg/provider/github/parse_payload.go @@ -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{ diff --git a/pkg/provider/github/parse_payload_test.go b/pkg/provider/github/parse_payload_test.go index 5636f0cc2..fc74a1e8c 100644 --- a/pkg/provider/github/parse_payload_test.go +++ b/pkg/provider/github/parse_payload_test.go @@ -65,6 +65,7 @@ var samplePRevent = github.PullRequestEvent{ User: &github.User{ Login: github.String("user"), }, + Title: github.String("my first PR"), }, Repo: sampleRepo, } @@ -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) diff --git a/test/github_pullrequest_test.go b/test/github_pullrequest_test.go index 8d91d8db4..d963ac3ba 100644 --- a/test/github_pullrequest_test.go +++ b/test/github_pullrequest_test.go @@ -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") diff --git a/test/github_push_test.go b/test/github_push_test.go index b58a7ba7b..02445887e 100644 --- a/test/github_push_test.go +++ b/test/github_push_test.go @@ -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) + } +} diff --git a/test/testdata/pipelinerun-cel-annotation-for-title-match.yaml b/test/testdata/pipelinerun-cel-annotation-for-title-match.yaml new file mode 100644 index 000000000..4cf7b597e --- /dev/null +++ b/test/testdata/pipelinerun-cel-annotation-for-title-match.yaml @@ -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"]