def merge_pull_request()

in webhook-app/webhooks.py [0:0]


def merge_pull_request(repo, pull, commit_sha=None):
    """Merges a pull request."""

    # only merge pulls that are labeled automerge
    labels = [label.name for label in pull.issue().labels()]
    if 'automerge' not in labels:
        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))
        return

    # Only merge pulls that have been approved!
    if not github_helper.is_pr_approved(pull):
        logging.info('Not merging {}, not approved.'.format(pull))
        return

    # By supplying the sha here, it ensures that the PR will only be
    # merged if that sha is the HEAD of the branch.
    logging.info('Merging {}.'.format(pull))
    github_helper.squash_merge_pr(pull, sha=commit_sha)

    # Delete the branch if it's in this repo. ALSO DON'T DELETE MASTER.
    if (pull.head.ref != 'master' and
            '/'.join(pull.head.repo) == repo.full_name):
        repo.ref('heads/{}'.format(pull.head.ref)).delete()