diff --git a/pkg/pipelineascode/cancel_pipelineruns.go b/pkg/pipelineascode/cancel_pipelineruns.go index fc284ef299..d3edf2acd7 100644 --- a/pkg/pipelineascode/cancel_pipelineruns.go +++ b/pkg/pipelineascode/cancel_pipelineruns.go @@ -46,7 +46,7 @@ func (p *PacRun) cancelAllInProgressBelongingToClosedPullRequest(ctx context.Con if len(prs.Items) == 0 { msg := fmt.Sprintf("no pipelinerun found for repository: %v and pullRequest %v", p.event.Repository, p.event.PullRequestNumber) - p.eventEmitter.EmitMessage(repo, zap.InfoLevel, "RepositoryPipelineRun", msg) + p.eventEmitter.EmitMessage(repo, zap.InfoLevel, "CancelInProgress", msg) return nil } @@ -144,7 +144,7 @@ func (p *PacRun) cancelPipelineRunsOpsComment(ctx context.Context, repo *v1alpha if len(prs.Items) == 0 { msg := fmt.Sprintf("no pipelinerun found for repository: %v , sha: %v and pulRequest %v", p.event.Repository, p.event.SHA, p.event.PullRequestNumber) - p.eventEmitter.EmitMessage(repo, zap.InfoLevel, "RepositoryPipelineRun", msg) + p.eventEmitter.EmitMessage(repo, zap.InfoLevel, "CancelInProgress", msg) return nil } @@ -183,7 +183,7 @@ func (p *PacRun) cancelPipelineRuns(ctx context.Context, prs *tektonv1.PipelineR defer wg.Done() if _, err := action.PatchPipelineRun(ctx, p.logger, "cancel patch", p.run.Clients.Tekton, &pr, cancelMergePatch); err != nil { errMsg := fmt.Sprintf("failed to cancel pipelineRun %s/%s: %s", pr.GetNamespace(), pr.GetName(), err.Error()) - p.eventEmitter.EmitMessage(repo, zap.ErrorLevel, "RepositoryPipelineRun", errMsg) + p.eventEmitter.EmitMessage(repo, zap.ErrorLevel, "CancelInProgress", errMsg) } }(ctx, pr) } diff --git a/test/pkg/gitea/test.go b/test/pkg/gitea/test.go index f9a4ae9eb0..1a0453ef63 100644 --- a/test/pkg/gitea/test.go +++ b/test/pkg/gitea/test.go @@ -27,6 +27,7 @@ import ( v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" "github.com/tektoncd/pipeline/pkg/names" "gotest.tools/v3/assert" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -72,6 +73,26 @@ func PostCommentOnPullRequest(t *testing.T, topt *TestOpts, body string) { assert.NilError(t, err) } +func checkEvents(t *testing.T, events *corev1.EventList, topts *TestOpts) { + t.Helper() + newEvents := make([]corev1.Event, 0) + // filter out events that are not related to the test like checking for cancelled pipelineruns + for i := len(events.Items) - 1; i >= 0; i-- { + topts.ParamsRun.Clients.Log.Infof("Reason is %s", events.Items[i].Reason) + if events.Items[i].Reason == "CancelInProgress" { + continue + } + newEvents = append(newEvents, events.Items[i]) + } + if len(newEvents) > 0 { + topts.ParamsRun.Clients.Log.Infof("0 events expected in case of failure but got %d", len(newEvents)) + for _, em := range newEvents { + topts.ParamsRun.Clients.Log.Infof("Event: Reason: %s Type: %s ReportingInstance: %s Message: %s", em.Reason, em.Type, em.ReportingInstance, em.Message) + } + t.FailNow() + } +} + func AddLabelToIssue(t *testing.T, topt *TestOpts, label string) { var targetID int64 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()) { } assert.Assert(t, len(events.Items) != 0, "events expected in case of failure but got 0") } else if !topts.SkipEventsCheck { - 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)) + checkEvents(t, events, topts) } return ctx, cleanup } @@ -369,7 +390,7 @@ func NewPR(t *testing.T, topts *TestOpts) func() { } assert.Assert(t, len(events.Items) != 0, "events expected in case of failure but got 0") } else if !topts.SkipEventsCheck { - 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)) + checkEvents(t, events, topts) } return cleanup }