Files
seaweedfs/seaweed-block/.github/scripts/pr-gate-base-test.py
T
Ping Qiu 6dffc0cc60 ci: run PR review gates read-only from a separate base checkout (#1013)
Merge note (batch b6; pull_request_target spec-trace workflow has a startup_failure, so its checks run locally):
- One Test run (47206535775) succeeded (go test (linux), process-level tests (real binaries)) on the integration tree 2b51fa97 = main e8b56283 + #1013 #1008  in this order; these squash merges reproduce that tree, verified after the last one.
- spec-trace-check.sh and public-text-check.sh, run with main's scripts against this head and body: both exit 0.
- Code review ACCEPT on 57ec9ee8 under the stated contract: the spec-trace and post-merge-review workflows run on pull_request with read-only permissions on hosted runners and check out the base branch's validators, so they give authors early feedback; they are advisory, and the merge gate runs the base branch's checks locally before every merge.
- Workflow review: both gate workflows, test.yml and the base-checkout test script reviewed at this exact head (review 6177749775).
- Security review ACCEPT on 57ec9ee8: nothing from the PR is executed; the base is checked out separately, the head is read as objects only, title and body pass through the environment, and the token is contents:read.
2026-12-03 13:48:31 -08:00

59 lines
3.9 KiB
Python

"""Trusted-base gate contracts and a PR that replaces its own checkers."""
from pathlib import Path
import os, shutil, subprocess, tempfile, unittest
ROOT = Path(__file__).resolve().parents[2]
class BaseGateTests(unittest.TestCase):
def test_readonly_events_and_separate_base_checkout(self):
for filename in ('spec-trace.yml', 'post-merge-review.yml'):
with self.subTest(workflow=filename):
text=(ROOT/'.github/workflows'/filename).read_text()
self.assertIn(' pull_request:',text)
self.assertNotIn('pull_request_target:',text)
self.assertIn(' contents: read',text)
self.assertNotIn('secrets.',text)
self.assertIn(' runs-on: ubuntu-latest',text)
self.assertIn('ref: ${{ github.event.pull_request.base.sha }}',text)
self.assertIn('path: trusted-base',text)
self.assertIn('working-directory: trusted-base',text)
self.assertIn('persist-credentials: false',text)
self.assertNotIn('ref: ${{ github.event.pull_request.head.sha }}',text)
self.assertNotIn('ref: ${{ github.sha }}',text)
text=(ROOT/'.github/workflows/spec-trace.yml').read_text()
self.assertIn('go-version-file: trusted-base/go.mod',text)
def test_pr_cannot_replace_base_validators_or_allowlist(self):
with tempfile.TemporaryDirectory() as tmp:
root=Path(tmp); source=root/'source'; source.mkdir()
scripts=source/'.github/scripts'; scripts.mkdir(parents=True)
names=('spec-trace-check.sh','post-merge-review-check.sh','post-merge-review-changed.sh')
for name in names: shutil.copyfile(ROOT/'.github/scripts'/name,scripts/name)
allow=source/'.github/post-merge-review-allow.txt'; allow.write_text('docs/**\n')
def git(*args,cwd=source):
return subprocess.check_output(['git',*args],cwd=cwd,stderr=subprocess.PIPE,timeout=15)
git('init','-b','main'); git('config','user.name','Example'); git('config','user.email','example@example.invalid'); git('config','commit.gpgsign','false')
git('add','.'); git('commit','-m','base')
base=git('rev-parse','HEAD').decode().strip()
(source/'docs').mkdir(); (source/'docs/example.md').write_text('Documentation')
git('add','.'); git('commit','-m','documentation')
good=git('rev-parse','HEAD').decode().strip()
for name in ('spec-trace-check.sh','post-merge-review-check.sh'):
(scripts/name).write_text('#!/bin/sh\ntouch ../pr-code-ran\nexit 0\n')
allow.write_text('*\n')
git('add','.'); git('commit','-m','replace validators')
bad=git('rev-parse','HEAD').decode().strip()
checkout=root/'trusted-base'; git('clone','--quiet',str(source),str(checkout)); git('checkout','--detach',base,cwd=checkout)
self.assertEqual(git('rev-parse','HEAD',cwd=checkout).decode().strip(),base)
body=checkout/'body.txt'
valid='Feature: documentation\nMachine/INV: DOCS\nModel: NONE (documentation fixture only)\nRed-first: fixture validation\n'
for head,description,success in ((good,valid,True),(bad,'missing trace',False)):
body.write_text(description)
changes=git('diff','-z','--name-status','--no-renames',base+'...'+head,cwd=checkout)
for command in (['spec-trace-check.sh',str(body)],['post-merge-review-check.sh','.github/post-merge-review-allow.txt']):
result=subprocess.run(['bash','.github/scripts/'+command[0],*command[1:]],cwd=checkout,input=changes,capture_output=True,timeout=15)
self.assertEqual(result.returncode==0,success,result.stdout.decode()+result.stderr.decode())
self.assertFalse((root/'pr-code-ran').exists())
if __name__=='__main__': unittest.main()