Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
feat: add skip_path option in watch
Browse files Browse the repository at this point in the history
Add support for skip_path option

Add tests

Update README
  • Loading branch information
voluntadpear committed Mar 28, 2022
1 parent bbda0a1 commit 8e87cc1
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 6 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ Declare a list of
- assets/images/email
config:
trigger: email-deploy
- path: services/webhooks
skip_path: services/webhooks/*.md
config:
trigger: webhooks-deploy
```

### `path`
Expand All @@ -164,6 +168,14 @@ A list of paths can be provided to trigger the desired pipeline. Changes in any

A `path` can also be a glob pattern. For example specify `path: "**/*.md"` to match all markdown files.
### `skip_path`

If the `skip_path` specified here in the appears in the `diff` output, the `trigger` step will not be added to the dynamically generated `pipeline.yaml`. However, if the `diff` output contains matches with `path` that don't match `skip_path`, the `trigger` step will still be added to the dynamically generated `pipeline.yaml`.

A list of paths can be provided that should not trigger the desired pipeline. Changes in any of these paths will not initiate the pipeline provided in trigger.

A `skip_path` can also be a glob pattern. For example specify `path: "**/*.md"` to not add the `trigger` step if only markdown files are modified.

### `config`

Configuration supports 2 different step types.
Expand Down
17 changes: 16 additions & 1 deletion pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,25 @@ func stepsToTrigger(files []string, watch []WatchConfig) ([]Step, error) {
for _, p := range w.Paths {
for _, f := range files {
match, err := matchPath(p, f)

skip := false
for _, sp := range w.SkipPaths {
skipMatch, errSkip := matchPath(sp, f)

if errSkip != nil {
return nil, errSkip
}

if skipMatch {
skip = true
}
}

if err != nil {
return nil, err
}
if match {

if match && !skip {
steps = append(steps, w.Step)
break
}
Expand Down
86 changes: 86 additions & 0 deletions pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,93 @@ func TestPipelinesStepsToTrigger(t *testing.T) {
{Trigger: "txt"},
},
},
"skips service-2": {
ChangedFiles: []string{
"watch-path/text.txt",
},
WatchConfigs: []WatchConfig{
{
Paths: []string{"watch-path"},
Step: Step{Trigger: "service-1"},
},
{
Paths: []string{"watch-path"},
SkipPaths: []string{"watch-path/text.txt"},
Step: Step{Trigger: "service-2"},
}},
Expected: []Step{
{Trigger: "service-1"},
},
},
"skips extension wildcard": {
ChangedFiles: []string{
"text.secret.txt",
},
WatchConfigs: []WatchConfig{
{
Paths: []string{"*.txt"},
Step: Step{Trigger: "service-1"},
},
{
Paths: []string{"*.txt"},
SkipPaths: []string{"*.secret.txt"},
Step: Step{Trigger: "service-2"},
}},
Expected: []Step{
{Trigger: "service-1"},
},
},
"skips extension wildcard in subdir": {
ChangedFiles: []string{
"docs/text.secret.txt",
},
WatchConfigs: []WatchConfig{
{
Paths: []string{"**/*.txt"},
Step: Step{Trigger: "service-1"},
},
{
Paths: []string{"**/*.txt"},
SkipPaths: []string{"docs/*.txt"},
Step: Step{Trigger: "service-2"},
}},
Expected: []Step{
{Trigger: "service-1"},
},
},
"step is included even when one of the files is skipped": {
ChangedFiles: []string{
"docs/text.secret.txt",
"docs/text.txt",
},
WatchConfigs: []WatchConfig{
{
Paths: []string{"**/*.txt"},
Step: Step{Trigger: "service-1"},
},
{
Paths: []string{"**/*.txt"},
SkipPaths: []string{"docs/*.secret.txt"},
Step: Step{Trigger: "service-2"},
}},
Expected: []Step{
{Trigger: "service-1"},
{Trigger: "service-2"},
},
},
"fails if not path is included": {
ChangedFiles: []string{
"docs/text.txt",
},
WatchConfigs: []WatchConfig{
{
SkipPaths: []string{"docs/*.secret.txt"},
Step: Step{Trigger: "service-1"},
}},
Expected: []Step{},
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
steps, err := stepsToTrigger(tc.ChangedFiles, tc.WatchConfigs)
Expand Down
23 changes: 18 additions & 5 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ type HookConfig struct {

// WatchConfig Plugin watch configuration
type WatchConfig struct {
RawPath interface{} `json:"path"`
Paths []string
Step Step `json:"config"`
RawPath interface{} `json:"path"`
Paths []string
Step Step `json:"config"`
RawSkipPath interface{} `json:"skip_path"`
SkipPaths []string
}

// Step is buildkite pipeline definition
Expand Down Expand Up @@ -100,8 +102,8 @@ func (plugin *Plugin) UnmarshalJSON(data []byte) error {
plugin.Env = parseEnv(plugin.RawEnv)
plugin.RawEnv = nil

// Path can be string or an array of strings,
// handle both cases and create an array of paths.
// Path and SkipPath can be string or an array of strings,
// handle both cases and create an array of paths on both.
for i, p := range plugin.Watch {
switch p.RawPath.(type) {
case string:
Expand All @@ -112,13 +114,23 @@ func (plugin *Plugin) UnmarshalJSON(data []byte) error {
}
}

switch p.RawSkipPath.(type) {
case string:
plugin.Watch[i].SkipPaths = []string{plugin.Watch[i].RawSkipPath.(string)}
case []interface{}:
for _, v := range plugin.Watch[i].RawSkipPath.([]interface{}) {
plugin.Watch[i].SkipPaths = append(plugin.Watch[i].SkipPaths, v.(string))
}
}

if plugin.Watch[i].Step.Trigger != "" {
setBuild(&plugin.Watch[i].Step.Build)
}

appendEnv(&plugin.Watch[i], plugin.Env)

p.RawPath = nil
p.RawSkipPath = nil
}

return nil
Expand Down Expand Up @@ -165,6 +177,7 @@ func appendEnv(watch *WatchConfig, env map[string]string) {
watch.Step.RawEnv = nil
watch.Step.Build.RawEnv = nil
watch.RawPath = nil
watch.RawSkipPath = nil
}

// parse env in format from env=env-value to map[env] = env-value
Expand Down
7 changes: 7 additions & 0 deletions tests/command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ EOM
"config": {
"trigger": "markdown-pipeline"
}
},
{
"path": "**/*.md",
"skip_path": "**/*.secret.md",
"config": {
"trigger": "markdown-pipeline"
}
}
]
}
Expand Down

0 comments on commit 8e87cc1

Please sign in to comment.