mirror of
https://github.com/storytold/craft-common-actions.git
synced 2026-11-28 13:58:10 -08:00
nightly: recheck Sonar after the final workflow freshness pass (#350)
* Recheck Sonar after the final workflow freshness pass Sonar rereads (latest analysis and unresolved issues) now bracket the final workflow freshness pass in admission and revalidation, so a Sonar change during the last GitHub reads is refused while a workflow rerun during the first Sonar rereads still is. That costs two fixed reads (FIXED_READS 18 -> 20). Adds status/ahead_by missing-field cases and documents the room the fixed reads take out of the 640-read budget. * Say exactly what the Sonar bracket covers The bracket catches a Sonar change during the freshness reads and a workflow rerun during the first Sonar rereads; any resource can still change after its own last read. Comment and docs only.
This commit is contained in:
1 parent
82f995029c
commit
04d4087f02
3 files changed
+77
-13
No files matched your search
@@ -183,7 +183,15 @@ behind current main M only with verified ancestry, and must strictly advance the
|
||||
previous authenticated publication P; equal P/C refuses as already up to date.
|
||||
The release execution's own SHA is never substituted with C. A required-workflow
|
||||
shape whose fewest possible revalidation reads exceed the 640-read candidate
|
||||
budget refuses at contract time, before any provider read. Workflow-run `path`
|
||||
budget refuses at contract time, before any provider read. That estimate is
|
||||
`FIXED_READS` (20: the Sonar snapshot, gate and latest rereads, ancestry reads and
|
||||
the two closing Sonar reread pairs) plus the per-workflow reads, so the workflows
|
||||
may use at most 620 reads; a shape whose workflow reads alone total 640 refuses.
|
||||
Sonar rereads bracket the final workflow freshness pass, so a workflow rerun
|
||||
during the first Sonar rereads and a Sonar change during the freshness reads are
|
||||
both refused. Any resource can still change after its own last read (for example
|
||||
a workflow rerun during the closing Sonar rereads); publication-time revalidation
|
||||
narrows that window but does not remove it. Workflow-run `path`
|
||||
values may use GitHub's documented `path@branch` spelling only for the run's own
|
||||
branch; any other suffix refuses, and receipts and requests keep the bare path.
|
||||
|
||||
|
||||
@@ -36,9 +36,12 @@ MAX_JSON = 2 * 1024 * 1024
|
||||
ISSUE_METRICS = {'BUG': 'bugs', 'VULNERABILITY': 'vulnerabilities', 'CODE_SMELL': 'code_smells'}
|
||||
ACCEPTED_METRICS = {'bugs': 'BUG', 'vulnerabilities': 'VULNERABILITY', 'code_smells': 'CODE_SMELL', 'security_rating': 'VULNERABILITY', 'new_security_rating': 'VULNERABILITY'}
|
||||
FINGERPRINT = {'key', 'rule', 'component', 'type', 'severity', 'hash'}
|
||||
# Revalidation's fixed Sonar snapshot, gate/latest rereads, ancestry reads and the
|
||||
# post-collection unresolved-issue reread.
|
||||
FIXED_READS = 18
|
||||
# Revalidation's fixed Sonar snapshot, gate/latest rereads, ancestry reads and the two
|
||||
# post-collection Sonar reread pairs (latest analysis plus one issue page) that bracket the
|
||||
# final workflow freshness pass. minimum_reads() = FIXED_READS + per-workflow reads, and the
|
||||
# contract admits a shape only when that total is <= MAX_READS: per-workflow reads may total
|
||||
# at most MAX_READS - FIXED_READS, so a shape whose workflow reads alone total 640 refuses.
|
||||
FIXED_READS = 20
|
||||
|
||||
|
||||
def digest(value):
|
||||
@@ -274,8 +277,8 @@ def _workflows(reads, policy, producer, now):
|
||||
|
||||
|
||||
def _fresh_workflows(reads, policy, producer, facts):
|
||||
# Collection-wide final pass, after all workflows and Sonar reads. Earlier
|
||||
# workflows can be rerun while later evidence is being collected.
|
||||
# Collection-wide final pass, after all workflows and the first Sonar rereads (and
|
||||
# before the closing ones). Earlier workflows can be rerun while later evidence is collected.
|
||||
for wanted, selected in zip(policy['required_workflows'], facts):
|
||||
run = _run(reads, policy, producer, wanted)
|
||||
n.require((run['id'], run['run_attempt'], run['run_number']) == (selected['run_id'], selected['run_attempt'], selected['run_number']), 'newer quality run supersedes selection before collection completed')
|
||||
@@ -452,6 +455,24 @@ def _snapshot(reads, s, producer, now):
|
||||
return {'analysis': analysis, 'ce': task, 'ce_sha256': digest(task), 'check_id': check_id, 'gate': gate, 'remaining': remaining, 'fixes': fixes, 'scope_trust': 'provider-resolved', 'issue_count': len(rows), 'issues_sha256': digest(remaining), 'proofs_sha256': digest(fixes)}
|
||||
|
||||
|
||||
def _final_rereads(reads, s, policy, producer, snapshot, workflows, now, stage):
|
||||
"""Sonar rereads bracket the final workflow freshness pass.
|
||||
|
||||
This covers two cases: a workflow rerun during the first Sonar rereads is caught by the
|
||||
freshness pass, and a Sonar change (new analysis, severity or resolution) during the
|
||||
freshness reads is caught by the closing Sonar rereads. It does not close the race: each
|
||||
resource can still change after its own last read, for example a workflow rerun during the
|
||||
closing Sonar rereads, which read no workflow state. Publication-time revalidation narrows
|
||||
that window but does not remove it.
|
||||
"""
|
||||
for final in (False, True):
|
||||
n.require(_latest(reads, s, producer['source_commit'], now) == snapshot['analysis'],
|
||||
'latest Sonar analysis changed before ' + stage + ' completed')
|
||||
_same_unresolved(reads, s, snapshot)
|
||||
if not final:
|
||||
_fresh_workflows(reads, policy, producer, workflows)
|
||||
|
||||
|
||||
def admit_candidate(policy, producer, github, sonar, *, now):
|
||||
"""Read candidate quality first; caller must separately admit P/bootstrap."""
|
||||
s = contract(policy, producer)
|
||||
@@ -462,9 +483,7 @@ def admit_candidate(policy, producer, github, sonar, *, now):
|
||||
workflows = _workflows(reads, policy, producer, now)
|
||||
# Main may advance beyond C, but may not drop C during collection.
|
||||
_ancestry(reads, producer['source_commit'], None)
|
||||
n.require(_latest(reads, s, producer['source_commit'], now) == snapshot['analysis'], 'latest Sonar analysis changed before admission completed')
|
||||
_same_unresolved(reads, s, snapshot)
|
||||
_fresh_workflows(reads, policy, producer, workflows)
|
||||
_final_rereads(reads, s, policy, producer, snapshot, workflows, now, 'admission')
|
||||
return {'schema': 'craft-nightly-candidate-receipt/v3', 'policy_sha256': digest(policy), 'producer_sha256': digest(producer), 'source_commit': producer['source_commit'], 'sonar': snapshot, 'workflows': workflows, 'main_at_admission': ancestry['main']}
|
||||
|
||||
|
||||
@@ -493,7 +512,5 @@ def revalidate_candidate(policy, producer, receipt, github, sonar, *, now):
|
||||
_ancestry(reads, producer['source_commit'], None)
|
||||
n.require(_workflows(reads, policy, producer, now) == receipt['workflows'], 'selected exact-C quality runs changed/superseded')
|
||||
_ancestry(reads, producer['source_commit'], None)
|
||||
n.require(_latest(reads, s, producer['source_commit'], now) == snapshot['analysis'], 'latest Sonar analysis changed before revalidation completed')
|
||||
_same_unresolved(reads, s, snapshot)
|
||||
_fresh_workflows(reads, policy, producer, receipt['workflows'])
|
||||
_final_rereads(reads, s, policy, producer, snapshot, receipt['workflows'], now, 'revalidation')
|
||||
return receipt
|
||||
@@ -586,7 +586,8 @@ class CandidateTests(unittest.TestCase):
|
||||
|
||||
def test_lineage_page_with_files_or_missing_lineage_fields_refuses(self):
|
||||
good = {'status': 'ahead', 'ahead_by': 1, 'behind_by': 0, 'base_commit': {'sha': C}, 'merge_base_commit': {'sha': C}, 'commits': []}
|
||||
for change in ({'files': []}, {'commits': [{}, {}]}, {'commits': None}, {'merge_base_commit': None}, {'base_commit': None}, {'behind_by': None}):
|
||||
for change in ({'files': []}, {'commits': [{}, {}]}, {'commits': None}, {'merge_base_commit': None}, {'base_commit': None}, {'behind_by': None},
|
||||
{'status': None}, {'ahead_by': None}):
|
||||
data = {**good, **change}
|
||||
data = {key: value for key, value in data.items() if value is not None}
|
||||
self.f.compare_override = data
|
||||
@@ -612,5 +613,43 @@ class CandidateTests(unittest.TestCase):
|
||||
self.assertTrue(changed[0])
|
||||
|
||||
|
||||
def test_issue_change_during_final_workflow_freshness_reads_refuses(self):
|
||||
# The Sonar issue reread must come after the final freshness pass, or a change made
|
||||
# while those last GitHub reads run would go unnoticed.
|
||||
def severity(issues): issues[0]['severity'] = 'CRITICAL'
|
||||
def resolved(issues): issues.pop(0)
|
||||
for revalidate in (False, True):
|
||||
for label, mutate in (('severity', severity), ('resolved', resolved)):
|
||||
with self.subTest(revalidate=revalidate, change=label):
|
||||
self.f = Fixture()
|
||||
receipt = self.admit() if revalidate else None
|
||||
original = c._fresh_workflows
|
||||
def fresh(*args, **kwargs):
|
||||
mutate(self.f.issues) # Sonar changes while the final freshness reads run.
|
||||
return original(*args, **kwargs)
|
||||
with patch.object(c, '_fresh_workflows', side_effect=fresh) as called, \
|
||||
self.assertRaisesRegex(ValueError, 'Sonar issue inventory changed'):
|
||||
self.revalidate(receipt) if revalidate else self.admit()
|
||||
called.assert_called_once()
|
||||
|
||||
def test_read_budget_leaves_room_for_the_fixed_reads(self):
|
||||
# minimum_reads = FIXED_READS + per-workflow reads, admitted only when <= MAX_READS.
|
||||
def shape(extra_jobs):
|
||||
# 32 manual-input workflows, 14 + 2*jobs reads each; spread the extra jobs.
|
||||
return [{'workflow_id': 100+i, 'workflow_path': f'.github/workflows/ci-{i}.yml', 'workflow_blob': 'e'*40,
|
||||
'jobs': [{'name': f'job-{k}', 'app_id': 15368, 'applicability': 'manual-input-only' if k == 0 else 'required'}
|
||||
for k in range(1 + extra_jobs // 32 + (i < extra_jobs % 32))]} for i in range(32)]
|
||||
per_workflow = lambda workflows: c.minimum_reads(workflows) - c.FIXED_READS
|
||||
fits = shape(54) # 32*14 + 2*(32+54) = 620 = MAX_READS - FIXED_READS
|
||||
self.assertEqual((per_workflow(fits), c.minimum_reads(fits)), (c.MAX_READS - c.FIXED_READS, c.MAX_READS))
|
||||
self.f.policy['required_workflows'] = fits
|
||||
c.contract(self.f.policy, self.f.producer) # exactly at the budget: accepted
|
||||
for extra in (55, 64): # one job over the room left, and workflow reads alone of exactly 640
|
||||
workflows = shape(extra)
|
||||
self.f.policy['required_workflows'] = workflows
|
||||
with self.subTest(per_workflow=per_workflow(workflows)), self.assertRaisesRegex(ValueError, 'read budget'):
|
||||
c.contract(self.f.policy, self.f.producer)
|
||||
self.assertEqual(per_workflow(shape(64)), c.MAX_READS) # workflow reads of exactly 640 refuse once fixed reads are added
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user