Skip to content
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

[CI] Use github's graphQL to query review state directly #14661

Merged
merged 5 commits into from
Aug 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions ci/ci/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,27 +486,36 @@ async def _update_last_known_github_status(self, gh):
self.target_branch.state_changed = True

async def _update_github_review_state(self, gh):
latest_state_by_login = {}
async for review in gh.getiter(
f'/repos/{self.target_branch.branch.repo.short_str()}/pulls/{self.number}/reviews'
):
login = review['user']['login']
state = review['state']
# reviews is chronological, so later ones are newer statuses
if state != 'COMMENTED':
latest_state_by_login[login] = state

review_state = 'pending'
for login, state in latest_state_by_login.items():
if state == 'CHANGES_REQUESTED':
review_state = 'changes_requested'
break
if state == 'APPROVED':
review_state = 'approved'
else:
assert state in ('DISMISSED', 'COMMENTED', 'PENDING'), state
review_state_query = f"""
query {{
repository(owner: "{self.target_branch.branch.repo.owner}", name: "{self.target_branch.branch.repo.name}") {{
pullRequest(number: {self.number}) {{
reviewDecision
}}
}}
}}
"""

response = await gh.post('/graphql', data={'query': review_state_query})
review_decision = response['data']['repository']['pullRequest']['reviewDecision']

if review_decision == 'APPROVED':
review_state = 'approved'
elif review_decision == 'CHANGES_REQUESTED':
review_state = 'changes_requested'
elif review_decision == 'REVIEW_REQUIRED':
review_state = 'pending'
elif review_decision is None:
# This probably means the repo has no "required reviews" configuration. But CI shouldn't merge without
# at least one approval, so we'll treat this as "pending":
review_state = 'pending'
else:
# Should be impossible, per https://docs.github.com/en/graphql/reference/enums#pullrequestreviewdecision
log.error(f'{self.short_str()}: unexpected review decision from github: {review_decision}')
review_state = 'pending'

if review_state != self.review_state:
log.info(f'{self.short_str()}: review state changing from {self.review_state} => {review_state}')
self.set_review_state(review_state)
self.target_branch.state_changed = True

Expand Down