Skip to content

Commit

Permalink
Update pre-commit script to accept array 'test_plan_uri' + refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
PMartins03 committed Oct 2, 2023
1 parent 6f7c6ac commit cbbe97d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 38 deletions.
53 changes: 34 additions & 19 deletions scripts/check-file-names.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import argparse
from os import listdir
import os
import re


def main(argv = None):
def parse_args():
parser = argparse.ArgumentParser(
description="Checks if the file names are correct"
)
Expand All @@ -12,30 +12,45 @@ def main(argv = None):
nargs="+",
help="Every api to be checked by the script and their respective versions separated by a '_'. Examples: business_1.3, personal_1.0, consents_2.2."
)
args = parser.parse_args(argv)
return parser.parse_args()


def is_valid_filename(filename, api, version):
regex_pattern = r"^\d{8}_.+_(?P<api>[A-Za-z-]+)_v[12](.[0-9])?(?P<appends>(-[A-Z]{2,4})*)_(0[1-9]|[12]\d|3[01])-(0[1-9]|1[012])-(20\d\d)\."

if version == '1.0':
regex_pattern += r"(zip|ZIP)$"
else:
regex_pattern += r"json$"

pattern = re.compile(regex_pattern)
m = pattern.match(filename)

api_list = [api.split('_') for api in args.apis]
directories = [f"./submissions/functional/{api}/{version}.0" for api, version in api_list]
return (m is None or m.group('api') != api or len(filename.split('_')) != 5) and filename != ".DS_Store" and filename != "readme.md"


def check_filenames(apis):
wrong_files = []
for (api, version), directory in zip(api_list, directories):
regex_pattern = r"^\d{8}_.+_(?P<api>[A-Za-z-]+)_v[12](.[0-9])?(?P<appends>(-[A-Z]{2,4})*)_(0[1-9]|[12]\d|3[01])-(0[1-9]|1[012])-(20\d\d)\."
if version == '1.0':
regex_pattern += r"(zip|ZIP)$"
else:
regex_pattern += r"json$"

pattern = re.compile(regex_pattern)

for file in listdir(directory):
m = pattern.match(file)
if (m is None or m.group('api') != api or len(file.split('_')) != 5) and file != ".DS_Store" and file != "readme.md":

for api in apis:
api_name, version = api.split('_')
directory = f"./submissions/functional/{api_name}/{version}.0"

for file in os.listdir(directory):
if is_valid_filename(file, api_name, version):
wrong_files.append(file)

if len(wrong_files):
return wrong_files


def main():
args = parse_args()
wrong_files = check_filenames(args.apis)

if wrong_files:
print("The following file names are wrong: " + str(wrong_files))
return 1

return 0


Expand Down
58 changes: 39 additions & 19 deletions scripts/check-json-files.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,49 @@


def validate_json(json_obj, api_version_list):
if len(json_obj) != 4:
return False, 'json contains more keys than expected'
if 'api' not in json_obj or json_obj['api'] not in api_version_list:
return False, 'api field has an invalid value'
if 'sd' not in json_obj or not re.match(r'^\d+$', json_obj['sd']):
return False, 'sd field has an invalid value'
if 'docusign_id' not in json_obj or not re.match(r'^[A-Za-z\d]{8}-[A-Za-z\d]{4}-[A-Za-z\d]{4}-[A-Za-z\d]{4}-[A-Za-z\d]{12}$', json_obj['docusign_id']):
return False, 'docusign_id field has an invalid value'
if 'test_plan_uri' not in json_obj or not re.match(r'^https://web\.conformance\.directory\.opinbrasil\.com\.br/plan-detail\.html\?.*$', json_obj['test_plan_uri']):
return False, 'test_plan_uri field has an invalid value'
validation_rules = [
(len(json_obj) == 4, 'json contains more keys than expected'),
('api' in json_obj and json_obj['api'] in api_version_list, 'api field has an invalid value'),
('sd' in json_obj and re.match(r'^\d+$', json_obj['sd']), 'sd field has an invalid value'),
('docusign_id' in json_obj and re.match(r'^[A-Za-z\d]{8}-[A-Za-z\d]{4}-[A-Za-z\d]{4}-[A-Za-z\d]{4}-[A-Za-z\d]{12}$', json_obj['docusign_id']), 'docusign_id field has an invalid value'),
('test_plan_uri' in json_obj and (is_valid_test_plan_uri(json_obj['test_plan_uri'])), 'test_plan_uri field has an invalid value')
]

for rule, message in validation_rules:
if not rule:
return False, message

return True, 'approved'


def main(argv = None):
def is_valid_test_plan_uri(test_plan_uri):
if isinstance(test_plan_uri, str):
return bool(re.match(r'^https://web\.conformance\.directory\.opinbrasil\.com\.br/plan-detail\.html\?.*$', test_plan_uri))
elif isinstance(test_plan_uri, list):
return all(re.match(r'^https://web\.conformance\.directory\.opinbrasil\.com\.br/plan-detail\.html\?.*$', uri) for uri in test_plan_uri)
else:
return False


def parse_args():
parser = argparse.ArgumentParser(
description="Checks if the json files are correct"
description="Checks if the JSON files are correct"
)
parser.add_argument(
"apis",
nargs="+",
help="Every api to be checked by the script and their respective versions separated by a '_'. Examples: business_1.3, personal_1.0, consents_2.2."
help="Every API to be checked by the script and their respective versions separated by a '_'. Examples: business_1.3, personal_1.0, consents_2.2."
)
args = parser.parse_args(argv)
return parser.parse_args()


api_list = [api.split('_') for api in args.apis]
def check_json_files(apis):
api_list = [api.split('_') for api in apis]
api_version_list = [api + '_v' + version for api, version in api_list]
directories = [f"./submissions/functional/{api}/{version}.0" for api, version in api_list]

wrong_files = []

for directory in directories:
for filename in os.listdir(directory):
if filename.endswith(".json"):
Expand All @@ -44,10 +58,16 @@ def main(argv = None):
if not approved:
wrong_files.append((filename, message))

if len(wrong_files):
print('The following files contain invalid json objects:')
for file in wrong_files:
filename, message = file
return wrong_files


def main():
args = parse_args()
wrong_files = check_json_files(args.apis)

if wrong_files:
print('The following files contain invalid JSON objects:')
for filename, message in wrong_files:
print(f'{filename} ({message})')
return 1

Expand Down

0 comments on commit cbbe97d

Please sign in to comment.