Downport "Add a shared pre-commit to run eslintfix for frontend code changes"

This pre-commit hook will fail the commit when you have linter errors.
It will also only run on changed staged files.

Also cleaned up linter config in package and lint_test.

Change-Id: I70d906b5c5c0180d88e0d0ff9840387e9f013fe2
This commit is contained in:
Tao Zhou
2019-12-11 15:25:26 +01:00
committed by Thomas Draebing
parent a68dbd29cb
commit 3445b3d2c4
3 changed files with 49 additions and 2 deletions

View File

@@ -15,7 +15,7 @@
"scripts": {
"start": "polygerrit-ui/run-server.sh",
"test": "WCT_HEADLESS_MODE=1 WCT_ARGS='--verbose -l chrome' ./polygerrit-ui/app/run_test.sh",
"eslint": "./node_modules/eslint/bin/eslint.js --ignore-pattern 'bower_components/' --ignore-pattern 'gr-linked-text' --ignore-pattern 'scripts/vendor' --ext .html,.js polygerrit-ui/app",
"eslint": "./node_modules/eslint/bin/eslint.js --ext .html,.js polygerrit-ui/app",
"eslintfix": "npm run eslint -- --fix",
"test-template": "./polygerrit-ui/app/run_template_test.sh",
"polylint": "bazel test polygerrit-ui/app:polylint_test"

View File

@@ -30,4 +30,4 @@ cd ${UI_PATH}
# eslint installation.
npm link eslint eslint-config-google eslint-plugin-html
${eslint_bin} -c ${UI_PATH}/.eslintrc.json --ignore-pattern 'node_modules/' --ignore-pattern 'bower_components/' --ignore-pattern 'gr-linked-text' --ignore-pattern 'scripts/vendor' --ext .html,.js ${UI_PATH}
${eslint_bin} -c ${UI_PATH}/.eslintrc.json --ext .html,.js ${UI_PATH}

47
tools/dev-hooks/pre-commit Executable file
View File

@@ -0,0 +1,47 @@
#!/bin/sh
#
# Part of Gerrit Code Review (https://www.gerritcodereview.com/)
#
# Copyright (C) 2019 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# To enable this hook:
# - copy this file or content to ".git/hooks/pre-commit"
# - (optional if you copied this file) make it executable: `chmod +x .git/hooks/pre-commit`
set -ue
# gitroot, default to .
gitroot=$(git rev-parse --show-cdup)
gitroot=${gitroot:-.};
# eslint
eslint=${gitroot}/node_modules/eslint/bin/eslint.js
# Run eslint over changed frontend code
CHANGED_UI_FILES=$(git diff --cached --name-only -- '*.js' '*.html' | grep 'polygerrit-ui') && true
if [ "${CHANGED_UI_FILES}" ]; then
if $eslint --fix ${CHANGED_UI_FILES}; then
# Add again in case lint fix modified some files
git add ${CHANGED_UI_FILES}
exit 0
else
echo "Failed to fix all linter issues.";
exit 1
fi
else
echo "No UI files changed"
exit 0
fi