vscode-pull-request-github
vscode-pull-request-github copied to clipboard
Only first PR is selected for branches with multiple PRs
- Extension version: v0.46.1
- VSCode Version: v1.69.2
- OS: Darwin x64 21.5.0 - macOS Monterey v12.4
Steps to Reproduce:
- create a branch and make a pull request for that branch
- merge pr
- using the same branch, make another pull request
- The extension only picks up the first PR when you switch to that branch 4.1. Also, even if you use the extension UI to select a particular Pull request, the extension still picks up the first one.
Some thoughts:
- Maybe the extension should detect if multiple PRs exist for a branch and allow the user to choose.
- If a user selects a particular pull request from the extension's UI, that's the one that should be checked out.
Agreed. We don't have any support right now for when you have multiple PRs from 1 branch.
We also have a workflow where we open PRs from develop
=> main
branch, so the branch names aren't changing but new PRs do get created overtime.
I thought the extension would query GitHub to discover the now most recent, open pull request, but the extension always associated my branch with the older now closed/merged pull request.
Looking through the extension's code, I discovered that the branch to pull request association is tracked in git config
. So my workaround is to change my git config to point to the new pull request number then reload VS Code. I was actually hoping the extension was monitoring for git config changes and would have self-refreshed but it didn't seem to so I reloaded the window/editor manually.
You can change the setting with this command:
ORG="my-org" \
REPO="my-repo" \
BRANCH="develop" \
PR="4" \
&& git config --replace-all "branch.${BRANCH}.github-pr-owner-number" "${ORG}#${REPO}#${PR}"
You can check the setting with this command:
git config --list | grep -E "github-pr-owner-number"
Shell Function
I went as far as to add an alias function in my terminal's profile (e.g. ~/.bash_profile or ~/.zshrc
). Now I can just type vspr
(or your preferred alias) and the git config setting is updated automatically. This approach uses the GitHub CLI.
# Workaround to force the GitHub Pull Requests and Issues extension
# to associate to the most recent, open pull request for the checked out branch.
# https://github.com/microsoft/vscode-pull-request-github/issues/3798#issuecomment-1259003851
function vspr() {
local branch=$(git branch --show-current)
local number=$(gh pr list --head ${branch} --json number --jq '.[0].number')
if [ -z "${number}" ]; then
echo "-- no open pull request found for branch"
exit 1
fi
local pr_json=$(gh pr view ${number} --json headRepository,headRepositoryOwner)
local owner=$(jq -r '.headRepositoryOwner.login' <<< $pr_json)
local repo=$(jq -r '.headRepository.name' <<< $pr_json)
git config --replace-all "branch.${branch}.github-pr-owner-number" "${owner}#${repo}#${number}"
git config --list | grep -E "github-pr-owner-number"
}
@douglascayers thanks for sharing your workaround! I'll try to tackle this in the extension in October.
Thanks @alexr00 😄
While tinkering with my shell function, I came up with a slightly more complex version that supports when you're working on a fork and submitting PRs to the upstream repo. Just sharing in case anyone was interested. I'm sure the extension will do a much better job than what I've scratched together 😆
# Workaround to force the GitHub Pull Requests and Issues extension
# to associate to the most recent, open pull request for the checked out branch.
# https://github.com/microsoft/vscode-pull-request-github/issues/3798#issuecomment-1259003851
function vspr() {
local branch=$(git branch --show-current)
local repo_json=$(gh repo view --json name,owner,parent,isFork)
local is_fork=$(jq -r '.isFork' <<< $repo_json)
local fork_owner=$(jq -r '.owner.login' <<< $repo_json)
local fork_repo=$(jq -r '.name' <<< $repo_json)
local parent_owner=$(jq -r '.parent.owner.login' <<< $repo_json)
local parent_repo=$(jq -r '.parent.name' <<< $repo_json)
local repos_arr=("${fork_owner}/${fork_repo}")
if [ "${is_fork}" = "true" ]; then
repos_arr+=("${parent_owner}/${parent_repo}")
fi
local states_arr=(
"open"
"all"
)
local pr_json=""
local pr_number=""
local pr_owner=""
local pr_repo=""
for owner_repo in "${repos_arr[@]}"; do
for state in "${states_arr[@]}"; do
pr_number="$(gh pr list --repo ${owner_repo} --head ${branch} --state ${state} --limit 1 --json number --jq '.[0].number')"
if [ ! -z "${pr_number}" ]; then
pr_json=$(gh pr view --repo ${owner_repo} --json isCrossRepository ${pr_number})
pr_on_parent=$(jq -r '.isCrossRepository' <<< $pr_json)
if [ "${pr_on_parent}" = "true" ]; then
pr_owner="${parent_owner}"
pr_repo="${parent_repo}"
else
pr_owner="${fork_owner}"
pr_repo="${fork_repo}"
fi
break 2
fi
done
done
if [ -z "${pr_number}" ]; then
echo "no pull request found for branch ${branch}"
exit 1
fi
git config --replace-all "branch.${branch}.github-pr-owner-number" "${pr_owner}#${pr_repo}#${pr_number}"
git config --list | grep -E "github-pr-owner-number"
}
4.1. Also, even if you use the extension UI to select a particular Pull request, the extension still picks up the first one.
The incoming change doesn't fix this part of the issue, but it will try to find the open PR, and barring that, the most recent PR.
To verify: 0. Install the pre-release version of the extension
- Make a PR.
- Close or merge the PR, but don't delete the branch.
- Make another PR from the same branch.
- Reload and verify that the Active Pull Request view shows your 2nd PR.