Skip to content

gh pr create -fr doesn't play nice with codeowners #9604

Open

Agent suggestions

Public preview

Description

@mcmanustfj

Describe the bug

gh --version 2.46.0

When I gh pr create -f -r $reviewer in a repository with codeowners, github will automatically request a codeowner review, even if $reviewer is a codeowner. Additionally, sometimes github removes request from one or more of the reviewers I specified!

Steps to reproduce the behavior

This might take multiple attempts. In a repository with multiple codeowners,

  1. gh pr create -fr $reviewer (where $reviewer is a codeowner)
  2. Open up the PR and see
    | "$user requested a review from $team as a code owner x minutes ago"
    | "$user requested review from $reviewer and $other codeowner (assigned from $team) x minutes ago"
    or sometimes
    | "$user requested review from $reviewer and $other codeowner (assigned from $team) and removed request fro $reviewer x minutes ago"

Expected vs actual behavior

I would expect that if I request a review from a code owner to not automatically request reviews from new codeowners. I would especially expect that the reviewers I request don't disappear!

Logs

This invocation had two reviewers in it, and one was removed:
"$user requested review from $reviewer1 and $reviewer2 and removed request for $reviewer2"

GH_DEBUG=api gh pr create results

* Request at 2024-09-11 16:45:54.515347457 -0400 EDT m=+2.427704602
* Request to https://api.github.com/graphql
> POST /graphql HTTP/1.1
> Host: api.github.com
> Accept: application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview
> Authorization: token ████████████████████
> Content-Length: 157
> Content-Type: application/json; charset=utf-8
> Graphql-Features: merge_queue
> Time-Zone: America/New_York
> User-Agent: GitHub CLI 2.46.0

GraphQL query:
query RepositoryResolveMetadataIDs {
u000: user(login:"$reviewer1"){id,login}
u001: user(login:"$reviewer2"){id,login}
}
GraphQL variables: null

< HTTP/2.0 200 OK
< Access-Control-Allow-Origin: *
< Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
< Content-Security-Policy: default-src 'none'
< Content-Type: application/json; charset=utf-8
< Date: Wed, 11 Sep 2024 20:45:54 GMT
< Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
< Server: github.com
< Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
< Vary: Accept-Encoding, Accept, X-Requested-With
< X-Accepted-Oauth-Scopes: repo
< X-Content-Type-Options: nosniff
< X-Frame-Options: deny
< X-Github-Media-Type: github.v4; param=merge-info-preview.nebula-preview; format=json
< X-Github-Request-Id: E298:236D42:970B306:12389EA2:66E20182
< X-Oauth-Client-Id: $not-sure-if-sensitive
< X-Oauth-Scopes: gist, read:org, repo
< X-Ratelimit-Limit: 5000
< X-Ratelimit-Remaining: 4990
< X-Ratelimit-Reset: 1726090479
< X-Ratelimit-Resource: graphql
< X-Ratelimit-Used: 10
< X-Xss-Protection: 0

{
  "data": {
    "u000": {
      "id": "$id1",
      "login": "$reviewer1"
    },
    "u001": {
      "id": "$id2",
      "login": "$reviewer2"
    }
  }
}

* Request took 135.91325ms
* Request at 2024-09-11 16:45:54.65150025 -0400 EDT m=+2.563857388
* Request to https://api.github.com/graphql
> POST /graphql HTTP/1.1
> Host: api.github.com
> Accept: application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview
> Authorization: token ████████████████████
⣾> Content-Length: 390
> Content-Type: application/json; charset=utf-8
> Graphql-Features: merge_queue
> Time-Zone: America/New_York
> User-Agent: GitHub CLI 2.46.0

GraphQL query:
mutation PullRequestCreate($input: CreatePullRequestInput!) {
      createPullRequest(input: $input) {
        pullRequest {
          id
          url
        }
      }
  }
GraphQL variables: {"input":{"baseRefName":"main","body":"redacted\n","draft":false,"headRefName":"temp-pr","maintainerCanModify":true,"repositoryId":"R_kgDOHe0SJw","title":"test pr please ignore"}}

⣷< HTTP/2.0 200 OK
< Access-Control-Allow-Origin: *
< Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
< Content-Security-Policy: default-src 'none'
< Content-Type: application/json; charset=utf-8
< Date: Wed, 11 Sep 2024 20:45:55 GMT
< Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
< Server: github.com
< Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
< Vary: Accept-Encoding, Accept, X-Requested-With
< X-Accepted-Oauth-Scopes: repo
< X-Content-Type-Options: nosniff
< X-Frame-Options: deny
< X-Github-Media-Type: github.v4; param=merge-info-preview.nebula-preview; format=json
< X-Github-Request-Id: E298:236D42:970B36C:12389F55:66E20182
< X-Oauth-Client-Id: $not-sure-if-sensitive
< X-Oauth-Scopes: gist, read:org, repo
< X-Ratelimit-Limit: 5000
< X-Ratelimit-Remaining: 4989
< X-Ratelimit-Reset: 1726090479
< X-Ratelimit-Resource: graphql
< X-Ratelimit-Used: 11
< X-Xss-Protection: 0

{
  "data": {
    "createPullRequest": {
      "pullRequest": {
        "id": "PR_kwDOHe0SJ857NymJ",
        "url": "https://github.com/$owner/$repo/pull/333"
      }
    }
  }
}

* Request took 886.966774ms
* Request at 2024-09-11 16:45:55.538616713 -0400 EDT m=+3.450973862
* Request to https://api.github.com/graphql
> POST /graphql HTTP/1.1
> Host: api.github.com
> Accept: application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview
> Authorization: token ████████████████████
> Content-Length: 280
> Content-Type: application/json; charset=utf-8
> Graphql-Features: merge_queue
> Time-Zone: America/New_York
> User-Agent: GitHub CLI 2.46.0

GraphQL query:
mutation PullRequestCreateRequestReviews($input: RequestReviewsInput!) {
      requestReviews(input: $input) { clientMutationId }
    }
GraphQL variables: {"input":{"pullRequestId":"PR_kwDOHe0SJ857NymJ","union":true,"userIds":["$id1","$id2"]}}

⡿< HTTP/2.0 200 OK
< Access-Control-Allow-Origin: *
< Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
< Content-Security-Policy: default-src 'none'
< Content-Type: application/json; charset=utf-8
< Date: Wed, 11 Sep 2024 20:45:56 GMT
< Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
< Server: github.com
< Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
< Vary: Accept-Encoding, Accept, X-Requested-With
< X-Accepted-Oauth-Scopes: repo
< X-Content-Type-Options: nosniff
< X-Frame-Options: deny
< X-Github-Media-Type: github.v4; param=merge-info-preview.nebula-preview; format=json
< X-Github-Request-Id: E298:236D42:970B603:1238A45F:66E20183
< X-Oauth-Client-Id: $not-sure-if-sensitive
< X-Oauth-Scopes: gist, read:org, repo
< X-Ratelimit-Limit: 5000
< X-Ratelimit-Remaining: 4988
< X-Ratelimit-Reset: 1726090479
< X-Ratelimit-Resource: graphql
< X-Ratelimit-Used: 12
< X-Xss-Protection: 0

{
  "data": {
    "requestReviews": {
      "clientMutationId": null
    }
  },
  "extensions": {
    "warnings": [
      {
        "type": "DEPRECATION",
        "message": "The id $id1 is deprecated. Update your cache to use the next_global_id from the data payload.",
        "data": {
          "legacy_global_id": "$id1",
          "next_global_id": "U_kgDOAAJf5g"
        },
        "link": "https://docs.github.com"
      }
    ]
  }
}

It looks like gh is sending the CreatePullRequest then immediately afterwards updating the reviewers.

Activity

  1. added
    bug
    on Sep 11, 2024
  2. added
    needs-triage
    on Sep 11, 2024
  3. changed the title [-]gh create -fr doesn't play nice with codeowners[/-] [+]gh pr create -fr doesn't play nice with codeowners[/+] on Sep 11, 2024
  4. added
    gh-pr
    on Sep 11, 2024
  5. andyfeller commented on Sep 11, 2024

    @andyfeller
    Contributor

    @mcmanustfj : thank you for taking the time to follow up on this gh pr experience! let's dig into this and figure out what is possible.

    When I gh pr create -f -r $reviewer in a repository with codeowners, github will automatically request a codeowner review, even if $reviewer is a codeowner. Additionally, sometimes github removes request from one or more of the reviewers I specified!

    Could you share any information about whether this repository has a repository ruleset or branch protection rule that requires a review from a codeowner in order to merge the PR?

    I'm assuming you have one or both in place, which affects how Pull Requests determines the reviewers.

    From Require a pull request before merging:

    Optionally, you can choose to require reviews from code owners. If you do, any pull request that modifies content with a code owner must be approved by that code owner before the pull request can be merged into the protected branch. Note that if code has multiple owners, an approval from any of the code owners will be sufficient to meet this requirement. For more information, see "About code owners."

    Essentially, Pull Request is strongly opinionated about using the code owners information to handle reviewers, not who you specify. If this rule is not in place, then the subsequent call to assign the reviewers should work.

    Additionally, you might also consider assigning additional people using -a, --assignee login.

  6. mcmanustfj commented on Sep 11, 2024

    @mcmanustfj
    Author

    Just branch protection with "Require reviews from Code owners".
    Everyone I specify is also a code owner, the repo only has one code owner group.

  7. andyfeller commented on Sep 11, 2024

    @andyfeller
    Contributor

    Just branch protection with "Require reviews from Code owners". Everyone I specify is also a code owner, the repo only has one code owner group.

    Thanks, that's what I thought.

    If you tried creating this PR in the UI, it doesn't act any differently, does it? Meaning, you don't have control over selecting the reviewers; they are automatically assigned based on the code owners file and changes made.

  8. mcmanustfj commented on Sep 11, 2024

    @mcmanustfj
    Author

    I can select reviewers when creating the PR in the UI, and it has never deselected my selection or add new reviewers (as long as one of my reviewers is a codeowner); I'm pretty confident I would have noticed if it had, and I tried twice just now to check.

    In other words, it does act differently, and I would expect gh pr create to work how the web UI works.

  9. mcmanustfj commented on Sep 11, 2024

    @mcmanustfj
    Author

    this might be because the UI doesn't use the same http api and instead has the requested reviewers in the POST data.

    Notably, it shows the "requested review from $a and $b" before saying "requested review from $team as code owner".

    This might be a limitation of the HTTP API that gh cli uses, since there's no input for reviewers in the public github api

  10. andyfeller commented on Sep 12, 2024

    @andyfeller
    Contributor

    I can select reviewers when creating the PR in the UI, and it has never deselected my selection or add new reviewers (as long as one of my reviewers is a codeowner); I'm pretty confident I would have noticed if it had, and I tried twice just now to check.

    @mcmanustfj : that is a good point, 👉 let's see how much of your situation we can recreate in one of my repos as there are multiple factors involved as I'd like to figure this out. 👍

    From my experiments below, I see the explicitly specified -r, --reviewer being honored despite an auto assigned code owner from the configured team. 🤔

    Experiments I ran so far

    Experiment conditions

    Given the following:

    Experiment 1: Creating PR in UI with repo protected by repo ruleset, assigning code owner team members

    PR: tinyfists/gh-pr-test#5

    Result: the additional reviewer I added is preserved while the code reviewer team has member assigned while preserving team as configured

    Screenshot of GitHub PR creation UI, selecting reviewer outside of code owner team

    Screenshot of GitHub PR created, code reviewer assigned based on team membership while preserving explicit reviewer

    Experiment 2: Creating PR with gh pr -f -r USER under Experiment 1 conditions

    PR: tinyfists/gh-pr-test#6

    Result: the additional reviewer I added is preserved while the code reviewer team has member assigned while preserving team as configured 🤔

    $ gh pr create -f -r enyil  
    
    Creating pull request for simple-change into main in tinyfists/gh-pr-test
    
    https://github.com/tinyfists/gh-pr-test/pull/6

    Screenshot of GitHub PR created, code reviewer assigned based on team membership while preserving explicit reviewer

  11. added
    more-info-needed
    on Sep 12, 2024
  12. mcmanustfj commented on Sep 12, 2024

    @mcmanustfj
    Author

    is the user you requested in experiment 2 part of the codeowner team? Also, the behaviour isn't deterministic, sometimes you need to try a couple times to get it to show - additionally, it might be more likely for weird stuff to happen if you request multiple reviewers, but I'm not confident on that

  13. mcmanustfj commented on Sep 12, 2024

    @mcmanustfj
    Author

    The 'use case' for this is when I want a specific person (or people) who are code owners to review the PR, and not to add extraneous reviewers, since I've already specified a codeowner to review

  14. mcmanustfj commented on Sep 13, 2024

    @mcmanustfj
    Author

    maybe it's unreliable because when you make a new PR it takes a second for github to auto-assign codeowners, and if the HTTP call to add reviewers resolves before the codeowners resolve, it replaces the reviewers with the codeowners

  15. mcmanustfj commented on Sep 18, 2024

    @mcmanustfj
    Author

    @andyfeller do you need any more input?

  16. mcmanustfj commented on Sep 25, 2024

    @mcmanustfj
    Author

    @andyfeller do you have another question?

  17. andyfeller commented on Oct 7, 2024

    @andyfeller
    Contributor

    @mcmanustfj : apologies for the delay following up while out of office 🙇

    is the user you requested in experiment 2 part of the codeowner team? Also, the behaviour isn't deterministic, sometimes you need to try a couple times to get it to show - additionally, it might be more likely for weird stuff to happen if you request multiple reviewers, but I'm not confident on that

    No, enyil was not a code owner given the lack of the shield icon next to his name. This experiment was intended to show a non-codeowner reviewer would be accepted and not cleared by repo rule.

    maybe it's unreliable because when you make a new PR it takes a second for github to auto-assign codeowners, and if the HTTP call to add reviewers resolves before the codeowners resolve, it replaces the reviewers with the codeowners

    I'd appreciate if you could record a screen capture walking through these unreliable scenarios.

    Again, this depends on how you have 1) repository ruleset or 2) branch protection rules setup along with how any codeowner team's review settings are configured.

  18. mcmanustfj commented on Oct 8, 2024

    @mcmanustfj
    Author

    To confirm, this is specifically happening when the requested reviewer is in the code owner group. My main use case for gh pr create -fr $reviewer is when $reviewer is part of the codeowner group, and the problem is when the PR automatically adds someone else from the codeowner group and sometimes removes whoever I added to the codeowner group.

    There is no repository ruleset configured, and the branch protection rules are just as follows:
    image

    A screen capture wouldn't be very helpful since the action all happens offscreen - I create the PR with a one-line command and then I open the webpage.

    As I mentioned in the OP, the failure cases are
    | "$user requested a review from $team as a code owner x minutes ago"
    | "$user requested review from $reviewer and $other-codeowner (assigned from $team) x minutes ago"
    or sometimes
    | "$user requested review from $reviewer and $other-codeowner (assigned from $team) and removed request fro $reviewer x minutes ago"

    Sometimes it works fine and I just see "$user requested review from $reviewer x minutes ago"

  19. magnush0lm commented on Nov 10, 2025

    @magnush0lm

    I am experiencing what I think is the exact same thing:

    • gh version 2.81.0 (2025-10-01), GitHub Enterprise Server 3.18.0
    • repository has a CODEOWNERS file
    • pull requests are made with gh.exe pr create --body "body_text" --reviewer user1,user2..userN --title "some_title"
    • several of the users user1,user2..userN are present in CODEOWNERS, but not all
    • gh gives exit code 1 and gives output Warning: 2 uncommitted changes pull request update failed: GraphQL: Could not add requested reviewers to pull request. (requestReviews)

    The issue seems to be a race condition between Github processing the CODEOWNERS file and gh adding reviewers.
    It only happens once in a while (seldom) and we had no issue at all with this before the CODEOWNERS file was introduced.
    The PR is made just fine - apart from missing reviewer(s) - giving the impression that creating the PR and adding the reviewers are two different API calls.
    When looking in the Github web console (<org>/<repo>/pull/<number>), sometimes, the resulting PR will only have <my_username> requested review from <user(s)> as code owners, while other times it will have that same text, but with one (or more!) <my_username> requested review from <user(s)>... below.
    Users already added from CODEOWNERS will sometimes be in the second/added by gh list, other times not, giving additional clues to this being a race condition.

  20. cli-triage commented on Nov 7, 2026

    @cli-triage

    Partly reproduced on 2.46.0.

    Using test data, the command succeeds and creates the pull request before sending a separate reviewer request:

    $ gh pr create -R example.invalid/hello --head feature --base main --title test --body test --reviewer reviewer1 --reviewer reviewer2
    https://github.com/example.invalid/name

    Exit status: 0.

    The same two-step behavior remains in 2.103.3 and the current source build. The reviewer request is additive, but this test cannot simulate GitHub's asynchronous CODEOWNERS assignment, so it cannot reproduce the intermittent removal itself. Reports from two versions support keeping this open for investigation.

    Supporting reports

    Generated by issue triage.

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

    buggh-prmore-info-neededneeds-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions