Skip to content

gh pr edit --add-reviewer requires checkout of a very specific branch #10071

Open

Description

@TWiStErRob

Describe the bug

Latest gh on GHA at the moment.
https://github.com/cli/cli/releases/tag/v2.63.2 was released 7 days ago.

Steps to reproduce the behavior

  1. We're trying to add a team reviewer:
on:
  pull_request:
    types:
      - edited
jobs:
  job:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - run: gh pr edit "${PR_NUMBER}" --add-reviewer "${REVIEWER_TEAM}"
        env:
          GH_TOKEN: ${{ github.token }}
          GH_REPO: ${{ github.repository }}
          REVIEWER_TEAM: my-org/my-team
          PR_NUMBER: ${{ github.event.pull_request.number }}
  1. The output is:
could not determine current branch: failed to run git: fatal: not a git repository (or any of the parent directories): .git

even though we clearly defined the repository with GH_REPO, so gh should go into auto-detect mode.

  1. Add a checkout step
- uses: actions/checkout@v4
  1. The output is:
could not determine current branch: failed to run git: not on any branch
  1. Configure the checkout step
- uses: actions/checkout@v4
  with:
    ref: ${{ github.event.pull_request.head.ref }}
  1. Finally got a proper error message that we need permissions for organisation.teams -> https://github.com/orgs/community/discussions/113519

Expected vs actual behavior

Expected behavior would be that we're not forced to clone the repo just to get the branch name, which is seemingly not even used in the command.

Activity

  1. williammartin commented on Dec 16, 2024

    @williammartin
    Member

    Hey @TWiStErRob,

    Where does ${PR_NUMBER} number come from? Are you sure it is set correctly? You're right that pr edit should not be looking up the branch name if you've provided the PR number and GH_REPO.

    Also, just as an aside, I think you could probably provide the pull_request.url as an argument and not set GH_REPO at all: https://docs.github.com/en/webhooks/webhook-events-and-payloads?actionType=edited#pull_request

    I'll have another look at the code and see whether I can spot anything unusual.

  2. williammartin commented on Dec 16, 2024

    @williammartin
    Member

    FWIW this worked for me:

    ➜ GH_REPO=williammartin-test-org/test-repo gh pr edit 3 --title test
    https://github.com/williammartin-test-org/test-repo/pull/3
    

    Which further makes me wonder whether there's any chance PR_NUMBER is unset, and therefore this is resolving to something like:

    ➜  cd /tmp
    ➜  /tmp GH_REPO=williammartin-test-org/test-repo gh pr edit --title test
    could not determine current branch: failed to run git: fatal: not a git repository (or any of the parent directories): .git
    
  3. TWiStErRob commented on Dec 16, 2024

    @TWiStErRob
    ContributorAuthor

    PR_NUMBER is set correctly, defined in env: right below the line.

    image

    It's strange that it works now. In a public open-source repo it's the image above, when running in an internal cloud org we get #6274.

    We couldn't've been hallucinating this thought, I'll try to see if I can dig up some history.


    I think you could probably provide the pull_request.url as an argument and not set GH_REPO at all: https://docs.github.com/en/webhooks/webhook-events-and-payloads?actionType=edited#pull_request

    That's a top tip! Thanks.

  4. fregante commented on May 30, 2025

    @fregante

    I think you could probably provide the pull_request.url as an argument and not set GH_REPO at all: docs.github.com/en/webhooks/webhook-events-and-payloads?actionType=edited#pull_request

    I've been using this for a while and this week it started failing

        env:
          PR_URL: ${{github.event.pull_request.html_url}}
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        steps:
          - run: 'gh pr edit "$PR_URL" --add-label "admin console"
    Image

    Using --repo made it work again, this is probably a regression:

        env:
          PR: ${{github.event.pull_request.number}}
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        steps:
          - run: 'gh pr edit $PR --repo ${{ github.repository }} --add-label "admin console"
  5. BagToad commented on May 30, 2025

    @BagToad
    Member

    @fregante I would make sure that when that step runs, that the CWD is still in the repository. I don't think we've made changes to the way the repo resolution works, but maybe I'm wrong.

    Can you provide the full workflow YAML please? Redact anything you need to.

  6. fregante commented on May 30, 2025

    @fregante

    that the CWD is still in the repository

    But that's the point, I passed a full URL to the resource so it knows the repo already. gh is perfectly able to run from any folder, in fact it works with the explicit --repo flag.

    Can you provide the full workflow YAML please? Redact anything you need to.

    Here's the workflow minus some extra conditions to add/remove certain labels

    name: PR
    on: pull_request
    
    jobs:
      labels:
        runs-on: ubuntu-latest
        permissions:
          # See https://github.com/cli/cli/issues/6274#issuecomment-2439692639
          contents: read
          pull-requests: write
          checks: read
          statuses: read
          actions: read
          repository-projects: read
        env:
          PR_URL: ${{github.event.pull_request.html_url}}
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        steps:
          - run: 'gh pr edit "$PR_URL" --add-label "admin-console"
  7. gr2m commented on May 31, 2025

    @gr2m

    Hi there, I see gh pr edit started to fail once 4 days ago, and continuously 2 days ago. It's open source so you can see the workflow and action build history:

    https://github.com/vercel/ai/actions/workflows/assign-team-pull-request.yml

    Please let me know if there is anything we can do to debug this.

  8. user202729 commented on Jun 2, 2025

    @user202729

    I open a new issue regarding the new bug (since this one is old it's likely a different bug, but might have the same cause)

    #11055 (comment)

  9. BagToad commented on Jun 3, 2025

    @BagToad
    Member

    @fregante Sorry, I just realized I did not reply to you! Thank you for clarifying and you're absolutely right.

    @williammartin has a quick fix out for this in #11057 and we'll do a patch release soon to publish it.

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

    bugSomething isn't workinggh-prrelating to the gh pr commandmore-info-neededMore info needed from user/contributorneeds-triageneeds to be reviewed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions