Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[qa] Add pre-push hook #161 #162

Merged
merged 1 commit into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,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