Skip to content

Commit

Permalink
Fix incoming webhook PLR match with generateName
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
zakisk committed Dec 13, 2024
1 parent bbd6739 commit 27d08ed
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
4 changes: 3 additions & 1 deletion docs/content/docs/guide/incoming_webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
8 changes: 2 additions & 6 deletions pkg/matcher/annotation_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/matcher/annotation_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down
12 changes: 6 additions & 6 deletions test/github_incoming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 27d08ed

Please sign in to comment.