Skip to content

Commit c2b17f0

Browse files
chmouelpipelines-as-code[bot]
authored andcommitted
refactor: Refine cancellation event reason
• Changed event reason from RepositoryPipelineRun to CancelInProgress for pipeline run cancellation messages. • Updated end-to-end tests to ignore events with the CancelInProgress reason. • This provided clearer event reporting specific to cancellation actions. • This prevented tests from spuriously failing due to expected cancellation events Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent 7ba56ff commit c2b17f0

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

pkg/pipelineascode/cancel_pipelineruns.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (p *PacRun) cancelAllInProgressBelongingToClosedPullRequest(ctx context.Con
4646
if len(prs.Items) == 0 {
4747
msg := fmt.Sprintf("no pipelinerun found for repository: %v and pullRequest %v",
4848
p.event.Repository, p.event.PullRequestNumber)
49-
p.eventEmitter.EmitMessage(repo, zap.InfoLevel, "RepositoryPipelineRun", msg)
49+
p.eventEmitter.EmitMessage(repo, zap.InfoLevel, "CancelInProgress", msg)
5050
return nil
5151
}
5252

@@ -144,7 +144,7 @@ func (p *PacRun) cancelPipelineRunsOpsComment(ctx context.Context, repo *v1alpha
144144
if len(prs.Items) == 0 {
145145
msg := fmt.Sprintf("no pipelinerun found for repository: %v , sha: %v and pulRequest %v",
146146
p.event.Repository, p.event.SHA, p.event.PullRequestNumber)
147-
p.eventEmitter.EmitMessage(repo, zap.InfoLevel, "RepositoryPipelineRun", msg)
147+
p.eventEmitter.EmitMessage(repo, zap.InfoLevel, "CancelInProgress", msg)
148148
return nil
149149
}
150150

@@ -183,7 +183,7 @@ func (p *PacRun) cancelPipelineRuns(ctx context.Context, prs *tektonv1.PipelineR
183183
defer wg.Done()
184184
if _, err := action.PatchPipelineRun(ctx, p.logger, "cancel patch", p.run.Clients.Tekton, &pr, cancelMergePatch); err != nil {
185185
errMsg := fmt.Sprintf("failed to cancel pipelineRun %s/%s: %s", pr.GetNamespace(), pr.GetName(), err.Error())
186-
p.eventEmitter.EmitMessage(repo, zap.ErrorLevel, "RepositoryPipelineRun", errMsg)
186+
p.eventEmitter.EmitMessage(repo, zap.ErrorLevel, "CancelInProgress", errMsg)
187187
}
188188
}(ctx, pr)
189189
}

test/pkg/gitea/test.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
2828
"github.com/tektoncd/pipeline/pkg/names"
2929
"gotest.tools/v3/assert"
30+
corev1 "k8s.io/api/core/v1"
3031
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132
)
3233

@@ -72,6 +73,26 @@ func PostCommentOnPullRequest(t *testing.T, topt *TestOpts, body string) {
7273
assert.NilError(t, err)
7374
}
7475

76+
func checkEvents(t *testing.T, events *corev1.EventList, topts *TestOpts) {
77+
t.Helper()
78+
newEvents := make([]corev1.Event, 0)
79+
// filter out events that are not related to the test like checking for cancelled pipelineruns
80+
for i := len(events.Items) - 1; i >= 0; i-- {
81+
topts.ParamsRun.Clients.Log.Infof("Reason is %s", events.Items[i].Reason)
82+
if events.Items[i].Reason == "CancelInProgress" {
83+
continue
84+
}
85+
newEvents = append(newEvents, events.Items[i])
86+
}
87+
if len(newEvents) > 0 {
88+
topts.ParamsRun.Clients.Log.Infof("0 events expected in case of failure but got %d", len(newEvents))
89+
for _, em := range newEvents {
90+
topts.ParamsRun.Clients.Log.Infof("Event: Reason: %s Type: %s ReportingInstance: %s Message: %s", em.Reason, em.Type, em.ReportingInstance, em.Message)
91+
}
92+
t.FailNow()
93+
}
94+
}
95+
7596
func AddLabelToIssue(t *testing.T, topt *TestOpts, label string) {
7697
var targetID int64
7798
allLabels, _, err := topt.GiteaCNX.Client.ListRepoLabels(topt.Opts.Organization, topt.Opts.Repo, gitea.ListLabelsOptions{})
@@ -249,7 +270,7 @@ func TestPR(t *testing.T, topts *TestOpts) (context.Context, func()) {
249270
}
250271
assert.Assert(t, len(events.Items) != 0, "events expected in case of failure but got 0")
251272
} else if !topts.SkipEventsCheck {
252-
assert.Assert(t, len(events.Items) == 0, fmt.Sprintf("no events expected but got %v in %v ns, items: %+v", len(events.Items), topts.TargetNS, events.Items))
273+
checkEvents(t, events, topts)
253274
}
254275
return ctx, cleanup
255276
}
@@ -369,7 +390,7 @@ func NewPR(t *testing.T, topts *TestOpts) func() {
369390
}
370391
assert.Assert(t, len(events.Items) != 0, "events expected in case of failure but got 0")
371392
} else if !topts.SkipEventsCheck {
372-
assert.Assert(t, len(events.Items) == 0, fmt.Sprintf("no events expected but got %v in %v ns, items: %+v", len(events.Items), topts.TargetNS, events.Items))
393+
checkEvents(t, events, topts)
373394
}
374395
return cleanup
375396
}

0 commit comments

Comments
 (0)