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

Commit

Permalink
Allow multiple watch paths for single target
Browse files Browse the repository at this point in the history
Sometimes you'll want to trigger a single pipeline based on multiple
paths not sharing a common root. This change allows you to specify
an array of paths to watch for a single target in addition to preserving
the old behavior of a single string.

I believe this change also unsurfaced a bug with the existing diff tests
where `buildkite-agent pipeline upload` was expected to be called in the
tests but shouldn't acutally be called based on the code in the command
hook.

Signed-off-by: Elliott Davis <elliott@excellent.io>
  • Loading branch information
Elliott Davis committed Jan 29, 2019
1 parent 70a7250 commit 9702162
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 26 deletions.
28 changes: 23 additions & 5 deletions lib/shared.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,34 @@ function plugin_read_list() {
local prefix="BUILDKITE_PLUGIN_MONOREPO_DIFF_$1"
local suffix="$2"
local parameter="${prefix}_0_${suffix}"
local chained_parameter="${prefix}_0_${suffix}_0"

if [[ -n "${!parameter:-}" ]]; then
# Reads nested list of multiple watch paths
if [[ -n "${!parameter:-}" || -n "${!chained_parameter:-}" ]]; then
local i=0
local parameter="${prefix}_${i}_${suffix}"
while [[ -n "${!parameter:-}" ]]; do
echo "${!parameter}"
while [[ -n "${!parameter:-}" || -n "${!chained_parameter:-}" ]]; do
local parameter="${prefix}_${i}_${suffix}"
# check for single path oldschool
if [[ -n "${!parameter:-}" ]]; then
echo "${!parameter}"
else
# check for new style
local params=()
local j=0
while [[ -n "${!chained_parameter:-}" ]]; do
params+=("${!chained_parameter}")
j=$((j+1))
chained_parameter="${prefix}_${i}_${suffix}_${j}"
done
echo "${params[@]}"
fi
i=$((i+1))
chained_parameter="${prefix}_${i}_${suffix}_0"
parameter="${prefix}_${i}_${suffix}"
done
elif [[ -n "${!prefix:-}" ]]; then
fi
# Read one watch with one path
if [[ -n "${!prefix:-}" ]]; then
echo "${!prefix}"
fi
}
Expand Down
12 changes: 6 additions & 6 deletions lib/watch.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ set -ueo pipefail
function has_changed() {
local diff_output=$1
local watched_path=$2

if echo "$diff_output" | grep -q "^$watched_path" ; then
return 0
else
return 1
fi
for path in $watched_path; do
if echo "$diff_output" | grep -q "^$path" ; then
return 0
fi
done
return 1
}

function get_index_of_pipelines_to_trigger() {
Expand Down
3 changes: 2 additions & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ configuration:
type: array
properties:
path:
type: string
type: [string, array]
minimum: 1
config:
type: object
properties:
Expand Down
14 changes: 0 additions & 14 deletions tests/diff.bats
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ load '/usr/local/lib/bats/load.bash'
@test "Run the specified inline diff command" {
export BUILDKITE_PLUGIN_MONOREPO_DIFF_DIFF="cat $PWD/tests/mocks/diff1"

stub buildkite-agent \
"pipeline upload : echo uploading"

run $PWD/hooks/command

unstub buildkite-agent
assert_success
assert_output --partial "services/foo/serverless.yml"
assert_output --partial "services/bar/config.yml"
Expand All @@ -21,25 +17,18 @@ load '/usr/local/lib/bats/load.bash'
@test "Run the specified shell script" {
export BUILDKITE_PLUGIN_MONOREPO_DIFF_DIFF="$PWD/tests/mocks/diff2.sh"

stub buildkite-agent \
"pipeline upload : echo uploading"

run $PWD/hooks/command

unstub buildkite-agent
assert_success
assert_output --partial "diff 2"
}

@test "Run default diff command when shell script is not specified" {
stub git \
"diff --name-only HEAD~1 : echo services/git/head-minus-1.yml"
stub buildkite-agent \
"pipeline upload : echo uploading"

run $PWD/hooks/command

unstub buildkite-agent
unstub git
assert_success
assert_output --partial "services/git/head-minus-1.yml"
Expand All @@ -48,9 +37,6 @@ load '/usr/local/lib/bats/load.bash'
@test "Exits on shell script error" {
export BUILDKITE_PLUGIN_MONOREPO_DIFF_DIFF="$PWD/tests/mocks/thisdoesnotexist.sh"

stub buildkite-agent \
"pipeline upload : echo uploading"

run $PWD/hooks/command

[ "$status" -eq 1 ]
Expand Down
28 changes: 28 additions & 0 deletions tests/trigger.bats
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@ load '/usr/local/lib/bats/load.bash'
assert_output --partial " - trigger: slug-for-bar"
}

@test "Generates trigger with multiple paths" {
export BUILDKITE_PLUGIN_MONOREPO_DIFF_DIFF="cat $PWD/tests/mocks/diff1"
# mixed string and array behavior
export BUILDKITE_PLUGIN_MONOREPO_DIFF_WATCH_0_PATH="services/foo"
export BUILDKITE_PLUGIN_MONOREPO_DIFF_WATCH_0_CONFIG_TRIGGER="slug-for-foo"
export BUILDKITE_PLUGIN_MONOREPO_DIFF_WATCH_1_PATH="services/path-not-in-diff-1"
export BUILDKITE_PLUGIN_MONOREPO_DIFF_WATCH_1_CONFIG_TRIGGER="slug-for-path-not-in-diff"
# path not in diff
export BUILDKITE_PLUGIN_MONOREPO_DIFF_WATCH_2_PATH_0="services/path-not-in-diff-2"
export BUILDKITE_PLUGIN_MONOREPO_DIFF_WATCH_2_CONFIG_TRIGGER="slug-for-path-not-in-diff"
# one path in diff, one path not in diff
export BUILDKITE_PLUGIN_MONOREPO_DIFF_WATCH_3_PATH_0="services/bar"
export BUILDKITE_PLUGIN_MONOREPO_DIFF_WATCH_3_PATH_1="services/far"
export BUILDKITE_PLUGIN_MONOREPO_DIFF_WATCH_3_CONFIG_TRIGGER="slug-for-bar"
export BUILDKITE_PLUGIN_MONOREPO_DIFF_WATCH_3_CONFIG_LABEL="Bar service deployment"
export DEBUG=true

stub buildkite-agent \
"pipeline upload : echo uploading"

run $PWD/hooks/command

unstub buildkite-agent
assert_success
assert_output --partial " - trigger: slug-for-foo"
assert_output --partial " - trigger: slug-for-bar"
}

@test "Uses user defined label if it exist" {
export BUILDKITE_PLUGIN_MONOREPO_DIFF_DIFF="cat $PWD/tests/mocks/diff1"
export BUILDKITE_PLUGIN_MONOREPO_DIFF_WATCH_0_PATH="services/foo"
Expand Down

0 comments on commit 9702162

Please sign in to comment.