perf(ci): tier PR builds and skip routine release packaging
Gitea CI / Check code and tests (pull_request) Successful in 7m19s
Gitea CI / Build Android APK (pull_request) Successful in 4m34s
Gitea CI / Build Windows x64 app (pull_request) Successful in 3m18s
Gitea CI / Audit and package Windows x64 (pull_request) Successful in 55s
Gitea CI / Assemble and validate CI artifacts (pull_request) Successful in 48s
Gitea CI / Gitea CI passed (pull_request) Successful in 2s
Gitea CI / Check code and tests (pull_request) Successful in 7m19s
Gitea CI / Build Android APK (pull_request) Successful in 4m34s
Gitea CI / Build Windows x64 app (pull_request) Successful in 3m18s
Gitea CI / Audit and package Windows x64 (pull_request) Successful in 55s
Gitea CI / Assemble and validate CI artifacts (pull_request) Successful in 48s
Gitea CI / Gitea CI passed (pull_request) Successful in 2s
This commit is contained in:
1 parent
20f5c1036d
commit
ca2efdaf18
5 files changed
+246
-11
No files matched your search
@@ -0,0 +1,70 @@
|
||||
"""Select PR compilation and packaging scope from the complete Git change set."""
|
||||
|
||||
import os
|
||||
from pathlib import Path, PurePosixPath
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def packaging_change(path):
|
||||
path = path.lower()
|
||||
return (
|
||||
path.startswith(('.gitea/', '.github/workflows/', '.github/actions/',
|
||||
'tool/', 'android/', 'windows/',
|
||||
'packages/cutdex_native/android/',
|
||||
'packages/cutdex_native/windows/',
|
||||
'lib/integrations/app_update',
|
||||
'lib/features/settings/update'))
|
||||
or PurePosixPath(path).name in {'pubspec.yaml', 'pubspec.lock', '.metadata'}
|
||||
or (path.startswith('test/') and 'update' in path)
|
||||
)
|
||||
|
||||
|
||||
def documentation_change(path):
|
||||
path = PurePosixPath(path.lower())
|
||||
return (
|
||||
str(path).startswith(('docs/', '.github/issue_template/'))
|
||||
or str(path) == '.github/pull_request_template.md'
|
||||
or (len(path.parts) == 1 and path.suffix in {'.md', '.rst'})
|
||||
or (len(path.parts) == 1 and path.name in {'license', 'notice'})
|
||||
or (str(path).startswith('packages/') and path.name == 'readme.md')
|
||||
)
|
||||
|
||||
|
||||
def select_scope(event, paths):
|
||||
paths = list(paths)
|
||||
if event != 'pull_request' or not paths or any(packaging_change(path) for path in paths):
|
||||
return {'build-app': True, 'package-app': True}
|
||||
return {'build-app': not all(documentation_change(path) for path in paths),
|
||||
'package-app': False}
|
||||
|
||||
|
||||
def changed_paths(base, head):
|
||||
for sha in (base, head):
|
||||
if not re.fullmatch(r'[0-9a-f]{40}', sha):
|
||||
raise ValueError('PR base and head must be full Git commit SHAs')
|
||||
result = subprocess.run(
|
||||
['git', 'diff', '--name-only', '--no-renames', '-z', f'{base}...{head}'],
|
||||
check=True, capture_output=True,
|
||||
)
|
||||
return result.stdout.decode('utf-8', errors='surrogateescape').rstrip('\0').split('\0') if result.stdout else []
|
||||
|
||||
|
||||
def main():
|
||||
event = os.environ['CI_EVENT_NAME']
|
||||
paths = changed_paths(os.environ.get('CI_PR_BASE_SHA', ''),
|
||||
os.environ.get('CI_PR_HEAD_SHA', '')) if event == 'pull_request' else []
|
||||
scope = select_scope(event, paths)
|
||||
with Path(os.environ['GITHUB_OUTPUT']).open('a', encoding='utf-8') as stream:
|
||||
for name, enabled in scope.items():
|
||||
stream.write(f'{name}={str(enabled).lower()}\n')
|
||||
print(f"CI scope: compile={scope['build-app']}, package={scope['package-app']}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except (KeyError, ValueError, subprocess.CalledProcessError) as error:
|
||||
print(f'Cannot determine CI scope: {error}', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,99 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from ci_scope import changed_paths, main, select_scope
|
||||
|
||||
|
||||
class ScopeTests(unittest.TestCase):
|
||||
def test_main_and_manual_runs_always_produce_release_artifacts(self):
|
||||
for event in ('push', 'workflow_dispatch'):
|
||||
with self.subTest(event=event):
|
||||
self.assertEqual(select_scope(event, ['README.md']),
|
||||
{'build-app': True, 'package-app': True})
|
||||
|
||||
def test_regular_code_pr_compiles_both_platforms_without_packaging(self):
|
||||
self.assertEqual(select_scope('pull_request', ['lib/main.dart', 'test/app_test.dart']),
|
||||
{'build-app': True, 'package-app': False})
|
||||
|
||||
def test_documentation_only_pr_skips_platform_jobs(self):
|
||||
self.assertEqual(select_scope('pull_request', ['README.md', 'docs/setup.md',
|
||||
'packages/cutdex_ui/README.md', '.github/PULL_REQUEST_TEMPLATE.md']),
|
||||
{'build-app': False, 'package-app': False})
|
||||
|
||||
def test_documentation_does_not_hide_code_changes(self):
|
||||
self.assertEqual(select_scope('pull_request', ['README.md', 'lib/main.dart']),
|
||||
{'build-app': True, 'package-app': False})
|
||||
|
||||
def test_packaging_changes_force_full_validation(self):
|
||||
for path in ('.gitea/workflows/ci.yml', '.gitea/actions/upload-artifact/action.yml',
|
||||
'tool/gitea/ci_scope.py', 'tool/windows/windows_updates.py',
|
||||
'windows/runner/main.cpp.in', 'android/app/build.gradle.kts',
|
||||
'pubspec.yaml', 'pubspec.lock', 'packages/cutdex_ui/pubspec.yaml',
|
||||
'packages/cutdex_native/windows/CMakeLists.txt',
|
||||
'lib/integrations/app_update_bridge.dart',
|
||||
'lib/features/settings/update_controller.dart', 'test/app_updates_test.dart'):
|
||||
with self.subTest(path=path):
|
||||
self.assertEqual(select_scope('pull_request', ['README.md', path]),
|
||||
{'build-app': True, 'package-app': True})
|
||||
|
||||
def test_arbitrary_runtime_markdown_is_not_treated_as_documentation(self):
|
||||
self.assertTrue(select_scope('pull_request', ['assets/help.md'])['build-app'])
|
||||
|
||||
def test_empty_change_set_is_conservative(self):
|
||||
self.assertEqual(select_scope('pull_request', []),
|
||||
{'build-app': True, 'package-app': True})
|
||||
|
||||
def test_invalid_or_missing_shas_are_rejected(self):
|
||||
for base, head in (('', 'a' * 40), ('--help', 'b' * 40), ('a' * 40, 'bad')):
|
||||
with self.subTest(base=base, head=head), self.assertRaises(ValueError):
|
||||
changed_paths(base, head)
|
||||
|
||||
def test_failed_git_comparison_cannot_skip_builds(self):
|
||||
with patch('ci_scope.subprocess.run', side_effect=subprocess.CalledProcessError(1, ['git'])):
|
||||
with self.assertRaises(subprocess.CalledProcessError):
|
||||
changed_paths('a' * 40, 'b' * 40)
|
||||
|
||||
def test_outputs_propagate_as_action_boolean_strings(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
output = Path(directory) / 'output'
|
||||
with patch.dict(os.environ, {'CI_EVENT_NAME': 'pull_request',
|
||||
'CI_PR_BASE_SHA': 'a' * 40, 'CI_PR_HEAD_SHA': 'b' * 40,
|
||||
'GITHUB_OUTPUT': str(output)}), patch('ci_scope.changed_paths', return_value=['lib/main.dart']):
|
||||
main()
|
||||
self.assertEqual(output.read_text(), 'build-app=true\npackage-app=false\n')
|
||||
|
||||
def test_deleted_build_file_is_detected_when_renamed_into_docs(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
def git(*args):
|
||||
return subprocess.check_output(['git', '-C', str(root), *args], text=True).strip()
|
||||
git('init', '--quiet')
|
||||
git('config', 'user.name', 'CI scope test')
|
||||
git('config', 'user.email', 'ci-scope@example.invalid')
|
||||
(root / 'tool').mkdir()
|
||||
(root / 'tool/package.py').write_text('packaging input\n')
|
||||
git('add', '.')
|
||||
git('commit', '--quiet', '-m', 'base')
|
||||
base = git('rev-parse', 'HEAD')
|
||||
(root / 'docs').mkdir()
|
||||
(root / 'tool/package.py').rename(root / 'docs/package.md')
|
||||
git('add', '-A')
|
||||
git('commit', '--quiet', '-m', 'move build file')
|
||||
head = git('rev-parse', 'HEAD')
|
||||
original = os.getcwd()
|
||||
try:
|
||||
os.chdir(root)
|
||||
paths = changed_paths(base, head)
|
||||
finally:
|
||||
os.chdir(original)
|
||||
self.assertIn('tool/package.py', paths)
|
||||
self.assertEqual(select_scope('pull_request', paths),
|
||||
{'build-app': True, 'package-app': True})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user