@@ -27,13 +27,14 @@ var cancelMergePatch = map[string]interface{}{
2727 },
2828}
2929
30- // cancelAllInProgressBelongingToPullRequest cancels all in-progress PipelineRuns
31- // that belong to a specific pull request in the given repository.
32- func (p * PacRun ) cancelAllInProgressBelongingToPullRequest (ctx context.Context , repo * v1alpha1.Repository ) error {
33- labelSelector := getLabelSelector (map [string ]string {
34- keys .URLRepository : formatting .CleanValueKubernetes (p .event .Repository ),
35- keys .PullRequest : strconv .Itoa (int (p .event .PullRequestNumber )),
36- })
30+ // cancelInProgressPipelineRunsForRepository cancels all in-progress PipelineRuns
31+ // that are selected by the given labelSelectorMap and for which the matchFunc returns true.
32+ func (p * PacRun ) cancelInProgressPipelineRunsForRepository (ctx context.Context , repo * v1alpha1.Repository , labelSelectorMap map [string ]string , matchFunc matchingCond ) error {
33+ labelSelectorMap [keys .URLRepository ] = formatting .CleanValueKubernetes (p .event .Repository )
34+
35+ labelSelector := getLabelSelector (labelSelectorMap )
36+ p .run .Clients .Log .Infof ("cancel-in-progress: selecting pipelineRuns to cancel with labels: %v" , labelSelector )
37+
3738 prs , err := p .run .Clients .Tekton .TektonV1 ().PipelineRuns (repo .Namespace ).List (ctx , metav1.ListOptions {
3839 LabelSelector : labelSelector ,
3940 })
@@ -48,20 +49,29 @@ func (p *PacRun) cancelAllInProgressBelongingToPullRequest(ctx context.Context,
4849 return nil
4950 }
5051
51- p .cancelPipelineRuns (ctx , prs , repo , func (_ tektonv1.PipelineRun ) bool {
52- return true
53- })
52+ p .cancelPipelineRuns (ctx , prs , repo , matchFunc )
5453
5554 return nil
5655}
5756
58- // cancelInProgressMatchingPR cancels all PipelineRuns associated with a given repository and pull request,
57+ // cancelAllInProgressBelongingToPullRequest cancels all in-progress PipelineRuns
58+ // which are associated with the event's Pull Request.
59+ func (p * PacRun ) cancelAllInProgressBelongingToPullRequest (ctx context.Context , repo * v1alpha1.Repository ) error {
60+ return p .cancelInProgressPipelineRunsForRepository (
61+ ctx ,
62+ repo ,
63+ map [string ]string {keys .PullRequest : strconv .Itoa (int (p .event .PullRequestNumber ))},
64+ func (_ tektonv1.PipelineRun ) bool { return true },
65+ )
66+ }
67+
68+ // cancelInProgressExceptMatchingPR cancels all PipelineRuns associated with a given repository and pull request,
5969// except for the one that triggered the cancellation. It first checks if the cancellation is in progress
6070// and if the repository has a concurrency limit. If a concurrency limit is set, it returns an error as
6171// cancellation is not supported with concurrency limits. It then retrieves the original pull request name
6272// from the annotations and lists all PipelineRuns with matching labels. For each PipelineRun that is not
6373// already done, cancelled, or gracefully stopped, it patches the PipelineRun to cancel it.
64- func (p * PacRun ) cancelInProgressMatchingPR (ctx context.Context , matchPR * tektonv1.PipelineRun , repo * v1alpha1.Repository ) error {
74+ func (p * PacRun ) cancelInProgressExceptMatchingPR (ctx context.Context , matchPR * tektonv1.PipelineRun , repo * v1alpha1.Repository ) error {
6575 if matchPR == nil {
6676 return nil
6777 }
@@ -79,77 +89,53 @@ func (p *PacRun) cancelInProgressMatchingPR(ctx context.Context, matchPR *tekton
7989 }
8090
8191 labelMap := map [string ]string {
82- keys .URLRepository : formatting .CleanValueKubernetes (p .event .Repository ),
8392 keys .OriginalPRName : prName ,
8493 }
8594 if p .event .TriggerTarget == triggertype .PullRequest {
8695 labelMap [keys .PullRequest ] = strconv .Itoa (p .event .PullRequestNumber )
8796 }
88- labelSelector := getLabelSelector (labelMap )
89- p .run .Clients .Log .Infof ("cancel-in-progress: selecting pipelineRuns to cancel with labels: %v" , labelSelector )
90- prs , err := p .run .Clients .Tekton .TektonV1 ().PipelineRuns (matchPR .GetNamespace ()).List (ctx , metav1.ListOptions {
91- LabelSelector : labelSelector ,
92- })
93- if err != nil {
94- return fmt .Errorf ("failed to list pipelineRuns : %w" , err )
95- }
96-
97- p .cancelPipelineRuns (ctx , prs , repo , func (pr tektonv1.PipelineRun ) bool {
98- // skip our own for cancellation
99- if sourceBranch , ok := pr .GetAnnotations ()[keys .SourceBranch ]; ok {
100- // NOTE(chmouel): Every PR has their own branch and so is every push to different branch
101- // it means we only cancel pipelinerun of the same name that runs to
102- // the unique branch. Note: HeadBranch is the branch from where the PR
103- // comes from in git jargon.
104- if sourceBranch != p .event .HeadBranch {
105- p .logger .Infof ("cancel-in-progress: skipping pipelinerun %v/%v as it is not from the same branch, annotation source-branch: %s event headbranch: %s" , pr .GetNamespace (), pr .GetName (), sourceBranch , p .event .HeadBranch )
106- return false
97+ return p .cancelInProgressPipelineRunsForRepository (
98+ ctx ,
99+ repo ,
100+ labelMap ,
101+ func (pr tektonv1.PipelineRun ) bool {
102+ // skip our own for cancellation
103+ if sourceBranch , ok := pr .GetAnnotations ()[keys .SourceBranch ]; ok {
104+ // NOTE(chmouel): Every PR has their own branch and so is every push to different branch
105+ // it means we only cancel pipelinerun of the same name that runs to
106+ // the unique branch. Note: HeadBranch is the branch from where the PR
107+ // comes from in git jargon.
108+ if sourceBranch != p .event .HeadBranch {
109+ p .logger .Infof ("cancel-in-progress: skipping pipelinerun %v/%v as it is not from the same branch, annotation source-branch: %s event headbranch: %s" , pr .GetNamespace (), pr .GetName (), sourceBranch , p .event .HeadBranch )
110+ return false
111+ }
107112 }
108- }
109113
110- return pr .GetName () != matchPR .GetName ()
111- })
112- return nil
114+ return pr .GetName () != matchPR .GetName ()
115+ })
113116}
114117
115118// cancelPipelineRunsOpsComment cancels all PipelineRuns associated with a given repository and pull request.
116119// when the user issue a cancel comment.
117120func (p * PacRun ) cancelPipelineRunsOpsComment (ctx context.Context , repo * v1alpha1.Repository ) error {
118- labelSelector := getLabelSelector (map [string ]string {
119- keys .URLRepository : formatting .CleanValueKubernetes (p .event .Repository ),
120- keys .SHA : formatting .CleanValueKubernetes (p .event .SHA ),
121- })
121+ labelMap := map [string ]string {
122+ keys .SHA : formatting .CleanValueKubernetes (p .event .SHA ),
123+ }
122124
123125 if p .event .TriggerTarget == triggertype .PullRequest {
124- labelSelector = getLabelSelector ( map [string ]string {
126+ labelMap = map [string ]string {
125127 keys .PullRequest : strconv .Itoa (p .event .PullRequestNumber ),
126- })
127- }
128-
129- prs , err := p .run .Clients .Tekton .TektonV1 ().PipelineRuns (repo .Namespace ).List (ctx , metav1.ListOptions {
130- LabelSelector : labelSelector ,
131- })
132- if err != nil {
133- return fmt .Errorf ("failed to list pipelineRuns : %w" , err )
134- }
135-
136- if len (prs .Items ) == 0 {
137- msg := fmt .Sprintf ("no pipelinerun found for repository: %v , sha: %v and pulRequest %v" ,
138- p .event .Repository , p .event .SHA , p .event .PullRequestNumber )
139- p .eventEmitter .EmitMessage (repo , zap .InfoLevel , "RepositoryPipelineRun" , msg )
140- return nil
128+ }
141129 }
142130
143- p . cancelPipelineRuns (ctx , prs , repo , func (pr tektonv1.PipelineRun ) bool {
131+ return p . cancelInProgressPipelineRunsForRepository (ctx , repo , labelMap , func (pr tektonv1.PipelineRun ) bool {
144132 if p .event .TargetCancelPipelineRun != "" {
145133 if prName , ok := pr .GetAnnotations ()[keys .OriginalPRName ]; ! ok || prName != p .event .TargetCancelPipelineRun {
146134 return false
147135 }
148136 }
149137 return true
150138 })
151-
152- return nil
153139}
154140
155141func (p * PacRun ) cancelPipelineRuns (ctx context.Context , prs * tektonv1.PipelineRunList , repo * v1alpha1.Repository , condition matchingCond ) {
0 commit comments