From d35c8c3d8baaa1fe3185ddf900d57710e04262ce Mon Sep 17 00:00:00 2001 From: koralowiec <36413794+koralowiec@users.noreply.github.com> Date: Mon, 30 Dec 2024 17:41:14 +0000 Subject: [PATCH 1/8] ci: add basic feature version check --- .github/scripts/version-checker.py | 80 ++++++++++++++++++++ .github/workflows/feature-version-check.yaml | 19 +++++ 2 files changed, 99 insertions(+) create mode 100755 .github/scripts/version-checker.py create mode 100644 .github/workflows/feature-version-check.yaml diff --git a/.github/scripts/version-checker.py b/.github/scripts/version-checker.py new file mode 100755 index 000000000..0de7ce17c --- /dev/null +++ b/.github/scripts/version-checker.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 + +import os +import subprocess +import json +import argparse + + +def get_changed_files(base_branch): + """Get a list of changed files between the current branch and the base branch.""" + try: + result = subprocess.run( + ['git', 'diff', '--name-only', base_branch], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + check=True + ) + changed_files = result.stdout.decode('utf-8').splitlines() + return changed_files + except subprocess.CalledProcessError as e: + print(f"Error: {e.stderr.decode('utf-8')}") + return [] + + +def get_version_from_branch(directory, branch): + """Get the version from the devcontainer-feature.json file in the specified branch.""" + try: + result = subprocess.run( + ['git', 'show', f'{branch}:{directory}/devcontainer-feature.json'], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + check=True + ) + data = json.loads(result.stdout.decode('utf-8')) + return data.get('version') + except subprocess.CalledProcessError as e: + print(f"Error: {e.stderr.decode('utf-8')}") + return None + except json.JSONDecodeError as e: + print( + f"Error decoding JSON from {directory}/devcontainer-feature.json in {branch}: {e}") + return None + + +def check_version_bump(directory, base_branch): + """Check if the version in the current branch is greater than the version in the base branch.""" + current_version = get_version_from_branch(directory, 'HEAD') + base_version = get_version_from_branch(directory, base_branch) + + if current_version and base_version: + if current_version > base_version: + print( + f"✅ Version bump detected for {directory}. Current version: {current_version}, Base version: {base_version}") + else: + print( + f"❗ Version bump required for {directory}. Current version: {current_version}, Base version: {base_version}") + else: + print(f"Error: Could not determine versions for {directory}") + + +def main(): + parser = argparse.ArgumentParser( + description="Check for version bumps in changed directories.") + parser.add_argument('--base-branch', type=str, default='main', + help='Base branch to compare changes against') + args = parser.parse_args() + + base_branch = args.base_branch + changed_files = get_changed_files(base_branch) + + # Get unique directories under src/ that have changes + changed_dirs = {os.path.dirname( + file) for file in changed_files if file.startswith('src/')} + + for directory in changed_dirs: + check_version_bump(directory, base_branch) + + +if __name__ == "__main__": + main() diff --git a/.github/workflows/feature-version-check.yaml b/.github/workflows/feature-version-check.yaml new file mode 100644 index 000000000..0209ac482 --- /dev/null +++ b/.github/workflows/feature-version-check.yaml @@ -0,0 +1,19 @@ +name: Feature Version Bump Check + +on: + pull_request: + types: [opened, synchronize] + +jobs: + check-version: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Run feature version check script + run: | + python .github/scripts/feature-version-check.py From 20e2850ea8ca6c71c1f6594df8eb9cc95565d1ee Mon Sep 17 00:00:00 2001 From: koralowiec <36413794+koralowiec@users.noreply.github.com> Date: Mon, 30 Dec 2024 18:10:49 +0000 Subject: [PATCH 2/8] ci: fail if version bump required --- .github/scripts/version-checker.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/.github/scripts/version-checker.py b/.github/scripts/version-checker.py index 0de7ce17c..386c22b01 100755 --- a/.github/scripts/version-checker.py +++ b/.github/scripts/version-checker.py @@ -42,21 +42,26 @@ def get_version_from_branch(directory, branch): return None -def check_version_bump(directory, base_branch): +def is_version_bump_required(directory, base_branch): """Check if the version in the current branch is greater than the version in the base branch.""" current_version = get_version_from_branch(directory, 'HEAD') base_version = get_version_from_branch(directory, base_branch) + bump_required = False + if current_version and base_version: if current_version > base_version: print( f"✅ Version bump detected for {directory}. Current version: {current_version}, Base version: {base_version}") else: + bump_required = True print( f"❗ Version bump required for {directory}. Current version: {current_version}, Base version: {base_version}") else: print(f"Error: Could not determine versions for {directory}") + return bump_required + def main(): parser = argparse.ArgumentParser( @@ -72,8 +77,14 @@ def main(): changed_dirs = {os.path.dirname( file) for file in changed_files if file.startswith('src/')} + bump_required = False for directory in changed_dirs: - check_version_bump(directory, base_branch) + check_result = is_version_bump_required(directory, base_branch) + if check_result: + bump_required = True + + if bump_required: + exit(1) if __name__ == "__main__": From 7375695ab50266992b08543a2f1aabe19e6ee604 Mon Sep 17 00:00:00 2001 From: koralowiec <36413794+koralowiec@users.noreply.github.com> Date: Mon, 30 Dec 2024 18:11:25 +0000 Subject: [PATCH 3/8] chore(devcontainer): add GitHub Actions extension --- .devcontainer/devcontainer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index e27f15483..e060f08b8 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -9,7 +9,8 @@ "DavidAnson.vscode-markdownlint", "nefrob.vscode-just-syntax", "timonwong.shellcheck", - "ms-vscode-remote.remote-containers" + "ms-vscode-remote.remote-containers", + "github.vscode-github-actions" ] } }, From f83c8b75137db85638c7017d914ca8980b15d424 Mon Sep 17 00:00:00 2001 From: koralowiec <36413794+koralowiec@users.noreply.github.com> Date: Mon, 13 Jan 2025 18:42:18 +0000 Subject: [PATCH 4/8] ci: fix path to the version check script --- .github/workflows/feature-version-check.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/feature-version-check.yaml b/.github/workflows/feature-version-check.yaml index 0209ac482..93c67be60 100644 --- a/.github/workflows/feature-version-check.yaml +++ b/.github/workflows/feature-version-check.yaml @@ -16,4 +16,4 @@ jobs: - name: Run feature version check script run: | - python .github/scripts/feature-version-check.py + python .github/scripts/version-checker.py From 5e03ab20ee5a0cfbecf4968106666ef9146bdffc Mon Sep 17 00:00:00 2001 From: koralowiec <36413794+koralowiec@users.noreply.github.com> Date: Mon, 13 Jan 2025 18:48:56 +0000 Subject: [PATCH 5/8] chore(devcontainer): add act feature --- .devcontainer/devcontainer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index e060f08b8..b566055bc 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -21,7 +21,8 @@ "ghcr.io/lukewiwa/features/shellcheck:0.2.3": {}, "ghcr.io/guiyomh/features/just:0": {}, "ghcr.io/devcontainers-extra/features/pre-commit:2": {}, - "ghcr.io/devcontainers-extra/features/tmux-homebrew:1": {} + "ghcr.io/devcontainers-extra/features/tmux-homebrew:1": {}, + "ghcr.io/devcontainers-extra/features/act:1": {} }, "postCreateCommand": "/bin/bash -ex ./.devcontainer/setup.sh > postCreateCommand.log" } \ No newline at end of file From 0dedaf65c9f3d6307bb13e457f60f2c1d9bf56cb Mon Sep 17 00:00:00 2001 From: koralowiec <36413794+koralowiec@users.noreply.github.com> Date: Mon, 13 Jan 2025 18:56:15 +0000 Subject: [PATCH 6/8] ci: exit script immediately after a git command error --- .github/scripts/version-checker.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/scripts/version-checker.py b/.github/scripts/version-checker.py index 386c22b01..fb6ba1745 100755 --- a/.github/scripts/version-checker.py +++ b/.github/scripts/version-checker.py @@ -19,7 +19,7 @@ def get_changed_files(base_branch): return changed_files except subprocess.CalledProcessError as e: print(f"Error: {e.stderr.decode('utf-8')}") - return [] + exit(1) def get_version_from_branch(directory, branch): @@ -35,11 +35,11 @@ def get_version_from_branch(directory, branch): return data.get('version') except subprocess.CalledProcessError as e: print(f"Error: {e.stderr.decode('utf-8')}") - return None + exit(1) except json.JSONDecodeError as e: print( f"Error decoding JSON from {directory}/devcontainer-feature.json in {branch}: {e}") - return None + exit(1) def is_version_bump_required(directory, base_branch): From e3265ad7e0553ca8703d60e7706f05a277c012cd Mon Sep 17 00:00:00 2001 From: koralowiec <36413794+koralowiec@users.noreply.github.com> Date: Mon, 13 Jan 2025 19:05:04 +0000 Subject: [PATCH 7/8] ci: fetch all branches for version check --- .github/workflows/feature-version-check.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/feature-version-check.yaml b/.github/workflows/feature-version-check.yaml index 93c67be60..5532fce4f 100644 --- a/.github/workflows/feature-version-check.yaml +++ b/.github/workflows/feature-version-check.yaml @@ -9,6 +9,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 - uses: actions/setup-python@v5 with: From 0379fad616b1506a0c383938ae00bec728491ffb Mon Sep 17 00:00:00 2001 From: koralowiec <36413794+koralowiec@users.noreply.github.com> Date: Mon, 27 Jan 2025 18:09:08 +0000 Subject: [PATCH 8/8] ci: use remote name in git commands --- .github/scripts/version-checker.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/scripts/version-checker.py b/.github/scripts/version-checker.py index fb6ba1745..bb6adc851 100755 --- a/.github/scripts/version-checker.py +++ b/.github/scripts/version-checker.py @@ -10,7 +10,7 @@ def get_changed_files(base_branch): """Get a list of changed files between the current branch and the base branch.""" try: result = subprocess.run( - ['git', 'diff', '--name-only', base_branch], + ['git', 'diff', '--name-only', f'origin/{base_branch}'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True @@ -25,8 +25,10 @@ def get_changed_files(base_branch): def get_version_from_branch(directory, branch): """Get the version from the devcontainer-feature.json file in the specified branch.""" try: + branch_ref = 'HEAD' if branch == 'HEAD' else f'origin/{branch}' result = subprocess.run( - ['git', 'show', f'{branch}:{directory}/devcontainer-feature.json'], + ['git', 'show', + f'{branch_ref}:{directory}/devcontainer-feature.json'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True @@ -66,7 +68,8 @@ def is_version_bump_required(directory, base_branch): def main(): parser = argparse.ArgumentParser( description="Check for version bumps in changed directories.") - parser.add_argument('--base-branch', type=str, default='main', + parser.add_argument('--base-branch', type=str, + default=os.environ.get('GITHUB_BASE_REF', 'main'), help='Base branch to compare changes against') args = parser.parse_args()