Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions post-cdash-status
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/bin/bash

set -eo pipefail
Comment on lines 2 to -3

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets keep this, you can invoke commands that might exit this script in its own subshell


readonly API_BASE="https://api.github.com/repos/${GITHUB_REPOSITORY}"

function require_cmd() {
Expand All @@ -28,12 +26,27 @@ require_env_var "GITHUB_TOKEN"

#==============================================================================

statuses=$(curl -q -s \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"${API_BASE}/commits/${COMMIT_SHA}/statuses" |\
jq -r '[.[].context] | @json')
readonly status_response=$(
curl -q -s \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall that curl has a native json parser somehow.

-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"${API_BASE}/commits/${COMMIT_SHA}/statuses")

# The API returns an empty array if no statuses were found
# This can happen if the COMMIT_SHA doesn't point to the head of the branch/PR
if [ "true" == $(jq -r '. == []' <<<$status_response) ]; then
echo "No statuses found for '${API_BASE}/commits/${COMMIT_SHA}'"
exit 1
fi

readonly statuses=$(jq -r '[.[].context] | @json' <<< $status_response)
if [ -z "$statuses" ]; then
echo "Failed to parse statuses for '${API_BASE}/commits/${COMMIT_SHA}'"
echo -e "Request returned:\n$status_response"
exit 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exit 2

fi

post_body="$(cat<<EOF
{
Expand All @@ -47,13 +60,22 @@ EOF

post_url="${API_BASE}/statuses/${COMMIT_SHA}"

if jq -re "all(. != \"${GITHUB_STATUS_NAME}\")" <<<"${statuses}"; then
if [ "true" == $(jq -re "all(. != \"${GITHUB_STATUS_NAME}\")" <<<"${statuses}") ]; then
echo "Need to post a status for context ${GITHUB_STATUS_NAME}"

curl -X POST -q -s \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d "${post_body}" "${post_url}"
readonly response=$(
curl -X POST -q -s \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d "${post_body}" "${post_url}")

if [ "false" == $(jq -r 'has("creator")') <<<"$response" ]; then
echo "Failed to create status for '${API_BASE}/commits/${COMMIT_SHA}'"
echo "$response"
exit 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exit 3

fi
else
echo "No status updates needed."
fi
Loading