Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh and REST API unable to work with same-org forked repos #6462

@tonglil

Description

@tonglil

Describe the bug

GitHub introduce the feature of forking internally here: https://github.blog/changelog/2022-06-27-improved-innersource-collaboration-and-enterprise-fork-policies/

Fork a repository to the same organization as its parent

Previously, a repository could be forked only to a different organization or user account.

Now, a repository can be forked to the same organization as its parent repository, addressing situations where people are working in one organization and don’t want to fork a repository to a different organization or user account.

But I don't think you've updated your API or tools to support this.


The REST API to create a pull request from an internally forked repository does not work.

However visiting an URL like this works for generating a PR: https://github.com/myorg/myrepo/compare/main...myorg:myrepoforked:mybranch?expand=1

However this does not work programmatically via the API:

# in the myrepoforked directory on the mybranch branch
gh api --method POST -H "Accept: application/vnd.github.v3+json" repos/myorg/myrepo/pulls -F title=test -F body=test -F base=main -F head='myorg:myrepoforked:mybranch'
{
  "message": "Validation Failed",
  "errors": [
    {
      "resource": "PullRequest",
      "field": "head",
      "code": "invalid"
    }
  ],
  "documentation_url": "https://docs.github.com/rest/reference/pulls#create-a-pull-request"
}
gh: Validation Failed (HTTP 422)

This is the same issue when using curl directly on the GH API.

I can workaround this by:

# in the myrepoforked directory on the mybranch branch
gh pr create --repo "myorg/myrepo" --title "test" --body "test"

^ actually this doesn't work as expected, running into this error:

gh pr create --repo "myorg/myrepo" --title "test" --body "test"

Creating pull request for mybranch into main in myorg/myrepo

pull request create failed: GraphQL: Head sha can't be blank, Base sha can't be blank, No commits between main and mybranch, Head ref must be a branch (createPullRequest)

I went through these issues that seem relevant but can't find a solution:

However, this type of forking is poorly supported as when I go to list out PRs, things start failing again:

# in the myrepoforked directory on the mybranch branch
gh pr list --repo "myorg/myrepo" --head "mybranch" --state all

Showing 2 of 2 pull requests in myorg/myrepo that match your search

#27  test  myorg:mybranch  about 28 minutes ago
#26  test  mybranch             about 1 hour ago

^ notice the PR from myrepoforked is myorg:mybranch and a PR from the branch in myrepo is just mybranch.

Now if I wanted to filter on the forked repo's mybranch and not the original repo's mybranch, it fails:

# in the myrepoforked directory on the mybranch branch
gh pr list --repo "myorg/myrepo" --head "myorg:mybranch" --state all
no pull requests match your search in myorg/myrepo

Steps to reproduce the behavior

  1. Create a private or internal repository in an organization
  2. Fork that repo inside the organization under a different name
  3. Create a branch in the fork
  4. Attempt to open a PR with the fork's branch back to the original repository with the REST API or the gh CLI tool
  5. Failure

Expected vs actual behavior

Expected: working with branches in forks with different names in the same organization/user should work.
Actual: does not work.

Logs

gh --version
gh version 2.18.0 (2022-10-18)
https://github.com/cli/cli/releases/tag/v2.18.0

Related

This is related to #2300 and #4859 from what I can find, but note that the method of providing the "base" repo via the --repo flag is not in parity with the REST documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingp2Affects more than a few users but doesn't prevent core functions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions