Skip to content

Commit

Permalink
fix: strip spaces and newlines from the comment before matching
Browse files Browse the repository at this point in the history
We are now stripping spaces and newlines from the beginning and the end to make
sure that the regex matches correctly. The ^ will match the beginning of the
comment and $ will match the end of the comment without newlines or space.

Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel authored and pipelines-as-code[bot] committed Feb 13, 2025
1 parent f1b8b8e commit 7051c93
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
4 changes: 2 additions & 2 deletions docs/content/docs/guide/gitops_commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ grep "string" /tmp/comment

## Custom GitOps Commands

Using the [on-comment]({{< relref "/docs/guide/matchingevents.md#matching-a-pipelinerun-on-a-regexp-in-a-comment" >}}) annotation on your `PipelineRun`, you can define custom GitOps commands that will be triggered by comments on the Pull Request.
Using the [on-comment]({{< relref "/docs/guide/matchingevents.md#matching-a-pipelinerun-on-a-regex-in-a-comment" >}}) annotation on your `PipelineRun`, you can define custom GitOps commands that will be triggered by comments on the Pull Request.

See the [on-comment]({{< relref "/docs/guide/matchingevents.md#matching-a-pipelinerun-on-a-regexp-in-a-comment" >}}) guide for more detailed information.
See the [on-comment]({{< relref "/docs/guide/matchingevents.md#matching-a-pipelinerun-on-a-regex-in-a-comment" >}}) guide for more detailed information.

For a complete example, you can see how Pipelines-as-Code's own repo implemented some prow comments via the `on-comment` annotation:

Expand Down
48 changes: 28 additions & 20 deletions docs/content/docs/guide/matchingevents.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,26 @@ and you have a `Pull Request` changing the files `.tekton/pipelinerun.yaml`,
`on-path-change-ignore` annotation will ignore the `***.md` and `***.yaml`
files.

## Matching a PipelineRun on a Regexp in a comment
## Matching a PipelineRun on a Regex in a comment

{{< tech_preview "Matching PipelineRun on regexp in comments" >}}
{{< tech_preview "Matching PipelineRun on regex in comments" >}}
{{< support_matrix github_app="true" github_webhook="true" gitea="true" gitlab="true" bitbucket_cloud="false" bitbucket_server="false" >}}

You can match a PipelineRun on a comment on a Pull Request or a [Pushed Commit]({{< relref "/docs/guide/running.md#gitops-commands-on-pushed-commits">}})
with the annotation `pipelinesascode.tekton.dev/on-comment`.
You can trigger a PipelineRun based on a comment on a Pull Request or a [Pushed
Commit]({{< relref
"/docs/guide/running.md#gitops-commands-on-pushed-commits">}}) using the
annotation `pipelinesascode.tekton.dev/on-comment`.

The comment is a regexp and if a newly created comment has this regexp it will
automatically match the PipelineRun and start it.
The comment is treated as a regular expression (regex). The spaces and newlines
are stripped at the beginning or the end of the comment before matching so `^`
will match the beginning of the comment and `$` will match the end of the
comment without newlines or space.

For example:
If a new comment on a Pull Request matches the specified regex, the PipelineRun
will be triggered and started. This only applies to newly created comments;
updates or edits to existing comments will not trigger the PipelineRun.

Example:

```yaml
---
Expand All @@ -198,23 +206,23 @@ metadata:
pipelinesascode.tekton.dev/on-comment: "^/merge-pr"
```

This will match the merge-pr `PipelineRun` when a comment on a pull request
This will trigger the merge-pr PipelineRun when a comment on a pull request
starts with `/merge-pr`.

When the PipelineRun that has been triggered with the `on-comment` annotation
gets started, the template variable `{{ trigger_comment }}` gets set. See the
documentation [here]({{< relref "/docs/guide/gitops_commands.md#accessing-the-comment-triggering-the-pipelinerun" >}})
When a PipelineRun is getting triggered by the `on-comment` annotation starts,
the template variable {{ trigger_comment }} is set. For more details, refer to
the [documentation]({{< relref
"/docs/guide/gitops_commands.md#accessing-the-comment-triggering-the-pipelinerun"
>}}).

Note that the `on-comment` annotation will respect the `pull_request` [Policy]({{< relref "/docs/guide/policy" >}}) rule,
so only users in the `pull_request` policy will be able to trigger the
PipelineRun.
Note that the on-comment annotation adheres to the pull_request [Policy]({{<
relref "/docs/guide/policy" >}}) rule. Only users specified in the pull_request
policy will be able to trigger the PipelineRun.

{{< hint info >}}

- The `on-comment` annotation is supported on `pull_request`. On `push` events,
it is only supported [when targeting the main branch without arguments]({{< relref
"/docs/guide/gitops_commands.md#gitops-commands-on-pushed-commits" >}}).

The on-comment annotation is supported for pull_request events. For push events,
it is only supported [when targeting the main branch without arguments]({{<
relref "/docs/guide/gitops_commands.md#gitops-commands-on-pushed-commits" >}}).
{{< /hint >}}

## Matching PipelineRun to a Pull Request labels
Expand Down Expand Up @@ -304,7 +312,7 @@ You can find more information about the CEL language spec here:
<https://github.com/google/cel-spec/blob/master/doc/langdef.md>
{{< /hint >}}

### Matching a PipelineRun to a branch with a regexp
### Matching a PipelineRun to a branch with a regex

In a CEL expression, you can match a field name using a regular expression. For
example, if you want to trigger a `PipelineRun` for the`pull_request` event and
Expand Down
5 changes: 4 additions & 1 deletion pkg/matcher/annotation_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@ func MatchPipelinerunByAnnotation(ctx context.Context, logger *zap.SugaredLogger
logger.Warnf("could not compile regexp %s from pipelineRun %s", targetComment, prName)
continue
}
if re.MatchString(event.TriggerComment) {

strippedComment := strings.TrimSpace(
strings.TrimPrefix(strings.TrimSuffix(event.TriggerComment, "\r\n"), "\r\n"))
if re.MatchString(strippedComment) {
event.EventType = opscomments.OnCommentEventType.String()
logger.Infof("matched pipelinerun with name: %s on gitops comment: %q", prName, event.TriggerComment)
matchedPRs = append(matchedPRs, prMatch)
Expand Down
4 changes: 2 additions & 2 deletions pkg/matcher/annotation_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,7 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "pipeline-on-comment",
Annotations: map[string]string{
keys.OnComment: "^/hello-world",
keys.OnComment: "^/hello-world$",
},
},
}
Expand Down Expand Up @@ -1690,7 +1690,7 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
args: args{
pruns: []*tektonv1.PipelineRun{pipelineGood, pipelineOnComment},
runevent: info.Event{
TriggerComment: "/hello-world",
TriggerComment: " /hello-world \r\n",
TriggerTarget: "pull_request",
EventType: opscomments.OnCommentEventType.String(),
BaseBranch: "main",
Expand Down

0 comments on commit 7051c93

Please sign in to comment.