From d6bb79cf605019364b913bdee9d273f859139dd6 Mon Sep 17 00:00:00 2001 From: C Freeman Date: Fri, 13 Dec 2024 13:59:11 -0500 Subject: [PATCH] Fix CI for DM XMLs (#34907) * Fix CI for data model changes * test change * Apply suggestions from code review * Update .github/workflows/check-data-model-directory-updates.yaml * update the workflow * Restyled by isort * linter * Not sure why this isn't running, remove condition * a bit more * Exit with correct code * simplify the checkout a bit * also allow scraper version changes * Revert "test change" This reverts commit 67eea597c6c31e9de77db868f38b058e48f91189. --------- Co-authored-by: Restyled.io --- .../check-data-model-directory-updates.yaml | 28 ++++++---- scripts/dm_xml_ci_change_enforcement.py | 52 +++++++++++++++++++ 2 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 scripts/dm_xml_ci_change_enforcement.py diff --git a/.github/workflows/check-data-model-directory-updates.yaml b/.github/workflows/check-data-model-directory-updates.yaml index 305da81d9da817..7bd20c87e0e4ec 100644 --- a/.github/workflows/check-data-model-directory-updates.yaml +++ b/.github/workflows/check-data-model-directory-updates.yaml @@ -12,20 +12,28 @@ # See the License for the specific language governing permissions and # limitations under the License. -name: Check for changes to data_model directory without a sha update +name: Data model directory checks on: pull_request: - paths: - - "data_model/**" jobs: - check-submodule-update-label: - name: Check for changes to data_model directory without a sha update + check-data_model-updates: + name: Check for updates to data model directory without SHA updates runs-on: ubuntu-latest - if: "git diff --name-only HEAD^..HEAD data_model/ | grep -q spec_sha" + container: + image: ghcr.io/project-chip/chip-build steps: - - name: Error Message - run: echo This pull request attempts to update data_model directory, but is missing updates to spec_sha file with the latest version of the sha. Files in the data_model directory are generated automatically and should not be updated manually. - - name: Fail Job - run: exit 1 + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 2 + - name: Check for changes to master data_model directory without a SHA update + run: | + python3 scripts/dm_xml_ci_change_enforcement.py data_model/master + - name: Check for changes to 1.3 data_model directory without a SHA update + run: | + python3 scripts/dm_xml_ci_change_enforcement.py data_model/1.3 + - name: Check for changes to 1.4 data_model directory without a SHA update + run: | + python3 scripts/dm_xml_ci_change_enforcement.py data_model/1.4 \ No newline at end of file diff --git a/scripts/dm_xml_ci_change_enforcement.py b/scripts/dm_xml_ci_change_enforcement.py new file mode 100644 index 00000000000000..0efe40f66faa49 --- /dev/null +++ b/scripts/dm_xml_ci_change_enforcement.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +# Copyright (c) 2024 Project CHIP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import os +import subprocess +import sys + +import click + + +@click.command() +@click.argument('dir', nargs=1, type=click.Path(exists=True)) +def check_dm_directory(dir): + clusters = os.path.join(dir, 'clusters') + device_types = os.path.join(dir, 'device_types') + if not os.path.isdir(clusters) or not os.path.isdir(device_types): + print(f"Invalid data model directory {dir}") + sys.exit(1) + + # We are operating in a VM, and although there is a checkout, it is working in a scratch directory + # where the ownership is different than the runner. + # Adding an exception for this directory so that git can function properly. + subprocess.run("git config --global --add safe.directory '*'", shell=True) + + def check_dir(dir): + cmd = f'git diff HEAD^..HEAD --name-only -- {dir}' + output = subprocess.check_output(cmd, shell=True).decode().splitlines() + if output and 'spec_sha' not in output and 'scraper_version' not in output: + print(f'Data model directory {dir} had changes to the following files without a corresponding update to the spec SHA') + print(output) + print("Note that the data_model directory files are automatically updated by a spec scraper and should not be manually updated.") + return 1 + return 0 + + ret = check_dir(clusters) + check_dir(device_types) + sys.exit(ret) + + +if __name__ == '__main__': + check_dm_directory()