Skip to content

Commit

Permalink
fix static checks
Browse files Browse the repository at this point in the history
  • Loading branch information
lizrabuya committed Sep 18, 2023
1 parent 560b39e commit 2820a88
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ jobs:
run: docker run -v "$PWD":/mnt -w /mnt mvdan/shfmt -ci -i 2 -l -d -- *.sh

- name: shellcheck
run: docker run -v "$PWD":/mnt -w /mnt koalaman/shellcheck -- *.sh test/*.bats

- name: misc files formatting
run: docker run -v "$PWD":/mnt -w /mnt tmknom/prettier --check .
run: docker run -v "$PWD":/mnt -w /mnt koalaman/shellcheck -- *.sh tests/*.bats

tests:
name: Unit tests
Expand Down
53 changes: 25 additions & 28 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ function get_github_env_json() {
}

function get_build_env_vars_json() {
BUILD_ENV_VARS=$(
jq -c -s 'add' \
<(echo "$1") \
<(echo "$2") \
<(echo "$3")
)
BUILD_ENV_VARS=$(
jq -c -s 'add' \
<(echo "$1") \
<(echo "$2") \
<(echo "$3")
)
echo "$BUILD_ENV_VARS"
}

Expand Down Expand Up @@ -77,28 +77,27 @@ BUILD_ENV_VARS="${BUILD_ENV_VARS:-}"

DELETE_EVENT_JSON=""
if is_delete_event; then
DELETE_EVENT_JSON="$(get_delete_event_json)"
DELETE_EVENT_JSON="$(get_delete_event_json)"
fi

if [[ "$BUILD_ENV_VARS" ]]; then
if ! echo "$BUILD_ENV_VARS" | jq empty; then
echo ""
echo "Error: BUILD_ENV_VARS provided invalid JSON: $BUILD_ENV_VARS"
exit 1
if ! echo "$BUILD_ENV_VARS" | jq empty; then
echo ""
echo "Error: BUILD_ENV_VARS provided invalid JSON: $BUILD_ENV_VARS"
exit 1
fi
fi

BUILD_ENV_VARS_JSON="$(get_build_env_vars_json "$DELETE_EVENT_JSON" "$BUILD_ENV_VARS" "$(get_github_env_json)")"


# Use jq’s --arg properly escapes string values for us
JSON=$(
jq -c -n \
--arg COMMIT "$COMMIT" \
--arg BRANCH "$BRANCH" \
--arg COMMIT "$COMMIT" \
--arg BRANCH "$BRANCH" \
--arg MESSAGE "$MESSAGE" \
--arg NAME "$NAME" \
--arg EMAIL "$EMAIL" \
--arg NAME "$NAME" \
--arg EMAIL "$EMAIL" \
'{
"commit": $COMMIT,
"branch": $BRANCH,
Expand All @@ -111,7 +110,7 @@ JSON=$(
)

# Link pull request if pull request id is specified
if [[ ! -z "$PULL_REQUEST_ID" ]]; then
if [[ -n "$PULL_REQUEST_ID" ]]; then
JSON=$(echo "$JSON" | jq -c --arg PULL_REQUEST_ID "$PULL_REQUEST_ID" '. + {pull_request_id: $PULL_REQUEST_ID}')
fi

Expand All @@ -136,9 +135,9 @@ fi
# Add additional env vars as a nested object
FINAL_JSON=""
if [[ "$BUILD_ENV_VARS_JSON" ]]; then
FINAL_JSON=$(
echo "$JSON" | jq -c --argjson env "$BUILD_ENV_VARS_JSON" '. + {env: $env}'
)
FINAL_JSON=$(
echo "$JSON" | jq -c --argjson env "$BUILD_ENV_VARS_JSON" '. + {env: $env}'
)
else
FINAL_JSON=$JSON
fi
Expand All @@ -156,7 +155,7 @@ RESPONSE=$(
) || CODE=$?

if [ $CODE -ne 0 ]; then
MESSAGE=$(echo "$RESPONSE" | jq .message 2> /dev/null || true)
MESSAGE=$(echo "$RESPONSE" | jq .message 2>/dev/null || true)
if [[ -n "$MESSAGE" ]] && [[ "$MESSAGE" != 'null' ]]; then
echo -n "Buildkite API call failed: $MESSAGE"
fi
Expand All @@ -166,17 +165,15 @@ fi
echo ""
echo "Build created:"
URL=$(echo "$RESPONSE" | jq --raw-output ".web_url")
echo $URL
echo "$URL"

# Provide JSON and Web URL as outputs for downstream actions
# use environment variable $GITHUB_OUTPUT, or fall back to deprecated set-output command
# https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
if [[ -n "${GITHUB_OUTPUT:-}" ]]
then
echo "json=$RESPONSE" >> ${GITHUB_OUTPUT}
echo "url=$URL" >> ${GITHUB_OUTPUT}
if [[ -n "${GITHUB_OUTPUT:-}" ]]; then
echo "json=$RESPONSE" >>"${GITHUB_OUTPUT}"
echo "url=$URL" >>"${GITHUB_OUTPUT}"
else
echo "::set-output name=json::$RESPONSE"
echo "::set-output name=url::$URL"
fi

fi
8 changes: 5 additions & 3 deletions tests/entrypoint.bats
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bats
#!/usr/bin/env ./test/libs/bats/bin/bats

# load "${BATS_PLUGIN_PATH}/load.bash"

Expand All @@ -22,14 +22,16 @@ teardown() {

@test "Prints error and fails if \$BUILDKITE_API_ACCESS_TOKEN isn't set" {
run "${PWD}"/entrypoint.sh
["$output" = "You must set the BUILDKITE_API_ACCESS_TOKEN environment variable"]
assert_output --partial "You must set the BUILDKITE_API_ACCESS_TOKEN environment variable"
assert_failure
}

@test "Prints error and fails if \${{ inputs.pipeline }} isn't set" {
export BUILDKITE_API_ACCESS_TOKEN="123"

run "${PWD}"/entrypoint.sh
["$output" = "You must set the INPUT_PIPELINE environment variable"]
assert_output --partial "You must set the PIPELINE environment variable"
assert_failure
}

# @test "Creates a build with defaults" {
Expand Down

0 comments on commit 2820a88

Please sign in to comment.