in webhook-app/webhooks.py [0:0]
def commit_status_complete_merge_on_travis(data):
"""When all statuses on a PR are green, this hook will automatically
merge it if it's labeled with 'automerge'.
Status data reference:
https://developer.github.com/v3/activity/events/types/#statusevent
"""
# TODO: Idea - if automerge has been triggered and the status fails,
# nag the committer to fix?
# If it's not successful don't even bother.
if data['state'] != 'success':
logging.info('Status not successful, returning.')
return
# NOTE: I'm not sure if there's a better way to do this. But, it seems
# the status change message doesn't tell you which PR the commit is
# from. Indeed, it's possible for a commit to actually be in multiple
# PRs. Anyways, the idea here is to get all open PRs with the
# tag 'automerge' and this commit in the PR.
commit_sha = data['commit']['sha']
gh = github_helper.get_client()
repository = github_helper.get_repository(gh, data)
# Sleep for about 15 seconds. Github's search index needs a few seconds
# before it'll find the results.
time.sleep(15)
query = '{} type:pr label:automerge is:open repo:{}'.format(
commit_sha, data['repository']['full_name'])
logging.info('Querying with: {}'.format(query))
results = gh.search_issues(query=query)
# Covert to pull requests so we can get the commits.
pulls = [result.issue.pull_request() for result in results]
logging.info('Found {} potential PRs: {}'.format(
len(pulls), pulls))
# See if this commit is in the PR.
# this check isn't actually strictly necessary as the search above will
# only return PRs that are 'green' which means we can safely merge all
# of them. But, whatever, I'll leave it here for now anyway.
pulls = [
pull for pull in pulls
if commit_sha in [commit.sha for commit in pull.commits()]]
logging.info('Commit {} is present in PRs: {}'.format(
commit_sha, pulls))
# Merge!
for pull in pulls:
merge_pull_request(repository, pull, commit_sha=commit_sha)