Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc78a713b8 |
No files matched your search
@@ -57,6 +57,7 @@ jobs:
|
||||
python3 tests/test_ci_maintenance.py
|
||||
python3 tests/test_rollout_version.py
|
||||
python3 tests/test_enforce_orb_checks.py
|
||||
python3 tests/test_native_next_gates.py
|
||||
python3 tests/test_enroll_consumers.py
|
||||
python3 tests/test_reconcile_enrollment.py
|
||||
python3 tests/test_discover_enrollment.py
|
||||
|
||||
@@ -83,7 +83,7 @@ class FinishTests(unittest.TestCase):
|
||||
enforce.assert_called_once_with(current)
|
||||
|
||||
def test_unknown_gate_profile_never_merges_or_enforces(self):
|
||||
self.plan['profile'] = 'frontend-next'
|
||||
self.plan['profile'] = 'marketing'
|
||||
with mock.patch.object(finish.remote, 'command') as command, mock.patch.object(finish.enforcement, 'inspect') as inspect:
|
||||
self.assertEqual('unsupported-gates', finish.merge_ready(self.plan, self.result)['state'])
|
||||
self.assertEqual('unsupported-gates', finish.enforce_current(self.plan)['state'])
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
"""Recognize native ownership without duplicate CI or weaker PR protections."""
|
||||
import base64
|
||||
import copy
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import unittest
|
||||
from unittest import mock
|
||||
import yaml
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tools'))
|
||||
import enforce_orb_checks as enforcement
|
||||
import finish_enrollment as finish
|
||||
|
||||
|
||||
class NativeTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.pin = 'a' * 40
|
||||
self.head = 'b' * 40
|
||||
self.caller = enforcement.native.bridge.canonical_next(self.pin)
|
||||
self.signed = True
|
||||
self.worker_drift = False
|
||||
self.runs = [{'id': i, 'name': gate['context'], 'app': {'id': 15368},
|
||||
'status': 'completed', 'conclusion': 'success'}
|
||||
for i, gate in enumerate(enforcement.native.gates())]
|
||||
self.record = {'id': 1, 'enforcement': 'active', 'target': 'branch',
|
||||
'source_type': 'Repository', 'source': 'QRun-IO/example', 'bypass_actors': [],
|
||||
'rules': [{'type': 'required_status_checks', 'parameters': {
|
||||
'strict_required_status_checks_policy': False,
|
||||
'required_status_checks': [{'context': g['context'], 'integration_id': g['app_id']}
|
||||
for g in enforcement.gates('frontend-next')]}}]}
|
||||
|
||||
def request(self, path, method='GET', payload=None):
|
||||
self.assertEqual('GET', method, 'Recognition must not mutate correct protections')
|
||||
if '/contents/' in path:
|
||||
if '.github/workflows/ci.yml?' in path:
|
||||
raw = yaml.safe_dump(self.caller)
|
||||
elif 'repos/QRun-IO/qqq-orb/' in path:
|
||||
name = path.split('/contents/')[1].split('?')[0]
|
||||
raw = (ROOT / name).read_text(encoding='utf-8') + ('# drift' if self.worker_drift else '')
|
||||
else:
|
||||
data = ({'profile': 'frontend-next', 'orb_version': '0.8', 'default_branch': 'main'}
|
||||
if '/.qqq-ci.yml?' in path else enforcement.standard.canonical_caller('frontend-next', '0.8'))
|
||||
raw = yaml.safe_dump(data)
|
||||
return {'type': 'file', 'encoding': 'base64', 'content': base64.b64encode(raw.encode()).decode()}
|
||||
if path.endswith('/commits/' + self.pin):
|
||||
return {'commit': {'verification': {'verified': self.signed}}}
|
||||
if '/check-runs?' in path:
|
||||
return {'check_runs': copy.deepcopy(self.runs)}
|
||||
if '/status?' in path:
|
||||
return {'statuses': []}
|
||||
if '/rulesets/1' in path:
|
||||
return copy.deepcopy(self.record)
|
||||
if '/rules/branches/' in path:
|
||||
return [{'type': 'required_status_checks', 'ruleset_source_type': 'Repository',
|
||||
'ruleset_source': 'QRun-IO/example', 'ruleset_id': 1}]
|
||||
if path.endswith('/protection'):
|
||||
return None
|
||||
if '/branches/' in path:
|
||||
return {'commit': {'sha': self.head}}
|
||||
return {'archived': False}
|
||||
|
||||
def test_main_recognition_retains_all_eight_pr_gates_without_image_run(self):
|
||||
plan = enforcement.inspect('QRun-IO/example', 'main', '0.8', self.request)
|
||||
self.assertFalse(plan['changed'])
|
||||
self.assertEqual('rulesets', plan['protection_mode'])
|
||||
self.assertEqual(enforcement.gates('frontend-next'), plan['required_status_checks']['checks'])
|
||||
self.assertEqual(8, len(plan['gates']))
|
||||
enforcement.enforce(plan, self.request)
|
||||
|
||||
def test_caller_drift_unsigned_and_changed_workers_fail_closed(self):
|
||||
self.caller['jobs']['ci']['uses'] = 'untrusted/repo/ci.yml@' + self.pin
|
||||
with self.assertRaisesRegex(ValueError, 'canonical'):
|
||||
enforcement.inspect('QRun-IO/example', 'main', '0.8', self.request)
|
||||
self.caller = enforcement.native.bridge.canonical_next(self.pin)
|
||||
self.signed = False
|
||||
with self.assertRaisesRegex(ValueError, 'not verified'):
|
||||
enforcement.inspect('QRun-IO/example', 'main', '0.8', self.request)
|
||||
self.signed = True
|
||||
self.worker_drift = True
|
||||
with self.assertRaisesRegex(ValueError, 'supported contract'):
|
||||
enforcement.inspect('QRun-IO/example', 'main', '0.8', self.request)
|
||||
|
||||
def test_missing_main_gate_wrong_app_and_missing_pr_protection_reject(self):
|
||||
self.runs[0]['app']['id'] = 18001
|
||||
with self.assertRaises(ValueError):
|
||||
enforcement.inspect('QRun-IO/example', 'main', '0.8', self.request)
|
||||
self.runs[0]['app']['id'] = 15368
|
||||
self.record['rules'][0]['parameters']['required_status_checks'].pop()
|
||||
with self.assertRaisesRegex(ValueError, 'missing app-bound gate'):
|
||||
enforcement.inspect('QRun-IO/example', 'main', '0.8', self.request)
|
||||
|
||||
def test_pr_qualification_requires_both_image_architectures(self):
|
||||
with mock.patch.object(finish.remote, 'api', side_effect=self.request):
|
||||
missing = finish.qualification('QRun-IO/example', self.head, 'frontend-next')
|
||||
self.assertEqual(sorted(enforcement.native.PR_GATES[-2:]), missing)
|
||||
for i, name in enumerate(enforcement.native.PR_GATES[-2:], 20):
|
||||
self.runs.append({'id': i, 'name': name, 'app': {'id': 15368},
|
||||
'status': 'completed', 'conclusion': 'success'})
|
||||
self.assertEqual([], finish.qualification('QRun-IO/example', self.head, 'frontend-next'))
|
||||
|
||||
def test_develop_has_no_native_push_contract(self):
|
||||
with self.assertRaisesRegex(ValueError, 'main only'):
|
||||
enforcement.native.validate('QRun-IO/example', self.head, 'develop', self.request, enforcement.standard.load_yaml)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -19,10 +19,15 @@ PROFILE_GATES = {profile: 'verify' for profile in (
|
||||
PROFILE_GATES.update({'all': 'verify-and-smoke', 'cli': 'test_only',
|
||||
'app-starter': 'test_only', 'android': 'test-and-build',
|
||||
'mcp-policy': 'validate'})
|
||||
PROFILES = set(PROFILE_GATES)
|
||||
PROFILES = set(PROFILE_GATES) | {'frontend-next'}
|
||||
_native_spec = importlib.util.spec_from_file_location('native_next_gates', ROOT / 'tools/native_next_gates.py')
|
||||
native = importlib.util.module_from_spec(_native_spec)
|
||||
_native_spec.loader.exec_module(native)
|
||||
|
||||
|
||||
def gates(profile):
|
||||
if profile == 'frontend-next':
|
||||
return native.gates(pull_request=True)
|
||||
return [{'context': name, 'app_id': 18001}
|
||||
for name in ('setup', PROFILE_GATES[profile])]
|
||||
|
||||
@@ -51,12 +56,12 @@ def api(path, method='GET', payload=None):
|
||||
return json.loads(result.stdout) if result.stdout.strip() else None
|
||||
|
||||
|
||||
def required_checks(before, runs, desired_gates=DESIRED):
|
||||
"""Append app-bound gates only after both succeeded; preserve all other gates."""
|
||||
for desired in desired_gates:
|
||||
def required_checks(before, runs, desired_gates=DESIRED, observed_gates=None):
|
||||
"""Require successful event-appropriate gates and preserve every PR requirement."""
|
||||
for desired in (desired_gates if observed_gates is None else observed_gates):
|
||||
matches = [run for run in runs if run['name'] == desired['context'] and run['app']['id'] == desired['app_id']]
|
||||
if not matches or max(matches, key=lambda run: run['id'])['conclusion'] != 'success':
|
||||
raise ValueError('Current branch head has no successful CircleCI ' + desired['context'])
|
||||
raise ValueError('Current branch head has no successful app-bound gate: ' + desired['context'])
|
||||
prior = (before or {}).get('required_status_checks') or {}
|
||||
checks = [dict(check) for check in prior.get('checks', [])]
|
||||
represented = {check['context'] for check in checks}
|
||||
@@ -116,6 +121,10 @@ def inspect(repo, branch, version, request=api):
|
||||
if profile not in PROFILES:
|
||||
raise ValueError('Profile has no supported shared branch gates')
|
||||
desired_gates = gates(profile)
|
||||
observed_gates = desired_gates
|
||||
if profile == 'frontend-next':
|
||||
native.validate(repo, head, branch, request, standard.load_yaml)
|
||||
observed_gates = native.gates()
|
||||
rules = request(root + '/rules/branches/' + branch)
|
||||
rulesets, ruleset_strict = verified_rulesets(repo, rules, request, desired_gates) if rules else ([], False)
|
||||
before = request(prefix + '/protection')
|
||||
@@ -131,7 +140,7 @@ def inspect(repo, branch, version, request=api):
|
||||
if len(batch) < 100:
|
||||
break
|
||||
page += 1
|
||||
desired = required_checks(before, runs, desired_gates)
|
||||
desired = required_checks(before, runs, desired_gates, observed_gates)
|
||||
if use_ruleset:
|
||||
return {'repository': repo, 'branch': branch, 'head': head, 'before': before, 'gates': desired_gates,
|
||||
'required_status_checks': {'strict': False, 'checks': desired_gates},
|
||||
|
||||
@@ -6,7 +6,7 @@ import reconcile_enrollment as remote
|
||||
|
||||
|
||||
def qualification(repo, head, profile='qbit'):
|
||||
"""Require both app-bound orb gates and every advertised latest check/status."""
|
||||
"""Require profile-specific app-bound gates and every advertised latest check/status."""
|
||||
checks, page = [], 1
|
||||
while True:
|
||||
batch = remote.api(f'repos/{repo}/commits/{head}/check-runs?filter=latest&per_page=100&page={page}')['check_runs']
|
||||
@@ -17,7 +17,7 @@ def qualification(repo, head, profile='qbit'):
|
||||
reasons = []
|
||||
for gate in enforcement.gates(profile):
|
||||
name = gate['context']
|
||||
found = [check for check in checks if check['name'] == name and check['app']['id'] == 18001]
|
||||
found = [check for check in checks if check['name'] == name and check['app']['id'] == gate['app_id']]
|
||||
if not found or max(found, key=lambda check: check['id'])['conclusion'] != 'success':
|
||||
reasons.append(name)
|
||||
for check in checks:
|
||||
@@ -50,6 +50,8 @@ def merge_ready(plan, result):
|
||||
if pr['state'] != 'MERGED':
|
||||
if pr['baseRefOid'] != plan['head'] or remote.api(f'repos/{repo}/branches/{plan["branch"]}')['commit']['sha'] != plan['head']:
|
||||
return {'state': 'stale-base', 'url': result['url']}
|
||||
if plan['profile'] == 'frontend-next':
|
||||
enforcement.native.validate(repo, head, plan['branch'], remote.api, enforcement.standard.load_yaml)
|
||||
pending = qualification(repo, head, plan['profile'])
|
||||
if pending:
|
||||
return {'state': 'waiting-checks', 'checks': pending, 'url': result['url']}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
"""Validate the canonical native bridge and its immutable shared worker contract."""
|
||||
import base64
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
spec = importlib.util.spec_from_file_location('next_bridge', ROOT / 'src/scripts/generate_next_workflow.py')
|
||||
bridge = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(bridge)
|
||||
MAIN_GATES = [
|
||||
'ci / Typecheck, lint, unit tests and coverage',
|
||||
'ci / Mocked e2e (Playwright, chromium)',
|
||||
'ci / Collaboration e2e (MSW; chromium, firefox, webkit, mobile, tablet)',
|
||||
'ci / Bundle budget (static export)', 'ci / Storybook build',
|
||||
'acceptance / acceptance gate',
|
||||
]
|
||||
PR_GATES = MAIN_GATES + ['dashboard-image / build (amd64, ubuntu-24.04)',
|
||||
'dashboard-image / build (arm64, ubuntu-24.04-arm)']
|
||||
|
||||
|
||||
def gates(pull_request=False):
|
||||
return [{'context': name, 'app_id': 15368} for name in (PR_GATES if pull_request else MAIN_GATES)]
|
||||
|
||||
|
||||
def validate(repo, head, branch, request, load_yaml):
|
||||
"""Only main has a push trigger; require exact caller and reviewed worker bytes."""
|
||||
if branch != 'main':
|
||||
raise ValueError('Next native branch verification supports main only')
|
||||
|
||||
def content(owner, path, ref):
|
||||
item = request(f'repos/{owner}/contents/{path}?ref={ref}')
|
||||
if item.get('type') != 'file' or item.get('encoding') != 'base64':
|
||||
raise ValueError('Native workflow must be a regular file')
|
||||
return base64.b64decode(item['content']).decode('utf-8').replace('\r\n', '\n')
|
||||
|
||||
caller = load_yaml(content(repo, '.github/workflows/ci.yml', head))
|
||||
try:
|
||||
pin = caller['jobs']['ci']['uses'].rsplit('@', 1)[1]
|
||||
expected = bridge.canonical_next(pin)
|
||||
except (KeyError, TypeError, IndexError, ValueError):
|
||||
raise ValueError('Native caller has no valid immutable worker pin') from None
|
||||
if caller != expected:
|
||||
raise ValueError('Native caller differs from the canonical shared bridge')
|
||||
commit = request('repos/QRun-IO/qqq-orb/commits/' + pin)
|
||||
if not commit['commit']['verification']['verified']:
|
||||
raise ValueError('Shared native worker commit is not verified')
|
||||
for name in ('next-ci.yml', 'next-acceptance.yml', 'next-dashboard-image.yml'):
|
||||
path = '.github/workflows/' + name
|
||||
if content('QRun-IO/qqq-orb', path, pin) != (ROOT / path).read_text(encoding='utf-8'):
|
||||
raise ValueError('Pinned native worker differs from the supported contract: ' + name)
|
||||
return pin
|
||||
Reference in new issue
Block a user