ci: enforce feature fix and hotfix branch version policy

This commit is contained in:
Darío Sevilla committed 2026-10-13 09:54:56 +00:00
1 parent a9ff685bf0
commit 018cd3834e
6 files changed
+165 -3

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
"""Validate PR routing and committed Android versions without executing PR code."""
import argparse
import re
import subprocess
def version_at(ref):
content = subprocess.check_output(
['git', 'show', f'{ref}:app/build.gradle.kts'], text=True
)
names = re.findall(r'^\s*versionName\s*=\s*"(\d+)\.(\d+)\.(\d+)"\s*$', content, re.M)
codes = re.findall(r'^\s*versionCode\s*=\s*(\d+)\s*$', content, re.M)
if len(names) != 1 or len(codes) != 1:
raise ValueError('Expected one literal x.y.z versionName and one versionCode')
return tuple(map(int, names[0])), int(codes[0])
def validate(base_branch, head_branch, old, new, old_code, new_code, major_approved=False):
if base_branch == 'main' and head_branch == 'development':
expected = (old[0], old[1] + 1, old[2])
elif base_branch == 'main' and head_branch.startswith('hotfix/'):
expected = (old[0], old[1], old[2] + 1)
elif base_branch == 'development' and head_branch.startswith(('feat/', 'fix/')):
expected = (old[0], old[1], old[2] + 1)
else:
raise ValueError('PRs must be feat/* or fix/* -> development, hotfix/* -> main, or development -> main')
if major_approved:
expected = (old[0] + 1, old[1], old[2])
if new != expected:
raise ValueError(f'Expected version {".".join(map(str, expected))}, found {".".join(map(str, new))}')
if new_code <= old_code or not 1 <= new_code <= 2_100_000_000:
raise ValueError('Committed versionCode must increase and remain within Android limits')
def main():
parser = argparse.ArgumentParser(description=__doc__)
for name in ('base-ref', 'head-ref', 'base-branch', 'head-branch'):
parser.add_argument(f'--{name}', required=True)
parser.add_argument('--major-approved', action='store_true')
args = parser.parse_args()
old, old_code = version_at(args.base_ref)
new, new_code = version_at(args.head_ref)
try:
validate(args.base_branch, args.head_branch, old, new, old_code, new_code, args.major_approved)
except ValueError as error:
raise SystemExit(str(error)) from error
print(f'Version policy passed: {old} -> {new}, versionCode {old_code} -> {new_code}')
if __name__ == '__main__':
main()
+35
View File
@@ -0,0 +1,35 @@
import unittest
from scripts.check_version_policy import validate
class VersionPolicyTest(unittest.TestCase):
def test_allowed_transitions(self):
for base, head, new in [('main', 'development', (0, 14, 4)),
('main', 'hotfix/map', (0, 13, 5)),
('development', 'feat/routes', (0, 13, 5)),
('development', 'fix/map', (0, 13, 5))]:
with self.subTest(base=base, head=head):
validate(base, head, (0, 13, 4), new, 20, 21)
def test_reject_invalid_routes_and_versions(self):
for base, head, new, code in [('main', 'feat/routes', (0, 13, 5), 21),
('main', 'fix/map', (0, 13, 5), 21),
('development', 'hotfix/map', (0, 13, 5), 21),
('main', 'other', (0, 13, 5), 21),
('other', 'fix/map', (0, 13, 5), 21),
('main', 'development', (0, 14, 0), 21),
('main', 'hotfix/map', (1, 13, 4), 21),
('development', 'feat/routes', (0, 13, 4), 21),
('main', 'hotfix/map', (0, 13, 5), 20)]:
with self.subTest(base=base, head=head, new=new, code=code):
with self.assertRaises(ValueError):
validate(base, head, (0, 13, 4), new, 20, code)
def test_explicit_major_approval(self):
validate('main', 'hotfix/major', (0, 13, 4), (1, 13, 4), 20, 21, True)
with self.assertRaises(ValueError):
validate('main', 'feat/major', (0, 13, 4), (1, 13, 4), 20, 21, True)
if __name__ == '__main__':
unittest.main()