git-machete icon indicating copy to clipboard operation
git-machete copied to clipboard

Does git machete support creating a PR from a fork to the upstream repo?

Open cspotcode opened this issue 2 years ago • 5 comments

Forgive my skipping the issue template. I'm not sure if git machete is meant to be used for the common open-source workflow, where you're creating a PR from your fork repo to the project's main repo? So the PR's base and head are on different remotes.

git machete github create-pr is giving me this error, and I'm not sure if I should dig into the problem, or if this is a known limitation of git machete.

Creating a PR from foo to development... {'message': 'Validation Failed', 'errors': [{'resource': 'PullRequest', 'field': 'head', 'code': 'invalid'}], 'documentation_url': 'https://docs.github.com/rest/reference/pulls#create-a-pull-request'}

I have two remotes, origin which is the project's main repo, and me which is my github fork. foo is a branch pushed to my fork, and development is the default branch of the project's main repo.

cspotcode avatar May 03 '23 00:05 cspotcode

EDIT: debug logs not needed anymore

~Huh this flow should definitely be supported and I'm pretty sure I've used github create-pr in a similar manner a couple times already (beyond development of git-machete itself)... still, I'll investigate that. In the meantime, if you could re-run the failing command in debug mode and attach the output:~

git machete github create-pr --debug

~I hope the data included in the debug (like repo URL, branch names etc.) won't be subject to any NDAs on your side (given it's open source).~ ~Note that access tokens will all be redacted in the output, you can ofc verify yourself by grepping for ghp.~

PawelLipski avatar May 03 '23 11:05 PawelLipski

Whoops okay... indeed, support for this scenario seems to be missing 😯 not sure how come we've missed that. To be added soon

PawelLipski avatar May 03 '23 13:05 PawelLipski

It's perhaps moot, since you can't exactly do stacked PRs when working with github forks. The problem is, if I want to create stacked PRs for contributing to an OSS project ossteam/libfoo from my fork cspotcode/libfoo:

First PR is from cspotcode/libfoo branch branch-1 into ossteam/libfoo branch master Second PR is from cspotcode/libfoo branch branch-2 into cspotcode/libfoo branch branch-1

That second PR will not appear in the PR queue for ossteam/libfoo since it's not targetting a branch on their repo, it's targetting a branch on mine. Kinda breaks the workflow. Not git-machete's fault, there's nothing you can really do. I suppose I could make the first PR, wait for it to merge, and then be able to submit the second one.

cspotcode avatar May 03 '23 22:05 cspotcode

It's perhaps moot, since you can't exactly do stacked PRs when working with github forks

Yeah, GitHub doesn't support working with stacked PRs on forks :/ Anyway, as a quick workaround to keep the effects of git machete github create-pr on PRs directly to origin branch, you can use git machete github anno-prs right after creating the PR, this works correctly for PRs from forks. Lmk if you find any issues with that approach, that should be way easier to fix than adding support for forks in create-pr 🤔

PawelLipski avatar May 04 '23 06:05 PawelLipski

Still, I think getting errors like this one:

Creating a draft PR from foo-bar-1 to master... __fire_github_api_request(method=POST, path=/repos/VirtusLab/git-machete/pulls, request_body={'head': 'foo-bar-1', 'base': 'master', 'title': 'wip', 'body': '', 'draft': 'True'}): firing a POST request to https://api.github.com/repos/VirtusLab/git-machete/pulls with a bearer token and request body {'head': 'foo-bar-1', 'base': 'master', 'title': 'wip', 'body': '', 'draft': 'True'}
{'resource': 'PullRequest', 'field': 'head', 'code': 'invalid'}

should be avoided.

What I'd go for here is to add support for creating PRs from forks indeed. More specifically:

  1. before creating a PR, check whether remote for tracking base and head branches are different
  2. if they're different, use the "head": "fork:branch" format instead of "head": "branch" in POST request body (TBD how to do it for GitLab, see #1189)
  3. display an extra warning to the user that it won't work for stacked PRs as well due to GitHub's limitations (TBD how it looks like in GitLab, as well)

PawelLipski avatar Apr 02 '24 20:04 PawelLipski

Soon to be released in v3.26.4!

PawelLipski avatar Aug 07 '24 16:08 PawelLipski