Repository navigation
gh pr list: no support for --head <owner>:<branch> syntax #10945
Description
Activity
- changed the title
[-]`gh pr list`: `--head` doesn't support the `<owner>:<branch>` syntax[/-][+]`gh pr list`: no support for `--head <owner>:<branch>` syntax[/+]on May 7, 2025 Thanks for submitting this issue, @muzimuzhi! 🙏
This seems like a limit/restriction in our GraphQL API. Please note that
pr createuses thecreatePullRequestmutation, whilepr listuses therepositoryobject'spullRequestsfields. So, they're two different things. Both of these support aheadRefNameargument, but the behaviour is different (i.e. one supports<owner>:<branch>and one doesn't).As you pointed out
ghpass theheadRefNameargument with the provided--headoption, if any. Anyway, I attached the query forpr listat the end, in case you wanted to investigate it more.That said, filtering of
<owner>can be done at theghside after retrieving the PRs (i.e. stripping the<owner>:part, if any, calling the GQL API, and then applying a filter on the results). However, IMHO,ghshouldn't be involved in such a niche data processings, because, for one the GQL API could change in the future and support<owner>:<branch>format for filtering PRs. Also, as a client,ghbehaviour should not be very different from the GH API.The combination of
--jsonand--jqis a powerful tool for such scenarios. For example, this command will find the URL of the PR corresponding to one a specific user:GH_PAGER="" gh -R latex3/latex3 pr list --state all \ --head "typos" \ --json id,title,url,headRefName,headRepositoryOwner \ --jq '.[] | select(.headRepositoryOwner.login == "mbertucci47") | .url'
@muzimuzhi, do you think the
--json/--jqapproach could help with your situation?GQL query
Below is the query
ghuses to fetch the list of pull requests (variables are populated based on your case). You can try this GQL query in the Explorer.Variables:
{ "headBranch": "typos", "limit": 30, "owner": "latex3", "repo": "latex3", "state": [ "OPEN", "CLOSED", "MERGED" ] }query PullRequestList($owner: String!, $repo: String!, $limit: Int!, $endCursor: String, $baseBranch: String, $headBranch: String, $state: [PullRequestState!] = OPEN) { repository(owner: $owner, name: $repo) { pullRequests( # << Here states: $state baseRefName: $baseBranch headRefName: $headBranch first: $limit after: $endCursor orderBy: {field: CREATED_AT, direction: DESC} ) { totalCount nodes { number title state url headRefName headRefOid headRepository { name } headRepositoryOwner { id login ... on User { name } } isCrossRepository isDraft createdAt } pageInfo { hasNextPage endCursor } } } }
- addedmore-info-neededMore info needed from user/contributorMore info needed from user/contributorgh-prrelating to the gh pr commandrelating to the gh pr commandand removedbugSomething isn't workingSomething isn't workingneeds-triageneeds to be reviewedneeds to be reviewed
on May 8, 2025 @muzimuzhi, do you think the
--json/--jqapproach could help with your situation?Yes, that would be enough.
Perhaps the doc of
gh pr listcould be more explicit on the supported syntax of--headvalue.- removedmore-info-neededMore info needed from user/contributorMore info needed from user/contributor
on May 9, 2025 Great!
However, I'm not sure about including this explicitly in the docs, mostly because it's not either stated in the API docs. I mean, what if it changes in the future?
But adding another example in the
--helpdocs seems like a good idea. This at least shows how the--headoption can be used:EXAMPLES ... # List PRs with a specific head branch name $ gh pr list --head "typo"I'll now close this issue as it seems completed, and create a new one for adding the
--headusage example.@muzimuzhi After discussing this with @williammartin, we decided there's value in your request. So, I reopened this.
What's needed
To recap,
pr listneeds to do client side filtering when the provided--headvalue is of the form<owner>:<branch>(aka a qualifed ref).The change could be a bit complex due to the way
pr listworks. That is,pr listuses one of two queries (repository.pullRequestsandsearch) based on the provided options. For example, if the--authoroption is assigned, then asearchtakes place. Therefore, the support for--head <owner>:<branch>should be covered in both queries.Acceptance Criteria
Normal listing
Given I have a cross-repository PR whose head branch is on my fork
When I rungh pr list -R <base-repo> --head <my-login>:<branch>
Then the PR is listedSearch listing
Given I have a cross-repository PR whose head branch is on my fork
When I rungh pr list -R <base-repo> --head <my-login>:<branch> --author <my-login>
Then the PR is listedNote
The
--authoroption is to forcepr listto do asearchquery. So any other option that only works with thesearchshould lead to the same result.Docs
--headusageWhen I run
gh pr list --help
Then the usage of the--headoption mentions both<branch>and<owner>:<branch>syntaxes are supported.Example
When I run
gh pr list --help
Then there are example usages for both--head <user>:<branch>and--head <branch>syntax.Note
The example for
--head <branch>is going to be added by #10957.- addedenhancementa request to improve CLIa request to improve CLIcoreThis issue is not accepting PRs from outside contributorsThis issue is not accepting PRs from outside contributors
on May 9, 2025
Describe the bug
gh pr listonly supports the--head <branch>syntax but not--head <owner>:<branch>.Meanwhile,
gh pr create --head <owner>:<branch>is supported since v2.71.0 (added by #10513 in fixing #575).Need to check other options which accept a branch name.
https://cli.github.com/manual/gh_pr_list
https://cli.github.com/manual/gh_pr_create
Affected version
gh version 2.72.0 (2025-04-30)
https://github.com/cli/cli/releases/tag/v2.72.0
Steps to reproduce the behavior
--head "typos"finds two closed PRs in latex3/latex repository.But when specific owner name is provided (`--head ":typos"), no PR is found.
Logs
Adding
GH_DEBUG=api, it seems the only useful info is that the<owner>:<branch>was correctly passed on to GraphQL variables