From d6152a03206ed78fef741f271c3a0b73f47aa766 Mon Sep 17 00:00:00 2001 From: Zaki Shaikh Date: Sun, 8 Dec 2024 09:00:37 +0530 Subject: [PATCH] Fix incoming webhook PLR match with generateName fixed an issue in PAC when pipelinerun name is defined via genetateName field it wasn't matching pipelinrun. https://issues.redhat.com/browse/SRVKP-6808 Signed-off-by: Zaki Shaikh --- docs/content/docs/guide/incoming_webhook.md | 4 +++- pkg/matcher/annotation_matcher.go | 8 ++------ pkg/matcher/annotation_matcher_test.go | 2 +- test/github_incoming_test.go | 12 ++++++------ 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/docs/content/docs/guide/incoming_webhook.md b/docs/content/docs/guide/incoming_webhook.md index 3099ce4a9..2f647d9c1 100644 --- a/docs/content/docs/guide/incoming_webhook.md +++ b/docs/content/docs/guide/incoming_webhook.md @@ -88,7 +88,9 @@ stringData: After setting this up, you will be able to start the PipelineRun with a POST request sent to the controller URL appended with /incoming. The request includes the very-secure-shared-secret, the repository name (repo), the target -branch (main), and the PipelineRun name (or the generateName if used) (target_pipelinerun). +branch (main), and the PipelineRun name. + +You can use the `generateName` field as the PipelineRun name but you will need to make sure to specify the hyphen (-) at the end. As an example here is a curl snippet starting the PipelineRun: diff --git a/pkg/matcher/annotation_matcher.go b/pkg/matcher/annotation_matcher.go index 0801085bb..ca2c37997 100644 --- a/pkg/matcher/annotation_matcher.go +++ b/pkg/matcher/annotation_matcher.go @@ -344,12 +344,8 @@ func MatchRunningPipelineRunForIncomingWebhook(eventType, incomingPipelineRun st } for _, pr := range prs { - // check incomingPipelineRun with pr name - if incomingPipelineRun == pr.GetName() { - return []*tektonv1.PipelineRun{pr} - } - // check incomingPipelineRun with pr generateName - if incomingPipelineRun == strings.TrimSuffix(pr.GetGenerateName(), "-") { + // check incomingPipelineRun with pr name or generateName + if incomingPipelineRun == pr.GetName() || incomingPipelineRun == pr.GetGenerateName() { return []*tektonv1.PipelineRun{pr} } } diff --git a/pkg/matcher/annotation_matcher_test.go b/pkg/matcher/annotation_matcher_test.go index 211a0dbc4..0ecea8550 100644 --- a/pkg/matcher/annotation_matcher_test.go +++ b/pkg/matcher/annotation_matcher_test.go @@ -2190,7 +2190,7 @@ func TestMatchRunningPipelineRunForIncomingWebhook(t *testing.T) { name: "return matched pipelinerun for matching pipelinerun generateName", runevent: info.Event{ EventType: "incoming", - TargetPipelineRun: "pr1", + TargetPipelineRun: "pr1-", }, pruns: []*tektonv1.PipelineRun{ { diff --git a/test/github_incoming_test.go b/test/github_incoming_test.go index 1705403e0..05866834d 100644 --- a/test/github_incoming_test.go +++ b/test/github_incoming_test.go @@ -34,7 +34,7 @@ func TestGithubAppIncoming(t *testing.T) { }, randomedString, randomedString, triggertype.Incoming.String(), map[string]string{}) assert.NilError(t, err) - verifyIncomingWebhook(t, randomedString, entries, false, false) + verifyIncomingWebhook(t, randomedString, "pipelinerun-incoming", entries, false, false) } func TestGithubSecondIncoming(t *testing.T) { @@ -45,7 +45,7 @@ func TestGithubSecondIncoming(t *testing.T) { }, randomedString, randomedString, triggertype.Incoming.String(), map[string]string{}) assert.NilError(t, err) - verifyIncomingWebhook(t, randomedString, entries, false, true) + verifyIncomingWebhook(t, randomedString, "pipelinerun-incoming", entries, false, true) } func TestGithubWebhookIncoming(t *testing.T) { @@ -56,7 +56,7 @@ func TestGithubWebhookIncoming(t *testing.T) { }, randomedString, randomedString, triggertype.Incoming.String(), map[string]string{}) assert.NilError(t, err) - verifyIncomingWebhook(t, randomedString, entries, true, false) + verifyIncomingWebhook(t, randomedString, "pipelinerun-incoming", entries, true, false) } // TestGithubAppIncomingForDifferentEvent tests that a Pipelinerun with the incoming event @@ -70,10 +70,10 @@ func TestGithubAppIncomingForDifferentEvent(t *testing.T) { }, randomedString, randomedString, triggertype.PullRequest.String(), map[string]string{}) assert.NilError(t, err) - verifyIncomingWebhook(t, randomedString, entries, false, false) + verifyIncomingWebhook(t, randomedString, "pipelinerun-incoming-", entries, false, false) } -func verifyIncomingWebhook(t *testing.T, randomedString string, entries map[string]string, onWebhook, onSecondController bool) { +func verifyIncomingWebhook(t *testing.T, randomedString, pipelinerunName string, entries map[string]string, onWebhook, onSecondController bool) { ctx := context.Background() ctx, runcnx, opts, ghprovider, err := tgithub.Setup(ctx, onSecondController, onWebhook) assert.NilError(t, err) @@ -120,7 +120,7 @@ func verifyIncomingWebhook(t *testing.T, randomedString string, entries map[stri runcnx.Clients.Log.Infof("Commit %s has been created and pushed to branch %s", sha, vref.GetURL()) incomingURL := fmt.Sprintf("%s/incoming?repository=%s&branch=%s&pipelinerun=%s&secret=%s", opts.ControllerURL, - randomedString, randomedString, "pipelinerun-incoming", incomingSecreteValue) + randomedString, randomedString, pipelinerunName, incomingSecreteValue) body := `{"params":{"the_best_superhero_is":"Superman"}}` client := &http.Client{} req, err := http.NewRequestWithContext(ctx, http.MethodPost, incomingURL, strings.NewReader(body))