Skip to content

Commit 7d2bfef

Browse files
authored
Merge pull request Homebrew#130382 from Homebrew/approval-check
workflows: improve approval check
2 parents fb9d5ed + 5fd508d commit 7d2bfef

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

.github/workflows/automerge.yml

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,30 @@ jobs:
9696
env:
9797
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9898
PR: ${{ steps.pr.outputs.number }}
99+
QUERY: |-
100+
query ($owner: String!, $name: String!, $pr: Int!) {
101+
repository(owner: $owner, name: $name) {
102+
pullRequest(number: $pr) {
103+
reviewDecision
104+
commits(last: 1) {
105+
nodes {
106+
commit {
107+
checkSuites(last: 100) {
108+
nodes {
109+
conclusion
110+
workflowRun {
111+
workflow {
112+
name
113+
}
114+
}
115+
}
116+
}
117+
}
118+
}
119+
}
120+
}
121+
}
122+
}
99123
run: |
100124
attempt=0
101125
max_attempts=5
@@ -105,41 +129,33 @@ jobs:
105129
do
106130
attempt=$(( attempt + 1 ))
107131
108-
approved=false
109-
complete=false
110-
mergeable=false
111-
112-
review_data="$(
113-
gh api \
114-
--header 'Accept: application/vnd.github+json' \
115-
--header 'X-GitHub-Api-Version: 2022-11-28' \
116-
"repos/$GH_REPO/pulls/$PR/reviews"
132+
query_response="$(
133+
gh api graphql \
134+
--field owner='{owner}' \
135+
--field name='{repo}' \
136+
--field pr="$PR" \
137+
--raw-field query="$QUERY" \
138+
--jq '.data.repository.pullRequest'
117139
)"
118-
if jq --exit-status 'any(.[].state; .== "APPROVED")' <<< "$review_data" &&
119-
jq --exit-status 'all(.[].state; .!= "CHANGES_REQUESTED" )' <<< "$review_data"
120-
then
121-
approved=true
122-
fi
123-
124-
if gh pr checks "$PR"
125-
then
126-
complete=true
127-
fi
128140
129-
pr_data="$(
141+
approved="$(
142+
jq --raw-output '.reviewDecision == "APPROVED"' <<< "$query_response"
143+
)"
144+
complete="$(
145+
jq --raw-output \
146+
'.commits.nodes[].commit.checkSuites.nodes |
147+
map(select(.workflowRun.workflow.name == "CI")) |
148+
last | .conclusion == "SUCCESS"' <<< "$query_response"
149+
)"
150+
# See https://github.com/octokit/octokit.net/issues/1763 for possible `mergeable_state` values.
151+
mergeable="$(
130152
gh api \
131153
--header 'Accept: application/vnd.github+json' \
132154
--header 'X-GitHub-Api-Version: 2022-11-28' \
133-
"repos/$GH_REPO/pulls/$PR"
155+
"repos/{owner}/{repo}/pulls/$PR" \
156+
--jq '(.mergeable_state == "clean") and (.draft | not)'
134157
)"
135158
136-
# See https://github.com/octokit/octokit.net/issues/1763 for possible values.
137-
if jq --exit-status '.mergeable_state == "clean"' <<< "$pr_data" &&
138-
jq --exit-status '.draft | not' <<< "$pr_data"
139-
then
140-
mergeable=true
141-
fi
142-
143159
if [[ "$approved" = "true" ]] &&
144160
[[ "$complete" = "true" ]] &&
145161
[[ "$mergeable" = "true" ]] ||

0 commit comments

Comments
 (0)