Skip to content

Commit 4280871

Browse files
committed
tools: Add scripts to push/reset to pull request.
1 parent 781883a commit 4280871

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

tools/push-to-pull-request

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/bin/bash
2+
set -e
3+
4+
usage () {
5+
cat >&2 <<EOF
6+
usage: $0 PULL_REQUEST_ID [REMOTE]
7+
8+
Force-push our HEAD to the given GitHub pull request branch.
9+
10+
Useful for a maintainer to run just before pushing to master,
11+
after tweaking the branch and/or rebasing to latest. This causes
12+
GitHub to see the subsequent push to master as representing a
13+
merge of the PR, rather than requiring the PR to be manually
14+
(and to the casual observer misleadingly) closed instead.
15+
16+
REMOTE defaults to the value of the Git config variable
17+
\`zulip.zulipRemote\` if set, else to \`upstream\`.
18+
19+
See also \`reset-to-pull-request\`.
20+
EOF
21+
exit 1
22+
}
23+
24+
remote_default="$(git config zulip.zulipRemote || echo upstream)"
25+
26+
pr_id="$1"
27+
remote="${2:-"$remote_default"}"
28+
29+
if [ -z "$pr_id" ]; then
30+
usage
31+
fi
32+
33+
remote_url="$(git config remote."$remote".url)"
34+
repo_fq="$(echo "$remote_url" | perl -lne 'print $1 if (
35+
m, ^ git\@github\.com:
36+
([^/]+ / [^/]+?)
37+
(?:\.git)?
38+
$ ,x )')"
39+
40+
if [ -z "$repo_fq" ]; then
41+
# We're pretty specific about what we expect the URL to look like;
42+
# there are probably more cases we could legitimately cover, which
43+
# we can add if/when they come up for someone.
44+
echo "error: couldn't parse remote URL as GitHub repo" >&2
45+
exit 1
46+
fi
47+
48+
# See https://developer.github.com/v3/pulls/#get-a-single-pull-request .
49+
# This is the old REST API; the new GraphQL API does look neat, but it
50+
# seems to require authentication even for simple lookups of public data,
51+
# and that'd be a pain for a simple script like this.
52+
pr_url=https://api.github.com/repos/"${repo_fq}"/pulls/"${pr_id}"
53+
pr_details="$(curl -s "$pr_url")"
54+
55+
pr_jq () {
56+
echo "$pr_details" | jq "$@"
57+
}
58+
59+
if [ "$(pr_jq -r .message)" = "Not Found" ]; then
60+
echo "Invalid PR URL: $pr_url"
61+
exit 1
62+
fi
63+
64+
if [ "$(pr_jq .maintainer_can_modify)" != "true" ]; then
65+
# This happens when the PR has already been merged or closed, or
66+
# if the contributor has turned off the (default) setting to allow
67+
# maintainers of the target repo to push to their PR branch.
68+
#
69+
# The latter seems to be rare (in Greg's experience doing the
70+
# manual equivalent of this script for many different
71+
# contributors, none have ever chosen this setting), but give a
72+
# decent error message if it does happen.
73+
echo "error: PR already closed, or contributor has disallowed pushing to branch" >&2
74+
exit 1
75+
fi
76+
77+
pr_head_repo_fq="$(pr_jq -r .head.repo.full_name)"
78+
pr_head_refname="$(pr_jq -r .head.ref)"
79+
80+
set -x
81+
exec git push git@github.com:"$pr_head_repo_fq" +@:"$pr_head_refname"

tools/reset-to-pull-request

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
set -e
3+
4+
if ! git diff-index --quiet HEAD; then
5+
set +x
6+
echo "There are uncommitted changes:"
7+
git status --short
8+
echo "Doing nothing to avoid losing your work."
9+
exit 1
10+
fi
11+
12+
remote_default="$(git config zulip.zulipRemote || echo upstream)"
13+
14+
request_id="$1"
15+
remote=${2:-"$remote_default"}
16+
17+
set -x
18+
git fetch "$remote" "pull/$request_id/head"
19+
git reset --hard FETCH_HEAD

0 commit comments

Comments
 (0)