the-custodian/docs/changes/netkingdom-target-2026-10-17/apps-release-source-integration-r1/apply_source.py

96 lines
5.2 KiB
Python

"""Integrate only exact independently accepted build source and owner tasks."""
from pathlib import Path
import argparse
import hashlib
import json
import re
import subprocess
import sys
HERE = Path(__file__).resolve().parent
ROOT = Path('/home/worsch/railiance-apps')
PACKET = HERE.parent / 'apps-forgejo-release-build-r2'
OLD_PACKET = HERE.parent / 'apps-forgejo-release-build-r1'
BASE = '944b30ad63ab054cc24617e374e177d34cd572da'
PLAN = 'workplans/RAPPS-WP-0016-forgejo-netkingdom-login.md'
def sha(data):
return hashlib.sha256(data).hexdigest()
def git(*args):
return subprocess.check_output(['git', '-C', str(ROOT), *args],
stderr=subprocess.PIPE, timeout=45).decode().strip()
parser = argparse.ArgumentParser()
parser.add_argument('--review', type=Path, required=True)
parser.add_argument('--review-sha256', required=True)
args = parser.parse_args()
assert sha(args.review.read_bytes()) == args.review_sha256.removeprefix('sha256:')
review = json.loads(args.review.read_text())
assert review['source_integration_accepted'] is True
assert review['verdict'] == 'accept'
refinement = json.loads((PACKET / 'source-refinement.json').read_text())
assert sha((PACKET / 'source-refinement.json').read_bytes()) == review['candidate_source_refinement_sha256'].removeprefix('sha256:')
assert refinement['owner_postimages'] == review['owner_postimages']
patch_hash = sha((PACKET / 'apps-release-build-source.patch').read_bytes())
assert 'sha256:' + patch_hash == refinement['patch_sha256']
assert review['candidate_patch_sha256'].removeprefix('sha256:') == patch_hash
assert git('rev-parse', 'HEAD') == BASE
assert git('ls-remote', 'origin', 'refs/heads/main').split()[0] == BASE
assert set(git('diff', '--name-only', 'HEAD').splitlines()) <= {'.custodian-brief.md', 'WORK-RECORDS.md'}
originals = {n: {'worktree_sha256': sha((ROOT / n).read_bytes()),
'index': git('ls-files', '--stage', '--', n)}
for n in ('.custodian-brief.md', 'WORK-RECORDS.md') if (ROOT / n).exists()}
inventory = json.loads((OLD_PACKET / 'source-inventory.json').read_text())
for n, expected in inventory['canonical_public_files_unchanged'].items():
assert sha((ROOT / n).read_bytes()) == expected, n
for n, expected in refinement['owner_postimages'].items():
assert sha((PACKET / 'owner-source' / n).read_bytes()) == expected.removeprefix('sha256:'), n
if n == 'Makefile':
assert (ROOT / n).read_bytes() == (OLD_PACKET / 'preimages' / n).read_bytes()
else:
assert not (ROOT / n).exists(), n
assert (ROOT / PLAN).read_bytes() == (HERE / 'preimage.md').read_bytes()
plans = {str(p.relative_to(ROOT)): sha(p.read_bytes()) for p in (ROOT / 'workplans').rglob('*.md')}
before_blocks = re.findall(r'```task\n.*?```', (ROOT / PLAN).read_text(), re.S)
candidate = (HERE / 'workplan.md').read_text()
assert len(before_blocks) == 5 and all(b in candidate for b in before_blocks)
sys.path.insert(0, '/home/worsch/repo-manager/src')
from repo_manager.identifiers import ensure_missing_work_record_identifiers
preview = ensure_missing_work_record_identifiers(ROOT, execute=False)
assert preview['ok'] and not preview['assignments'] and not preview['files_changed']
for n in refinement['owner_postimages']:
target = ROOT / n
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes((PACKET / 'owner-source' / n).read_bytes())
(ROOT / PLAN).write_text(candidate)
preview = ensure_missing_work_record_identifiers(ROOT, execute=False)
(HERE / 'identifier-preview.json').write_text(json.dumps(preview, indent=2) + '\n')
assert preview['ok'] and preview['files_changed'] == [PLAN] and len(preview['assignments']) == 4
assigned = ensure_missing_work_record_identifiers(ROOT, execute=True)
(HERE / 'identifier-assignment.json').write_text(json.dumps(assigned, indent=2) + '\n')
assert assigned['ok'] and assigned['files_changed'] == [PLAN]
for n, expected in plans.items():
if n != PLAN:
assert sha((ROOT / n).read_bytes()) == expected, n
for n, state in originals.items():
assert sha((ROOT / n).read_bytes()) == state['worktree_sha256']
assert git('ls-files', '--stage', '--', n) == state['index']
assert all(b in (ROOT / PLAN).read_text() for b in before_blocks)
files = [{'path': n, 'sha256': sha((ROOT / n).read_bytes())}
for n in [*refinement['owner_postimages'], PLAN]]
cfg = {'owner': 'railiance-apps', 'base_revision': BASE,
'message': 'Add pinned Forgejo release build path and track operational integration', 'files': files}
(HERE / 'publication-inputs.json').write_text(json.dumps(cfg, indent=2) + '\n')
(HERE / 'source-application.json').write_text(json.dumps({
'base': 'git:' + BASE, 'review_sha256': 'sha256:' + sha(args.review.read_bytes()),
'patch_sha256': 'sha256:' + patch_hash, 'files': files,
'original_task_blocks_preserved': len(before_blocks), 'other_workplans_preserved': len(plans) - 1,
'unrelated_state': originals, 'native_actions': False,
'image_values_and_registry_values_unchanged': True,
'formal_owner_assent': False}, indent=2) + '\n')
print(json.dumps({'status': 'source_applied', 'files': len(files), 'new_task_ids': 4,
'original_task_blocks_preserved': 5, 'native_actions': False}))