Skip to content

Commit 91ee790

Browse files
committed
fix: Match source and head branch in full path
before this, when source or head branch in cancel-in-progress matching code having full path weren't being matched. e.g. refs/heads/branch Signed-off-by: Zaki Shaikh <zashaikh@redhat.com>
1 parent 9684630 commit 91ee790

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

pkg/pipelineascode/cancel_pipelineruns.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"strconv"
7+
"strings"
78
"sync"
89

910
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
@@ -52,7 +53,7 @@ func (p *PacRun) cancelAllInProgressBelongingToClosedPullRequest(ctx context.Con
5253
// Note: The 'selection.NotIn' operator is used to exclude PipelineRuns that have the
5354
// 'cancel-in-progress' annotation explicitly set to 'false', effectively opting them out of cancellation.
5455
labelsMap = map[string]string{keys.CancelInProgress: "false"}
55-
operator = selection.NotIn
56+
operator = selection.NotIn //codespell:ignore 'NotIn'
5657
} else {
5758
// When the 'cancel-in-progress' setting is disabled globally via the Pipelines-as-Code ConfigMap,
5859
// filter and list only those PipelineRuns that explicitly override the global setting by having the
@@ -153,7 +154,7 @@ func (p *PacRun) cancelInProgressMatchingPipelineRun(ctx context.Context, matchP
153154
// it means we only cancel pipelinerun of the same name that runs to
154155
// the unique branch. Note: HeadBranch is the branch from where the PR
155156
// comes from in git jargon.
156-
if sourceBranch != p.event.HeadBranch {
157+
if strings.TrimPrefix(sourceBranch, "refs/heads/") != strings.TrimPrefix(p.event.HeadBranch, "refs/heads/") {
157158
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)
158159
return false
159160
}

pkg/pipelineascode/cancel_pipelineruns_test.go

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,62 @@ func TestCancelInProgressMatchingPipelineRun(t *testing.T) {
10331033
},
10341034
wantLog: "cancel-in-progress for event push is enabled via PipelineRun annotation",
10351035
},
1036+
{
1037+
name: "matching PipelineRun when source branch annotation is having full path refs/heads/",
1038+
event: &info.Event{
1039+
Repository: "foo",
1040+
SHA: "foosha",
1041+
HeadBranch: "head",
1042+
EventType: string(triggertype.Push),
1043+
TriggerTarget: triggertype.Push,
1044+
PullRequestNumber: pullReqNumber,
1045+
},
1046+
pipelineRuns: []*pipelinev1.PipelineRun{
1047+
{
1048+
ObjectMeta: metav1.ObjectMeta{
1049+
Name: "pr-foo-1",
1050+
Namespace: "foo",
1051+
Labels: map[string]string{
1052+
keys.EventType: string(triggertype.Push),
1053+
keys.OriginalPRName: "pr-foo",
1054+
keys.URLRepository: formatting.CleanValueKubernetes("foo"),
1055+
keys.SHA: formatting.CleanValueKubernetes("foosha"),
1056+
},
1057+
Annotations: map[string]string{
1058+
keys.OriginalPRName: "pr-foo",
1059+
keys.Repository: "foo",
1060+
keys.SourceBranch: "refs/heads/head",
1061+
keys.CancelInProgress: "true",
1062+
},
1063+
},
1064+
Spec: pipelinev1.PipelineRunSpec{},
1065+
},
1066+
{
1067+
ObjectMeta: metav1.ObjectMeta{
1068+
Name: "pr-foo-2",
1069+
Namespace: "foo",
1070+
Labels: map[string]string{
1071+
keys.OriginalPRName: "pr-foo",
1072+
keys.URLRepository: formatting.CleanValueKubernetes("foo"),
1073+
keys.SHA: formatting.CleanValueKubernetes("foosha"),
1074+
keys.EventType: string(triggertype.Push),
1075+
},
1076+
Annotations: map[string]string{
1077+
keys.OriginalPRName: "pr-foo",
1078+
keys.Repository: "foo",
1079+
keys.SourceBranch: "head",
1080+
keys.CancelInProgress: "true",
1081+
},
1082+
},
1083+
Spec: pipelinev1.PipelineRunSpec{},
1084+
},
1085+
},
1086+
repo: fooRepo,
1087+
cancelledPipelineRuns: map[string]bool{
1088+
"pr-foo-2": true,
1089+
},
1090+
wantLog: "cancel-in-progress for event push is enabled via PipelineRun annotation",
1091+
},
10361092
{
10371093
name: "skip/cancel in progress with concurrency limit",
10381094
event: &info.Event{
@@ -1466,8 +1522,8 @@ func TestGetLabelSelector(t *testing.T) {
14661522
labelsMap: map[string]string{
14671523
keys.CancelInProgress: "false",
14681524
},
1469-
operator: selection.NotIn,
1470-
want: "pipelinesascode.tekton.dev/cancel-in-progress notin (false)",
1525+
operator: selection.NotIn, //codespell:ignore 'NotIn'
1526+
want: "pipelinesascode.tekton.dev/cancel-in-progress notin (false)", //codespell:ignore 'NotIn'
14711527
},
14721528
}
14731529

0 commit comments

Comments
 (0)