Skip to content

Commit

Permalink
feat: add workflow to check version consistency (#8)
Browse files Browse the repository at this point in the history
* feat: add workflow to check version consistency

* fix: remove dependencies and change 'on'

* fix: fix example

* fix: reconcile versions across schema

* refactor: sync language and 'on' of both workflows

* refactor: final cleanup

* refactor: adapt feedback

* fix: typo

* docs: comment code

* feat: handle datapackages

* fix: fix pattern and checks

* refactor: apply black and upgrade yml
  • Loading branch information
Pierlou authored Apr 4, 2024
1 parent 3e3fdb2 commit f8e6300
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 7 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/assert_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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")

for schema_path in to_check:
with open(schema_path, "r") as f:
schema = json.load(f)
version = schema["version"]

errors = check(schema, version)
if errors:
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]}'"
raise Exception(message)
20 changes: 20 additions & 0 deletions .github/workflows/assert_version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.10"

- run: python .github/workflows/assert_version.py

File renamed without changes.
6 changes: 3 additions & 3 deletions exemple-valide.csv
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
id,date_creation,nom,classification
42,2020-02-27,Brouette,"[""Classification A""]"
44,2020-02-25,Pelle,"[""Classification A"",""Classification B"",""Classification C""]"
45,2020-02-25,Bus,"[""Classification A"",""Classification C""]"
42,2020-02-27,Brouette,Classification A
44,2020-02-25,Pelle,Classification C
45,2020-02-25,Bus,Classification A
8 changes: 4 additions & 4 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
],
"countryCode":"FR",
"homepage":"https://github.com/mon-orga/schema-objets-importants",
"path":"https://github.com/mon-orga/schema-objets-importants/raw/v0.1.1/schema.json",
"image":"https://github.com/mon-orga/schema-objets-importants/raw/v0.1.1/irve.png",
"path":"https://github.com/mon-orga/schema-objets-importants/raw/v0.1.0/schema.json",
"image":"https://github.com/mon-orga/schema-objets-importants/raw/v0.1.0/irve.png",
"licenses":[
{
"title":"Etalab Licence Ouverte 2.0",
Expand All @@ -22,7 +22,7 @@
{
"title":"Fichier valide (CSV)",
"name":"exemple-valide-csv",
"path":"https://github.com/mon-orga/schema-objets-importants/raw/v0.1.1/exemple-valide.csv"
"path":"https://github.com/mon-orga/schema-objets-importants/raw/v0.1.0/exemple-valide.csv"
}
],
"sources":[
Expand All @@ -33,7 +33,7 @@
],
"created":"2018-06-29",
"lastModified":"2019-05-06",
"version":"0.1.1",
"version":"v0.1.0",
"contributors":[
{
"title":"John Smith",
Expand Down

0 comments on commit f8e6300

Please sign in to comment.