|
| 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" |
0 commit comments