Skip to content

Commit

Permalink
Merge pull request #26 from EnricoMi/branch-show-error
Browse files Browse the repository at this point in the history
Make action show API error message and curl error
  • Loading branch information
lizrabuya authored Feb 17, 2023
2 parents 144ab0c + 9688e82 commit c9823da
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 8 deletions.
14 changes: 12 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,25 @@ if [[ "${BUILD_ENV_VARS:-}" ]]; then
fi
fi

CODE=0
RESPONSE=$(
curl \
--fail \
--fail-with-body \
--silent \
--show-error \
-X POST \
-H "Authorization: Bearer ${BUILDKITE_API_ACCESS_TOKEN}" \
"https://api.buildkite.com/v2/organizations/${ORG_SLUG}/pipelines/${PIPELINE_SLUG}/builds" \
-d "$JSON" | tr -d '\n'
)
) || CODE=$?

if [ $CODE -ne 0 ]; then
MESSAGE=$(echo "$RESPONSE" | jq .message 2> /dev/null || true)
if [[ -n "$MESSAGE" ]] && [[ "$MESSAGE" != 'null' ]]; then
echo -n "Buildkite API call failed: $MESSAGE"
fi
exit $CODE
fi

echo ""
echo "Build created:"
Expand Down
88 changes: 82 additions & 6 deletions tests/entrypoint.bats
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ teardown() {
EXPECTED_JSON='{"commit":"a-sha","branch":"a-branch","message":"","author":{"name":"The Pusher","email":"pusher@pusher.com"}}'
RESPONSE_JSON='{"web_url": "https://buildkite.com/build-url"}'

stub curl "--fail --silent -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$RESPONSE_JSON'"
stub curl "--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$RESPONSE_JSON'"

run "${PWD}"/entrypoint.sh

Expand All @@ -65,7 +65,7 @@ teardown() {
EXPECTED_JSON='{"commit":"custom-commit","branch":"a-branch","message":"","author":{"name":"The Pusher","email":"pusher@pusher.com"}}'
RESPONSE_JSON='{"web_url": "https://buildkite.com/build-url"}'

stub curl "--fail --silent -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$RESPONSE_JSON'"
stub curl "--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$RESPONSE_JSON'"

run "${PWD}"/entrypoint.sh

Expand All @@ -87,7 +87,7 @@ teardown() {
EXPECTED_JSON='{"commit":"a-sha","branch":"custom-branch","message":"","author":{"name":"The Pusher","email":"pusher@pusher.com"}}'
RESPONSE_JSON='{"web_url": "https://buildkite.com/build-url"}'

stub curl "--fail --silent -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$RESPONSE_JSON'"
stub curl "--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$RESPONSE_JSON'"

run "${PWD}"/entrypoint.sh

Expand All @@ -109,7 +109,7 @@ teardown() {
EXPECTED_JSON='{"commit":"a-sha","branch":"a-branch","message":"A custom message","author":{"name":"The Pusher","email":"pusher@pusher.com"}}'
RESPONSE_JSON='{"web_url": "https://buildkite.com/build-url"}'

stub curl "--fail --silent -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$RESPONSE_JSON'"
stub curl "--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$RESPONSE_JSON'"

run "${PWD}"/entrypoint.sh

Expand All @@ -131,7 +131,7 @@ teardown() {
EXPECTED_JSON='{"commit":"a-sha","branch":"a-branch","message":"","author":{"name":"The Pusher","email":"pusher@pusher.com"},"env":{"FOO":"bar"}}'
RESPONSE_JSON='{"web_url": "https://buildkite.com/build-url"}'

stub curl "--fail --silent -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$RESPONSE_JSON'"
stub curl "--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$RESPONSE_JSON'"

run "${PWD}"/entrypoint.sh

Expand All @@ -156,7 +156,7 @@ teardown() {
EXPECTED_JSON='{"commit":"a-sha","branch":"a-branch","message":"","author":{"name":"The Pusher","email":"pusher@pusher.com"},"env":{"FOO":"bar"}}'
RESPONSE_JSON='{"web_url": "https://buildkite.com/build-url"}'

stub curl "--fail --silent -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$RESPONSE_JSON'"
stub curl "--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$RESPONSE_JSON'"

assert_file_not_exist $GITHUB_OUTPUT
assert_not_exist $GITHUB_OUTPUT
Expand All @@ -177,6 +177,82 @@ teardown() {
unstub curl
}

@test "Prints curl error on HTTP error" {
export BUILDKITE_API_ACCESS_TOKEN="123"
export PIPELINE="my-org/my-pipeline"

EXPECTED_JSON='{"commit":"a-sha","branch":"a-branch","message":"","author":{"name":"The Pusher","email":"pusher@pusher.com"}}'

stub curl "--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo 'curl: (22) The requested URL returned error: 401' >&2; exit 22"

run "${PWD}"/entrypoint.sh

assert_output --partial "curl: (22) The requested URL returned error: 401"
refute_output --partial "Buildkite API call failed"

assert_failure 22

unstub curl
}

@test "Prints curl error and ignores non-JSON response on HTTP error" {
export BUILDKITE_API_ACCESS_TOKEN="123"
export PIPELINE="my-org/my-pipeline"

EXPECTED_JSON='{"commit":"a-sha","branch":"a-branch","message":"","author":{"name":"The Pusher","email":"pusher@pusher.com"}}'
RESPONSE_HTML='<html></html>'

stub curl "--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$RESPONSE_HTML'; echo 'curl: (22) The requested URL returned error: 401' >&2; exit 22"

run "${PWD}"/entrypoint.sh

assert_output --partial "curl: (22) The requested URL returned error: 401"
refute_output --partial "Buildkite API call failed"
refute_output --partial "parse error"

assert_failure 22

unstub curl
}

@test "Prints curl error but not null JSON response message on HTTP error" {
export BUILDKITE_API_ACCESS_TOKEN="123"
export PIPELINE="my-org/my-pipeline"

EXPECTED_JSON='{"commit":"a-sha","branch":"a-branch","message":"","author":{"name":"The Pusher","email":"pusher@pusher.com"}}'
RESPONSE_JSON='{"message": null}'

stub curl "--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$RESPONSE_JSON'; echo 'curl: (22) The requested URL returned error: 401' >&2; exit 22"

run "${PWD}"/entrypoint.sh

assert_output --partial "curl: (22) The requested URL returned error: 401"
refute_output --partial "Buildkite API call failed"

assert_failure 22

unstub curl
}

@test "Prints curl error and JSON response message on HTTP error" {
export BUILDKITE_API_ACCESS_TOKEN="123"
export PIPELINE="my-org/my-pipeline"

EXPECTED_JSON='{"commit":"a-sha","branch":"a-branch","message":"","author":{"name":"The Pusher","email":"pusher@pusher.com"}}'
RESPONSE_JSON='{"message": "Error Message."}'

stub curl "--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$RESPONSE_JSON'; echo 'curl: (22) The requested URL returned error: 401' >&2; exit 22"

run "${PWD}"/entrypoint.sh

assert_output --partial "curl: (22) The requested URL returned error: 401"
assert_output --partial 'Buildkite API call failed: "Error Message."'

assert_failure 22

unstub curl
}

@test "Prints error and fails if \$BUILD_ENV_VARS is not valid JSON" {
export BUILDKITE_API_ACCESS_TOKEN="123"
export PIPELINE="my-org/my-pipeline"
Expand Down

0 comments on commit c9823da

Please sign in to comment.