Skip to content

Commit

Permalink
Fix some wording and example in the on-path-change/ignore doc
Browse files Browse the repository at this point in the history
Fixes #1840

Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel committed Dec 4, 2024
1 parent 29a4243 commit 8c40e2e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
20 changes: 15 additions & 5 deletions docs/content/docs/guide/authoringprs.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ metadata:
annotations:
pipelinesascode.tekton.dev/on-target-branch: "[main]"
pipelinesascode.tekton.dev/on-event: "[pull_request]"
pipelinesascode.tekton.dev/on-path-change: "[docs/***.md, manual/***.rst]"
pipelinesascode.tekton.dev/on-path-change: "[docs/**.md, manual/**.rst]"
```

This configuration will match and trigger the `PipelineRun` named
Expand All @@ -177,6 +177,16 @@ The patterns used are [glob](https://en.wikipedia.org/wiki/Glob_(programming))
patterns, not regexp. Here are some
[examples](https://github.com/gobwas/glob?tab=readme-ov-file#example) from the
library used for matching.

The `tkn pac` cli has a handy [globbing command]({{< relref "/docs/guide/cli.md#test-globbing-pattern" >}})
to test the glob pattern matching:

```bash
tkn pac info globbing "[PATTERN]"
```

will match the files with `[PATTERN]` in the current directory.

{{< /hint >}}

### Matching a PipelineRun by Ignoring Specific Path Changes
Expand All @@ -191,8 +201,8 @@ You still need to specify the event type and target branch. If you have a [CEL
expression](#matching-pipelinerun-by-path-change) the `on-path-change-ignore`
annotation will be ignored

This example triggers a `PipelineRun` when there are no changes in the `docs`
directory:
This PipelineRun will run when there are changes outside the docs
folder:

```yaml
metadata:
Expand Down Expand Up @@ -335,11 +345,11 @@ PipelineRun.
### Matching PipelineRun by path change

> *NOTE*: `Pipelines-as-Code` supports two ways to match files changed in a particular event. The `.pathChanged` suffix function supports [glob
pattern](https://github.com/ganbarodigital/go_glob#what-does-a-glob-pattern-look-like) and does not support different types of "changes" i.e. added, modified, deleted and so on. The other option is to use the `files.` property (`files.all`, `files.added`, `files.deleted`, `files.modified`, `files.renamed`) which can target specific types of changed files and supports using CEL expressions i.e. `files.all.exists(x, x.matches('renamed.go'))`.
pattern](https://github.com/gobwas/glob#example) and does not support different types of "changes" i.e. added, modified, deleted and so on. The other option is to use the `files.` property (`files.all`, `files.added`, `files.deleted`, `files.modified`, `files.renamed`) which can target specific types of changed files and supports using CEL expressions i.e. `files.all.exists(x, x.matches('renamed.go'))`.

If you want to have a PipelineRun running only if a path has
changed you can use the `.pathChanged` suffix function with a [glob
pattern](https://github.com/ganbarodigital/go_glob#what-does-a-glob-pattern-look-like). Here
pattern](https://github.com/gobwas/glob#example). Here
is a concrete example matching every markdown files (as files who has the `.md`
suffix) in the `docs` directory :

Expand Down
4 changes: 2 additions & 2 deletions docs/content/docs/guide/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@ GitHub API, but you can specify a custom GitHub API URL using the

{{< /details >}}

{{< details "tkn pac info globing" >}}
{{< details "tkn pac info globbing" >}}

### Test globing pattern
### Test globbing pattern

The `tkn pac info globbing` command allows you to test glob patterns to see if
they match, for example, when using the `on-patch-change` annotation.
Expand Down
5 changes: 5 additions & 0 deletions pkg/cmd/tknpac/info/globbing.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,20 @@ func globbingCommand(ioStreams *cli.IOStreams) *cobra.Command {
}
}

matched := false
g := glob.MustCompile(expression)
err := filepath.WalkDir(dir, func(path string, _ fs.DirEntry, _ error) error {
// remove the dir prefix with a regexp
p := strings.TrimPrefix(path, dir+"/")
if g.Match(p) {
matched = true
fmt.Fprintf(ioStreams.Out, "%s\n", p)
}
return nil
})
if err == nil && !matched {
return fmt.Errorf("no files matched the expression %s", expression)
}
return err
},
Annotations: map[string]string{
Expand Down
26 changes: 23 additions & 3 deletions pkg/cmd/tknpac/info/globbing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestGlobbing(t *testing.T) {
tests := []struct {
name string
pattern string
wantErr bool
wantErr string
files []string
str string
}{
Expand All @@ -30,18 +30,34 @@ func TestGlobbing(t *testing.T) {
"hello/moto.md",
},
},
{
name: "not matched/File Globbing",
pattern: "***/*.el",
files: []string{
"README.md",
"docs/blah.md",
"hello/moto.md",
},
wantErr: "no files matched",
},
{
name: "String Pattern",
pattern: "refs/*/release-*",
str: "refs/heads/release-foo",
},
{
name: "not matched/String Pattern",
pattern: "blah/*/release-*",
str: "refs/heads/release-foo",
wantErr: "has not matched",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.str != "" {
output, err := tknpactest.ExecCommandNoRun(globbingCommand, "-s", tt.str, tt.pattern)
if tt.wantErr {
assert.Assert(t, tt.wantErr, err != nil, "wantErr: %v, got: %v", tt.wantErr, err)
if tt.wantErr != "" {
assert.ErrorContains(t, err, tt.wantErr, "error: %v", err)
return
}
golden.Assert(t, output, strings.ReplaceAll(fmt.Sprintf("%s.golden", t.Name()), "/", "-"))
Expand All @@ -58,6 +74,10 @@ func TestGlobbing(t *testing.T) {
f.Close()
}
output, err := tknpactest.ExecCommandNoRun(globbingCommand, "-d", tmpdir.Path(), tt.pattern)
if tt.wantErr != "" {
assert.ErrorContains(t, err, tt.wantErr, "error: %v", err)
return
}
assert.NilError(t, err)
golden.Assert(t, output, strings.ReplaceAll(fmt.Sprintf("%s.golden", t.Name()), "/", "-"))
})
Expand Down
2 changes: 1 addition & 1 deletion test/pkg/cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra"
)

// ExecCommandNoRun setup a cobra command for running with params run and
// ExecCommand setup a cobra command for running with params run and
// custom iostream.
func ExecCommand(runcnx *params.Run, cmd func(*params.Run, *cli.IOStreams) *cobra.Command, args ...string) (string, error) {
bufout := new(bytes.Buffer)
Expand Down

0 comments on commit 8c40e2e

Please sign in to comment.