forked from kvokka/features
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion-checker.py
More file actions
executable file
·104 lines (83 loc) · 3.43 KB
/
Copy pathversion-checker.py
File metadata and controls
executable file
·104 lines (83 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/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:
error_msg = e.stderr.decode('utf-8')
# Check if the file does not exist in the branch
if "fatal: path" in error_msg and "exists on disk, but not in" in error_msg:
return None
print(f"Error: {error_msg}")
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)
if base_version is None:
print(f"ℹ️ {directory} is a new feature, skipping version bump check")
return False
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()