Skip to content

Commit 3325ba7

Browse files
committed
fix: cancel-in-progress with retest comment
fix cancel-in-progress on /retest /test /ok-to-test or any gitops comments variable. Since when we set the trigger event to the test comment like retest-all instead of pull_request on the label of the pipelinerun we can't rely on that to group the pipelinerun for cancellation. We now have a helper function to do a label select to any pipelinerun that has the gitops comments as event_type or pull_request Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent 60733a0 commit 3325ba7

File tree

5 files changed

+108
-18
lines changed

5 files changed

+108
-18
lines changed

pkg/opscomments/comments.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55
"regexp"
66
"strings"
77

8+
"go.uber.org/zap"
9+
810
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
911
"github.com/openshift-pipelines/pipelines-as-code/pkg/events"
1012
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
1113
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
12-
"go.uber.org/zap"
1314
)
1415

1516
var (
@@ -122,6 +123,20 @@ func IsAnyOpsEventType(eventType string) bool {
122123
eventType == OnCommentEventType.String()
123124
}
124125

126+
// AnyOpsKubeLabelInSelector will output a Kubernetes label out of all possible
127+
// CommentEvent Type for selection.
128+
func AnyOpsKubeLabelInSelector() string {
129+
return fmt.Sprintf("%s,%s,%s,%s,%s,%s,%s,%s",
130+
TestSingleCommentEventType.String(),
131+
TestAllCommentEventType.String(),
132+
RetestAllCommentEventType.String(),
133+
RetestSingleCommentEventType.String(),
134+
CancelCommentSingleEventType.String(),
135+
CancelCommentAllEventType.String(),
136+
OkToTestCommentEventType.String(),
137+
OnCommentEventType.String())
138+
}
139+
125140
func GetPipelineRunFromTestComment(comment string) string {
126141
if strings.Contains(comment, testComment) {
127142
return getNameFromComment(testComment, comment)

pkg/opscomments/comments_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package opscomments
22

33
import (
4+
"strings"
45
"testing"
56

7+
"go.uber.org/zap"
8+
zapobserver "go.uber.org/zap/zaptest/observer"
9+
"gotest.tools/v3/assert"
10+
rtesting "knative.dev/pkg/reconciler/testing"
11+
612
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
713
"github.com/openshift-pipelines/pipelines-as-code/pkg/events"
814
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
915
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
1016
testclient "github.com/openshift-pipelines/pipelines-as-code/pkg/test/clients"
1117
testnewrepo "github.com/openshift-pipelines/pipelines-as-code/pkg/test/repository"
12-
"go.uber.org/zap"
13-
zapobserver "go.uber.org/zap/zaptest/observer"
14-
"gotest.tools/v3/assert"
15-
rtesting "knative.dev/pkg/reconciler/testing"
1618
)
1719

1820
func TestLabelsBackwardCompat(t *testing.T) {
@@ -727,3 +729,7 @@ func TestGetPipelineRunAndBranchNameFromCancelComment(t *testing.T) {
727729
})
728730
}
729731
}
732+
733+
func TestAnyOpsKubeLabelInSelector(t *testing.T) {
734+
assert.Assert(t, strings.Contains(AnyOpsKubeLabelInSelector(), RetestSingleCommentEventType.String()))
735+
}

pkg/pipelineascode/cancel_pipelineruns.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
1717
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
1818
"github.com/openshift-pipelines/pipelines-as-code/pkg/formatting"
19+
"github.com/openshift-pipelines/pipelines-as-code/pkg/opscomments"
1920
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
2021
)
2122

@@ -51,12 +52,14 @@ func (p *PacRun) cancelInProgress(ctx context.Context, matchPR *tektonv1.Pipelin
5152
labelMap := map[string]string{
5253
keys.URLRepository: formatting.CleanValueKubernetes(p.event.Repository),
5354
keys.OriginalPRName: prName,
54-
keys.EventType: p.event.TriggerTarget.String(),
5555
}
5656
if p.event.TriggerTarget == triggertype.PullRequest {
5757
labelMap[keys.PullRequest] = strconv.Itoa(p.event.PullRequestNumber)
5858
}
5959
labelSelector := getLabelSelector(labelMap)
60+
if p.event.TriggerTarget == triggertype.PullRequest {
61+
labelSelector += fmt.Sprintf(",%s in (pull_request, %s)", keys.EventType, opscomments.AnyOpsKubeLabelInSelector())
62+
}
6063
p.run.Clients.Log.Infof("cancel-in-progress: selecting pipelineRuns to cancel with labels: %v", labelSelector)
6164
prs, err := p.run.Clients.Tekton.TektonV1().PipelineRuns(matchPR.GetNamespace()).List(ctx, metav1.ListOptions{
6265
LabelSelector: labelSelector,

pkg/pipelineascode/cancel_pipelineruns_test.go

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
2020
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
2121
"github.com/openshift-pipelines/pipelines-as-code/pkg/formatting"
22+
"github.com/openshift-pipelines/pipelines-as-code/pkg/opscomments"
2223
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
2324
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/clients"
2425
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
@@ -492,7 +493,7 @@ func TestCancelInProgress(t *testing.T) {
492493
},
493494
},
494495
{
495-
name: "cancel in progress",
496+
name: "match/cancel in progress",
496497
event: &info.Event{
497498
Repository: "foo",
498499
SHA: "foosha",
@@ -537,7 +538,58 @@ func TestCancelInProgress(t *testing.T) {
537538
wantLog: "cancel-in-progress: cancelling pipelinerun foo/",
538539
},
539540
{
540-
name: "cancel in progress exclude not belonging to same push branch",
541+
name: "match/cancel in progress from /retest",
542+
event: &info.Event{
543+
Repository: "foo",
544+
SHA: "foosha",
545+
HeadBranch: "head",
546+
EventType: string(triggertype.PullRequest),
547+
TriggerTarget: triggertype.PullRequest,
548+
PullRequestNumber: pullReqNumber,
549+
},
550+
pipelineRuns: []*pipelinev1.PipelineRun{
551+
{
552+
ObjectMeta: metav1.ObjectMeta{
553+
Name: "pr-foo",
554+
Namespace: "foo",
555+
Labels: fooRepoLabels,
556+
Annotations: map[string]string{
557+
keys.CancelInProgress: "true",
558+
keys.OriginalPRName: "pr-foo",
559+
keys.Repository: "foo",
560+
keys.SourceBranch: "head",
561+
},
562+
},
563+
Spec: pipelinev1.PipelineRunSpec{},
564+
},
565+
{
566+
ObjectMeta: metav1.ObjectMeta{
567+
Name: "pr-foo-1",
568+
Namespace: "foo",
569+
Labels: map[string]string{
570+
keys.OriginalPRName: "pr-foo",
571+
keys.URLRepository: formatting.CleanValueKubernetes("foo"),
572+
keys.SHA: formatting.CleanValueKubernetes("foosha"),
573+
keys.PullRequest: strconv.Itoa(pullReqNumber),
574+
keys.EventType: string(opscomments.RetestAllCommentEventType),
575+
},
576+
Annotations: map[string]string{
577+
keys.CancelInProgress: "true", keys.OriginalPRName: "pr-foo",
578+
keys.Repository: "foo",
579+
keys.SourceBranch: "head",
580+
},
581+
},
582+
Spec: pipelinev1.PipelineRunSpec{},
583+
},
584+
},
585+
repo: fooRepo,
586+
cancelledPipelineRuns: map[string]bool{
587+
"pr-foo-1": true,
588+
},
589+
wantLog: "cancel-in-progress: cancelling pipelinerun foo/pr-foo-1",
590+
},
591+
{
592+
name: "match/cancel in progress exclude not belonging to same push branch",
541593
event: &info.Event{
542594
Repository: "foo",
543595
SHA: "foosha",
@@ -609,7 +661,7 @@ func TestCancelInProgress(t *testing.T) {
609661
wantLog: "skipping pipelinerun foo/pr-foo-1 as it is not from the same branch",
610662
},
611663
{
612-
name: "cancel in progress exclude not belonging to same pr",
664+
name: "match/cancel in progress exclude not belonging to same pr",
613665
event: &info.Event{
614666
Repository: "foo",
615667
SHA: "foosha",
@@ -684,9 +736,8 @@ func TestCancelInProgress(t *testing.T) {
684736
},
685737
wantLog: "cancel-in-progress: cancelling pipelinerun foo/",
686738
},
687-
688739
{
689-
name: "cancel in progress with concurrency limit",
740+
name: "skip/cancel in progress with concurrency limit",
690741
event: &info.Event{
691742
Repository: "foo",
692743
SHA: "foosha",

test/gitea_test.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ import (
1414

1515
"code.gitea.io/sdk/gitea"
1616
"github.com/google/go-github/v66/github"
17+
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
18+
"github.com/tektoncd/pipeline/pkg/names"
19+
yaml "gopkg.in/yaml.v2"
20+
"gotest.tools/v3/assert"
21+
"gotest.tools/v3/env"
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"knative.dev/pkg/apis"
24+
1725
pacapi "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
1826
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
1927
tknpacdelete "github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/tknpac/deleterepo"
@@ -36,13 +44,6 @@ import (
3644
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/scm"
3745
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/secret"
3846
twait "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait"
39-
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
40-
"github.com/tektoncd/pipeline/pkg/names"
41-
"gopkg.in/yaml.v2"
42-
"gotest.tools/v3/assert"
43-
"gotest.tools/v3/env"
44-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
45-
"knative.dev/pkg/apis"
4647
)
4748

4849
// successRegexp will match a success text paac comment,sometime it includes html tags so we need to consider that.
@@ -441,6 +442,20 @@ func TestGiteaConfigCancelInProgress(t *testing.T) {
441442
}
442443
}
443444
assert.Equal(t, cancelledPr, 1, "only one pr should have been canceled")
445+
446+
// Test that cancelling works with /retest
447+
tgitea.PostCommentOnPullRequest(t, topts, "/retest")
448+
topts.ParamsRun.Clients.Log.Info("Waiting 10 seconds before a new retest")
449+
time.Sleep(10 * time.Second) // “Evil does not sleep. It waits.” - Galadriel
450+
tgitea.PostCommentOnPullRequest(t, topts, "/retest")
451+
tgitea.WaitForStatus(t, topts, "heads/"+targetRef, "", false)
452+
453+
for _, pr := range prs.Items {
454+
if pr.GetStatusCondition().GetCondition(apis.ConditionSucceeded).GetReason() == "Cancelled" {
455+
cancelledPr++
456+
}
457+
}
458+
assert.Equal(t, cancelledPr, 2, "tweo pr should have been canceled")
444459
}
445460

446461
func TestGiteaPush(t *testing.T) {

0 commit comments

Comments
 (0)