Created
January 24, 2017 00:56
-
-
Save corburn/c6320a2212723125b9a0fdf227e2c69d to your computer and use it in GitHub Desktop.
git pre-commit hook to lint .travis.yml using api.travis-ci.org/lint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# A hook to check for lint warnings when .travis.yml is modified | |
# by uploading it to api.travis-ci.org/lint. | |
# | |
# A similar result could be achieved by uploading the configuration | |
# to https://lint.travis-ci.org/ before commiting. | |
# Redirect output to stderr. | |
exec 1>&2 | |
# Is .travis.yml among the the modified files? | |
if git diff --cached --name-only | grep --quiet ".travis.yml"; then | |
cat <<EOF | |
Uploading .travis.yml to api.travis-ci.org/lint to check for lint warnings. | |
If you know what you are doing you can disable this check using: | |
git commit --no-verify | |
EOF | |
# Upload .travis.yml to the Travis API lint endpoint. | |
JSON_RESPONSE=$(curl -L -T .travis.yml --max-time 15 https://api.travis-ci.org/lint) | |
CURL_EXIT_CODE=$? | |
# TODO: report a different error for exit code 28 (timeout) | |
# The timeout could be a temporary issue where the user could try again. | |
if [ "$CURL_EXIT_CODE" -ne 0 ]; then | |
cat <<EOF | |
Error: curl failed with exit code $CURL_EXIT_CODE uploading .travis.yml to api.travis-ci.org/lint. | |
You can also check your .travis.yml configuration on https://lint.travis-ci.org/ | |
If you know what you are doing you can disable this check using: | |
git commit --no-verify | |
EOF | |
exit 1 | |
fi | |
# Use POSIX substring parameter expansion to check for warnings in the response. | |
# http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 | |
# | |
# Example response without warnings: | |
# { | |
# "lint" : { | |
# "warnings" : [] | |
# } | |
# } | |
# | |
# Example response with warnings: | |
# { | |
# "lint": { | |
# "warnings": [ | |
# { | |
# "key": ["jdk"], | |
# "message": "specified jdk, but ruby does not include jruby" | |
# } | |
# ] | |
# } | |
# } | |
# https://docs.travis-ci.com/api#linting | |
if [ "${JSON_RESPONSE#*"message"}" != "$JSON_RESPONSE" ]; then | |
cat << EOF | |
Error: api.travis-ci.org/lint returned the following response: | |
EOF | |
# Try a few commands to pretty print the JSON response. | |
# If none are available, fallback to printing the raw response. | |
# The /dev/null redirects hide "command not found" messages. | |
echo $JSON_RESPONSE | { | |
jq . 2>/dev/null \ | |
|| python -m json.tool 2>/dev/null | { pygmentize -g 2>/dev/null || cat; } 2>/dev/null \ | |
|| json_pp 2>/dev/null \ | |
|| cat; \ | |
} | |
# Abort commit | |
exit 1 | |
fi | |
fi | |
# Proceed with commit | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment