Skip to content

Toggle review ready label #35665

Toggle review ready label

Toggle review ready label #35665

# Marks a PR with the review_ready_label label if
# there are:
# - There are no failed CI jobs
# - There are no Pending CI jobs excluding Tide
# - The PR isn't in draft state
# Removes the label if any of those checks fail.
name: Toggle review ready label
on:
status:
pull_request_target:
types: [ready_for_review, converted_to_draft]
jobs:
toggle-label:
runs-on: ubuntu-latest
permissions: # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#permissions
contents: write # Required for Setting label
pull-requests: write # Required for Setting label
steps:
- name: Toggle review ready label
shell: bash
run: |
# Example jq_status_output:
# {"state":"success","context":"docs/readthedocs.org:ci-framework"}
# {"state":"success","context":"ci/prow/images"}
# {"state":"success","context":"ci/prow/ansible-test"}
# {"state":"pending","context":"tide"}
# {"state":"failure","context":"rdoproject.org/github-check"}
# Example pr_search_result:
# [{"isDraft":false,"number":1159}]
set -x
# Get CI job status output and parse it for failed or pending jobs (excluding Tide)
status_output=$(gh api -H "${accept_header}" -H "${api_header}" repos/${GITHUB_REPOSITORY}/status/${GH_EVENT_SHA})
jq_status_output=$(echo "${status_output}" | jq -c '.statuses[] | {state: .state, context: .context}')
ready=true
for row in ${jq_status_output}; do
_jq() {
echo "${row}" | jq -r "${1}"
}
if [ $(_jq '.state') == 'failure' ]; then
echo "$(_jq '.context') failed, exiting"
ready=false
break
fi
if [ $(_jq '.state') == 'pending' ] && [ $(_jq '.context') != 'tide' ] ; then
echo "$(_jq '.context') still pending, exiting"
ready=false
break
fi
done
# Get the PR number and draft state from the commit SHA, to pass the commit SHA must only be in one PR
pr_search_result=$(gh pr list --search "${GH_EVENT_SHA}" -R "${GITHUB_REPOSITORY}" --json number,isDraft | jq -c)
pr_search_result_length=$(echo "${pr_search_result}" | jq length)
if [ "$pr_search_result_length" -ne 1 ]; then
echo "Commit not found or in multiple PRs"
exit 0
fi
# Define draft_status and set $ready false if PR is in draft state
draft_status=$(echo "${pr_search_result}" | jq '.[].isDraft')
if [ "$draft_status" = "true" ]; then
ready=false
fi
# Define pr_number and set or remove review_ready_label on PR
pr_number=$(echo "${pr_search_result}" | jq '.[].number')
if [ "$ready" = "true" ]; then
echo "Setting label: ${review_ready_label} on PR: ${pr_number}"
gh pr edit "${pr_number}" --add-label "${review_ready_label}" -R "${GITHUB_REPOSITORY}"
else
echo "Removing label: ${review_ready_label} from PR: ${pr_number}"
gh pr edit "${pr_number}" --remove-label "${review_ready_label}" -R "${GITHUB_REPOSITORY}"
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
accept_header: 'Accept: application/vnd.github+json'
api_header: 'X-GitHub-Api-Version: 2022-11-28'
GH_PAGER: ''
review_ready_label: "Ready For Review"
GH_EVENT_SHA: ${{ github.event_name == 'status' && github.event.commit.SHA || github.event.pull_request.head.sha }}