From 6ff7e3001c9be4cd24f420484ed6206b05950cc8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Tue, 10 Dec 2019 14:46:49 +0100
Subject: [PATCH] build: check config.json validity during build (#38354)

---
 create_themes_json.py                      | 36 ++++++++++++++++++++++
 static/alpes-maritimes-2018/config.json    |  4 +--
 static/bouches-du-rhone/config.json        |  2 +-
 static/entrouvert/config.json              |  2 +-
 static/grenoble-metropole-2019/config.json |  2 +-
 static/quimper/config.json                 |  2 +-
 static/toulouse-metropole/config.json      |  2 +-
 7 files changed, 43 insertions(+), 7 deletions(-)

diff --git a/create_themes_json.py b/create_themes_json.py
index f0332d5d..4143cb92 100644
--- a/create_themes_json.py
+++ b/create_themes_json.py
@@ -3,6 +3,8 @@
 import argparse
 import json
 import os
+import re
+import sys
 import xml.etree.ElementTree as ET
 
 parser = argparse.ArgumentParser()
@@ -10,6 +12,7 @@ parser.add_argument('--overlay', dest='overlay', type=str)
 args = parser.parse_args()
 
 # get themes
+errors = False
 themes = []
 for dirname in sorted(os.listdir('static')):
     config = os.path.join('static', dirname, 'config.json')
@@ -28,8 +31,41 @@ for dirname in sorted(os.listdir('static')):
     if args.overlay:
         theme['module'] = 'publik-base'
         theme['overlay'] = args.overlay
+
+    # validation checks
+    theme_color = theme['variables'].get('theme_color')
+    if theme_color and not re.match(r'#[0-9a-fA-F]{6}$', theme_color):
+        print('E: invalid theme color for %s (%s)' % (dirname, theme_color), file=sys.stderr)
+        errors = True
+    if theme.get('settings'):
+        for settings_app in theme['settings']:
+            if settings_app not in ('authentic2', 'combo', 'fargo', 'wcs'):
+                print('E: unknown application key for %s (%s)' % (dirname, settings_app), file=sys.stderr)
+                errors = True
+                continue
+            if settings_app == 'combo':
+                for settings_key in theme['settings'][settings_app].keys():
+                    if settings_key not in (
+                         'COMBO_ASSET_SLOTS.update',
+                         'COMBO_MAP_ATTRIBUTION',
+                         'COMBO_MAP_DEFAULT_POSITION',
+                         'COMBO_MAP_MAX_BOUNDS',
+                         'COMBO_MAP_TILE_URLTEMPLATE',
+                         'COMBO_PUBLIC_TEMPLATES.update',
+                         'JSON_CELL_TYPES.update',
+                         'PWA_NOTIFICATION_BADGE_URL',
+                         'PWA_NOTIFICATION_ICON_URL',
+                         'WCS_CATEGORY_ASSET_SLOTS',
+                         'WCS_FORM_ASSET_SLOTS',
+                         ):
+                        print('E: unknown settings key for %s (%s)' % (dirname, settings_key), file=sys.stderr)
+                        errors = True
+
     themes.append(theme)
 
+if errors:
+    sys.exit(1)
+
 # get parameters
 parameters = {'primary-color': {'type': 'color'}}
 if os.path.exists('help/fr/misc-scss.page'):
diff --git a/static/alpes-maritimes-2018/config.json b/static/alpes-maritimes-2018/config.json
index 184a10c9..3b98af51 100644
--- a/static/alpes-maritimes-2018/config.json
+++ b/static/alpes-maritimes-2018/config.json
@@ -2,7 +2,7 @@
   "label": "Alpes-Maritimes 2018",
   "settings": {
       "combo": {
-          "COMBO_ASSET_SLOTS": {
+          "COMBO_ASSET_SLOTS.update": {
               "social:dossiers:autonomie": {
                   "label": "Pictogramme dossier autonomie"
               },
@@ -10,7 +10,7 @@
                   "label": "Pictogramme dossier handicap"
               }
           },
-          "JSON_CELL_TYPES": {
+          "JSON_CELL_TYPES.update": {
               "desappairage": {
                   "form": [
                     {
diff --git a/static/bouches-du-rhone/config.json b/static/bouches-du-rhone/config.json
index ecfbef43..0092d84e 100644
--- a/static/bouches-du-rhone/config.json
+++ b/static/bouches-du-rhone/config.json
@@ -7,7 +7,7 @@
   },
   "settings": {
       "combo": {
-          "JSON_CELL_TYPES": {
+          "JSON_CELL_TYPES.update": {
               "cd13_liste_des_dossiers": {
                   "cache_duration": 10,
                   "name": "Liste des dossiers",
diff --git a/static/entrouvert/config.json b/static/entrouvert/config.json
index ea498cd2..5805338e 100644
--- a/static/entrouvert/config.json
+++ b/static/entrouvert/config.json
@@ -6,7 +6,7 @@
   },
   "settings": {
     "combo": {
-      "JSON_CELL_TYPES": {
+      "JSON_CELL_TYPES.update": {
         "references": {
           "name": "References",
           "url": "https://gestion.entrouvert.org/api/references/",
diff --git a/static/grenoble-metropole-2019/config.json b/static/grenoble-metropole-2019/config.json
index ba761f68..b3b896f7 100644
--- a/static/grenoble-metropole-2019/config.json
+++ b/static/grenoble-metropole-2019/config.json
@@ -17,7 +17,7 @@
               "corner1": {"lat": "45.2307", "lng": "5.6640"},
               "corner2": {"lat": "45.1179", "lng": "5.7980"}
           },
-          "JSON_CELL_TYPES": {
+          "JSON_CELL_TYPES.update": {
               "user": {
                   "cache_duration": 60,
                   "name": "Cellule usager",
diff --git a/static/quimper/config.json b/static/quimper/config.json
index 7b6c5461..34d87641 100644
--- a/static/quimper/config.json
+++ b/static/quimper/config.json
@@ -22,7 +22,7 @@
       },
       "COMBO_MAP_TILE_URLTEMPLATE": "https://tiles.entrouvert.org/hdm/{z}/{x}/{y}.png",
       "COMBO_MAP_ATTRIBUTION": "&copy; <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap</a>",
-      "JSON_CELL_TYPES": {
+      "JSON_CELL_TYPES.update": {
         "parkings": {
           "url": "https://places-parkings.quimper-communaute.bzh/WebServiceInfosGeolocFront.asmx/GetGeoJsonListDysplayItemByType?type=parkings",
           "name": "Parkings",
diff --git a/static/toulouse-metropole/config.json b/static/toulouse-metropole/config.json
index 4635541c..51030775 100644
--- a/static/toulouse-metropole/config.json
+++ b/static/toulouse-metropole/config.json
@@ -7,7 +7,7 @@
   },
   "settings": {
     "combo": {
-      "JSON_CELL_TYPES": {
+      "JSON_CELL_TYPES.update": {
         "communes-toulouse": {
           "url": "https://data.toulouse-metropole.fr/api/records/1.0/search/?dataset=communes-de-toulouse-metropole&sort=ville&rows=100",
           "name": "Communes de Toulouse",
-- 
2.24.0

