Created
September 27, 2022 22:44
-
-
Save douglascayers/b009bf12007ba9cfe79f94739b184a5f to your computer and use it in GitHub Desktop.
Shell function to set the git config `branch.${branch}.github-pr-owner-number` for GitHub Pull Requests and Issues extension
This file contains 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
# 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" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment