|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Requires [gh](https://cli.github.com/), [jq](https://jqlang.github.io), git, and grep. Also awk if you pass a URL. |
| 4 | + |
| 5 | +# This script can be used to "purple-merge" PRs that are supposed to land as a single commit, using the "Squash and Merge" feature of GitHub. |
| 6 | +# To land a PR with this tool: |
| 7 | +# 1. Run `git node land <pr-id-or-url> --fixupAll` |
| 8 | +# 2. Copy the hash of the commit at the top of the PR branch. |
| 9 | +# 3. Run `tools/actions/merge.sh <pr-id-or-url> <commit-hash>`. |
| 10 | + |
| 11 | +set -xe |
| 12 | + |
| 13 | +pr=$1 |
| 14 | +commit_head=$2 |
| 15 | +shift 2 || { echo "Expected two arguments"; exit 1; } |
| 16 | + |
| 17 | +OWNER=nodejs |
| 18 | +REPOSITORY=node |
| 19 | + |
| 20 | +if expr "X$pr" : 'Xhttps://github.com/[^/]\{1,\}/[^/]\{1,\}/pull/[0-9]\{1,\}' >/dev/null; then |
| 21 | + OWNER="$(echo "$pr" | awk 'BEGIN { FS = "/" } ; { print $4 }')" |
| 22 | + REPOSITORY="$(echo "$pr" | awk 'BEGIN { FS = "/" } ; { print $5 }')" |
| 23 | + pr="$(echo "$pr" | awk 'BEGIN { FS = "/" } ; { print $7 }')" |
| 24 | +elif ! expr "X$pr" : 'X[0-9]\{1,\}' >/dev/null; then |
| 25 | + echo "The first argument should be the PR ID or URL" |
| 26 | +fi |
| 27 | + |
| 28 | +git log -1 HEAD --pretty='format:%B' | git interpret-trailers --parse --no-divider | \ |
| 29 | + grep -q -x "^PR-URL: https://github.com/$OWNER/$REPOSITORY/pull/$pr$" || { |
| 30 | + echo "Invalid PR-URL trailer" |
| 31 | + exit 1 |
| 32 | + } |
| 33 | +git log -1 HEAD^ --pretty='format:%B' | git interpret-trailers --parse --no-divider | \ |
| 34 | + grep -q -x "^PR-URL: https://github.com/$OWNER/$REPOSITORY/pull/$pr$" && { |
| 35 | + echo "Refuse to squash and merge a PR landing in more than one commit" |
| 36 | + exit 1 |
| 37 | + } |
| 38 | + |
| 39 | +commit_title=$(git log -1 --pretty='format:%s') |
| 40 | +commit_body=$(git log -1 --pretty='format:%b') |
| 41 | + |
| 42 | +jq -n \ |
| 43 | + --arg title "${commit_title}" \ |
| 44 | + --arg body "${commit_body}" \ |
| 45 | + --arg head "${commit_head}" \ |
| 46 | + '{merge_method:"squash",commit_title:$title,commit_message:$body,sha:$head}' > output.json |
| 47 | +cat output.json |
| 48 | +if ! gh api -X PUT "repos/${OWNER}/${REPOSITORY}/pulls/${pr}/merge" --input output.json > output; then |
| 49 | + cat output |
| 50 | + echo "Failed to merge $pr" |
| 51 | + rm output output.json |
| 52 | + exit 1 |
| 53 | +fi |
| 54 | +cat output |
| 55 | +if ! commits="$(jq -r 'if .merged then .sha else error("not merged") end' < output)"; then |
| 56 | + echo "Failed to merge $pr" |
| 57 | + rm output output.json |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | +rm output.json output |
| 61 | + |
| 62 | +gh pr comment "$pr" --repo "$OWNER/$REPOSITORY" --body "Landed in $commits" |
0 commit comments