Skip to content

Commit

Permalink
ci: add pre-commit-hook
Browse files Browse the repository at this point in the history
  • Loading branch information
cubercsl committed Dec 17, 2021
1 parent 9e818de commit ac0be98
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions .github/workflows/merge_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@
"> 1%",
"last 2 versions",
"not dead"
]
],
"gitHooks": {
"pre-commit": "scripts/pre-commit"
}
}
27 changes: 27 additions & 0 deletions scripts/pre-commit
Original file line number Diff line number Diff line change
@@ -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" $@
21 changes: 19 additions & 2 deletions scripts/validate
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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'))

0 comments on commit ac0be98

Please sign in to comment.