-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ct-logs-main' of github.com:aau-network-security/richki…
…t into ct-logs-main
- Loading branch information
Showing
28 changed files
with
635 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.pytest_cache | ||
.githooks | ||
.docs | ||
.github/logo | ||
.github/workflows | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import argparse | ||
import re | ||
import sys | ||
|
||
|
||
def check(name): | ||
"""Check a git branch name against gitflow naming conventions. | ||
This is most likely the function you are looking for. | ||
""" | ||
if name in ( # First level only branches | ||
'master', | ||
'develop', | ||
): | ||
return True | ||
elif len(name.split('/')) == 2: | ||
# some have two levels separated by / | ||
return checkSecondLevel(name) | ||
else: | ||
# Default | ||
print(f'Error: Did not recognise "{name}" as a valid branch.') | ||
return False | ||
|
||
|
||
def checkLen(string, min_len, max_len): | ||
if len(string) < min_len: | ||
print( | ||
f'Error: {string} is too short' | ||
f' (it is {len(string)}, minimum is {min_len})' | ||
) | ||
return False | ||
if len(string) > max_len: | ||
print( | ||
f'Error: {string} is too long' | ||
f' (it is {len(string)}, maximum is {max_len})' | ||
) | ||
return False | ||
else: | ||
return True | ||
|
||
|
||
def checkSecondLevel(name): | ||
"""Checks the name to be a valid gitflow branch name containing a `/`. | ||
This is intended for internal use, and asumes a single `/` to be | ||
present in `name`. | ||
""" | ||
category, label = name.split('/') | ||
|
||
if category in ( # valid categories | ||
'feature', | ||
'hotfix', | ||
): | ||
return checkLabel(label) | ||
elif category in ( # Not currently validating release branch names | ||
'release', | ||
): | ||
return True | ||
else: | ||
print(f'Error: Did not recognise "{category}" as a valid category') | ||
return False | ||
|
||
|
||
def checkLabel(label): | ||
"""Checks the label to have a description of one or more words | ||
(lowercase alphanumerics), joined by a dash (`-`), followed by an | ||
issue reference. | ||
Example: word-and-numb3r-#1 | ||
""" | ||
# Description | ||
desc_re = r'(?P<description>[a-z0-9]+(?:-[a-z0-9]+)*)' # one or more words | ||
desc_re = r'^' + desc_re # must be at begining | ||
m = re.search(desc_re, label) | ||
if not m: | ||
print( | ||
f'Error: No valid description in "{label}"' | ||
f' (Expected it to start with lowercase alphanumeric and dashes' | ||
f' like this: ex4mple-description)' | ||
) | ||
return False | ||
|
||
if not checkLen(m.groupdict()['description'], 10, 25): | ||
return False | ||
|
||
# Issue reference | ||
issue_re = r'(?P<issue>#[0-9]+)' # hashtag and integer | ||
issue_re = issue_re + r'$' # must be at end | ||
if not re.search(issue_re, label): | ||
print( | ||
f'Error: No issue reference in "{label}"' | ||
f' (Expected it to in like this: ...-#1)' | ||
) | ||
return False | ||
|
||
# Dash seperator | ||
label_re = desc_re + r'-' + issue_re | ||
if not re.search(label_re, label): | ||
print( | ||
f'Error: Missing dash between description and issue reference ' | ||
f' in "{label}"' | ||
) | ||
return False | ||
|
||
return True # no problems found | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
parser = argparse.ArgumentParser( | ||
description='Validate branch name according to gitflow', | ||
) | ||
parser.add_argument( | ||
'-t', '--test', dest='test', action='store_const', | ||
const=True, default=False, | ||
help='Run the built in tests and exit', | ||
) | ||
parser.add_argument( | ||
'name', metavar='NAME', type=str, | ||
help='The branch name to check' | ||
) | ||
args = parser.parse_args() | ||
|
||
if not args.test: | ||
success = check(args.name) | ||
sys.exit(not success) | ||
|
||
print('Starting built-in self-testing') | ||
print('Expect error messages, but not AssertionError\'s') | ||
assert check('master') | ||
assert check('develop') | ||
assert not check('random') # no custom at top level | ||
assert not check('alkshjdg') # no custom at top level | ||
assert not check('master/asdasdasdasdasdasd') # nothing below master | ||
assert not check('develop/asdasdasdasdasdas') # nothing below develop | ||
assert check('feature/some-feature-#9') # good | ||
assert not check('feature/2-shrt-fe#1') # too short | ||
assert not check('feature/very-long-description-here-#1') # too long | ||
print('Done - either all tests passed or you disable `assert`') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env bash | ||
|
||
## Would be nice to have linting before commit | ||
if ! [ -x "$(command -v autopep8)" ] || [ "$(pip3 list | | ||
cut -d " " -f 1 | | ||
grep -xF "$package_name" | grep autopep8)" != "autopep8" ] | ||
then | ||
echo 'autopep8 is NOT installed, linting test may fail on CI ... ' | ||
echo 'consider to install autopep8, you may use following commands: ' | ||
echo 'Debian: [ sudo apt-get install -y python-autopep8 ] ' | ||
echo 'MacOS: [ brew install autopep8 ]' | ||
echo 'You may consider to install it into virtual environment of your project:' | ||
echo 'source venv/bin/activate' | ||
echo 'pip3 install autopep8' | ||
echo 'autopep8 should be available in your system, to do not face with linting problem.' | ||
exit 1 | ||
else | ||
echo 'Linting...' | ||
echo 'Going to root directory of the project' | ||
cd ../richkit | ||
autopep8 --in-place --recursive --max-line-length=100 --exclude docs/source/conf.py,venv,__pycache__,old,build,dist . | ||
fi | ||
|
||
python3 .githooks/check-branch-name.py "$(git rev-parse --abbrev-ref HEAD)" | ||
exit $? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env sh | ||
|
||
NC='\033[0m' | ||
RED='\033[0;31m' | ||
ORANGE='\033[0;33m' | ||
GREEN='\033[0;32m' | ||
|
||
if [ "$MAXMIND_LICENSE_KEY" = "" ] ; then | ||
echo "${ORANGE} Warning: Environment variable for MAXMINDDB could not be found, proceeding without it, check README file " | ||
fi | ||
# change directory to /richkit | ||
|
||
cd /richkit | ||
|
||
echo "${GREEN}1. Checking flake8 linting ... " | ||
# test that number of violations does not increase | ||
FLAKE8_ERROR_CNT=$(flake8 . -qq --count --exit-zero --max-complexity=10 --max-line-length=127 --exclude venv,__pycache__,docs/source/conf.py,old,build,dist) | ||
FLAKE8_ERROR_LIMIT=25 | ||
if [ "$FLAKE8_ERROR_CNT" -gt "$FLAKE8_ERROR_LIMIT" ] ; then | ||
echo "${RED}Failed because the number of errors from flake8 increased (This: $FLAKE8_ERROR_CNT Previously: $FLAKE8_ERROR_LIMIT)" 1>&2 | ||
false | ||
exit 1 | ||
fi | ||
echo "${ORANGE}Number of validation errors from flake8 is: $FLAKE8_ERROR_CNT (Limit is: $FLAKE8_ERROR_LIMIT)" | ||
|
||
|
||
echo "${GREEN}2. Testing module .... " | ||
echo "${NC}" | ||
coverage run --source=richkit -m pytest -Werror /richkit/richkit | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
FROM ubuntu | ||
|
||
# provide environment variable as MAXMIND_LICENSE_KEY | ||
# when you run docker image see readme | ||
|
||
|
||
RUN apt-get update && apt-get install -y python3 python3-pip | ||
|
||
COPY requirements.txt /richkit/requirements.txt | ||
|
||
COPY richkit /richkit/richkit | ||
|
||
|
||
RUN pip3 install -r /richkit/requirements.txt | ||
|
||
RUN pip3 install coverage pytest sphinx flake8 | ||
|
||
COPY .github/local-test/run-test.sh /richkit/richkit/run-test.sh | ||
|
||
CMD ["/richkit/richkit/run-test.sh"] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.