Skip to content

Commit c1233eb

Browse files
zakiskpipelines-as-code[bot]
authored andcommitted
fix: Triggering PipelineRun on label addition in Github PRs
fixed an issue where PAC is triggering PipelineRuns on label addition on PRs when on-label annotation is not explicitly used and on-cel-expression is being used for matching event in PipelineRun definition. Now, before matching annotations it is checking for label_update event type and on-label annoation and if it doesn't present then ignoring the PipelineRun with a log message and still preserving on-cel-expression precendence over on-label. https://issues.redhat.com/browse/SRVKP-7378 https://issues.redhat.com/browse/SRVKP-7443 Signed-off-by: Zaki Shaikh <[email protected]>
1 parent de47bef commit c1233eb

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

pkg/matcher/annotation_matcher.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ func MatchPipelinerunByAnnotation(ctx context.Context, logger *zap.SugaredLogger
226226
if event.EventType == opscomments.NoOpsCommentEventType.String() || event.EventType == opscomments.OnCommentEventType.String() {
227227
continue
228228
}
229+
230+
// If the event is a pull_request and the event type is label_update, but the PipelineRun
231+
// does not contain an 'on-label' annotation, do not match this PipelineRun, as it is not intended for this event.
232+
_, ok := prun.GetObjectMeta().GetAnnotations()[keys.OnLabel]
233+
if event.TriggerTarget == triggertype.PullRequest && event.EventType == string(triggertype.LabelUpdate) && !ok {
234+
logger.Infof("label update event, PipelineRun %s does not have a on-label for any of those labels: %s", prName, strings.Join(event.PullRequestLabel, "|"))
235+
continue
236+
}
237+
229238
if celExpr, ok := prun.GetObjectMeta().GetAnnotations()[keys.OnCelExpression]; ok {
230239
out, err := celEvaluate(ctx, celExpr, event, vcx)
231240
if err != nil {
@@ -278,9 +287,6 @@ func MatchPipelinerunByAnnotation(ctx context.Context, logger *zap.SugaredLogger
278287
}
279288
logger.Infof("matched PipelineRun with name: %s, annotation Label: %q", prName, key)
280289
prMatch.Config["label"] = key
281-
} else if event.EventType == string(triggertype.LabelUpdate) {
282-
logger.Infof("label update event, PipelineRun %s does not have a on-label for any of those labels: %s", prName, strings.Join(event.PullRequestLabel, "|"))
283-
continue
284290
}
285291

286292
if key, ok := prun.GetObjectMeta().GetAnnotations()[keys.OnPathChangeIgnore]; ok {

pkg/matcher/annotation_matcher_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,37 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
16401640
"matching pipelineruns to event: URL=https://hello/moto, target-branch=main, source-branch=source, target-event=pull_request, labels=documentation, pull-request=10",
16411641
},
16421642
},
1643+
{
1644+
name: "no-on-label-annotation-on-pr",
1645+
args: args{
1646+
pruns: []*tektonv1.PipelineRun{
1647+
{
1648+
ObjectMeta: metav1.ObjectMeta{
1649+
Name: "pipeline-label",
1650+
Annotations: map[string]string{
1651+
keys.OnEvent: "[pull_request]",
1652+
keys.OnTargetBranch: "[main]",
1653+
},
1654+
},
1655+
},
1656+
pipelineGood,
1657+
},
1658+
runevent: info.Event{
1659+
URL: "https://hello/moto",
1660+
TriggerTarget: triggertype.PullRequest,
1661+
EventType: string(triggertype.LabelUpdate),
1662+
HeadBranch: "source",
1663+
BaseBranch: "main",
1664+
PullRequestNumber: 10,
1665+
PullRequestLabel: []string{"documentation"},
1666+
},
1667+
},
1668+
wantErr: true,
1669+
wantLog: []string{
1670+
"label update event, PipelineRun pipeline-label does not have a on-label for any of those labels: documentation",
1671+
"label update event, PipelineRun pipeline-good does not have a on-label for any of those labels: documentation",
1672+
},
1673+
},
16431674
{
16441675
name: "match-on-comment",
16451676
args: args{

test/github_pullrequest_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,32 @@ func TestGithubSecondCancelInProgressPRClosed(t *testing.T) {
395395
assert.Equal(t, res.CheckRuns[0].GetConclusion(), "cancelled")
396396
}
397397

398+
func TestGithubPullRequestNoOnLabelAnnotation(t *testing.T) {
399+
ctx := context.Background()
400+
g := &tgithub.PRTest{
401+
Label: "Github PullRequest",
402+
YamlFiles: []string{"testdata/pipelinerun-pr-cel-expression.yaml"},
403+
}
404+
g.RunPullRequest(ctx, t)
405+
defer g.TearDown(ctx, t)
406+
407+
g.Cnx.Clients.Log.Infof("Creating a label bug on PullRequest")
408+
_, _, err := g.Provider.Client.Issues.AddLabelsToIssue(ctx,
409+
g.Options.Organization,
410+
g.Options.Repo, g.PRNumber,
411+
[]string{"bug"})
412+
assert.NilError(t, err)
413+
414+
// let's wait 10 secs and check every second that a PipelineRun is created or not.
415+
for i := 0; i < 10; i++ {
416+
prs, err := g.Cnx.Clients.Tekton.TektonV1().PipelineRuns(g.TargetNamespace).List(ctx, metav1.ListOptions{})
417+
assert.NilError(t, err)
418+
// after adding a label on the PR we need to make sure that it doesn't trigger another PipelineRun.
419+
assert.Equal(t, len(prs.Items), 1)
420+
time.Sleep(1 * time.Second)
421+
}
422+
}
423+
398424
// Local Variables:
399425
// compile-command: "go test -tags=e2e -v -info TestGithubPullRequest$ ."
400426
// End:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
apiVersion: tekton.dev/v1beta1
3+
kind: PipelineRun
4+
metadata:
5+
name: "\\ .PipelineName //"
6+
annotations:
7+
pipelinesascode.tekton.dev/target-namespace: "\\ .TargetNamespace //"
8+
pipelinesascode.tekton.dev/on-cel-expression: event == "\\ .TargetEvent //" && target_branch == "\\ .TargetBranch //"
9+
spec:
10+
pipelineSpec:
11+
tasks:
12+
- name: task
13+
displayName: "The Task name is Task"
14+
taskSpec:
15+
steps:
16+
- name: task
17+
image: registry.access.redhat.com/ubi9/ubi-micro
18+
command: ["/bin/echo", "HELLOMOTO"]

0 commit comments

Comments
 (0)