Skip to content

Commit

Permalink
fix: Correct event-type setting for on-comment prs
Browse files Browse the repository at this point in the history
This commit fixes the event-type setting for PipelineRuns that match
the `on-comment` annotation. Previously, the `event_type` was set to
`no-ops-comment` instead of `on-comment` when a git provider's issue
comment event matched a comment in an annotation. This fix ensures that
the `event_type` is correctly set.

on-comment is a brand new TP feature, so no worries that we may have
breaking things for users relying on that behaviour.

Issue: https://issue.redhat.com/browse/SRVKP-5779
Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel authored and savitaashture committed Jul 5, 2024
1 parent be424e4 commit 7663fb1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
19 changes: 19 additions & 0 deletions pkg/pipelineascode/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,25 @@ func (p *PacRun) getPipelineRunsFromRepo(ctx context.Context, repo *v1alpha1.Rep
p.event.AccountID = ""
}

// NOTE(chmouel): Initially, matching is performed here to accurately
// expand dynamic matching in events. This expansion is crucial for
// applying dynamic variables, such as setting the `event_type` to
// `on-comment` when matching a git provider's issue comment event with a
// comment in an annotation. Although matching occurs three times within
// this loop, which might seem inefficient, it's essential to maintain
// current functionality without introducing potential errors or behavior
// changes. Refactoring for optimization could lead to significant
// challenges in tracking down issues. Despite the repetition, the
// performance impact is minimal, involving only a loop and a few
// conditions.
if p.event.TargetTestPipelineRun == "" {
rtypes, err := resolve.ReadTektonTypes(ctx, p.logger, rawTemplates)
if err != nil {
return nil, err
}
// Don't fail or do anything if we don't have a match yet, we will do it properly later in this function
_, _ = matcher.MatchPipelinerunByAnnotation(ctx, p.logger, rtypes.PipelineRuns, p.run, p.event, p.vcx)
}
// Replace those {{var}} placeholders user has in her template to the run.Info variable
allTemplates := p.makeTemplate(ctx, repo, rawTemplates)

Expand Down
7 changes: 6 additions & 1 deletion test/github_pullrequest_test_comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,14 @@ func TestGithubSecondOnCommentAnnotation(t *testing.T) {
repo, err := twait.UntilRepositoryUpdated(ctx, g.Cnx.Clients, waitOpts)
assert.NilError(t, err)
g.Cnx.Clients.Log.Infof("Check if we have the repository set as succeeded")
assert.Assert(t, repo.Status[len(repo.Status)-1].Conditions[0].Status == corev1.ConditionTrue)
assert.Equal(t, repo.Status[len(repo.Status)-1].Conditions[0].Status, corev1.ConditionTrue)
assert.Equal(t, *repo.Status[len(repo.Status)-1].EventType, opscomments.OnCommentEventType.String())
lastPrName := repo.Status[len(repo.Status)-1].PipelineRunName

err = twait.RegexpMatchingInPodLog(context.Background(), g.Cnx, g.TargetNamespace, fmt.Sprintf("tekton.dev/pipelineRun=%s", lastPrName), "step-task", *regexp.MustCompile(triggerComment), "", 2)
assert.NilError(t, err)

err = twait.RegexpMatchingInPodLog(context.Background(), g.Cnx, g.TargetNamespace, fmt.Sprintf("tekton.dev/pipelineRun=%s", lastPrName), "step-task", *regexp.MustCompile(fmt.Sprintf(
"The event is %s", opscomments.OnCommentEventType.String())), "", 2)
assert.NilError(t, err)
}
1 change: 1 addition & 0 deletions test/testdata/TestGiteaOnCommentAnnotation.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
The comment is:
/hello-world revision=main custom1=thisone custom2="another one" custom3="a \"quote\""
The event is on-comment
The revision is main
The custom1 value is thisone
The custom2 value is another one
Expand Down
1 change: 1 addition & 0 deletions test/testdata/pipelinerun-on-comment-annotation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ spec:
cat <<EOF
{{ trigger_comment }}
EOF
echo "The event is {{ event_type }}"
echo "The revision is {{ revision }}"
echo "The custom1 value is {{ custom1 }}"
echo "The custom2 value is {{ custom2 }}"
Expand Down

0 comments on commit 7663fb1

Please sign in to comment.