Skip to content
This repository was archived by the owner on Nov 18, 2021. It is now read-only.
This repository was archived by the owner on Nov 18, 2021. It is now read-only.

no safe gating on PR merge commits #1002

Open

Description

@aspiers

GitHub supports required status checks, which is a form of gating which can prevent pull requests from being merged if the head of the PR branch does not pass some (external) automated CI. However, if it does pass, there is no subsequent gate trigger for running external CI on the tree resulting from merging the PR back into its target branch. This means that if the target branch has advanced beyond the original merge base of the PR branch, the target branch can potentially be broken by merging the PR, due to some unexpected (non-textual) conflict between the change in the PR and the changes made to the target branch since the PR was started (or last rebased).

This is a fundamental flaw in GitHub's approach to gating (status checks). In contrast, Zuul is a CI system which gets this right. There may be others too.

It is worth noting that Travis CI tests the merge between the origin and the PR branch; however because the merge commit is generated by Travis CI rather than by GitHub, there is still a race window, e.g.

  1. A PR is opened or updated
  2. Travis CI creates a merge commit of that PR's head with its target branch
  3. Travis CI starts a test build against that merge commit
  4. While the Travis build is running (or even before it but after step 2), someone merges something else into the target branch which is incompatible with the PR being tested, but not generating any textual conflicts
  5. The Travis build succeeds and reports back to GitHub, thereby passing the required status check
  6. Someone clicks on the Merge button
  7. GitHub merges the PR
  8. The target branch is now broken because the PR was incompatible with the changes introduced in step 4

Presumably the only way to avoid this race condition would be for GitHub to generate the merge commits itself via a pipeline similar to Zuul's, and then hand them off to whichever external CI systems are configured to receive CI build triggers.

Activity

  1. aspiers commented on Apr 30, 2018

    @aspiers
    CollaboratorAuthor

    There is one workaround, which is to enable the Require branches to be up to date before merging option:

    screenshot_20180430_200115

    However this is sub-optimal, because then every time a PR is merged, all the other PRs become outdated and need to be rebased, requiring all the status checks to be re-run.

  2. aspiers commented on Apr 30, 2018

    @aspiers
    CollaboratorAuthor

    I just remembered that I had a private email conversation with GitHub Engineering way back in 2015 about this. I'll paste the thread in, one comment per email.

  3. aspiers commented on Apr 30, 2018

    @aspiers
    CollaboratorAuthor

    Date: Wed, 16 Sep 2015 15:55:30 +0100
    From: Adam Spiers
    To: [email protected]
    Subject: Required status checks prevent merging of outdated pull requests

    Hi there,

    Thanks a lot for your recently announced new feature regarding required status checks:

    https://github.com/blog/2051-protected-branches-and-required-status-checks

    This is a really awesome feature, and my team immediately enabled them for a few of our repos. However I now see that this prevents merging of pull requests which are based on an outdated version of the target branch.

    For example, crowbar/crowbar-core#14 is (at the time of writing this) based on 2c9703094ec, whereas master has advanced to cafc8a8df91 since it was submitted. Even though all three status checks succeeded, the web UI displayed:

    This branch is out-of-date with the base branch
    Merge the latest changes from master into this branch.
    

    and there was an "Update branch" button, and no usable "Merge pull request" button. Once we reverted the "required status checks" option, this UI vanished and was replaced with the traditional one which allows merging.

    I can understand why this behaviour was implemented, but it's problematic for a few reasons:

    1. There is no explanation in the UI why direct merging is no longer possible: not in the PR, nor is there any warning about this side-effect in the repo settings UI where the required status checks feature gets enabled.

    2. There is no explanation in the UI of exactly what clicking the "Update branch" button would do.

    3. None of this behaviour is documented anywhere, at least as far as I can find. It's not even mentioned in the blog.

    4. There are perfectly valid reasons for repo owners to allow merging of outdated pull requests, even when it is required for all pull requests to pass CI checks. Enforcing pull requests to be up to date before merging turns CI from a parallelized process where multiple PRs can be checked at the same time, into a mostly serial one, where merging any single PR advances the branch head and forces all other outstanding PRs to be updated before they can be retested, let alone merged. This requires a) significantly more human interaction, and b) significantly more CI build power. In many cases the extra effort is simply not justified.

      The repo owner should be free to choose their own gating policy. With github's current implementation of required status checks, it's an all or nothing approach. Ideally, the setting determining whether status checks are required should be decoupled from the requirement for a PR to be up to date before it can be merged. They are orthogonal considerations, so all four of the following policies should be supported:

      1. Any PR can be merged (even outdated PRs failing status checks)
      2. Only up-to-date PRs can be merged (even if they fail status checks)
      3. Only PRs passing the status checks can be merged (even if they're out of date)
      4. Only up-to-date PRs passing the status checks can be merged

      This is because the repo owner's level of trust in their CI is independent of their level of trust that an automatic merge won't break anything.

    On a related topic, you may want to consider taking a leaf out of OpenStack's book, by adding support for a two-pass gating mechanism where status checks are first run on the branch and then later on merge commits which merge the PR into the target branch, before the target branch is actually updated with the merge.

    OpenStack's gate works roughly like this:

    1. Developer submits a review to Gerrit (reviews are Gerrit's equivalent of github pull requests)

    2. CI gating tests automatically run on the review branch. If they pass, Gerrit displays a "Verified: +1" vote from the CI system.

    3. A core developer on the team gives the review a special +1 vote in the "Approved" category.

    4. The CI system automatically performs a test merge of the branch into the trunk and re-runs the gating tests on that merge commit.

    5. If the gating tests succeed on the merge commit, Gerrit displays a "Verified: +2" vote. At this point the trunk is automatically fast-forwarded to the merge commit.

    This is of course more complicated than GitHub's single-pass approach, but it has the distinct advantage of catching any potential breakage which could be caused by an automated merge, whilst maintaining a fully parallelizable feedback loop for newly submitted changes.

    For more information, please see:

    http://docs.openstack.org/infra/manual/developers.html#code-review

    Hope this feedback helps. Thanks a lot for listening!

    Adam

  4. aspiers commented on Apr 30, 2018

    @aspiers
    CollaboratorAuthor

    Date: Wed, 16 Sep 2015 09:00:18 -0700
    From: "Adam Roben (GitHub Staff)" [email protected]
    To: Adam Spiers
    Subject: Re: Required status checks prevent merging of outdated pull requests

    Hi Adam-

    Thanks for getting in touch and including so much detail.

    Your point about it being unclear that PRs must be up to date before merging when using required status checks is a good one. We'll think about ways to make that clearer.

    We have talked about making the up-to-date requirement optional, though I don't think we've considered offering that requirement even when no required status checks are used. Thanks for mentioning that possibility.

    We've also talked about ways to have CI systems test the merge commit directly so that you don't have to update your branch before merging. That should make the whole process less onerous, as you pointed out (though it would still require CI to retest the merge any time the base branch is updated).

    Thanks for the pointer to the OpenStack docs and all your other thoughts!

    -Adam

  5. aspiers commented on Apr 30, 2018

    @aspiers
    CollaboratorAuthor

    Date: Wed, 16 Sep 2015 18:26:20 +0100
    From: Adam Spiers
    To: "Adam Roben (GitHub Staff)" [email protected]
    Subject: Re: Required status checks prevent merging of outdated pull requests

    Hi Adam, thanks so much for the swift and useful reply!

    Just briefly:

    Adam Roben (GitHub Staff) [email protected] wrote:

    Hi Adam-

    Thanks for getting in touch and including so much detail.

    Your point about it being unclear that PRs must be up to date before merging when using required status checks is a good one. We'll think about ways to make that clearer.

    Awesome, thanks :)

    We have talked about making the up-to-date requirement optional, though I don't think we've considered offering that requirement even when no required status checks are used. Thanks for mentioning that possibility.

    Maybe I'm misunderstanding something here - by "when no required status checks are used", do you mean "when the required status checks option is disabled"? If so, that's the way it already is and has been for years: when the option is disabled, it's possible to merge any PR even if it's out of date. But I'm sure you already know that so I guess you meant something else; I'm not sure what though.

    We've also talked about ways to have CI systems test the merge commit directly so that you don't have to update your branch before merging. That should make the whole process less onerous, as you pointed out (though it would still require CI to retest the merge any time the base branch is updated).

    Right. At first I thought that it would be possible for GitHub to entirely delegate that responsibility to the external CI system, since it's free to test in any way it wants, including performing a merge from the base prior to testing.

    For example, if a pull request P has base commit B and head commit H, an external CI system could:

    1. create a temporary test branch pointing to B
    2. merge in H, resulting in a new merge commit M
    3. run CI on M
    4. report the results to github as normal via a POST to /repos/:owner/:repo/statuses/H (not .../M, since commit M wouldn't exist on GitHub at this point)

    But then I realised that reporting the results back like this (i.e. as results for commit H) would result in ambiguous information: it would be impossible for GitHub to know whether the result was referring to commit H or commit M. And this ambiguity would prevent GitHub from shepherding a more advanced workflow which treated results of CI run on PR branch heads differently to results of CI run on merged PR branches. I haven't thought about this for very long yet, but if I was in the shoes of a GitHub engineer interested in potentially supporting two-pass CI, I would probably consider extending the status API so that it could unambiguously track statuses for both normal commits and branch commits. The API already allows server-side merging, and after creating a merge commit, its status can be set in the standard way, via a POST to /repos/:owner/:repo/statuses/M. So it seems that maybe the API would only need to be extended in roughly two directions in order to support this:

    1. Extend API calls which return pull requests so that in addition to returning data about head, they also return data about merge if there is a (speculative) merge commit M merging H into the target branch. (I am not yet sure whether it would make sense to allow multiple merge commits per head commit in this model.)

    2. Extend API calls which return commit statuses so that they also return the statuses of any corresponding merge commit(s), if they exist.

    Similarly, the web UI could then expose when a commit in a PR had also had a corresponding merge commit created and tested, and that would open up the possibility of adding options to enforce more complicated two-pass workflows similar to the OpenStack one I already described - especially the option to enforce that a PR can only be merged if a speculative merge commit already passed through the CI gate.

    But this is based on < 30 minutes of thought, so I might be a bit off base here ;-)

    Thanks for the pointer to the OpenStack docs and all your other thoughts!

    You're very welcome! Thanks again for being so willing to listen to feedback!

    Cheers,
    Adam

  6. aspiers commented on Apr 30, 2018

    @aspiers
    CollaboratorAuthor

    Date: Thu, 17 Sep 2015 09:20:16 -0700
    From: "Adam Roben (GitHub Staff)" [email protected]
    To: Adam Spiers
    Subject: Re: Required status checks prevent merging of outdated pull requests

    We have talked about making the up-to-date requirement optional, though I don't think we've considered offering that requirement even when no required status checks are used. Thanks for mentioning that possibility.

    Maybe I'm misunderstanding something here - by "when no required status checks are used", do you mean "when the required status checks option is disabled"?

    I mean if the option is disabled, or if the option is enabled but you haven't enabled any particular checks.

    If so, that's the way it already is and has been for years: when the option is disabled, it's possible to merge any PR even if it's out of date. But I'm sure you already know that so I guess you meant something else; I'm not sure what though.

    Sorry about that. You mentioned four scenarios:

    1. Any PR can be merged (even outdated PRs failing status checks)
    2. Only up-to-date PRs can be merged (even if they fail status checks)
    3. Only PRs passing the status checks can be merged (even if they're out of date)
    4. Only up-to-date PRs passing the status checks can be merged

    What I meant was that we hadn't considered scenario 2 before. Scenarios 1 and 4 are possible today. Scenario 3 is something we've been considering. But I don't think scenario 2 has come up in our conversations.

    But then I realised that reporting the results back like this (i.e. as results for commit H) would result in ambiguous information: it would be impossible for GitHub to know whether the result was referring to commit H or commit M. And this ambiguity would prevent GitHub from shepherding a more advanced workflow which treated results of CI run on PR branch heads differently to results of CI run on merged PR branches.

    That's right. The strategy you described is actually what Travis CI does today. (They actually do two builds; one for commit H and one for commit M, and report them both back to commit H.) The ambiguity about what was actually tested is definitely a problem.

    I would probably consider extending the status API so that it could unambiguously track statuses for both normal commits and branch commits.

    That is an interesting approach. We've been thinking about perhaps providing an API to allow posting a status to a Git tree instead of a Git commit. This makes sense because usually CI systems only care about the code in the working directory, rather than the commit ancestry itself, so they really are testing the tree and not the commit. This would allow interesting things like letting statuses persist across rebases, and could help clear up the ambiguity between whether commit H or commit M were tested. But this is all still early in the design phase; we don't have a specific solution in mind yet.

    Thanks again for all your thoughts. They'll certainly inform our discussion in the future.

    -Adam

  7. aspiers commented on Apr 30, 2018

    @aspiers
    CollaboratorAuthor

    Date: Thu, 17 Sep 2015 17:52:30 +0100
    From: Adam Spiers
    To: "Adam Roben (GitHub Staff)" [email protected]
    Subject: Re: Required status checks prevent merging of outdated pull requests

    Adam Roben (GitHub Staff) [email protected] wrote:

    We have talked about making the up-to-date requirement optional, though I don't think we've considered offering that requirement even when no required status checks are used. Thanks for mentioning that possibility.

    Maybe I'm misunderstanding something here - by "when no required status checks are used", do you mean "when the required status checks option is disabled"?

    I mean if the option is disabled, or if the option is enabled but you haven't enabled any particular checks.

    If so, that's the way it already is and has been for years: when the option is disabled, it's possible to merge any PR even if it's out of date. But I'm sure you already know that so I guess you meant something else; I'm not sure what though.

    Sorry about that. You mentioned four scenarios:

    1. Any PR can be merged (even outdated PRs failing status checks)
    2. Only up-to-date PRs can be merged (even if they fail status checks)
    3. Only PRs passing the status checks can be merged (even if they're out of date)
    4. Only up-to-date PRs passing the status checks can be merged

    What I meant was that we hadn't considered scenario 2 before. Scenarios 1 and 4 are possible today. Scenario 3 is something we've been considering. But I don't think scenario 2 has come up in our conversations.

    Right. IMHO scenario 3 is really important in order to avoid turning a parallelized process into a sequential one, as I previously mentioned. In our environment, some of our status checks can take 2 hours or longer, and we have quite a lot of pull requests open at any one time. So if we're in scenario 4, any time a PR gets merged, it effectively invalidates the lengthy and costly status checks of all the other PRs which are still open.

    Scenario 2 is probably less important to support, but I can still imagine a situations where the status checks are often expected to fail but it is still required to be able to merge. For example the status checks might be done by a buggy or unreliable test suite whilst there is an ongoing project to fix / stabilise this test suite. Or a coding style checker might have been integrated for advisory-only style warnings during a period where the project aims to gradually clean up coding style. Or a test coverage metrics checker might have been integrated to warn of decreasing coverage which is deemed acceptable during a transition period where large portions of the code are being rewritten.

    But then I realised that reporting the results back like this (i.e. as results for commit H) would result in ambiguous information: it would be impossible for GitHub to know whether the result was referring to commit H or commit M. And this ambiguity would prevent GitHub from shepherding a more advanced workflow which treated results of CI run on PR branch heads differently to results of CI run on merged PR branches.

    That's right. The strategy you described is actually what Travis CI does today. (They actually do two builds; one for commit H and one for commit M, and report them both back to commit H.)

    Ah yes of course! I knew that but somehow had totally forgotten about it in relation to this discussion :-)

    The ambiguity about what was actually tested is definitely a problem.

    I would probably consider extending the status API so that it could unambiguously track statuses for both normal commits and branch commits.

    That is an interesting approach. We've been thinking about perhaps providing an API to allow posting a status to a Git tree instead of a Git commit. This makes sense because usually CI systems only care about the code in the working directory, rather than the commit ancestry itself, so they really are testing the tree and not the commit. This would allow interesting things like letting statuses persist across rebases, and could help clear up the ambiguity between whether commit H or commit M were tested. But this is all still early in the design phase; we don't have a specific solution in mind yet.

    Oh wow, I love it! That's really cunning :-) Yes indeed - that would beautifully avoid the status checkers pointless rerunning their tests every time someone pushed a new head resulting from history rewriting operations which didn't affect the head's tree, such as rewording commit messages, squashing or splitting commits, reordering commits etc. A huge gain in CI efficiency!

    The only potential downside I can envision here is that it would prevent status checks from doing checks on history metadata such as commit messages. And I think that would be quite a loss, since status checks which enforce commit messages to adhere to repository-wide standards would be a very nice thing to have. Even though I haven't yet heard of anyone doing this via GitHub status checks, I know for sure that some projects do it via custom git hooks.

    But the per-tree status idea has so many advantages that IMHO it's too good to lose :-) Maybe you can support a hybrid approach, i.e. retaining the existing ability to provide status checks on commits, but also adding the ability to provide them on trees? That would have the additional advantage of allowing services such as Travis CI to migrate from per-commit to per-tree in their own time.

    Thanks again for all your thoughts. They'll certainly inform our discussion in the future.

    You're very welcome, and thank you so much for being willing to take the time to listen and respond! After having feedback ignored by so many other companies providing online services, it was a very pleasant surprise to this somewhat jaded and cynical hacker ;-)

    Cheers,
    Adam

  8. aspiers commented on Apr 30, 2018

    @aspiers
    CollaboratorAuthor

    Date: Thu, 17 Sep 2015 12:40:19 -0700
    From: "Adam Roben (GitHub Staff)" [email protected]
    To: Adam Spiers
    Subject: Re: Required status checks prevent merging of outdated pull requests

    Right. IMHO scenario 3 is really important in order to avoid turning a parallelized process into a sequential one, as I previously mentioned. In our environment, some of our status checks can take 2 hours or longer, and we have quite a lot of pull requests open at any one time. So if we're in scenario 4, any time a PR gets merged, it effectively invalidates the lengthy and costly status checks of all the other PRs which are still open.

    That makes sense.

    A huge gain in CI efficiency!

    Right now most CI services trigger builds in response to our "push" web hooks. To be more efficient they'd either need to keep track of what trees they've already built, or GitHub would need some new kind of web hook that is only sent when a never-seen-before tree is pushed to a PR. Something to think about for sure.

    Maybe you can support a hybrid approach, i.e. retaining the existing ability to provide status checks on commits, but also adding the ability to provide them on trees? That would have the additional advantage of allowing services such as Travis CI to migrate from per-commit to per-tree in their own time.

    That might work. We should consider it.

    Thanks again!

    -Adam

  9. jd commented on Jul 4, 2018

    @jd
  10. aspiers commented on Oct 21, 2018

    @aspiers
    CollaboratorAuthor

    @jd Thanks for the info! I see that they have recently updated things too.

  11. added
    parity
    on Feb 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    paritypull-requests

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions