Skip to content

Commit

Permalink
Add a CI check for JSON and YAML files
Browse files Browse the repository at this point in the history
  • Loading branch information
jstourac committed Oct 16, 2023
1 parent 845632f commit 1634ae8
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/code-quality.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
name: Code static analysis
on: [pull_request] # yamllint disable-line rule:truthy

permissions:
contents: read

jobs:
code-static-analysis:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Validate YAML files (best code practices check included)
id: validate-yaml-files
uses: ibiqlik/[email protected]
with:
config_file: ./ci/yamllint-config.yaml

# In some YAML files we use JSON strings, let's check these
- name: Validate JSON strings in YAML files (just syntax)
id: validate-json-strings-in-yaml-files
run: |
type json_verify || sudo apt-get install yajl-tools
bash ./ci/check-json.sh
- name: Validate JSON files (just syntax)
id: validate-json-files
run: |
type json_verify || sudo apt-get install yajl-tools
shopt -s globstar
ret_code=0
echo "-- Checking a regular '*.json' files"
for f in **/*.json; do echo "Checking: '${f}"; echo -n " > "; cat $f | json_verify || ret_code=1; done
echo "-- Checking a 'Pipfile.lock' files"
for f in **/Pipfile.lock; do echo "Checking: '${f}"; echo -n " > "; cat $f | json_verify || ret_code=1; done
echo "-- Checking a '*.ipynb' Jupyter notebook files"
for f in **/*.ipynb; do echo "Checking: '${f}"; echo -n " > "; cat $f | json_verify || ret_code=1; done
if test "${ret_code}" -ne 0; then
echo "There were errors in some of the checked files. Please run `json_verify` on such files and fix issues there."
fi
exit "${ret_code}"
45 changes: 45 additions & 0 deletions ci/check-json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash
#
# This script serves to check YAML files in this repository that contain particular
# key fields where JSON string is expected. Such JSON strings are extracted and
# validated via `json_verify` tool.
#
# Local execution: ./ci/check-json.sh
# Note: please execute from the root directory so that whole dir tree is checked
#
# In case of the PR on GitHub, this check is tied to GitHub actions automatically,
# see `.github/workflows` directory.

shopt -s globstar

function check_json() {
local f="${1}"
local string="${2}"

local ret_code=0

echo "" # Let's make some space from eventual previous file check
echo "Checking: '${f}' - for '${string}':"

if grep --quiet --extended-regexp "${string}" "${f}"; then
#if $(grep -e "${string}" "${f}"); then
jsons=$(yq -r ".spec.tags[].annotations.\"${string}\"" "${f}")

while IFS= read -r json; do
echo " ${json}"
echo -n " > "; echo "${json}" | json_verify || ret_code="${?}"
done <<< "${jsons}"
else
echo " Ignoring as this file doesn't contain necessary key field '${string}' for check"
fi

return "${ret_code}"
}

ret_code=0
for f in **/*.yml **/*.yaml; do
check_json "${f}" "opendatahub.io/notebook-software" || ret_code="${?}"
check_json "${f}" "opendatahub.io/notebook-python-dependencies" || ret_code="${?}"
done

exit "${ret_code}"
8 changes: 8 additions & 0 deletions ci/yamllint-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---

extends: default

rules:
line-length: disable
new-line-at-end-of-file:
level: warning

0 comments on commit 1634ae8

Please sign in to comment.