Search code examples
githubmergepermissionspull-requestcircleci

How to block merging of pull requests by committers in GitHub


I am looking for a way via GitHub (or CircleCI) settings to prevent the person who opens or commits to a pull request from being able to merge or approve that pull request.

So far I have the protection of a branch that requires approvals but post-approval I as PR creator and committer I still able to merge.


Solution

  • You need to be able to

    prevent the person that is involved in PR (create PR or make a commit) to be able to merge PR (or even approve it)

    A contributor who has created a PR cannot approve or request changes by default in GitHub, so that is already taken care of.

    Since a Pull Request is a GitHub feature, a PR merge can currently only be blocked by 2 ways

    • Using GitHub's settings
    • Using pre-receive hooks (only for GitHub Enterprise)

    Using GitHub's settings, you can only block merging by requiring either pull request reviews, status checks to pass, signed commits or linear history as shown under the branch protection settings.

    enter image description here

    or by allowing merge commits, squash merging or rebase merging as shown in the Merge button section under repo settings

    enter image description here

    If you are on GitHub Enterprise, you can use a pre-receive hook (documentation) like below and ensure that self merging PRs are blocked (This eg is here)

    if [[ "$GITHUB_VIA" = *"merge"* ]] && [[ "$GITHUB_PULL_REQUEST_AUTHOR_LOGIN" = "$GITHUB_USER_LOGIN" ]]; then
        echo "Blocking merging of your own pull request."
        exit 1
    fi
    
    exit 0
    

    Apart from the above, there is no other way currently to block self merging PRs on GitHub. And using CircleCI or any other CI workflow can only block merging for everybody(if you opt for the requirement of status checks on GitHub) or nobody, as it can't control the PR merge button.