Created
November 19, 2021 13:30
-
-
Save joepvd/9840f0db430670f4e26b37872755ce62 to your computer and use it in GitHub Desktop.
Pre commit hook for ocp-build-data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
# git remote prune origin | |
originating_branch() { | |
local i branch | |
i=0 | |
branch="" | |
while [[ -z "$branch" && "$i" < 100 ]]; do | |
branch=$(git branch -r --list 'origin/*' --contains "HEAD~$i" | awk '{print $1}') | |
i=$(($i+1)) | |
done | |
echo "Comparing to branch $branch" >/dev/stderr | |
echo "$branch" | |
} | |
filter=' | |
{ | |
releases: { | |
($assembly): .releases | to_entries[] | select(.key == $assembly).value | |
} | |
} | |
' | |
single_assembly_yml() { | |
local assembly | |
assembly="$1" | |
shift | |
yq --yaml-output -r --arg assembly "$assembly" "$filter" "$@" | |
} | |
single_assembly_json() { | |
# Why have two? Because jq is much faster than yq, as yq needs top load the python runtime | |
# on each invocation. | |
local assembly | |
assembly="$1" | |
shift | |
jq -r --arg assembly "$assembly" "$filter" "$@" | |
} | |
test_files=() | |
release_yml=0 | |
success=0 | |
originating_branch="$(originating_branch)" | |
changed_files=( $(git diff --name-only --diff-filter=ACMR --cached "$originating_branch") ) | |
for file in "${changed_files[@]}"; do | |
[[ "$file" =~ \.yml ]] || continue | |
[[ "$file" == group.yml || "$file" == bugzilla.yml || "$file" == streams.yml || "$file" == erratatool.yml ]] && continue | |
[[ "$file" == releases.yml ]] && release_yml=1 && continue | |
test_files+=("$file") | |
done | |
releases_yml_checker() { | |
local assembly base_yml | |
(( release_yml )) || return 0 | |
assemblies=() | |
base_json="$(git show "$originating_branch:releases.yml" | yq .)" | |
json="$(yq . releases.yml)" | |
yml="$(<releases.yml)" | |
checked_assemblies=() | |
for assembly in $( jq -r '.releases | to_entries[].key' <<<"$json" ); do | |
echo "Checking if assembly $assembly changed" >/dev/stderr | |
checked_assemblies+=("$assembly") | |
if ! diff -q <( | |
single_assembly_json "$assembly" <<<"$json" | |
) <( | |
single_assembly_json "$assembly" <<<"$base_json" | |
) >/dev/null 2>&1 | |
then | |
assemblies+=("$assembly") | |
fi | |
done | |
(( ${#assemblies[@]} == 0 )) && { | |
echo "No assemblies with diff even if diff. Data model change?">/dev/stderr | |
exit 1 | |
} | |
tmpdir="$(mktemp -d /tmp/ocp-precommit.XXX)" | |
cp group.yml "$tmpdir" | |
trap "rm -r $tmpdir" EXIT | |
for assembly in "${assemblies[@]}"; do | |
echo "Testing assembly $assembly" >/dev/stderr | |
single_assembly_yml "$assembly" <<<"$yml" >"$tmpdir/releases.yml" | |
validate-ocp-build-data "$tmpdir/releases.yml" | |
done | |
} | |
releases_yml_checker || success=1 | |
if (( ${#test_files[@]} > 0 )); then | |
validate-ocp-build-data "${test_files[@]}" || success=1 | |
fi | |
exit "$success" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment