From ac0be988ea5795409495702ca6054af2737028b3 Mon Sep 17 00:00:00 2001 From: cubercsl <2014cais01@gmail.com> Date: Thu, 16 Dec 2021 18:12:29 +0800 Subject: [PATCH] ci: add pre-commit-hook --- .github/workflows/deploy.yml | 21 +++++++++++++++++++++ .github/workflows/merge_check.yml | 10 ++++++++++ package.json | 5 ++++- scripts/pre-commit | 27 +++++++++++++++++++++++++++ scripts/validate | 21 +++++++++++++++++++-- 5 files changed, 81 insertions(+), 3 deletions(-) create mode 100755 scripts/pre-commit diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 2aa7c5f7..43cbbdf4 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -15,12 +15,33 @@ jobs: uses: actions/setup-node@main with: node-version: '15.x' + - name: Set up Python 3.9 + uses: actions/setup-python@v1 + with: + python-version: 3.9 + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install PyYAML jsonschema + - name: Validate yml files + run: | + python ./scripts/validate - name: Assemble yml files run: | mkdir -p public/conference && cd public/conference awk 1 `find ../../conference -name '*.yml' -not -path '**/types.yml'` > allconf.yml cp ../../conference/types.yml . cd ../.. + - name: Get yarn cache directory path + id: yarn-cache-dir-path + run: echo "::set-output name=dir::$(yarn cache dir)" + - uses: actions/cache@v2 + id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`) + with: + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- - name: Install dependency and build run: | yarn install diff --git a/.github/workflows/merge_check.yml b/.github/workflows/merge_check.yml index ca485389..ba2c839d 100644 --- a/.github/workflows/merge_check.yml +++ b/.github/workflows/merge_check.yml @@ -32,6 +32,16 @@ jobs: cat `find ../../conference -name '*.yml' -not -path '**/types.yml'` > allconf.yml cp ../../conference/types.yml . cd ../.. + - name: Get yarn cache directory path + id: yarn-cache-dir-path + run: echo "::set-output name=dir::$(yarn cache dir)" + - uses: actions/cache@v2 + id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`) + with: + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- - name: Install dependency and build run: | yarn install diff --git a/package.json b/package.json index f303e2f7..9a4c08ef 100644 --- a/package.json +++ b/package.json @@ -45,5 +45,8 @@ "> 1%", "last 2 versions", "not dead" - ] + ], + "gitHooks": { + "pre-commit": "scripts/pre-commit" + } } diff --git a/scripts/pre-commit b/scripts/pre-commit new file mode 100755 index 00000000..855d2b48 --- /dev/null +++ b/scripts/pre-commit @@ -0,0 +1,27 @@ +#!/bin/bash + +command_exists () { + command -v "$1" >/dev/null 2>&1 +} + + +python_module_exists () { + /usr/bin/env python -c "import $1" >/dev/null 2>&1 +} + +command_exists python || { + echo + echo "No python found, skip pre-commit hook." + echo + exit 0 +} + +python_module_exists yaml && python_module_exists jsonschema || { + echo + echo "To run pre-commit hook, please install the following modules." + echo "\$ pip install PyYAML jsonschema" + echo + exit 0 +} + +exec "`dirname -- "$0"`/validate" $@ diff --git a/scripts/validate b/scripts/validate index 74fc7ec9..1428dc4b 100755 --- a/scripts/validate +++ b/scripts/validate @@ -1,12 +1,20 @@ #!/usr/bin/env python +""" +pre-commit hook to check conference yml files + +To use it normally as git pre-commit hook, +make a symlink and install dependencies: +$ ln -s ../../scripts/validate .git/hooks/pre-commit +$ pip install PyYAML jsonschema +""" + import os import sys from io import StringIO from unittest import TestCase, TestLoader, TextTestRunner - import jsonschema import jsonschema.exceptions @@ -55,7 +63,16 @@ def run_test(testcase, msg): sys.exit(1) +def usage(): + print(__doc__) + + if __name__ == '__main__': + + if '-h' in sys.argv or '--help' in sys.argv: + usage() + sys.exit(0) + load_conference_yaml_schema() run_test(ConferenceTest, - msg=('\033[1;31mThere are {0} error(s) inside repo. Please fix the errors and commit again\033[m.')) + msg=('\033[1;31mThere are {0} error(s) inside repo. Please fix the errors and commit again.\033[m'))