Skip to content

Commit

Permalink
Merge pull request #24 from seek-oss/prune-remotes
Browse files Browse the repository at this point in the history
More aggressive filtering for update_prs option
  • Loading branch information
seekdave committed Jul 23, 2019
2 parents 9091b20 + 9c11765 commit 052fd11
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
6 changes: 3 additions & 3 deletions hooks/post-checkout
Expand Up @@ -21,7 +21,7 @@ EOL
}

trigger_prs() {
git fetch origin
git fetch origin --prune
export -f trigger_pr_branch
git ls-remote origin 'refs/pull/*/head' | sort -t'/' -k 3 -nr | xargs -P 20 -n 2 bash -c 'trigger_pr_branch $@' _
}
Expand All @@ -38,12 +38,12 @@ trigger_pr_branch() {
fi

# Unless the commit already exists in the default branch (i.e. PR is merged)
if git merge-base --is-ancestor "${sha}" "${BUILDKITE_PIPELINE_DEFAULT_BRANCH}" 2>/dev/null; then
if git merge-base --is-ancestor "${sha}" "origin/${BUILDKITE_PIPELINE_DEFAULT_BRANCH}" 2>/dev/null; then
return
fi

set +e
branch="$(git branch -a -q --contains "${sha}" 2>/dev/null | grep 'remotes/origin' | sed -e 's,^[[:space:]]*remotes/origin/,,g')"
branch="$(git branch -a -q --contains "${sha}" 2>/dev/null | grep 'remotes/origin' | grep -v -e 'remotes/origin/master' -e 'remotes/origin/HEAD' | sed -e 's,^[[:space:]]*remotes/origin/,,g')"
set -e

# Unless the PR branch was deleted
Expand Down
21 changes: 7 additions & 14 deletions tests/post-checkout.bats
Expand Up @@ -110,28 +110,21 @@ post_checkout_hook="$PWD/hooks/post-checkout"
"pipeline upload : echo UPLOADED_PIPELINE"

stub git \
"fetch origin : echo FETCHED" \
"ls-remote origin 'refs/pull/*/head' : echo deadbeef refs/pull/123/head" \
"merge-base --is-ancestor deadbeef dev : echo UNMERGED && false" \
"fetch origin --prune : echo FETCHED" \
"ls-remote origin refs/pull/*/head : echo beefbeef refs/pull/122/head deadbeef refs/pull/123/head" \
"merge-base --is-ancestor deadbeef origin/dev : echo UNMERGED && false" \
"branch -a -q --contains deadbeef : echo remotes/origin/MY_PR_BRANCH"

stub grep \
"remotes/origin : echo remotes/origin/MY_PR_BRANCH"

stub sed \
"-e 's,^[[:space:]]*remotes/origin/,,g' : echo MY_PR_BRANCH"

run "$post_checkout_hook"

assert_success
assert_output --partial "Updating PRs"
assert_output --partial "FETCHED"
assert_output --partial "UNMERGED"
assert_output --partial "Triggering new build of my-pipeline for PR 123"
assert_output --partial "UPLOADED_PIPELINE"
unstub buildkite-agent
unstub git
unstub grep
unstub sed
}

@test "Doesn't rebuild merged PR branches" {
Expand All @@ -142,9 +135,9 @@ post_checkout_hook="$PWD/hooks/post-checkout"
export BUILDKITE_BRANCH="dev"

stub git \
"fetch origin : echo FETCHED" \
"fetch origin --prune : echo FETCHED" \
"ls-remote origin 'refs/pull/*/head' : echo deadbeef refs/pull/123/head" \
"merge-base --is-ancestor deadbeef dev : echo MERGED"
"merge-base --is-ancestor deadbeef origin/dev : echo MERGED"

run "$post_checkout_hook"

Expand All @@ -163,7 +156,7 @@ post_checkout_hook="$PWD/hooks/post-checkout"
export BUILDKITE_BRANCH="dev"

stub git \
"fetch origin : echo FETCHED" \
"fetch origin --prune : echo FETCHED" \
"ls-remote origin 'refs/pull/*/head' : echo deadbeef refs/pull/123/head"

run "$post_checkout_hook"
Expand Down

0 comments on commit 052fd11

Please sign in to comment.