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 11GH_API_URL="https://api.github.com" 12PR_ID=$1; 13if [ -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 22fi 23if [ -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 27fi 28 29PATCH=$( curl -sL https://github.com/nodejs/node/pull/${PR_ID}.patch | grep '^From ' ) 30if FIRST_COMMIT="$( echo "$PATCH" | awk '/^From [0-9a-f]{40} / { if (count++ == 0) print $2 }' )"; then 31 MESSAGE=$( git show --quiet --format='format:%B' $FIRST_COMMIT ) 32 echo " 33*** Linting the first commit message for pull request ${PR_ID} 34*** according to the guidelines at https://goo.gl/p2fr5Q. 35*** Commit message for $(echo $FIRST_COMMIT | cut -c 1-10) is: 36${MESSAGE} 37" 38 npx -q core-validate-commit --no-validate-metadata "${FIRST_COMMIT}" 39fi 40