Skip to content

Commit f369f4f

Browse files
zakiskchmouel
authored andcommitted
fix: PipelineRun cancel-in-progress issue when generateName is used
When PipelineRun is specified using generateName field, PAC unable to filter out and cancel PipelineRuns based on the generateName field due to trailing hyphen in the generateName field, only alphanumeric characters are allowed at begining and end of Kubernetes resources labels thus getting error when filtering PipelineRuns based on originalPRName label. Thus this PR uses originalPRName label instead of annotation to filter the PipelineRun. https://issues.redhat.com/browse/SRVKP-7337 Signed-off-by: Zaki Shaikh <[email protected]>
1 parent c1233eb commit f369f4f

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

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

0 commit comments

Comments
 (0)