Skip to content

Commit 7051c93

Browse files
chmouelpipelines-as-code[bot]
authored andcommitted
fix: strip spaces and newlines from the comment before matching
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]>
1 parent f1b8b8e commit 7051c93

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

docs/content/docs/guide/gitops_commands.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ grep "string" /tmp/comment
104104

105105
## Custom GitOps Commands
106106

107-
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.
107+
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.
108108

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

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

docs/content/docs/guide/matchingevents.md

+28-20
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,26 @@ and you have a `Pull Request` changing the files `.tekton/pipelinerun.yaml`,
175175
`on-path-change-ignore` annotation will ignore the `***.md` and `***.yaml`
176176
files.
177177

178-
## Matching a PipelineRun on a Regexp in a comment
178+
## Matching a PipelineRun on a Regex in a comment
179179

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

183-
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">}})
184-
with the annotation `pipelinesascode.tekton.dev/on-comment`.
183+
You can trigger a PipelineRun based on a comment on a Pull Request or a [Pushed
184+
Commit]({{< relref
185+
"/docs/guide/running.md#gitops-commands-on-pushed-commits">}}) using the
186+
annotation `pipelinesascode.tekton.dev/on-comment`.
185187

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

189-
For example:
193+
If a new comment on a Pull Request matches the specified regex, the PipelineRun
194+
will be triggered and started. This only applies to newly created comments;
195+
updates or edits to existing comments will not trigger the PipelineRun.
196+
197+
Example:
190198

191199
```yaml
192200
---
@@ -198,23 +206,23 @@ metadata:
198206
pipelinesascode.tekton.dev/on-comment: "^/merge-pr"
199207
```
200208

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

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

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

212222
{{< hint info >}}
213-
214-
- The `on-comment` annotation is supported on `pull_request`. On `push` events,
215-
it is only supported [when targeting the main branch without arguments]({{< relref
216-
"/docs/guide/gitops_commands.md#gitops-commands-on-pushed-commits" >}}).
217-
223+
The on-comment annotation is supported for pull_request events. For push events,
224+
it is only supported [when targeting the main branch without arguments]({{<
225+
relref "/docs/guide/gitops_commands.md#gitops-commands-on-pushed-commits" >}}).
218226
{{< /hint >}}
219227

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

307-
### Matching a PipelineRun to a branch with a regexp
315+
### Matching a PipelineRun to a branch with a regex
308316

309317
In a CEL expression, you can match a field name using a regular expression. For
310318
example, if you want to trigger a `PipelineRun` for the`pull_request` event and

pkg/matcher/annotation_matcher.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ func MatchPipelinerunByAnnotation(ctx context.Context, logger *zap.SugaredLogger
204204
logger.Warnf("could not compile regexp %s from pipelineRun %s", targetComment, prName)
205205
continue
206206
}
207-
if re.MatchString(event.TriggerComment) {
207+
208+
strippedComment := strings.TrimSpace(
209+
strings.TrimPrefix(strings.TrimSuffix(event.TriggerComment, "\r\n"), "\r\n"))
210+
if re.MatchString(strippedComment) {
208211
event.EventType = opscomments.OnCommentEventType.String()
209212
logger.Infof("matched pipelinerun with name: %s on gitops comment: %q", prName, event.TriggerComment)
210213
matchedPRs = append(matchedPRs, prMatch)

pkg/matcher/annotation_matcher_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1520,7 +1520,7 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
15201520
ObjectMeta: metav1.ObjectMeta{
15211521
Name: "pipeline-on-comment",
15221522
Annotations: map[string]string{
1523-
keys.OnComment: "^/hello-world",
1523+
keys.OnComment: "^/hello-world$",
15241524
},
15251525
},
15261526
}
@@ -1690,7 +1690,7 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
16901690
args: args{
16911691
pruns: []*tektonv1.PipelineRun{pipelineGood, pipelineOnComment},
16921692
runevent: info.Event{
1693-
TriggerComment: "/hello-world",
1693+
TriggerComment: " /hello-world \r\n",
16941694
TriggerTarget: "pull_request",
16951695
EventType: opscomments.OnCommentEventType.String(),
16961696
BaseBranch: "main",

0 commit comments

Comments
 (0)