/
pr_push
executable file
·28 lines (20 loc) · 912 Bytes
/
pr_push
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env bash
# Force-push to a remote PR
# Execute from any git repository or pass the directory as first argument
set -ueo pipefail
DIR=${1:-$(pwd)}
pushd "$DIR" > /dev/null
UPSTREAM="${UPSTREAM:-upstream}"
repoSlug=$(git remote -v | grep "^$UPSTREAM" | head -n1 | perl -lne 's/github.com:?\/?(.*)\/(.*?)([.]git| )// or next; print $1,"/",$2')
prNumber=$(git rev-parse --abbrev-ref HEAD | cut -d/ -f 2)
echo "Recognized pr: $prNumber"
echo "Recognized base slug: $repoSlug"
# Detect headRef and headSlug via GitHub API
githubReq=$(curl -s "https://api.github.com/repos/${repoSlug}/pulls/${prNumber}")
headRef=$(echo "$githubReq" | jq -r '.head.ref')
headSlug=$(echo "$githubReq" | jq -r '.head.repo.full_name')
echo "Recognized head ref: $headRef"
echo "Recognized head slug: $headSlug"
# Send the update to the PR
git push --force "git@github.com:${headSlug}" "HEAD:${headRef}"
popd > /dev/null