Skip to content

Commit

Permalink
Merge pull request #88 from devcontainers-extra/ci/check-version-bump
Browse files Browse the repository at this point in the history
ci: check version bump
  • Loading branch information
koralowiec authored Jan 28, 2025
2 parents 906eb31 + 0379fad commit e8cf854
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
6 changes: 4 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
},
Expand All @@ -20,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"
}
94 changes: 94 additions & 0 deletions .github/scripts/version-checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/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', f'origin/{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')}")
exit(1)


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_ref}:{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')}")
exit(1)
except json.JSONDecodeError as e:
print(
f"Error decoding JSON from {directory}/devcontainer-feature.json in {branch}: {e}")
exit(1)


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(
description="Check for version bumps in changed directories.")
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()

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/')}

bump_required = False
for directory in changed_dirs:
check_result = is_version_bump_required(directory, base_branch)
if check_result:
bump_required = True

if bump_required:
exit(1)


if __name__ == "__main__":
main()
21 changes: 21 additions & 0 deletions .github/workflows/feature-version-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Feature Version Bump Check

on:
pull_request:
types: [opened, synchronize]

jobs:
check-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Run feature version check script
run: |
python .github/scripts/version-checker.py

0 comments on commit e8cf854

Please sign in to comment.