Skip to content

Commit c5da652

Browse files
authored
Merge branch 'main' into report-yaml-error-properly-as-comment
2 parents 97a1902 + 5ade485 commit c5da652

File tree

9 files changed

+161
-22
lines changed

9 files changed

+161
-22
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{

pkg/pipelineascode/cancel_pipelineruns.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ var cancelMergePatch = map[string]any{
2828
},
2929
}
3030

31-
// cancelAllInProgressBelongingToPullRequest cancels all in-progress PipelineRuns
31+
// cancelAllInProgressBelongingToClosedPullRequest cancels all in-progress PipelineRuns
3232
// that belong to a specific pull request in the given repository.
33-
func (p *PacRun) cancelAllInProgressBelongingToPullRequest(ctx context.Context, repo *v1alpha1.Repository) error {
33+
func (p *PacRun) cancelAllInProgressBelongingToClosedPullRequest(ctx context.Context, repo *v1alpha1.Repository) error {
3434
labelSelector := getLabelSelector(map[string]string{
3535
keys.URLRepository: formatting.CleanValueKubernetes(p.event.Repository),
3636
keys.PullRequest: strconv.Itoa(int(p.event.PullRequestNumber)),
@@ -70,7 +70,9 @@ func (p *PacRun) cancelInProgressMatchingPR(ctx context.Context, matchPR *tekton
7070
return nil
7171
}
7272

73-
prName, ok := matchPR.GetAnnotations()[keys.OriginalPRName]
73+
// As PipelineRuns are filtered by name, OriginalPRName should be taken from
74+
// labels instead of annotations because of constraints imposed by kube API.
75+
prName, ok := matchPR.GetLabels()[keys.OriginalPRName]
7476
if !ok {
7577
return nil
7678
}

pkg/pipelineascode/cancel_pipelineruns_test.go

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func TestCancelInProgressMatchingPR(t *testing.T) {
354354
ObjectMeta: metav1.ObjectMeta{
355355
Name: "pr-foo",
356356
Namespace: "foo",
357-
Labels: fooRepoLabels,
357+
Labels: map[string]string{},
358358
Annotations: map[string]string{
359359
keys.CancelInProgress: "true",
360360
},
@@ -523,9 +523,10 @@ func TestCancelInProgressMatchingPR(t *testing.T) {
523523
Namespace: "foo",
524524
Labels: fooRepoLabels,
525525
Annotations: map[string]string{
526-
keys.CancelInProgress: "true", keys.OriginalPRName: "pr-foo",
527-
keys.Repository: "foo",
528-
keys.SourceBranch: "head",
526+
keys.CancelInProgress: "true",
527+
keys.OriginalPRName: "pr-foo",
528+
keys.Repository: "foo",
529+
keys.SourceBranch: "head",
529530
},
530531
},
531532
Spec: pipelinev1.PipelineRunSpec{},
@@ -537,6 +538,54 @@ func TestCancelInProgressMatchingPR(t *testing.T) {
537538
},
538539
wantLog: "cancel-in-progress: cancelling pipelinerun foo/",
539540
},
541+
{
542+
name: "match/cancel in progress on PipelineRun generateName",
543+
event: &info.Event{
544+
Repository: "foo",
545+
SHA: "foosha",
546+
HeadBranch: "head",
547+
EventType: string(triggertype.PullRequest),
548+
TriggerTarget: triggertype.PullRequest,
549+
PullRequestNumber: pullReqNumber,
550+
},
551+
pipelineRuns: []*pipelinev1.PipelineRun{
552+
{
553+
ObjectMeta: metav1.ObjectMeta{
554+
GenerateName: "pr-foo-",
555+
Name: "pr-foo-1",
556+
Namespace: "foo",
557+
Labels: fooRepoLabels,
558+
Annotations: map[string]string{
559+
keys.CancelInProgress: "true",
560+
keys.OriginalPRName: "pr-foo",
561+
keys.Repository: "foo",
562+
keys.SourceBranch: "head",
563+
},
564+
},
565+
Spec: pipelinev1.PipelineRunSpec{},
566+
},
567+
{
568+
ObjectMeta: metav1.ObjectMeta{
569+
GenerateName: "pr-foo-",
570+
Name: "pr-foo-2",
571+
Namespace: "foo",
572+
Labels: fooRepoLabels,
573+
Annotations: map[string]string{
574+
keys.CancelInProgress: "true",
575+
keys.OriginalPRName: "pr-foo",
576+
keys.Repository: "foo",
577+
keys.SourceBranch: "head",
578+
},
579+
},
580+
Spec: pipelinev1.PipelineRunSpec{},
581+
},
582+
},
583+
repo: fooRepo,
584+
cancelledPipelineRuns: map[string]bool{
585+
"pr-foo-2": true,
586+
},
587+
wantLog: "cancel-in-progress: cancelling pipelinerun foo/pr-foo-2",
588+
},
540589
{
541590
name: "match/cancel in progress from /retest",
542591
event: &info.Event{
@@ -889,7 +938,7 @@ func TestCancelAllInProgressBelongingToPullRequest(t *testing.T) {
889938
},
890939
}
891940
pac := NewPacs(tt.event, nil, cs, &info.PacOpts{}, nil, logger, nil)
892-
err := pac.cancelAllInProgressBelongingToPullRequest(ctx, tt.repo)
941+
err := pac.cancelAllInProgressBelongingToClosedPullRequest(ctx, tt.repo)
893942
assert.NilError(t, err)
894943

895944
got, err := cs.Clients.Tekton.TektonV1().PipelineRuns("foo").List(ctx, metav1.ListOptions{})

pkg/pipelineascode/pipelineascode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func NewPacs(event *info.Event, vcx provider.Interface, run *params.Run, pacInfo
5858
func (p *PacRun) Run(ctx context.Context) error {
5959
matchedPRs, repo, err := p.matchRepoPR(ctx)
6060
if repo != nil && p.event.TriggerTarget == triggertype.PullRequestClosed {
61-
if err := p.cancelAllInProgressBelongingToPullRequest(ctx, repo); err != nil {
61+
if err := p.cancelAllInProgressBelongingToClosedPullRequest(ctx, repo); err != nil {
6262
return fmt.Errorf("error cancelling in progress pipelineRuns belonging to pull request %d: %w", p.event.PullRequestNumber, err)
6363
}
6464
return nil

test/github_pullrequest_test.go

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

389+
func TestGithubPullRequestNoOnLabelAnnotation(t *testing.T) {
390+
ctx := context.Background()
391+
g := &tgithub.PRTest{
392+
Label: "Github PullRequest",
393+
YamlFiles: []string{"testdata/pipelinerun-pr-cel-expression.yaml"},
394+
}
395+
g.RunPullRequest(ctx, t)
396+
defer g.TearDown(ctx, t)
397+
398+
g.Cnx.Clients.Log.Infof("Creating a label bug on PullRequest")
399+
_, _, err := g.Provider.Client.Issues.AddLabelsToIssue(ctx,
400+
g.Options.Organization,
401+
g.Options.Repo, g.PRNumber,
402+
[]string{"bug"})
403+
assert.NilError(t, err)
404+
405+
// let's wait 10 secs and check every second that a PipelineRun is created or not.
406+
for i := 0; i < 10; i++ {
407+
prs, err := g.Cnx.Clients.Tekton.TektonV1().PipelineRuns(g.TargetNamespace).List(ctx, metav1.ListOptions{})
408+
assert.NilError(t, err)
409+
// after adding a label on the PR we need to make sure that it doesn't trigger another PipelineRun.
410+
assert.Equal(t, len(prs.Items), 1)
411+
time.Sleep(1 * time.Second)
412+
}
413+
}
414+
389415
// Local Variables:
390416
// compile-command: "go test -tags=e2e -v -info TestGithubPullRequest$ ."
391417
// End:

test/gitlab_merge_request_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,15 +421,17 @@ func TestGitlabCancelInProgressOnPRClose(t *testing.T) {
421421
})
422422
assert.NilError(t, err)
423423

424-
runcnx.Clients.Log.Infof("Sleeping for 10 seconds to let the pipelinerun to be canceled")
425-
time.Sleep(10 * time.Second)
424+
err = twait.UntilPipelineRunHasReason(ctx, runcnx.Clients, v1.PipelineRunReasonCancelled, waitOpts)
425+
assert.NilError(t, err)
426426

427427
prs, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(context.Background(), metav1.ListOptions{})
428428
assert.NilError(t, err)
429429
assert.Equal(t, len(prs.Items), 1, "should have only one pipelinerun, but we have: %d", len(prs.Items))
430430
assert.Equal(t, prs.Items[0].GetStatusCondition().GetCondition(apis.ConditionSucceeded).GetReason(), "Cancelled", "should have been canceled")
431431

432-
repo, err := runcnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(targetNS).Get(ctx, targetNS, metav1.GetOptions{})
432+
// failing on `true` condition because for cancelled PipelineRun we want `false` condition.
433+
waitOpts.FailOnRepoCondition = corev1.ConditionTrue
434+
repo, err := twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts)
433435
assert.NilError(t, err)
434436

435437
laststatus := repo.Status[len(repo.Status)-1]

test/pkg/wait/wait.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ import (
1616
)
1717

1818
type Opts struct {
19-
RepoName string
20-
Namespace string
21-
MinNumberStatus int
22-
PollTimeout time.Duration
23-
AdminNS string
24-
TargetSHA string
19+
RepoName string
20+
Namespace string
21+
MinNumberStatus int
22+
PollTimeout time.Duration
23+
AdminNS string
24+
TargetSHA string
25+
FailOnRepoCondition corev1.ConditionStatus
2526
}
2627

2728
func UntilMinPRAppeared(ctx context.Context, clients clients.Clients, opts Opts, minNumber int) error {
@@ -58,7 +59,11 @@ func UntilRepositoryUpdated(ctx context.Context, clients clients.Clients, opts O
5859
}
5960
if len(prs.Items) > 0 {
6061
prConditions := prs.Items[0].Status.Conditions
61-
if len(prConditions) != 0 && prConditions[0].Status == corev1.ConditionFalse {
62+
if opts.FailOnRepoCondition == "" {
63+
opts.FailOnRepoCondition = corev1.ConditionFalse
64+
}
65+
66+
if len(prConditions) != 0 && prConditions[0].Status == opts.FailOnRepoCondition {
6267
return true, fmt.Errorf("pipelinerun has failed")
6368
}
6469
}
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)