Initial commit for domains variables
authoreyeokg <k.galczynski@eyeo.com>
Tue, 01 Oct 2024 08:02:53 +0200
changeset 25704 4c03dcd1e5aa
parent 25411 be3bc2647069
child 25705 d7bf4e5f34aa
Initial commit for domains variables
.gitignore
README.md
exceptionrules/domains-variables.json
pre-commit
new file mode 100644
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,1 @@
+pre-commit
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/README.md
@@ -0,0 +1,9 @@
+# Exceptionrules
+
+This is a project which contains filter rules in order for Acceptable Ads program to work
+
+***IMPORTANT:*** After cloning the project, you need to run the following command in order to add the pre-commit hook:
+
+```sh
+chmod +x pre-commit && mv pre-commit .git/hooks/pre-commit  
+```
new file mode 100644
--- /dev/null
+++ b/exceptionrules/domains-variables.json
@@ -0,0 +1,1 @@
+{}
\ No newline at end of file
new file mode 100755
--- /dev/null
+++ b/pre-commit
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+if ! [ -f "./exceptionrules/domains-variables.json" ]; then
+    echo "File does not exist"
+    exit 1
+fi
+staged_domains_variables=$(git show :./exceptionrules/domains-variables.json 2>/dev/null)
+
+if ! [ -n "$staged_domains_variables" ]; then
+    echo "No changes in domains variables"
+    exit 0
+fi
+
+if ! jq -e . >/dev/null 2>&1 <<<"$staged_domains_variables"; then
+    echo "Error: Invalid JSON content"
+    exit 1
+fi
+
+object=$(jq '.' <<< "$staged_domains_variables")
+keys=$(jq 'keys' <<< "$object")
+
+for key in $(jq -r '.[]' <<< "$keys"); do
+    if ! [[ "$key" =~ ^[[:alnum:]_]+$ ]]; then
+        echo "invalid key: $key, only alphanumeric characters and underscores are allowed"
+        invalid=true
+    fi
+    elements=$(jq -r ".[\"$key\"][]" <<< "$object")
+    for element in $elements; do
+        if ! [[ "$element" =~ ^[[:alnum:]*][[:alnum:]*.-]+$ ]]; then
+            echo "invalid domain in $key: $element"
+            invalid=true
+        fi
+    done
+done
+
+if [ "$invalid" = true ]; then
+    exit 1
+fi
+
+exit 0