Skip to content

Commit c5d07bb

Browse files
committed
fix: ensure cancel-in-progress targets PR PipelineRuns only
- Add event-type label to selector for PR-close cancellations - Prevent accidental cancellation of push-triggered PipelineRuns that share a PR number - Extend tests to cover mixed push/PR scenarios after merge Signed-off-by: Zaki Shaikh <zashaikh@redhat.com>
1 parent 1483793 commit c5d07bb

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

pkg/pipelineascode/cancel_pipelineruns.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func (p *PacRun) cancelAllInProgressBelongingToClosedPullRequest(ctx context.Con
4242
// First, build the label selector based on the URLRepository and PullRequest fields,
4343
// followed by applying filtering logic for the 'cancel-in-progress' annotation.
4444
labelSelector := getLabelSelector(labelsMap, operator)
45+
labelSelector += fmt.Sprintf(",%s in (pull_request, Merge_Request, %s)", keys.EventType, opscomments.AnyOpsKubeLabelInSelector())
4546

4647
if cancelInProgress == "true" {
4748
// When the 'cancel-in-progress' setting is enabled globally via the Pipelines-as-Code ConfigMap,

pkg/pipelineascode/cancel_pipelineruns_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,34 @@ func TestCancelAllInProgressBelongingToClosedPullRequest(t *testing.T) {
13161316
},
13171317
cancelledPipelineRuns: map[string]bool{},
13181318
},
1319+
// this can happen when a pull request is merged and a new push is made to the branch
1320+
// so PaC assign the merged pull request number to the push PipelineRun
1321+
{
1322+
name: "do not cancel push-triggered PipelineRuns on PR close",
1323+
event: &info.Event{
1324+
Repository: "foo",
1325+
PullRequestNumber: pullReqNumber,
1326+
TriggerTarget: "push",
1327+
},
1328+
repo: fooRepo,
1329+
pipelineRuns: []*pipelinev1.PipelineRun{
1330+
{
1331+
Spec: pipelinev1.PipelineRunSpec{},
1332+
ObjectMeta: metav1.ObjectMeta{
1333+
Name: "pr-foo-1",
1334+
Namespace: "foo",
1335+
Labels: map[string]string{
1336+
keys.OriginalPRName: "pr-foo",
1337+
keys.URLRepository: formatting.CleanValueKubernetes("foo"),
1338+
keys.PullRequest: strconv.Itoa(pullReqNumber),
1339+
keys.EventType: string(triggertype.Push),
1340+
keys.SHA: formatting.CleanValueKubernetes("foosha"),
1341+
},
1342+
},
1343+
},
1344+
},
1345+
cancelledPipelineRuns: map[string]bool{},
1346+
},
13191347
{
13201348
name: "exclude PipelineRun having cancel-in-progress set to false when global setting is true",
13211349
event: &info.Event{

0 commit comments

Comments
 (0)