From da05262af4a987812b529a963f9c5e33d293e664 Mon Sep 17 00:00:00 2001 From: DTLP Date: Wed, 4 Oct 2023 09:48:57 +0100 Subject: [PATCH] Add yaml format checking with yq Use yq to verify yaml files instead of relying solely on file format --- .github/workflows/pre-commit-hook.yaml | 2 ++ scripts/pre-commit | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pre-commit-hook.yaml b/.github/workflows/pre-commit-hook.yaml index 98a7c77..27071d5 100644 --- a/.github/workflows/pre-commit-hook.yaml +++ b/.github/workflows/pre-commit-hook.yaml @@ -20,5 +20,7 @@ jobs: - name: Install packages run: | sudo apt-get -y install git + - name: Set up yq + uses: frenck/action-setup-yq@v1 - name: Run pre-commit checks script run: make hooks-pre-commit-run diff --git a/scripts/pre-commit b/scripts/pre-commit index 60e699e..151c95d 100755 --- a/scripts/pre-commit +++ b/scripts/pre-commit @@ -15,7 +15,7 @@ fi PROM_VERSION="v2.28.1" -function is_yaml() { +function is_yaml_extension() { local file=$1 if [[ "${file}" == *yaml || "${file}" == *yml || "${file}" == *yaml.tmpl || "${file}" == *yml.tmpl ]]; then return 0 @@ -24,12 +24,24 @@ function is_yaml() { return 1 } +function is_valid_yaml() { + local file=$1 + if is_yaml_extension "${file}"; then + if yq -v "${file}" ; then + return 0 + fi + exit_code=1 + fi + + return 1 +} + # Check alerts function check_alerts() { local files=$(${GIT_CMD}) for f in ${files} do - if is_yaml "${f}"; then + if is_valid_yaml "${f}"; then if [[ $(head -1 "${f}") =~ "PROMETHEUS RULES" ]]; then docker run -i --entrypoint promtool -v $PWD/${f}:$PWD/${f} docker.io/prom/prometheus:${PROM_VERSION} check rules $PWD/${f} &> /dev/null if [ $? -ne 0 ]; then