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

Add check for required statuses. #51

Merged
merged 3 commits into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions webhook-app/github_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,39 @@ def get_pr_reviews(pr):
return reviews


def get_pr_required_statuses(pr):
"""Gets a list off all of the required statuses for a PR to be merged."""
statuses = pr.session.get(
'https://api.github.com/repos/{}/{}/branches/{}/protection/'
'required_status_checks/contexts'.format(
pr.repository[0], pr.repository[1], pr.base.ref)).json()

return statuses


def get_pr_statuses(pr):
"""Gets a list of currently reported statuses for the commit."""
statuses = pr.session.get(
'https://api.github.com/repos/{}/{}/commits/{}/'
'statuses'.format(
pr.repository[0], pr.repository[1], pr.head.sha)).json()

return [status['context'] for status in statuses]


def has_required_statuses(pr):
"""Returns True if the PR has all the protected statuses present."""

required = get_pr_required_statuses(pr)

if not len(required):
return True

listed = get_pr_statuses(pr)

return set(required).issubset(set(listed))


def is_pr_approved(pr):
"""True if the PR has been completely approved."""
review_requests = get_pr_requested_reviewers(pr)
Expand Down
5 changes: 5 additions & 0 deletions webhook-app/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ def merge_pull_request(repo, pull, commit_sha=None):
logging.info('Not merging {}, not labeled automerge'.format(pull))
return

# only merge if all required status are reported
if not github_helper.has_required_statuses(pull):
logging.info('Not merging {}, missing required status'.format(pull))
return

# only merge pulls that have all green statuses
if not github_helper.is_sha_green(repo, commit_sha):
logging.info('Not merging {}, not green.'.format(pull))
Expand Down