Skip to content

Commit 68db868

Browse files
chmouelpipelines-as-code[bot]
authored andcommitted
Revert "chore: experimental refactor"
This reverts commit 48c0ab3.
1 parent d8046cf commit 68db868

File tree

3 files changed

+61
-47
lines changed

3 files changed

+61
-47
lines changed

pkg/pipelineascode/cancel_pipelineruns.go

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ var cancelMergePatch = map[string]interface{}{
2727
},
2828
}
2929

30-
// cancelInProgressPipelineRunsForRepository cancels all in-progress PipelineRuns
31-
// that are selected by the given labelSelectorMap and for which the matchFunc returns true.
32-
func (p *PacRun) cancelInProgressPipelineRunsForRepository(ctx context.Context, repo *v1alpha1.Repository, labelSelectorMap map[string]string, matchFunc matchingCond) error {
33-
labelSelectorMap[keys.URLRepository] = formatting.CleanValueKubernetes(p.event.Repository)
34-
35-
labelSelector := getLabelSelector(labelSelectorMap)
36-
p.run.Clients.Log.Infof("cancel-in-progress: selecting pipelineRuns to cancel with labels: %v", labelSelector)
37-
30+
// cancelAllInProgressBelongingToPullRequest cancels all in-progress PipelineRuns
31+
// that belong to a specific pull request in the given repository.
32+
func (p *PacRun) cancelAllInProgressBelongingToPullRequest(ctx context.Context, repo *v1alpha1.Repository) error {
33+
labelSelector := getLabelSelector(map[string]string{
34+
keys.URLRepository: formatting.CleanValueKubernetes(p.event.Repository),
35+
keys.PullRequest: strconv.Itoa(int(p.event.PullRequestNumber)),
36+
})
3837
prs, err := p.run.Clients.Tekton.TektonV1().PipelineRuns(repo.Namespace).List(ctx, metav1.ListOptions{
3938
LabelSelector: labelSelector,
4039
})
@@ -49,29 +48,20 @@ func (p *PacRun) cancelInProgressPipelineRunsForRepository(ctx context.Context,
4948
return nil
5049
}
5150

52-
p.cancelPipelineRuns(ctx, prs, repo, matchFunc)
51+
p.cancelPipelineRuns(ctx, prs, repo, func(_ tektonv1.PipelineRun) bool {
52+
return true
53+
})
5354

5455
return nil
5556
}
5657

57-
// cancelAllInProgressBelongingToPullRequest cancels all in-progress PipelineRuns
58-
// which are associated with the event's Pull Request.
59-
func (p *PacRun) cancelAllInProgressBelongingToPullRequest(ctx context.Context, repo *v1alpha1.Repository) error {
60-
return p.cancelInProgressPipelineRunsForRepository(
61-
ctx,
62-
repo,
63-
map[string]string{keys.PullRequest: strconv.Itoa(int(p.event.PullRequestNumber))},
64-
func(_ tektonv1.PipelineRun) bool { return true },
65-
)
66-
}
67-
68-
// cancelInProgressExceptMatchingPR cancels all PipelineRuns associated with a given repository and pull request,
58+
// cancelInProgressMatchingPR cancels all PipelineRuns associated with a given repository and pull request,
6959
// except for the one that triggered the cancellation. It first checks if the cancellation is in progress
7060
// and if the repository has a concurrency limit. If a concurrency limit is set, it returns an error as
7161
// cancellation is not supported with concurrency limits. It then retrieves the original pull request name
7262
// from the annotations and lists all PipelineRuns with matching labels. For each PipelineRun that is not
7363
// already done, cancelled, or gracefully stopped, it patches the PipelineRun to cancel it.
74-
func (p *PacRun) cancelInProgressExceptMatchingPR(ctx context.Context, matchPR *tektonv1.PipelineRun, repo *v1alpha1.Repository) error {
64+
func (p *PacRun) cancelInProgressMatchingPR(ctx context.Context, matchPR *tektonv1.PipelineRun, repo *v1alpha1.Repository) error {
7565
if matchPR == nil {
7666
return nil
7767
}
@@ -89,53 +79,77 @@ func (p *PacRun) cancelInProgressExceptMatchingPR(ctx context.Context, matchPR *
8979
}
9080

9181
labelMap := map[string]string{
82+
keys.URLRepository: formatting.CleanValueKubernetes(p.event.Repository),
9283
keys.OriginalPRName: prName,
9384
}
9485
if p.event.TriggerTarget == triggertype.PullRequest {
9586
labelMap[keys.PullRequest] = strconv.Itoa(p.event.PullRequestNumber)
9687
}
97-
return p.cancelInProgressPipelineRunsForRepository(
98-
ctx,
99-
repo,
100-
labelMap,
101-
func(pr tektonv1.PipelineRun) bool {
102-
// skip our own for cancellation
103-
if sourceBranch, ok := pr.GetAnnotations()[keys.SourceBranch]; ok {
104-
// NOTE(chmouel): Every PR has their own branch and so is every push to different branch
105-
// it means we only cancel pipelinerun of the same name that runs to
106-
// the unique branch. Note: HeadBranch is the branch from where the PR
107-
// comes from in git jargon.
108-
if sourceBranch != p.event.HeadBranch {
109-
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)
110-
return false
111-
}
88+
labelSelector := getLabelSelector(labelMap)
89+
p.run.Clients.Log.Infof("cancel-in-progress: selecting pipelineRuns to cancel with labels: %v", labelSelector)
90+
prs, err := p.run.Clients.Tekton.TektonV1().PipelineRuns(matchPR.GetNamespace()).List(ctx, metav1.ListOptions{
91+
LabelSelector: labelSelector,
92+
})
93+
if err != nil {
94+
return fmt.Errorf("failed to list pipelineRuns : %w", err)
95+
}
96+
97+
p.cancelPipelineRuns(ctx, prs, repo, func(pr tektonv1.PipelineRun) bool {
98+
// skip our own for cancellation
99+
if sourceBranch, ok := pr.GetAnnotations()[keys.SourceBranch]; ok {
100+
// NOTE(chmouel): Every PR has their own branch and so is every push to different branch
101+
// it means we only cancel pipelinerun of the same name that runs to
102+
// the unique branch. Note: HeadBranch is the branch from where the PR
103+
// comes from in git jargon.
104+
if sourceBranch != p.event.HeadBranch {
105+
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)
106+
return false
112107
}
108+
}
113109

114-
return pr.GetName() != matchPR.GetName()
115-
})
110+
return pr.GetName() != matchPR.GetName()
111+
})
112+
return nil
116113
}
117114

118115
// cancelPipelineRunsOpsComment cancels all PipelineRuns associated with a given repository and pull request.
119116
// when the user issue a cancel comment.
120117
func (p *PacRun) cancelPipelineRunsOpsComment(ctx context.Context, repo *v1alpha1.Repository) error {
121-
labelMap := map[string]string{
122-
keys.SHA: formatting.CleanValueKubernetes(p.event.SHA),
123-
}
118+
labelSelector := getLabelSelector(map[string]string{
119+
keys.URLRepository: formatting.CleanValueKubernetes(p.event.Repository),
120+
keys.SHA: formatting.CleanValueKubernetes(p.event.SHA),
121+
})
124122

125123
if p.event.TriggerTarget == triggertype.PullRequest {
126-
labelMap = map[string]string{
124+
labelSelector = getLabelSelector(map[string]string{
127125
keys.PullRequest: strconv.Itoa(p.event.PullRequestNumber),
128-
}
126+
})
129127
}
130128

131-
return p.cancelInProgressPipelineRunsForRepository(ctx, repo, labelMap, func(pr tektonv1.PipelineRun) bool {
129+
prs, err := p.run.Clients.Tekton.TektonV1().PipelineRuns(repo.Namespace).List(ctx, metav1.ListOptions{
130+
LabelSelector: labelSelector,
131+
})
132+
if err != nil {
133+
return fmt.Errorf("failed to list pipelineRuns : %w", err)
134+
}
135+
136+
if len(prs.Items) == 0 {
137+
msg := fmt.Sprintf("no pipelinerun found for repository: %v , sha: %v and pulRequest %v",
138+
p.event.Repository, p.event.SHA, p.event.PullRequestNumber)
139+
p.eventEmitter.EmitMessage(repo, zap.InfoLevel, "RepositoryPipelineRun", msg)
140+
return nil
141+
}
142+
143+
p.cancelPipelineRuns(ctx, prs, repo, func(pr tektonv1.PipelineRun) bool {
132144
if p.event.TargetCancelPipelineRun != "" {
133145
if prName, ok := pr.GetAnnotations()[keys.OriginalPRName]; !ok || prName != p.event.TargetCancelPipelineRun {
134146
return false
135147
}
136148
}
137149
return true
138150
})
151+
152+
return nil
139153
}
140154

141155
func (p *PacRun) cancelPipelineRuns(ctx context.Context, prs *tektonv1.PipelineRunList, repo *v1alpha1.Repository, condition matchingCond) {

pkg/pipelineascode/cancel_pipelineruns_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ func TestCancelInProgressMatchingPR(t *testing.T) {
789789
if len(tt.pipelineRuns) > 0 {
790790
firstPr = tt.pipelineRuns[0]
791791
}
792-
err := pac.cancelInProgressExceptMatchingPR(ctx, firstPr, tt.repo)
792+
err := pac.cancelInProgressMatchingPR(ctx, firstPr, tt.repo)
793793
if tt.wantErrString != "" {
794794
assert.ErrorContains(t, err, tt.wantErrString)
795795
return

pkg/pipelineascode/pipelineascode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (p *PacRun) Run(ctx context.Context) error {
124124
}
125125
}
126126
p.manager.AddPipelineRun(pr)
127-
if err := p.cancelInProgressExceptMatchingPR(ctx, pr, repo); err != nil {
127+
if err := p.cancelInProgressMatchingPR(ctx, pr, repo); err != nil {
128128
p.eventEmitter.EmitMessage(repo, zap.ErrorLevel, "RepositoryPipelineRun", fmt.Sprintf("error cancelling in progress pipelineRuns: %s", err))
129129
}
130130
}(match, i)

0 commit comments

Comments
 (0)