From 08946329ff32a4a14815d3b2f7af504c28a53b05 Mon Sep 17 00:00:00 2001 From: Victor Campos Date: Fri, 8 Dec 2023 10:24:25 +0000 Subject: [PATCH] drafversion checker Introduce a new CI checker to ensure that draftversion fields are set correctly. --- .github/workflows/ci.yml | 9 +++ tools/acle_draftversion_value_check.py | 89 ++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 tools/acle_draftversion_value_check.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3c4e0d2f..db6a4915 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,3 +32,12 @@ jobs: - uses: gaurav-nelson/github-action-markdown-link-check@v1 with: config-file: '.github/workflows/markdown-link-check.json' + + draftversion-check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: check correctness of draftversion fields + run: python ./tools/acle_draftversion_value_check.py . diff --git a/tools/acle_draftversion_value_check.py b/tools/acle_draftversion_value_check.py new file mode 100644 index 00000000..2f420e6d --- /dev/null +++ b/tools/acle_draftversion_value_check.py @@ -0,0 +1,89 @@ +""" +" +" Copyright (C) 2023 Arm Ltd +" +""" + +from pathlib import Path +import re +import subprocess +import sys + + +def get_modified_file_list(repo_path: Path) -> list[str]: + """Use git to get the list of modified files in the patch set""" + starting_point = "origin/main" + cmd = ["git", "-C", str(repo_path), "diff", "--name-only", starting_point, "HEAD"] + + try: + git_result = subprocess.run(args=cmd, capture_output=True, check=True) + except subprocess.CalledProcessError as ex: + print( + f"Error: in command '{' '.join(cmd)}':\n{ex.stderr.decode()}", + file=sys.stderr, + ) + sys.exit(1) + + modified_files = git_result.stdout.decode().split("\n") + modified_files = [x for x in modified_files if x] + return modified_files + + +def check_file(file_path: Path) -> bool: + """Check if file has a draftversion field, and if so, that it's set to true""" + with file_path.open() as file: + print(f"Checking file {file_path}") + for line in file: + re_result = re.search(r"draftversion: (\w+)", line) + if not re_result: + continue + + value = re_result[1] + if value != "true": + print( + f"Error: file {file_path} has been modified but its draftversion" + " isn't set to true!", + file=sys.stderr, + ) + return False + + return True + + +def main() -> int: + """Check if any modified file has the wrong value for draftversion""" + if len(sys.argv) != 2: + print( + "Error: this script takes a single positional argument, which is the path" + " to the ACLE repository!", + file=sys.stderr, + ) + sys.exit(1) + + try: + repo_path = Path(sys.argv[1]).resolve(strict=True) + except FileNotFoundError: + print(f"Error: repository {sys.argv[1]} doesn't exist!", file=sys.stderr) + sys.exit(1) + + modified_files = get_modified_file_list(repo_path) + print("Files to check:") + if not modified_files: + print(" none") + else: + for path in modified_files: + print(f" - {path}") + print() + + success = True + for path in modified_files: + complete_path = repo_path / Path(path) + success = check_file(complete_path) and success + + print("\nEnd result: " + ("OK" if success else "issues found")) + + return not success + + +if __name__ == "__main__": + sys.exit(main())