bridge-node-server/tools/bitbucket-pipeline-scripts/eslint-changes.sh
Martin Donnelly 57bd6c8e6a init
2018-06-24 21:15:03 +01:00

79 lines
2.0 KiB
Bash

#!/bin/bash
#
# Script to run ESLint against all the files that have changed in this revision
#
ESLINT="$(git rev-parse --show-toplevel)/node_modules/.bin/eslint"
RUN_LINT=true
PASS_LINT=true
#
# There are two cases for what changes we should be looking for to lint:
# 1. When it's a merge onto master, we want the last commit (the merge), which will contain
# all changes.
# 2. When it's a PR we want all files in the whole branch, as the last commit may only
# be part of the full change. I.e. we want the difference between HEAD and master.
#
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
if [[ "$BRANCH_NAME" = "master" ]]; then
#
# Master branch, so just the last commit
#
echo "ESLint: master branch, so checking last commit."
JS_FILES_TO_LINT=$(git diff --name-only --diff-filter=ACM HEAD~1...HEAD | grep ".js$")
else
#
# Dev branch so we want all the commits in the branch
# There's a slight wrinkle in that bitbucket pipelines only clone this specific branch, so we
# have to fetch master first before we can tell where this branch starts
#
echo "ESLint: dev branch, so checking whole branch."
git fetch origin master:master
JS_FILES_TO_LINT=$(git diff --name-only --diff-filter=ACM master...HEAD | grep ".js$")
fi
#
# Check if we have any JS files to lint
#
if [[ "$JS_FILES_TO_LINT" = "" ]]; then
echo "ESLint: nothing to test"
RUN_LINT=false
PASS_LINT=true
else
echo "ESLint:"
# Check for eslint
if [[ ! -x "$ESLINT" ]]; then
echo " - Failed to find " "$ESLINT"
echo " - Please install node modules (npm install)"
fi
#
# Iterate throught the files and run them against ESLint
#
for FILE in $JS_FILES_TO_LINT
do
"$ESLINT" "$FILE"
if [[ "$?" == 0 ]]; then
echo " - Passed: $FILE"
else
echo " - Failed: $FILE"
PASS_LINT=false
fi
done
fi
#
# Check the results
#
if ! $RUN_LINT; then
echo "* ESLint: not run"
elif ! $PASS_LINT; then
echo "* ESLint: FAIL!"
exit 1
else
echo "* ESLint: pass"
fi
exit 0