Skip to content

fix: Fixed PipelineRun cancelling on PR closed#2039

Merged
chmouel merged 1 commit intotektoncd:mainfrom
zakisk:SRVKP-7456-fix-plr-cancellation-on-pr-close
Apr 14, 2025
Merged

fix: Fixed PipelineRun cancelling on PR closed#2039
chmouel merged 1 commit intotektoncd:mainfrom
zakisk:SRVKP-7456-fix-plr-cancellation-on-pr-close

Conversation

@zakisk
Copy link
Copy Markdown
Member

@zakisk zakisk commented Apr 11, 2025

issue: when a PipelineRun is running on PR and it doesn't have cancel-in-progress annotation specified, on the PR close PAC is cancelling the PipelinRun.

solution: in code on PR close, it wasn't checked before that a PipelineRun has cancel-in-progress annotation or not and PAC just cancel PipelineRuns regardless of feature enabled. Now cancel-in-progress annotation is added to PipelineRun labels so that it can be filtered using labelSelector.

https://issues.redhat.com/browse/SRVKP-7456

Changes

Submitter Checklist

  • 📝 Ensure your commit message is clear and informative. Refer to the How to write a git commit message guide. Include the commit message in the PR body rather than linking to an external site (e.g., Jira ticket).

  • ♽ Run make test lint before submitting a PR to avoid unnecessary CI processing. Consider installing pre-commit and running pre-commit install in the repository root for an efficient workflow.

  • ✨ We use linters to maintain clean and consistent code. Run make lint before submitting a PR. Some linters offer a --fix mode, executable with make fix-linters (ensure markdownlint and golangci-lint are installed).

  • 📖 Document any user-facing features or changes in behavior.

  • 🧪 While 100% coverage isn't required, we encourage unit tests for code changes where possible.

  • 🎁 If feasible, add an end-to-end test. See README for details.

  • 🔎 Address any CI test flakiness before merging, or provide a valid reason to bypass it (e.g., token rate limitations).

  • If adding a provider feature, fill in the following details:

    • GitHub App
    • GitHub Webhook
    • Gitea/Forgejo
    • GitLab
    • Bitbucket Cloud
    • Bitbucket Data Center

    (update the provider documentation accordingly)

@zakisk zakisk requested a review from chmouel April 11, 2025 11:46
Comment thread pkg/pipelineascode/cancel_pipelineruns.go
Comment thread test/github_pullrequest_test.go Outdated
// after adding a label on the PR we need to make sure that it doesn't trigger another PipelineRun.
assert.Equal(t, len(prs.Items), 1)
prReason := prs.Items[0].Status.GetConditions()[0].Reason
assert.Equal(t, prReason, string(tektonv1.PipelineRunReasonRunning), "Wanted PipelineRun to be running but it is ", prReason)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this loop would not do what expected....

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by this I expect that after PR close it should check PipelineRun is running for 10 secs (on 1 sec interval) to confirm that PR close doesn't cancel the PipelineRun.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the status of the PipelineRun is not changed in 10 secs then it means PipelineRun is not cancelled and running as expected.

Copy link
Copy Markdown
Member

@chmouel chmouel Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yeah but the thing is a bit fragile tho?

what about something likethis instead

	found := 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 = string(c.Reason)
				if prReason == string(tektonv1.PipelineRunReasonRunning) {
					found = true
					break
				}
			}
		}
		if found {
			break
		}
		time.Sleep(1 * time.Second)
	}
	assert.Equal(t, true, found, fmt.Sprintf("PipelineRun never reached 'Running' state, last reason: %v", prReason))

It now correctly iterates conditions using a range loop instead of assuming [0].
It explicitly checks for c.Type == apis.ConditionSucceeded.
The if found { break } wil make the loop to exit as soon as the desired state is detected, which is more efficient than the original loop which always ran 10 times.
Adds continue on list errors or count mismatches, allowing retries within the 10 attempts.

i haven't tested it fully

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good! but I want to observe for 10 secs that PipelineRun is still running and not getting cancelled. on found we may not break the loop. (will update accordingly)

@chmouel chmouel requested a review from Copilot April 11, 2025 11:59
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

@zakisk zakisk force-pushed the SRVKP-7456-fix-plr-cancellation-on-pr-close branch from a03ce54 to 77374f3 Compare April 11, 2025 12:57
@zakisk
Copy link
Copy Markdown
Member Author

zakisk commented Apr 11, 2025

/test linters

issue: when a PipelineRun is running on PR and it doesn't have
cancel-in-progress annotation specified, on the PR close
PAC is cancelling the PipelinRun.

solution: in code on PR close, it wasn't checked before that
a PipelineRun has cancel-in-progress annotation or not and
PAC just cancel PipelineRuns regardless of feature enabled.
Now cancel-in-progress annotation is added to PipelineRun
labels so that it can be filtered using labelSelector.

https://issues.redhat.com/browse/SRVKP-7456

Signed-off-by: Zaki Shaikh <zashaikh@redhat.com>
@zakisk zakisk force-pushed the SRVKP-7456-fix-plr-cancellation-on-pr-close branch from 77374f3 to 8d607dd Compare April 14, 2025 09:29
@chmouel chmouel added this pull request to the merge queue Apr 14, 2025
Merged via the queue into tektoncd:main with commit c6ff19c Apr 14, 2025
5 of 9 checks passed
@zakisk zakisk changed the title fix: Fixed PipelineRun triggering on PR closed fix: Fixed PipelineRun cancelling on PR closed Apr 15, 2025
@zakisk zakisk deleted the SRVKP-7456-fix-plr-cancellation-on-pr-close branch April 15, 2025 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants