-
Notifications
You must be signed in to change notification settings - Fork 107
85 lines (77 loc) · 3.66 KB
/
review-ready-label.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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 }}