diff --git a/.github/workflows/exceptions-json-sort.yml b/.github/workflows/exceptions-json-sort.yml deleted file mode 100644 index 00cbbc2f..00000000 --- a/.github/workflows/exceptions-json-sort.yml +++ /dev/null @@ -1,19 +0,0 @@ -name: Validate Exceptions JSON Sorting - -on: - pull_request: - branches: - - main - paths: - - "pkg/kor/exceptions/**" - -jobs: - sort_json: - runs-on: ubuntu-latest - - steps: - - name: Checkout Repository - uses: actions/checkout@v3 - - - name: Validte Exceptions JSON Sorting - run: make validate-exception-sorting diff --git a/.github/workflows/validate-exception-jsons.yml b/.github/workflows/validate-exception-jsons.yml new file mode 100644 index 00000000..1a7a9650 --- /dev/null +++ b/.github/workflows/validate-exception-jsons.yml @@ -0,0 +1,22 @@ +name: Validate Exception JSONs + +on: + pull_request: + branches: + - main + paths: + - "pkg/kor/exceptions/**" + +jobs: + sort_json: + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Validate Exception JSON Sorting + run: make validate-exception-sorting + + - name: Validate Exception Deduplication + run: make validate-exception-duplications diff --git a/Makefile b/Makefile index 08db44f2..1f40952f 100644 --- a/Makefile +++ b/Makefile @@ -36,4 +36,27 @@ validate-exception-sorting: done; \ if [ "$$PRINT_ERR" = 0 ]; then \ echo "Run the following command to sort all files recursively: make sort-exception-files"; \ - fi; \ \ No newline at end of file + fi; \ + +dedup-exception-files: + @echo "Deduplicating exception files..." + @find $(EXCEPTIONS_DIR) -type f -name '$(EXCEPTIONS_FILE_PATTERN)' -exec sh -c ' \ + jq '\''keys[0] as $$key | { ($$key): (.[$$key] | group_by(.Namespace, .ResourceName) | map(.[0])) }'\'' "$$1" > "$$1.tmp" && mv "$$1.tmp" "$$1" \ + ' sh {} \; + + +validate-exception-duplications: + @PRINT_ERR=1; \ + for file in $(wildcard $(EXCEPTIONS_DIR)/*/$(EXCEPTIONS_FILE_PATTERN)); do \ + DUPLICATES=$$(jq 'keys[0] as $$key | .[$$key] | group_by(.Namespace, .ResourceName) | map(select(length > 1))' "$$file"); \ + if [ "$$DUPLICATES" != "[]" ]; then \ + if [ "$$PRINT_ERR" = 1 ]; then \ + echo "The following JSON files contain duplications:"; \ + PRINT_ERR=0; \ + fi; \ + echo "\t$$file"; \ + fi; \ + done; \ + if [ "$$PRINT_ERR" = 0 ]; then \ + echo "Run the following command to deduplicate all files recursively: make dedup-exception-files"; \ + fi; \