From be058934f4c4f7ba3b073118b14201d82f636fee Mon Sep 17 00:00:00 2001 From: "T.Tian" Date: Sat, 23 Nov 2024 20:50:41 +0800 Subject: [PATCH] add CI against version check --- .github/workflows/build-test.yml | 32 +++++- .github/workflows/test-outdated-package.py | 108 +++++++++++++++++++++ ChangeLog | 9 ++ 3 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/test-outdated-package.py diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index d0fa7031..6a2281fd 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -10,13 +10,41 @@ on: workflow_dispatch: jobs: + # Check if initialization.c is up-to-date with Changelog + package-date-check: + runs-on: ubuntu-latest + defaults: + run: + shell: bash -l {0} + steps: + - uses: actions/checkout@v4 + - uses: conda-incubator/setup-miniconda@v3 + with: + python-version: "3.11" + activate-environment: sparc-test + conda-build-version: "24.9.0" + miniforge-version: latest # Fix according to https://github.com/conda-incubator/setup-miniconda?tab=readme-ov-file#example-10-miniforge + channels: conda-forge,defaults + channel-priority: true + - name: Install SPARC-X-API stable version for docparser + run: | + mamba install -c conda-forge pip setuptools + pip install git+https://github.com/SPARC-X/SPARC-X-API.git@v1.0.5 + - name: Convert parameters.json + run: | + python -m sparc.docparser --include-subdirs doc/.LaTeX + - name: Check missing parameters in test examples + run: | + # Usage: + # python test-outdated-package.py + python .github/workflows/test-outdated-package.py \ + ./parameters.json Changelog build-linux: runs-on: ubuntu-latest defaults: run: shell: bash -l {0} - strategy: - max-parallel: 5 + need: package-date-check steps: - uses: actions/checkout@v4 diff --git a/.github/workflows/test-outdated-package.py b/.github/workflows/test-outdated-package.py new file mode 100644 index 00000000..1ea3c138 --- /dev/null +++ b/.github/workflows/test-outdated-package.py @@ -0,0 +1,108 @@ +"""Test script to check if SPARC package version is older than +the latest Changelog entry + +We generally do not check the other way around since sometimes a trivial bump of SPARC version may be necessary +""" +import re +import json +from datetime import datetime +from pathlib import Path + + +def load_parameters_json(json_path): + """ + Load the parameters from the parameters.json file. + """ + with open(json_path, 'r') as f: + parameters = json.load(f) + if "sparc_version" not in parameters: + raise KeyError("The 'sparc_version' field is missing in parameters.json") + return parameters["sparc_version"] + + +def extract_latest_date_from_changelog(changelog_path): + """ + Extracts the latest date from the changelog file. + """ + date_patterns = [ + r"(?P\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{1,2}, \d{4})", + r"(?P\b(?:January|February|March|April|May|June|July|August|September|October|November|December) \d{1,2}, \d{4})", + ] + + latest_date = None + changelog_path = Path(changelog_path) + + with changelog_path.open("r") as f: + content = f.read() + + for pattern in date_patterns: + matches = re.findall(pattern, content) + for match in matches: + try: + # Convert matched date to datetime object + parsed_date = datetime.strptime(match, "%b %d, %Y") if "," in match else datetime.strptime(match, "%B %d, %Y") + if latest_date is None or parsed_date > latest_date: + latest_date = parsed_date + except ValueError: + continue # Skip invalid date formats + + if latest_date is None: + raise ValueError("No valid date found in the changelog.") + return latest_date + + +def check_version_against_changelog(parameters_json_path, changelog_path): + """ + Check if the package version in parameters.json is older than the latest changelog date. + """ + # Load sparc_version from parameters.json + sparc_version = load_parameters_json(parameters_json_path) + version_date = datetime.strptime(sparc_version, "%Y.%m.%d") + + # Extract the latest date from the changelog + latest_changelog_date = extract_latest_date_from_changelog(changelog_path) + + if version_date < latest_changelog_date: + print("Version Check Report:") + print("-" * 60) + print(f"ERROR: SPARC version ({version_date.strftime('%Y.%m.%d')}) " + f"is older than the latest changelog date ({latest_changelog_date.strftime('%Y.%m.%d')}).") + print("Please update initialization.c!") + print("-" * 60) + return False + else: + print("Version Check Report:") + print("-" * 60) + print("SUCCESS:") + print("-" * 60) + return True + + +def main(): + import argparse + parser = argparse.ArgumentParser( + description="Check if package version is up-to-date with the changelog." + ) + parser.add_argument( + "parameters_json", + type=str, + help="Path to the parameters.json file." + ) + parser.add_argument( + "changelog", + type=str, + help="Path to the changelog file." + ) + + args = parser.parse_args() + + # Run the version check + success = check_version_against_changelog(args.parameters_json, args.changelog) + if not success: + exit(1) + else: + exit(0) + + +if __name__ == "__main__": + main() diff --git a/ChangeLog b/ChangeLog index 8899d221..992f5edf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,15 @@ -Date -Name -changes +-------------- +Nov 23, 2024 +Name: Tian Tian +Changes: (doc, CI workflow) +1. Fix typo in SQ LaTeX doc +2. Add CI workflow to check missing parameters +3. Add CI workflow to validate and render LaTeX doc +4. Add CI workflow to check outdated initialization.c (SPARC version) if older than current Changelog + -------------- Nov 18, 2024 Name: Tian Tian, Lucas Timmerman