Skip to content

Instantly share code, notes, and snippets.

@pavelpulec
Last active August 11, 2024 11:12
Show Gist options
  • Save pavelpulec/7af5aa04fe66094d9a77990fc6f46a07 to your computer and use it in GitHub Desktop.
Save pavelpulec/7af5aa04fe66094d9a77990fc6f46a07 to your computer and use it in GitHub Desktop.
Make all GitHub Status Checks success.
#!/usr/bin/env bash
# Makes all GitHub Status Checks success.
# Usage:
# github-pass-checks https://github.com/<owner>/<repo>/pull/<pull_nr>
[ "$#" -eq 1 ] || {
basename=$(basename "$0")
echo "Usage: $basename https://github.com/<owner>/<repo>/pull/<pr_num>";
exit 1;
}
set -eu
all_failed_contexts=
owner=$(echo "$1" | cut -d/ -f4)
repo=$(echo "$1" | cut -d/ -f5)
pull_number=$(echo "$1" | cut -d/ -f7)
# shellcheck disable=SC2154
merge_commit_sha=$(curl -sSf \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$owner/$repo/pulls/$pull_number/commits" \
| jq -r '.[-1].sha'
)
page=1
while :; do
# shellcheck disable=SC2154
contexts=$(curl -sSf \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/$owner/$repo/commits/$merge_commit_sha/statuses?per_page=100&page=$page"
)
failed_contexts=$(echo "$contexts" \
| jq -r '.[] | select ( .state == "failure" ) | .context'
)
all_failed_contexts="$all_failed_contexts
$failed_contexts"
status_count=$(echo "$contexts" \
| jq -r '.[].state' \
| wc -l
)
page=$((page+1))
[ "$status_count" -lt 100 ] && break
done
# shellcheck disable=SC2162
echo "$all_failed_contexts" | while read -r context; do
[ -z "$context" ] && continue
# shellcheck disable=SC2154
curl \
-X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/$owner/$repo/statuses/$merge_commit_sha" \
-d "{
\"state\": \"success\",
\"description\": \"Manually overwritten to success.\",
\"context\": \"$context\"
}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment