From 9321011519eb9e4202bd8a0444afb46fa892e13f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibaut=20Barr=C3=A8re?= Date: Thu, 4 Apr 2024 10:10:18 +0200 Subject: [PATCH 1/3] Add checks This is a test for: - https://github.com/datagouv/datapackage-template/pull/1 --- .github/workflows/assert_version.py | 73 ++++++++++++++++++++++++++++ .github/workflows/assert_version.yml | 19 ++++++++ 2 files changed, 92 insertions(+) create mode 100644 .github/workflows/assert_version.py create mode 100644 .github/workflows/assert_version.yml diff --git a/.github/workflows/assert_version.py b/.github/workflows/assert_version.py new file mode 100644 index 0000000..79c2281 --- /dev/null +++ b/.github/workflows/assert_version.py @@ -0,0 +1,73 @@ +import json +import re +import os + +pattern = r'v?\d+\.\d+\.\d+' + + +def check(obj, version, parents=''): + """ + This functions recursively parses all fields in the schema looking for + version names that would not be the same as the one mentionned + in the 'version' field + """ + errors = [] + # if field is a string, we check for a potential version + if isinstance(obj, str): + tmp = re.search(pattern, obj) + if tmp and tmp[0] != version: + errors += [(parents, tmp[0])] + # if field is a list, we check every item + elif isinstance(obj, list): + for idx, k in enumerate(obj): + errors += check( + k, + version, + parents=parents + f'[{str(idx)}]' + ) + # if field is a dict, we check every value + elif isinstance(obj, dict): + for k in obj: + # not checking the fields + if k != 'fields': + errors += check( + obj[k], + version, + parents=parents + '.' + k if parents else k + ) + return errors + + +to_check = [] + +if 'schema.json' in os.listdir(): + to_check.append('schema.json') + +elif 'datapackage.json' in os.listdir(): + with open('datapackage.json', 'r') as f: + datapackage = json.load(f) + for r in datapackage['resources']: + to_check.append(r['schema']) + +else: + raise Exception('No required file found') + +message = '' +for schema_path in to_check: + with open(schema_path, 'r') as f: + schema = json.load(f) + homepage = schema['homepage'] + version = schema['version'] + + errors = check(schema, version) + if errors: + if message: + message += '\n\n' + message += ( + f"Versions are mismatched within the schema '{schema['name']}', " + f"expected version '{version}' but:" + ) + for e in errors: + message += f"\n- {e[0]} has version '{e[1]}'" +if message: + raise Exception(message) \ No newline at end of file diff --git a/.github/workflows/assert_version.yml b/.github/workflows/assert_version.yml new file mode 100644 index 0000000..ce5b720 --- /dev/null +++ b/.github/workflows/assert_version.yml @@ -0,0 +1,19 @@ +name: Vérification de la cohérence des versions dans tous les champs du schéma + +on: + push: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: 3.8 + + - run: python3 .github/workflows/assert_version.py \ No newline at end of file From 278521e38796db7e228906eb53ac0f419b4c7c2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibaut=20Barr=C3=A8re?= Date: Thu, 4 Apr 2024 10:31:22 +0200 Subject: [PATCH 2/3] Fix check by adding "v" in front of version --- dynamique/schema-dynamique.json | 2 +- statique/schema-statique.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamique/schema-dynamique.json b/dynamique/schema-dynamique.json index 371f18c..8d4c303 100644 --- a/dynamique/schema-dynamique.json +++ b/dynamique/schema-dynamique.json @@ -32,7 +32,7 @@ ], "created": "2022-10-28", "lastModified": "2024-01-16", - "version": "2.3.0", + "version": "v2.3.0", "contributors": [ { "title": "Geoffrey Aldebert", diff --git a/statique/schema-statique.json b/statique/schema-statique.json index d4b4084..9c6c48d 100644 --- a/statique/schema-statique.json +++ b/statique/schema-statique.json @@ -32,7 +32,7 @@ ], "created": "2018-06-29", "lastModified": "2022-10-10", - "version": "2.3.0", + "version": "v2.3.0", "contributors": [ { "title": "Alexandre Bulté", From 7f89269d5836ce244ce522f577beed00cf9a59d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibaut=20Barr=C3=A8re?= Date: Thu, 4 Apr 2024 15:50:40 +0200 Subject: [PATCH 3/3] Backport final changes --- .github/workflows/assert_version.py | 41 +++++++++++----------------- .github/workflows/assert_version.yml | 8 +++--- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/.github/workflows/assert_version.py b/.github/workflows/assert_version.py index 79c2281..496b15e 100644 --- a/.github/workflows/assert_version.py +++ b/.github/workflows/assert_version.py @@ -2,10 +2,10 @@ import re import os -pattern = r'v?\d+\.\d+\.\d+' +pattern = r"v?\d+\.\d+\.\d+" -def check(obj, version, parents=''): +def check(obj, version, parents=""): """ This functions recursively parses all fields in the schema looking for version names that would not be the same as the one mentionned @@ -20,54 +20,45 @@ def check(obj, version, parents=''): # if field is a list, we check every item elif isinstance(obj, list): for idx, k in enumerate(obj): - errors += check( - k, - version, - parents=parents + f'[{str(idx)}]' - ) + errors += check(k, version, parents=parents + f"[{str(idx)}]") # if field is a dict, we check every value elif isinstance(obj, dict): for k in obj: # not checking the fields - if k != 'fields': + if k != "fields": errors += check( obj[k], version, - parents=parents + '.' + k if parents else k + parents=parents + "." + k if parents else k ) return errors to_check = [] -if 'schema.json' in os.listdir(): - to_check.append('schema.json') +if "schema.json" in os.listdir(): + to_check.append("schema.json") -elif 'datapackage.json' in os.listdir(): - with open('datapackage.json', 'r') as f: +elif "datapackage.json" in os.listdir(): + with open("datapackage.json", "r") as f: datapackage = json.load(f) - for r in datapackage['resources']: - to_check.append(r['schema']) + for r in datapackage["resources"]: + to_check.append(r["schema"]) else: - raise Exception('No required file found') + raise Exception("No required file found") -message = '' for schema_path in to_check: - with open(schema_path, 'r') as f: + with open(schema_path, "r") as f: schema = json.load(f) - homepage = schema['homepage'] - version = schema['version'] + version = schema["version"] errors = check(schema, version) if errors: - if message: - message += '\n\n' - message += ( + message = ( f"Versions are mismatched within the schema '{schema['name']}', " f"expected version '{version}' but:" ) for e in errors: message += f"\n- {e[0]} has version '{e[1]}'" -if message: - raise Exception(message) \ No newline at end of file + raise Exception(message) diff --git a/.github/workflows/assert_version.yml b/.github/workflows/assert_version.yml index ce5b720..2bd821e 100644 --- a/.github/workflows/assert_version.yml +++ b/.github/workflows/assert_version.yml @@ -9,11 +9,11 @@ jobs: steps: - name: Checkout Repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Set up Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: - python-version: 3.8 + python-version: "3.10" - - run: python3 .github/workflows/assert_version.py \ No newline at end of file + - run: python .github/workflows/assert_version.py