Skip to content

Commit

Permalink
[qa] Add pre-push hook #161
Browse files Browse the repository at this point in the history
Closes #161
  • Loading branch information
purhan authored and pandafy committed Feb 18, 2022
1 parent 55c2eaa commit dd18840
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,12 @@ Install test requirements:
pip install -r requirements-test.txt
Set up the pre-push hook to run tests and QA checks automatically right before the git push action, so that if anything fails the push operation will be aborted:

.. code-block:: shell
openwisp-pre-push-hook --install
Create database:

.. code-block:: shell
Expand Down
82 changes: 82 additions & 0 deletions openwisp-pre-push-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python

import argparse
import json
import os
import subprocess
import sys


def get_top_level_dir():
top_level_dir = subprocess.Popen(
['git', 'rev-parse', '--show-toplevel'], stdout=subprocess.PIPE
).communicate()[0].decode('utf-8').strip()
return top_level_dir

def make_executable(f):
task = subprocess.Popen(
['chmod', '+x', f], stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
err = task.communicate()[1]
return err

def run_checks():
print("Initializing tests before pushing...")
try:
import openwisp_utils
except:
raise ImportError(
"'openwisp_utils' failed to import. Make sure all dependencies "
"are installed and virtual environment is activated."
)

# Trigger checks and report if they failed
try:
task = subprocess.Popen([f'{REPO_ROOT_DIR}/run-qa-checks', '--pre-push'])
task.communicate()
if task.returncode:
print("---------------------------")
print(
"Some checks failed. Please make sure they pass, "
"or use '--no-verify' to skip them"
)
sys.exit(1)
except FileNotFoundError:
print("Could not access 'run-qa-checks'")


def install_hook():
hook_file = os.path.join(HOOKS_DIR, 'pre-push')
symlink = os.path.islink(hook_file)
if symlink and os.path.exists(hook_file):
print('Symlink already exists')
else:
if symlink:
os.unlink(hook_file)
os.symlink(os.path.abspath(__file__), hook_file)
print("Symlink created, 'pre-push-hook' has been set up!")

# Make the hook file executable
err = make_executable(hook_file)
if err:
raise ValueError(err)


def main(args=None):
global REPO_ROOT_DIR, HOOKS_DIR
REPO_ROOT_DIR = get_top_level_dir()
HOOKS_DIR = os.path.join(REPO_ROOT_DIR, '.git', 'hooks')
parser = argparse.ArgumentParser()
parser.add_argument('remote', nargs='?', help='provided by git before push')
parser.add_argument('url', nargs='?', help='provided by git before push')
parser.add_argument(
'--install', action='store_true', default=False,
)
args = parser.parse_args(args=args)
if args.install:
return install_hook()
run_checks()


if __name__ == '__main__':
main()
39 changes: 38 additions & 1 deletion run-qa-checks
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
#!/bin/bash
set -e
openwisp-qa-check --migration-path "./tests/test_project/migrations"

qa-checks () {
openwisp-qa-check --migration-path "./tests/test_project/migrations"
}

pre-push-checks () {
checks=(
"./runtests.py --parallel"
)
for (( i = 0; i < ${#checks[@]} ; i++ )); do
eval "${checks[$i]}" || { echo "'${checks[$i]}' failed!"; exit 1; }
done
}

# If no arguments given, only run QA checks
if [[ $1 == "" ]]
then
qa-checks
exit 0
fi

# Parse arguments and run relevant checks
for i in "$@"
do
case $i in

# Used by openwisp-pre-push-hook
--pre-push)
qa-checks || { echo "qa-checks failed!"; exit 1; }
pre-push-checks
;;

# Any other argument is ignored
*)
qa-checks
;;
esac
done
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
'checkrst = openwisp_utils.qa:check_rst_files',
]
},
scripts=['openwisp-qa-check', 'openwisp-qa-format'],
scripts=['openwisp-qa-check', 'openwisp-qa-format', 'openwisp-pre-push-hook'],
zip_safe=False,
install_requires=['django-model-utils>=4.0.0,<4.1.0'],
extras_require={
Expand Down

0 comments on commit dd18840

Please sign in to comment.