Skip to content

Instantly share code, notes, and snippets.

@LeartS
Created January 22, 2017 15:21
Show Gist options
  • Save LeartS/4721944d3ca065f9259468bc4d4c7a80 to your computer and use it in GitHub Desktop.
Save LeartS/4721944d3ca065f9259468bc4d4c7a80 to your computer and use it in GitHub Desktop.
Gitlab update git hook to eslint commits and refuse if errors
#!/bin/bash
EXIT_CODE=0
REFNAME="$1"
OLDREV="$2"
NEWREV="$3"
# find file which have been Added, Copied, Modified or Renamed
UPDATED=`git diff-tree -r --diff-filter=ACMR --name-only $OLDREV $NEWREV '*.js'`
# alternative
# UPDATED=`git rev-list --objects master^..master | grep -e "\.js\$" | cut -f1 -d' ' | xargs git cat-file -p`
echo "Vediamo se hai fatto il bravo..."
sleep 2
echo
echo "------------------------------------------------------------------------"
echo "Files to Lint:"
echo "------------------------------------------------------------------------"
echo "$UPDATED"
echo "------------------------------------------------------------------------"
echo
echo "------> Linting....."
echo
if [ -z "$UPDATED" ]; then
echo "Nothing to lint."
exit 0
fi
# make tmp dir
TMP_DIR=`mktemp -d`
ESLINTRC=$TMP_DIR/.eslintrc
# extract .eslintrc file
git cat-file blob $NEWREV:.eslintrc > $ESLINTRC
# extract all updated files
for FILE in $UPDATED;
do
FILE_PATH=`dirname $FILE`
FULL_PATH=$TMP_DIR/$FILE_PATH
mkdir -p $FULL_PATH
git cat-file blob $NEWREV:$FILE > $TMP_DIR/$FILE
done
# Run ESLint with global node instead of gitlab-embedded one
OUTPUT=`PATH=/usr/bin:$PATH eslint --color --rule "import/no-unresolved: 0" $TMP_DIR`
EXIT_CODE=$?
SUMMARY=`echo "$OUTPUT" | tail -n 1`
# Cleanup
rm -rf $TMP_DIR
if [ -z "$OUTPUT" ]; then
echo "Perfetto! Good job."
exit 0
fi
if [ $EXIT_CODE -ne 0 ]; then
echo "$OUTPUT"
echo
echo "Qualcuno ha fatto il monello.. ritenta, sarai più fortunato!"
echo
sleep 1
exit $EXIT_CODE
fi
if [[ $SUMMARY != *"0 warnings"* ]]; then
echo "$OUTPUT"
echo
echo "Uhm, per questa volta passi.. la prossima volta però 0 warnings!"
echo
sleep 1
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment