Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR creation failing from Org fork with - {"resource":"PullRequest","field":"head","code":"invalid"} #2043

Closed
FoSix opened this issue Jun 6, 2023 · 5 comments · Fixed by #2044

Comments

@FoSix
Copy link

FoSix commented Jun 6, 2023

I know it's similar issues were already posted, I've read them, didn't find a solution, so bare with me for a sec 😄

TL;DR

The problem goes down to this error:

Error: Validation Failed: {"resource":"PullRequest","field":"head","code":"invalid"}

being thrown when creating a PR from a fork which belongs to an organisation, the same organisation to which the base repo belongs to.

SETUP

We have to repos under an organisation, both public, let's call them:

  • base - a repo to which we would like introduce changes, we have read only access, hence changes have to be merged by a PR from a fork
  • CI - hosting a workflow, triggered on pull_request

WORKFLOW

The workflow is simple, when a PR is created in the CI repo:

  • checkout the base repo
  • introduce some changes
  • run create-pull-request action to create a PR using a fork.

When the fork is a personal fork (not under organisation) everything works. The problem pops up when the fork is inside the same organisation as the base repo.

PAT used for this is the classic type with public_repo access. I did try with all possible accesses but it does not seem to be an access problem. The account for which PAT was generated is a regular organisation member with owner access to CI repo and an owner of both forks.


Below you can find links to test repos I used to reproduce the problem:

I did already spent a lot of time on this, so any help (even pointing out what I'm doing wrong) would be highly appreciated.

@peter-evans
Copy link
Owner

@FoSix Thankyou for your very detailed report of the issue. I notice that it has created the pull request branch successfully, but the PR creation failed. So it doesn't appear to be an issue related to token permissions.

My guess is that the action is struggling with the fact that the parent repo and fork are in the same org. That is not really normal since forks are usually made into a different org or user account. I'll investigate to see if there is something I can do to handle this case.

@FoSix
Copy link
Author

FoSix commented Jun 13, 2023

I did some testing with with GH's REST API and octokit/rest.js and apparently it is possible to create a PR from a fork in the same org not only from UI (GH CLI however does not work). I'll skip the REST part, but for JS (assuming the same test repos):

octokit.rest.pulls.create({
  owner: "fosixorg",
  repo: "base",
  head: "fosixorg:create-pull-request/patch",
  head_repo: "fosixorg/fork",
  base: "main",
  title: "script base PR",
  body: "created with GH script",
})

Proven to work here.

What does the trick is the head_repo parameter. And this is something that I think is missing in the action's code.

I'm not a JS developer so (unfortunately) I cannot propose a PR with code changes...

@peter-evans
Copy link
Owner

@FoSix Great work testing and finding that parameter! I've created a branch with the change to add head_repo in a PR here. It seems to be passing all of my tests. Would you be willing to test this version of the action and let me know if it solves your issue?

You can test the PR version of the action like this:

      - uses: peter-evans/create-pull-request@head-repo

If it works fine I'll merge it in and create a new release.

@FoSix
Copy link
Author

FoSix commented Jun 13, 2023

Would you be willing to test this version of the action and let me know if it solves your issue?

Sure!


UPDATE: @peter-evans it looks great, it seems to work, for both forks (org and personal)

@peter-evans
Copy link
Owner

Thank you for testing! ❤️

Released as v5.0.2 / v5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants