feat: Add Workflow to Check API Changes in PR #53
Workflow file for this run
This file contains 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
name: "Check API Changes" | |
on: | |
pull_request: | |
branches: [ "main" ] | |
types: | |
- "opened" | |
- "reopened" | |
- "synchronize" | |
- "labeled" | |
- "unlabeled" | |
jobs: | |
check-api-diff: | |
if: ${{ !contains(github.event.pull_request.labels.*.name, 'confirm/api-changes') }} | |
name: Diff CRDs | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout PR ref of lifecycle-manager | |
uses: actions/checkout@v4 | |
- name: Copy PR CRDs to temp dir | |
run: mkdir -p /tmp/pr-crds && cp ./config/crd/bases/* /tmp/pr-crds/ | |
- name: Checkout main of lifecycle-manager | |
uses: actions/checkout@v4 | |
with: | |
ref: main | |
- name: Copy main CRDs to temp dir | |
run: mkdir -p /tmp/main-branch-crds && cp ./config/crd/bases/* /tmp/main-branch-crds/ | |
- name: Compare names and number of CRD files | |
working-directory: /tmp | |
run: | | |
main_files=$(ls ./main-branch-crds) | |
pr_files=$(ls ./pr-crds) | |
if [[ "$pr_files" != "$main_files" ]]; then | |
echo "Number of CRD files do not match" | |
echo "Differences between main-branch-crds and pr-crds:" | |
diff -r main-branch-crds pr-crds || true | |
exit 1 | |
fi | |
echo "Number of CRD files match" | |
- name: Compare names of API versions in CRD files | |
working-directory: /tmp | |
run: | | |
compare_versions() { | |
main_file=$1 | |
pr_file=$2 | |
main_versions=$(yq e '.spec.versions[].name' "$main_file" | sort | tr '\n' ' ') | |
pr_versions=$(yq e '.spec.versions[].name' "$pr_file" | sort | tr '\n' ' ') | |
if [[ "$pr_versions" != "$main_versions" ]]; then | |
echo "API versions do not match in $pr_file" | |
echo "Versions on main: $main_versions" | |
echo "Versions in PR: $pr_versions" | |
exit 1 | |
fi | |
} | |
for file in $(ls ./pr-crds); do | |
compare_versions "./main-branch-crds/$file" "./pr-crds/$file" | |
done | |
echo "Name of API versions match in all CRD files" | |
- name: Compare API schemas in CRD files | |
working-directory: /tmp | |
run: | | |
compare_schemas() { | |
main_file=$1 | |
pr_file=$2 | |
version=$3 | |
main_schema_file="/tmp/main-crds-schema-${version}.yaml" | |
pr_schema_file="/tmp/pr-crds-schema-${version}.yaml" | |
yq '.spec.versions[] | select(.name == "'$version'") | .schema.openAPIV3Schema.properties | del(.. | select(has("description")).description)' "$main_file" > "$main_schema_file" | |
yq '.spec.versions[] | select(.name == "'$version'") | .schema.openAPIV3Schema.properties | del(.. | select(has("description")).description)' "$pr_file" > "$pr_schema_file" | |
if ! diff "$main_schema_file" "$pr_schema_file" ; then | |
echo "Schema differences found for version $version in $main_file and $pr_file" | |
exit 1 | |
fi | |
} | |
for file in $(ls ./pr-crds); do | |
main_file="./main-branch-crds/$file" | |
pr_file="./pr-crds/$file" | |
versions=$(yq e '.spec.versions[].name' "$pr_file") | |
for version in $versions; do | |
compare_schemas "$main_file" "$pr_file" "$version" | |
done | |
done | |
echo "API schemas match in all CRD files" |