-
Notifications
You must be signed in to change notification settings - Fork 2
Improve error handling in post-cdash-status #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -eo pipefail | ||
|
|
||
| readonly API_BASE="https://api.github.com/repos/${GITHUB_REPOSITORY}" | ||
|
|
||
| function require_cmd() { | ||
|
|
@@ -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 \ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exit 2 |
||
| fi | ||
|
|
||
| post_body="$(cat<<EOF | ||
| { | ||
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exit 3 |
||
| fi | ||
| else | ||
| echo "No status updates needed." | ||
| fi | ||
There was a problem hiding this comment.
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