Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/kubeinteraction/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func AddLabelsAndAnnotations(event *info.Event, pipelineRun *tektonv1.PipelineRu
annotations[keys.TargetProjectID] = strconv.Itoa(event.TargetProjectID)
}

if value, ok := pipelineRun.GetObjectMeta().GetAnnotations()[keys.CancelInProgress]; ok {
labels[keys.CancelInProgress] = value
}

for k, v := range labels {
pipelineRun.Labels[k] = v
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/kubeinteraction/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ func TestAddLabelsAndAnnotations(t *testing.T) {
event: event,
pipelineRun: &tektonv1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{},
Annotations: map[string]string{},
Labels: map[string]string{},
Annotations: map[string]string{
keys.CancelInProgress: "true",
},
},
},
repo: &apipac.Repository{
Expand Down Expand Up @@ -70,6 +72,8 @@ func TestAddLabelsAndAnnotations(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, tt.args.pipelineRun.Labels[keys.URLOrg], tt.args.event.Organization, "'%s' != %s",
tt.args.pipelineRun.Labels[keys.URLOrg], tt.args.event.Organization)
assert.Equal(t, tt.args.pipelineRun.Labels[keys.CancelInProgress], tt.args.pipelineRun.Annotations[keys.CancelInProgress], "'%s' != %s",
tt.args.pipelineRun.Labels[keys.CancelInProgress], tt.args.pipelineRun.Annotations[keys.CancelInProgress])
assert.Equal(t, tt.args.pipelineRun.Annotations[keys.URLOrg], tt.args.event.Organization, "'%s' != %s",
tt.args.pipelineRun.Annotations[keys.URLOrg], tt.args.event.Organization)
assert.Equal(t, tt.args.pipelineRun.Annotations[keys.ShaURL], tt.args.event.SHAURL)
Expand Down
9 changes: 3 additions & 6 deletions pkg/pipelineascode/cancel_pipelineruns.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ var cancelMergePatch = map[string]any{
// that belong to a specific pull request in the given repository.
func (p *PacRun) cancelAllInProgressBelongingToClosedPullRequest(ctx context.Context, repo *v1alpha1.Repository) error {
labelSelector := getLabelSelector(map[string]string{
keys.URLRepository: formatting.CleanValueKubernetes(p.event.Repository),
keys.PullRequest: strconv.Itoa(int(p.event.PullRequestNumber)),
keys.URLRepository: formatting.CleanValueKubernetes(p.event.Repository),
keys.PullRequest: strconv.Itoa(p.event.PullRequestNumber),
keys.CancelInProgress: "true",
})
prs, err := p.run.Clients.Tekton.TektonV1().PipelineRuns(repo.Namespace).List(ctx, metav1.ListOptions{
LabelSelector: labelSelector,
Expand Down Expand Up @@ -176,10 +177,6 @@ func (p *PacRun) cancelPipelineRuns(ctx context.Context, prs *tektonv1.PipelineR
continue
}

if pr.IsPending() {
Comment thread
chmouel marked this conversation as resolved.
p.logger.Infof("cancel-in-progress: skipping cancelling pipelinerun %v/%v in pending state", pr.GetNamespace(), pr.GetName())
}

p.logger.Infof("cancel-in-progress: cancelling pipelinerun %v/%v", pr.GetNamespace(), pr.GetName())
wg.Add(1)
go func(ctx context.Context, pr tektonv1.PipelineRun) {
Expand Down
104 changes: 100 additions & 4 deletions pkg/pipelineascode/cancel_pipelineruns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ func TestCancelInProgressMatchingPR(t *testing.T) {
}
}

func TestCancelAllInProgressBelongingToPullRequest(t *testing.T) {
func TestCancelAllInProgressBelongingToClosedPullRequest(t *testing.T) {
observer, _ := zapobserver.New(zap.InfoLevel)
logger := zap.New(observer).Sugar()

Expand All @@ -879,7 +879,7 @@ func TestCancelAllInProgressBelongingToPullRequest(t *testing.T) {
cancelledPipelineRuns map[string]bool
}{
{
name: "cancel all in progress PipelineRuns",
name: "cancel all in progress PipelineRuns with annotation set to true",
event: &info.Event{
Repository: "foo",
TriggerTarget: "pull_request",
Expand All @@ -891,15 +891,29 @@ func TestCancelAllInProgressBelongingToPullRequest(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "pr-foo-1",
Namespace: "foo",
Labels: fooRepoLabels,
Labels: map[string]string{
keys.OriginalPRName: "pr-foo",
keys.URLRepository: formatting.CleanValueKubernetes("foo"),
keys.SHA: formatting.CleanValueKubernetes("foosha"),
keys.PullRequest: strconv.Itoa(pullReqNumber),
keys.EventType: string(triggertype.PullRequest),
keys.CancelInProgress: "true",
},
},
Spec: pipelinev1.PipelineRunSpec{},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "pr-foo-2",
Namespace: "foo",
Labels: fooRepoLabels,
Labels: map[string]string{
keys.OriginalPRName: "pr-foo",
keys.URLRepository: formatting.CleanValueKubernetes("foo"),
keys.SHA: formatting.CleanValueKubernetes("foosha"),
keys.PullRequest: strconv.Itoa(pullReqNumber),
keys.EventType: string(triggertype.PullRequest),
keys.CancelInProgress: "true",
},
},
Spec: pipelinev1.PipelineRunSpec{},
},
Expand All @@ -909,6 +923,88 @@ func TestCancelAllInProgressBelongingToPullRequest(t *testing.T) {
"pr-foo-2": true,
},
},
{
name: "cancel all in progress PipelineRuns with annotation set to false",
event: &info.Event{
Repository: "foo",
TriggerTarget: "pull_request",
PullRequestNumber: pullReqNumber,
},
repo: fooRepo,
pipelineRuns: []*pipelinev1.PipelineRun{
{
ObjectMeta: metav1.ObjectMeta{
Name: "pr-foo-1",
Namespace: "foo",
Labels: map[string]string{
keys.OriginalPRName: "pr-foo",
keys.URLRepository: formatting.CleanValueKubernetes("foo"),
keys.SHA: formatting.CleanValueKubernetes("foosha"),
keys.PullRequest: strconv.Itoa(pullReqNumber),
keys.EventType: string(triggertype.PullRequest),
keys.CancelInProgress: "false",
},
},
Spec: pipelinev1.PipelineRunSpec{},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "pr-foo-2",
Namespace: "foo",
Labels: map[string]string{
keys.OriginalPRName: "pr-foo",
keys.URLRepository: formatting.CleanValueKubernetes("foo"),
keys.SHA: formatting.CleanValueKubernetes("foosha"),
keys.PullRequest: strconv.Itoa(pullReqNumber),
keys.EventType: string(triggertype.PullRequest),
keys.CancelInProgress: "false",
},
},
Spec: pipelinev1.PipelineRunSpec{},
},
},
cancelledPipelineRuns: map[string]bool{},
},
{
name: "cancel all in progress PipelineRuns with no annotation",
event: &info.Event{
Repository: "foo",
TriggerTarget: "pull_request",
PullRequestNumber: pullReqNumber,
},
repo: fooRepo,
pipelineRuns: []*pipelinev1.PipelineRun{
{
ObjectMeta: metav1.ObjectMeta{
Name: "pr-foo-1",
Namespace: "foo",
Labels: map[string]string{
keys.OriginalPRName: "pr-foo",
keys.URLRepository: formatting.CleanValueKubernetes("foo"),
keys.SHA: formatting.CleanValueKubernetes("foosha"),
keys.PullRequest: strconv.Itoa(pullReqNumber),
keys.EventType: string(triggertype.PullRequest),
},
},
Spec: pipelinev1.PipelineRunSpec{},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "pr-foo-2",
Namespace: "foo",
Labels: map[string]string{
keys.OriginalPRName: "pr-foo",
keys.URLRepository: formatting.CleanValueKubernetes("foo"),
keys.SHA: formatting.CleanValueKubernetes("foosha"),
keys.PullRequest: strconv.Itoa(pullReqNumber),
keys.EventType: string(triggertype.PullRequest),
},
},
Spec: pipelinev1.PipelineRunSpec{},
},
},
cancelledPipelineRuns: map[string]bool{},
},
{
name: "no PipelineRuns to cancel",
event: &info.Event{
Expand Down
63 changes: 60 additions & 3 deletions test/github_pullrequest_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build e2e
// +build e2e

package test

import (
Expand All @@ -13,6 +10,7 @@ import (
"time"

"github.com/google/go-github/v70/github"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
"gotest.tools/v3/golden"
Expand Down Expand Up @@ -421,6 +419,65 @@ func TestGithubPullRequestNoOnLabelAnnotation(t *testing.T) {
}
}

func TestGithubPullRequestNoPipelineRunCancelledOnPRClosed(t *testing.T) {
ctx := context.Background()
g := &tgithub.PRTest{
Label: "Github PullRequest",
YamlFiles: []string{"testdata/pipelinerun-gitops.yaml"},
NoStatusCheck: true,
}
g.RunPullRequest(ctx, t)
defer g.TearDown(ctx, t)

g.Cnx.Clients.Log.Infof("Waiting for the two pipelinerun to be created")
waitOpts := twait.Opts{
RepoName: g.TargetNamespace,
Namespace: g.TargetNamespace,
MinNumberStatus: 1,
PollTimeout: twait.DefaultTimeout,
TargetSHA: g.SHA,
}
err := twait.UntilPipelineRunCreated(ctx, g.Cnx.Clients, waitOpts)
assert.NilError(t, err)

g.Cnx.Clients.Log.Infof("Closing the PullRequest")
_, _, err = g.Provider.Client.PullRequests.Edit(ctx, g.Options.Organization, g.Options.Repo, g.PRNumber, &github.PullRequest{
State: github.Ptr("closed"),
})
assert.NilError(t, err)

isCancelled := false
var prReason string

for range 10 {
prs, err := g.Cnx.Clients.Tekton.TektonV1().PipelineRuns(g.TargetNamespace).List(ctx, metav1.ListOptions{})
if err != nil {
t.Logf("failed to list PipelineRuns: %v", err)
time.Sleep(1 * time.Second)
continue // try again
}
if len(prs.Items) != 1 {
t.Logf("expected 1 PipelineRun, got %d", len(prs.Items))
time.Sleep(1 * time.Second)
continue // try again
}
// Check all conditions, get the right one
conditions := prs.Items[0].Status.GetConditions()
for _, c := range conditions {
if c.Type == apis.ConditionSucceeded {
prReason = c.Reason
if prReason != string(tektonv1.PipelineRunReasonRunning) {
isCancelled = true
t.Logf("expected PipelineRun `Running`, got %s", prReason)
break
}
}
}
time.Sleep(1 * time.Second)
}
assert.Equal(t, false, isCancelled, fmt.Sprintf("PipelineRun got cancelled while we wanted it `Running`, last reason: %v", prReason))
}

// Local Variables:
// compile-command: "go test -tags=e2e -v -info TestGithubPullRequest$ ."
// End:
Loading