Last active
March 1, 2016 15:48
-
-
Save zapo/1ba29f026ee97cfc1ba9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
## -*- sh-basic-offset: 2 -*- | |
set -o errexit -o nounset -o pipefail | |
BASENAME=$(basename "$0") | |
BASEURL="https://api.travis-ci.com" | |
eval "$(docopts -A args -V - -h - : "$@" <<EOF | |
Usage: | |
$BASENAME [options] <repo> | |
$BASENAME --help | |
$BASENAME --version | |
Arguments: | |
<repo> Repository slug, eg: adgear/rtb-trader. | |
Options: | |
--build-id=<build_id> <build_id> to restart | |
--branch=<branch> Rebuild latest build of <branch> [default: master]. | |
--event-type=<event-type> Event type to rebuild [default: pull_request]. | |
--trace Print bash trace. | |
--help Show this message. | |
---- | |
$BASENAME | |
EOF | |
)" | |
${args[--trace]} && set -o xtrace | |
get_token() { | |
travis token --pro --no-interactive | |
} | |
DEFAULT_TOKEN=$(get_token) | |
: ${TOKEN:=$DEFAULT_TOKEN} | |
TRAVIS_AUTH_HEADER="Authorization: token ${TOKEN}" | |
CURL="curl --silent --fail" | |
get_builds() { | |
$CURL -H "$TRAVIS_AUTH_HEADER" \ | |
"${BASEURL}/repos/${args[<repo>]}/builds?event_type=${args[--event-type]}" | |
} | |
get_latest_build_id() { | |
get_builds |\ | |
jq -c "map(select(.branch == \"${args[--branch]}\" and .state == \"finished\")) | .[0].id" | |
} | |
get_build_id() { | |
if [ "${args[--build-id]}" != "" ]; then | |
echo ${args[--build-id]} | |
else | |
get_latest_build_id | |
fi | |
} | |
restart_build() { | |
local build_id; build_id=$1 | |
$CURL -X POST -H "$TRAVIS_AUTH_HEADER" \ | |
"${BASEURL}/builds/${build_id}/restart" |\ | |
jq '.result' | |
} | |
main() { | |
local build_id; build_id=$(get_build_id) | |
[ "$(restart_build $build_id)" == "true" ] | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Default event-type should probably be
push
.pull-request
events can disappear.