-
Notifications
You must be signed in to change notification settings - Fork 8.3k
gh pr create: Support Git's @{push} revision syntax for determining head ref
#10513
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
Merged
BagToad
merged 40 commits into
trunk
from
kw/575-detect-push-target-for-local-branches-without-upstream-configuration
Apr 15, 2025
Merged
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
b61b229
doc: fix PullRequestRefs comment
BagToad a8d01c7
feat: support `@{push}` revision syntax
BagToad 6db9048
refactor: Refactor pr create
BagToad dc48625
test(pr create): test --head=<remote>:<branch>
BagToad cd67c2e
test(pr create): AT assert head repo and head ref
BagToad 84a35ca
refactor: rename IsPushEnabled for clarity
BagToad 1848beb
test(pr create): unset gh-resolved
BagToad 10e3949
refactor(pr create): use prRefs.GetPRHeadLabel()
BagToad a5fe37f
test(pr create): add AT for remote:branch syntax
BagToad c3087cd
refactor(pr create): Refactor NewCreateContext
BagToad 178fb40
refactor(pr create): Use PrRefs in CreateContext
BagToad c0c5d91
refactor(pr create): use GetPRHeadLabel()
BagToad f50dac5
docs(pr create): fix <repo_name>:<branch> desc.
BagToad 9f1cb0c
refactor(pr create): simplify head remote logic
BagToad cf58910
refactor(pr create): named headRefName var
BagToad 911079c
docs(pr create): add comments
BagToad ba6e2ec
test(pr create): skip tests that create forks
BagToad 9b96e6c
doc(pr create): fix typo in test comments
BagToad da235b1
tests(pr create): remove irrelevant comments
BagToad 1ce3d99
test(pr create): add logical spacing between operations
BagToad bab7dc6
test(pr create): update repo owner names in tests
BagToad 54da786
fix(pr create): update error handling
BagToad e999976
refactor(pr create): add PullRequestRefs `HasHead`
BagToad d524cbd
docs(pr create): clarify comment on head branch syntax
BagToad e9e57f3
doc(pr create): fix typo in comments
BagToad 6dae35b
test(finder): change assert to require
BagToad 041f02c
docs(pr create): standard <user>:<branch> syntax
BagToad 7bb5d71
doc(pr create): improve head repo resolution comments
BagToad 15ea861
docs(pr create): help text, doc user:branch syntax
BagToad 81d00a3
fix(pr create): use existing local variables
BagToad 188e138
test(pr create): add AT for no local repo
BagToad bf7bf99
fix(pr create & stubs): handle exitcode in stubs
BagToad 0611d9d
test(pr create): fix user:branch syntax AT
BagToad 76de5f0
test(pr finder): run test in parallel
BagToad 6004fc2
fix(pr create): use curly brace for vars in AT
BagToad bdfec50
fix(pr create): use REPO var in AT
BagToad 339e1a2
docs(pr create): add link for user:branch syntax
BagToad 028caa3
docs(pr create): clarify BaseRepo type in submitPR
BagToad ebd147b
Use verbose upstream setting
williammartin a9dbda6
Rework ref usage when finding and creating PRs
williammartin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
acceptance/testdata/pr/pr-create-guesses-remote-from-sha.txtar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| env REPO=${SCRIPT_NAME}-${RANDOM_STRING} | ||
| env FORK=${REPO}-fork | ||
|
|
||
| # Use gh as a credential helper | ||
| exec gh auth setup-git | ||
|
|
||
| # Get the current username for the fork owner | ||
| exec gh api user --jq .login | ||
| stdout2env USER | ||
|
|
||
| # Create a repository with a file so it has a default branch | ||
| exec gh repo create ${ORG}/${REPO} --add-readme --private | ||
|
|
||
| # Defer repo cleanup | ||
| defer gh repo delete --yes ${ORG}/${REPO} | ||
|
|
||
| # Create a user fork of repository. This will be owned by USER. | ||
| exec gh repo fork ${ORG}/${REPO} --fork-name ${FORK} | ||
| sleep 5 | ||
|
|
||
| # Defer repo cleanup of fork | ||
| defer gh repo delete --yes ${USER}/${FORK} | ||
|
|
||
| # Retrieve fork repository information | ||
| exec gh repo view ${USER}/${FORK} --json id --jq '.id' | ||
| stdout2env FORK_ID | ||
|
|
||
| exec gh repo clone ${USER}/${FORK} | ||
| cd ${FORK} | ||
|
|
||
| # Prepare a branch to commit | ||
| exec git checkout -b feature-branch | ||
| exec git commit --allow-empty -m 'Upstream Commit' | ||
| exec git push upstream feature-branch | ||
|
|
||
| # Prepare an additional commit | ||
| exec git commit --allow-empty -m 'Fork Commit' | ||
| exec git push origin feature-branch | ||
|
|
||
| # Create the PR | ||
| exec gh pr create --title 'Feature Title' --body 'Feature Body' | ||
| stdout https://${GH_HOST}/${ORG}/${REPO}/pull/1 | ||
|
|
||
| # Check the PR is indeed created | ||
| exec gh pr view ${USER}:feature-branch --json headRefName,headRepository,baseRefName,isCrossRepository | ||
| stdout {"baseRefName":"main","headRefName":"feature-branch","headRepository":{"id":"${FORK_ID}","name":"${FORK}"},"isCrossRepository":true} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Use gh as a credential helper | ||
| exec gh auth setup-git | ||
|
|
||
| # Setup environment variables used for testscript | ||
| env REPO=${SCRIPT_NAME}-${RANDOM_STRING} | ||
|
|
||
| # Create a repository with a file so it has a default branch | ||
| exec gh repo create ${ORG}/${REPO} --add-readme --private | ||
|
|
||
| # Defer repo cleanup | ||
| defer gh repo delete --yes ${ORG}/${REPO} | ||
|
|
||
| # Clone the repo | ||
| exec gh repo clone ${ORG}/${REPO} | ||
|
|
||
| # Prepare a branch to PR | ||
| cd ${REPO} | ||
| exec git checkout -b feature-branch | ||
| exec git commit --allow-empty -m 'Empty Commit' | ||
| exec git push -u origin feature-branch | ||
|
|
||
| # Leave the repo so there's no local repo | ||
| cd ${WORK} | ||
|
|
||
| # Create the PR | ||
| exec gh pr create --title 'Feature Title' --body 'Feature Body' --repo ${ORG}/${REPO} --head feature-branch | ||
| stdout https://${GH_HOST}/${ORG}/${REPO}/pull/1 |
49 changes: 49 additions & 0 deletions
49
acceptance/testdata/pr/pr-create-respects-branch-pushremote.txtar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| skip 'it creates a fork owned by the user running the test' | ||
|
|
||
| # Setup environment variables used for testscript | ||
| env REPO=${SCRIPT_NAME}-${RANDOM_STRING} | ||
| env FORK=${REPO}-fork | ||
|
|
||
| # Use gh as a credential helper | ||
| exec gh auth setup-git | ||
|
|
||
| # Get the current username for the fork owner | ||
| exec gh api user --jq .login | ||
| stdout2env USER | ||
|
|
||
| # Create a repository to act as upstream with a file so it has a default branch | ||
| exec gh repo create ${ORG}/${REPO} --add-readme --private | ||
|
|
||
| # Defer repo cleanup of upstream | ||
| defer gh repo delete --yes ${ORG}/${REPO} | ||
|
|
||
| # Create a user fork of repository. This will be owned by USER. | ||
| exec gh repo fork ${ORG}/${REPO} --fork-name ${FORK} | ||
| sleep 5 | ||
|
|
||
| # Defer repo cleanup of fork | ||
| defer gh repo delete --yes ${USER}/${FORK} | ||
|
|
||
| # Retrieve fork repository information | ||
| exec gh repo view ${USER}/${FORK} --json id --jq '.id' | ||
| stdout2env FORK_ID | ||
|
|
||
| # Clone the repo | ||
| exec gh repo clone ${USER}/${FORK} | ||
| cd ${FORK} | ||
|
|
||
| # Prepare a branch where changes are pulled from the upstream default branch but pushed to fork | ||
| exec git checkout -b feature-branch | ||
| exec git branch --set-upstream-to upstream/main | ||
| exec git config branch.feature-branch.pushRemote origin | ||
|
andyfeller marked this conversation as resolved.
|
||
| exec git config unset remote.upstream.gh-resolved | ||
| exec git commit --allow-empty -m 'Empty Commit' | ||
| exec git push | ||
|
|
||
| # Create the PR spanning upstream and fork repositories | ||
| exec gh pr create --title 'Feature Title' --body 'Feature Body' | ||
| stdout https://${GH_HOST}/${ORG}/${REPO}/pull/1 | ||
|
|
||
| # Assert that the PR was created with the correct head repository and refs | ||
| exec gh pr view --json headRefName,headRepository,baseRefName,isCrossRepository | ||
| stdout {"baseRefName":"main","headRefName":"feature-branch","headRepository":{"id":"${FORK_ID}","name":"${FORK}"},"isCrossRepository":true} | ||
53 changes: 53 additions & 0 deletions
53
acceptance/testdata/pr/pr-create-respects-push-destination.txtar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| skip 'it creates a fork owned by the user running the test' | ||
|
|
||
| # Setup environment variables used for testscript | ||
| env REPO=${SCRIPT_NAME}-${RANDOM_STRING} | ||
| env FORK=${REPO}-fork | ||
|
|
||
| # Use gh as a credential helper | ||
| exec gh auth setup-git | ||
|
|
||
| # Get the current username for the fork owner | ||
| exec gh api user --jq .login | ||
| stdout2env USER | ||
|
|
||
| # Create a repository to act as upstream with a file so it has a default branch | ||
| exec gh repo create ${ORG}/${REPO} --add-readme --private | ||
|
|
||
| # Defer repo cleanup of upstream | ||
| defer gh repo delete --yes ${ORG}/${REPO} | ||
|
|
||
| # Create a user fork of repository. This will be owned by USER. | ||
| exec gh repo fork ${ORG}/${REPO} --fork-name ${FORK} | ||
| sleep 5 | ||
|
|
||
| # Defer repo cleanup of fork | ||
| defer gh repo delete --yes ${USER}/${FORK} | ||
|
|
||
| # Retrieve fork repository information | ||
| exec gh repo view ${USER}/${FORK} --json id --jq '.id' | ||
| stdout2env FORK_ID | ||
|
|
||
| # Clone the repo | ||
| exec gh repo clone ${USER}/${FORK} | ||
| cd ${FORK} | ||
|
|
||
| # Configure default push behavior so local and remote branches will be the same | ||
| exec git config push.default current | ||
|
andyfeller marked this conversation as resolved.
|
||
|
|
||
| # Prepare a branch where changes are pulled from the default branch instead of remote branch of same name | ||
| exec git checkout -b feature-branch | ||
|
williammartin marked this conversation as resolved.
|
||
| exec git branch --set-upstream-to origin/main | ||
| exec git rev-parse --abbrev-ref feature-branch@{upstream} | ||
| stdout origin/main | ||
| exec git config unset remote.upstream.gh-resolved | ||
|
BagToad marked this conversation as resolved.
|
||
| exec git commit --allow-empty -m 'Empty Commit' | ||
| exec git push | ||
|
|
||
| # Create the PR | ||
| exec gh pr create --title 'Feature Title' --body 'Feature Body' | ||
| stdout https://${GH_HOST}/${ORG}/${REPO}/pull/1 | ||
|
|
||
| # Assert that the PR was created with the correct head repository and refs | ||
| exec gh pr view --json headRefName,headRepository,baseRefName,isCrossRepository | ||
| stdout {"baseRefName":"main","headRefName":"feature-branch","headRepository":{"id":"${FORK_ID}","name":"${FORK}"},"isCrossRepository":true} | ||
49 changes: 49 additions & 0 deletions
49
acceptance/testdata/pr/pr-create-respects-remote-pushdefault.txtar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| skip 'it creates a fork owned by the user running the test' | ||
|
|
||
| # Setup environment variables used for testscript | ||
| env REPO=${SCRIPT_NAME}-${RANDOM_STRING} | ||
| env FORK=${REPO}-fork | ||
|
|
||
| # Use gh as a credential helper | ||
| exec gh auth setup-git | ||
|
|
||
| # Get the current username for the fork owner | ||
| exec gh api user --jq .login | ||
| stdout2env USER | ||
|
|
||
| # Create a repository to act as upstream with a file so it has a default branch | ||
| exec gh repo create ${ORG}/${REPO} --add-readme --private | ||
|
|
||
| # Defer repo cleanup of upstream | ||
| defer gh repo delete --yes ${ORG}/${REPO} | ||
|
|
||
| # Create a user fork of repository. This will be owned by USER. | ||
| exec gh repo fork ${ORG}/${REPO} --fork-name ${FORK} | ||
| sleep 5 | ||
|
|
||
| # Defer repo cleanup of fork | ||
| defer gh repo delete --yes ${USER}/${FORK} | ||
|
|
||
| # Retrieve fork repository information | ||
| exec gh repo view ${USER}/${FORK} --json id --jq '.id' | ||
| stdout2env FORK_ID | ||
|
|
||
| # Clone the repo | ||
| exec gh repo clone ${USER}/${FORK} | ||
| cd ${FORK} | ||
|
|
||
| # Prepare a branch where changes are pulled from the upstream default branch but pushed to fork | ||
| exec git checkout -b feature-branch | ||
| exec git branch --set-upstream-to upstream/main | ||
| exec git config remote.pushDefault origin | ||
|
andyfeller marked this conversation as resolved.
|
||
| exec git config unset remote.upstream.gh-resolved | ||
| exec git commit --allow-empty -m 'Empty Commit' | ||
| exec git push | ||
|
|
||
| # Create the PR spanning upstream and fork repositories | ||
| exec gh pr create --title 'Feature Title' --body 'Feature Body' | ||
| stdout https://${GH_HOST}/${ORG}/${REPO}/pull/1 | ||
|
|
||
| # Assert that the PR was created with the correct head repository and refs | ||
| exec gh pr view --json headRefName,headRepository,baseRefName,isCrossRepository | ||
| stdout {"baseRefName":"main","headRefName":"feature-branch","headRepository":{"id":"${FORK_ID}","name":"${FORK}"},"isCrossRepository":true} | ||
34 changes: 34 additions & 0 deletions
34
acceptance/testdata/pr/pr-create-respects-simple-pushdefault.txtar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # Setup environment variables used for testscript | ||
| env REPO=${SCRIPT_NAME}-${RANDOM_STRING} | ||
|
|
||
| # Use gh as a credential helper | ||
| exec gh auth setup-git | ||
|
|
||
| # Create a repository with a file so it has a default branch | ||
| exec gh repo create ${ORG}/${REPO} --add-readme --private | ||
|
|
||
| # Defer repo cleanup of repo | ||
| defer gh repo delete --yes ${ORG}/${REPO} | ||
| exec gh repo view ${ORG}/${REPO} --json id --jq '.id' | ||
| stdout2env REPO_ID | ||
|
|
||
| # Clone the repo | ||
| exec gh repo clone ${ORG}/${REPO} | ||
| cd ${REPO} | ||
|
|
||
| # Configure default push behavior so local and remote branches have to be the same | ||
| exec git config push.default simple | ||
|
BagToad marked this conversation as resolved.
|
||
|
|
||
| # Prepare a branch where changes are pulled from the default branch instead of remote branch of same name | ||
| exec git checkout -b feature-branch | ||
| exec git branch --set-upstream-to origin/main | ||
| exec git commit --allow-empty -m 'Empty Commit' | ||
| exec git push origin feature-branch | ||
|
|
||
| # Create the PR | ||
| exec gh pr create --title 'Feature Title' --body 'Feature Body' | ||
| stdout https://${GH_HOST}/${ORG}/${REPO}/pull/1 | ||
|
|
||
| # Assert that the PR was created with the correct head repository and refs | ||
| exec gh pr view --json headRefName,headRepository,baseRefName,isCrossRepository | ||
| stdout {"baseRefName":"main","headRefName":"feature-branch","headRepository":{"id":"${REPO_ID}","name":"${REPO}"},"isCrossRepository":false} | ||
47 changes: 47 additions & 0 deletions
47
acceptance/testdata/pr/pr-create-respects-user-colon-branch-syntax.txtar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| skip 'it creates a fork owned by the user running the test' | ||
|
|
||
| # Setup environment variables used for testscript | ||
| env REPO=${SCRIPT_NAME}-${RANDOM_STRING} | ||
| env FORK=${REPO}-fork | ||
|
|
||
| # Use gh as a credential helper | ||
| exec gh auth setup-git | ||
|
|
||
| # Get the current username for the fork owner | ||
| exec gh api user --jq .login | ||
| stdout2env USER | ||
|
|
||
| # Create a repository to act as upstream with a file so it has a default branch | ||
| exec gh repo create ${ORG}/${REPO} --add-readme --private | ||
|
|
||
| # Defer repo cleanup of upstream | ||
| defer gh repo delete --yes ${ORG}/${REPO} | ||
|
|
||
| # Create a user fork of repository. This will be owned by USER. | ||
| exec gh repo fork ${ORG}/${REPO} --fork-name ${FORK} | ||
| sleep 5 | ||
|
|
||
| # Defer repo cleanup of fork | ||
| defer gh repo delete --yes ${USER}/${FORK} | ||
|
|
||
| # Retrieve fork repository information | ||
| exec gh repo view ${USER}/${FORK} --json id --jq '.id' | ||
| stdout2env FORK_ID | ||
|
|
||
| # Clone the fork | ||
| exec gh repo clone ${USER}/${FORK} | ||
| cd ${FORK} | ||
|
|
||
| # Prepare a branch where changes are pulled from the upstream default branch but pushed to fork | ||
| exec git checkout -b feature-branch | ||
| exec git branch --set-upstream-to upstream/main | ||
| exec git commit --allow-empty -m 'Empty Commit' | ||
| exec git push origin feature-branch | ||
|
|
||
| # Create the PR spanning upstream and fork repositories | ||
| exec gh pr create --title 'Feature Title' --body 'Feature Body' --head ${USER}:feature-branch | ||
| stdout https://${GH_HOST}/${ORG}/${REPO}/pull/1 | ||
|
|
||
| # Assert that the PR was created with the correct head repository and refs | ||
| exec gh pr view ${USER}:feature-branch --json headRefName,headRepository,baseRefName,isCrossRepository | ||
| stdout {"baseRefName":"main","headRefName":"feature-branch","headRepository":{"id":"${FORK_ID}","name":"${FORK}"},"isCrossRepository":true} |
10 changes: 6 additions & 4 deletions
10
acceptance/testdata/pr/pr-create-without-upstream-config.txtar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| skip 'it creates a fork owned by the user running the test' | ||
|
|
||
| # Setup environment variables used for testscript | ||
| env REPO=${SCRIPT_NAME}-${RANDOM_STRING} | ||
| env FORK=${REPO}-fork | ||
|
|
||
| # Use gh as a credential helper | ||
| exec gh auth setup-git | ||
|
|
||
| # Get the current username for the fork owner | ||
| exec gh api user --jq .login | ||
| stdout2env USER | ||
|
|
||
| # Create a repository to act as upstream with a file so it has a default branch | ||
| exec gh repo create ${ORG}/${REPO} --add-readme --private | ||
|
|
||
| # Defer repo cleanup of upstream | ||
| defer gh repo delete --yes ${ORG}/${REPO} | ||
|
|
||
| # Create a user fork of repository. This will be owned by USER. | ||
| exec gh repo fork ${ORG}/${REPO} --fork-name ${FORK} | ||
| sleep 5 | ||
|
|
||
| # Defer repo cleanup of fork | ||
| defer gh repo delete --yes ${USER}/${FORK} | ||
|
|
||
| # Retrieve fork repository information | ||
| exec gh repo view ${USER}/${FORK} --json id --jq '.id' | ||
| stdout2env FORK_ID | ||
|
|
||
| # Clone the repo | ||
| exec gh repo clone ${USER}/${FORK} | ||
| cd ${FORK} | ||
|
|
||
| # Prepare a branch where changes are pulled from the upstream default branch but pushed to fork | ||
| exec git checkout -b feature-branch | ||
| exec git commit --allow-empty -m 'Empty Commit' | ||
| exec git push -u origin feature-branch | ||
|
|
||
| # Create the PR spanning upstream and fork repositories | ||
| exec gh pr create --title 'Feature Title' --body 'Feature Body' | ||
| stdout https://${GH_HOST}/${ORG}/${REPO}/pull/1 | ||
|
|
||
| # Assert that the PR was created with the correct head repository and refs | ||
| exec gh pr status | ||
| ! stdout 'There is no pull request associated with' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.