|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Shell script to lint the message of the first commit in a pull request. |
| 4 | +# |
| 5 | +# Depends on curl, git, node, npm and npx being in $PATH. |
| 6 | +# |
| 7 | +# The pull request is either: |
| 8 | +# 1) supplied as an argument to this shell script |
| 9 | +# 2) derived from the HEAD commit via the GitHub API |
| 10 | + |
| 11 | +GH_API_URL="https://api.github.com" |
| 12 | +PR_ID=$1; |
| 13 | +if [ -z "${PR_ID}" ]; then |
| 14 | + # Attempt to work out the PR number based on current HEAD |
| 15 | + if HEAD_COMMIT="$( git rev-parse HEAD )"; then |
| 16 | + if SEARCH_RESULTS="$( curl -s ${GH_API_URL}/search/issues?q=sha:${HEAD_COMMIT}+type:pr+repo:nodejs/node )"; then |
| 17 | + if FOUND_PR="$( node -p 'JSON.parse(process.argv[1]).items[0].number' "${SEARCH_RESULTS}" 2> /dev/null )"; then |
| 18 | + PR_ID=${FOUND_PR} |
| 19 | + fi |
| 20 | + fi |
| 21 | + fi |
| 22 | +fi |
| 23 | +if [ -z "${PR_ID}" ]; then |
| 24 | + echo "Unable to determine the pull request number to check. Please specify, " |
| 25 | + echo " e.g. $0 <PR_NUMBER>" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | +# Retrieve the first commit of the pull request via GitHub API |
| 29 | +# TODO: If we teach core-validate-commit to ignore "fixup!" and "squash!" |
| 30 | +# commits and lint messages for all commits in the pull request |
| 31 | +# we could simplify the following to: |
| 32 | +# npx -q core-validate-commit --no-validate-metadata ${GH_API_URL}/repos/nodejs/node/pulls/${PR_ID}/commits |
| 33 | +if PR_COMMITS="$( curl -s ${GH_API_URL}/repos/nodejs/node/pulls/${PR_ID}/commits )"; then |
| 34 | + if FIRST_COMMIT="$( node -p 'JSON.parse(process.argv[1])[0].url' "${PR_COMMITS}" 2> /dev/null )"; then |
| 35 | + echo "Linting the first commit message for pull request ${PR_ID}" |
| 36 | + echo "according to the guidelines at https://goo.gl/p2fr5Q." |
| 37 | + # Print the commit message to make it more obvious what is being checked. |
| 38 | + echo "Commit message for ${FIRST_COMMIT##*/} is:" |
| 39 | + node -p 'JSON.parse(process.argv[1])[0].commit.message' "${PR_COMMITS}" 2> /dev/null |
| 40 | + npx -q core-validate-commit --no-validate-metadata "${FIRST_COMMIT}" |
| 41 | + else |
| 42 | + echo "Unable to determine the first commit for pull request ${PR_ID}." |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | +fi |
0 commit comments