Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/scripts/fork-head-gate-workflow.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def bash(script, env, path=None):
inputs = (on.get("workflow_call") or {}).get("inputs") or {}
check("inputs: pr, config, check-name, required-lenses, environment (no pass-drafts: a draft is never posted for)",
set(inputs) == {"pr", "config", "check-name", "required-lenses", "environment"}, str(set(inputs)))
lenses_text = str(inputs.get("required-lenses", {}).get("description"))
check("required-lenses says when none may stand: only with the config's review block in enforce (#348)",
"None = refused" in lenses_text and "enforce" in lenses_text, lenses_text)
example_lenses = next((x for x in EXAMPLE.read_text().splitlines() if "required-lenses:" in x), "")
check("example: the required-lenses comment says the same (none = refused unless the review block enforces)",
"none = the gate refuses" in example_lenses and "enforce" in example_lenses, example_lenses)
check("config, check-name and required-lenses must be given (no default lens list); the Environment defaults to head-gate",
all(inputs.get(k, {}).get("required") is True and "default" not in inputs.get(k, {})
for k in ("config", "check-name", "required-lenses"))
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fork-head-gate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ on:
type: string
required: true
required-lenses:
description: Lens names, one per line, that must have read the head (the caller's; never from the pull request). None = refused.
description: Lens names, one per line, that must have read the head (the caller's; never from the pull request). None = refused, unless the config at the base tip has a review block in mode enforce (its path rules then name the lenses).
type: string
required: true
environment:
Expand Down
2 changes: 1 addition & 1 deletion fork-ci/actions/head-check/gate-caller.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ jobs:
pr: ${{ format('{0}', github.event.pull_request.number || github.event.issue.number || inputs.pr) }}
config: .github/pr-queue.json
check-name: CHECK_NAME
required-lenses: | # one lens name per line; none = the gate refuses
required-lenses: | # one lens name per line; none = the gate refuses, unless the config's review block is in enforce
LENS_NAME
38 changes: 24 additions & 14 deletions fork-ci/actions/head-check/head_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ def __init__(self, path, run=None, bare=None, remote='origin'):
# Bare, git logs in the way the queue does (gh's login, GH_TOKEN), never with a helper of the machine's config.
self.auth = pr_queue.GIT_AUTH if self.bare else ()

def git(self, *a):
def git(self, *a, errors='strict'):
"""errors: how git's output is decoded; 'surrogateescape' for file names, which need not be UTF-8."""
if self.run:
return self.run(*a)
with tempfile.TemporaryDirectory(prefix='head-check-') as empty:
return subprocess.run(['git', *GIT_SAFE, *self.auth, '--git-dir', self.git_dir, *a], cwd=empty, text=True,
capture_output=True, env={**os.environ, 'GIT_TERMINAL_PROMPT': '0',
errors=errors, capture_output=True, env={**os.environ, 'GIT_TERMINAL_PROMPT': '0',
'GIT_ATTR_NOSYSTEM': '1'})

def problem(self):
Expand Down Expand Up @@ -181,8 +182,9 @@ def same_change(checkout, head, read):
return False, f'{head[:12]} is not the change read at {read[:12]}, nor that change merged with the base'


def verdict(pr, review, checkout, required=None, pass_drafts=False):
"""(ok, reason, rows): rows is [(lens, ok, short reason)] per lens, required ones first."""
def verdict(pr, review, checkout, required=None, pass_drafts=False, rules_name_lenses=False):
"""(ok, reason, rows): rows is [(lens, ok, short reason)] per lens, required ones first. rules_name_lenses: the
config's review rules (enforce) added required lenses, so a missing one may come from there too."""
required = required or {}
head = str(pr.get('headRefOid') or '').lower()
if not SHA.fullmatch(head):
Expand All @@ -207,9 +209,9 @@ def verdict(pr, review, checkout, required=None, pass_drafts=False):
text = f'{"override " if override else "read "}{sha[:12]}: {how}'
rows.append(((f'{key} (required)' if key in required else key), bool(sha and read and ok), text))
if missing:
where = ' or the review rules of the config' if rules_name_lenses else ''
return False, (f'required lens without a read: {", ".join(missing)}. No counted lens comment has that name: '
f'check it in required-lenses (one name per line) or the review rules of the config; lenses '
f'found: {found}'), rows
f'check it in required-lenses (one name per line){where}; lenses found: {found}'), rows
if not read:
return False, why, rows
return ok, how, rows
Expand Down Expand Up @@ -261,8 +263,10 @@ def changed_paths(checkout, base, head):
f'or the histories are unrelated){": " + said if said else ""}')
changes = []
for b in bases:
# A file name need not be UTF-8: its bytes stay as lone surrogates, so the path is judged by its folder like any
# other (review_judge.shown writes them as \xNN), never a UnicodeDecodeError that fails the head unexplained.
d = checkout.git('diff', '--no-ext-diff', '--no-textconv', '--no-relative', '--ignore-submodules=none',
'--no-color', '--name-status', '-z', '-M', b, head, '--')
'--no-color', '--name-status', '-z', '-M', b, head, '--', errors='surrogateescape')
if d.returncode != 0:
return None, f'git diff {b[:12]} {head[:12]} failed: {(d.stderr or "").strip()[:200]}'
try:
Expand All @@ -286,14 +290,16 @@ def path_judgement(checkout, rules, config_path):
return None, str(e)


def path_row(mode, judgement):
"""The summary row of the path rules: what they decide (enforce) or would have said (shadow). Never a verdict of its
own: the required lens rows and the reason carry that."""
def path_row(mode, judgement, warnings=()):
"""The summary row of the path rules: what they decide (enforce) or would have said (shadow), and the config check's
warnings (shadow only: enforce refuses them). Never a verdict of its own: the required lens rows and the reason
carry that."""
said = ''.join(f'; config warning: {w}' for w in warnings)
if mode == 'enforce':
return ('path rules (enforce)', True, f'the path rules decide: {judgement.text()}; the required-lenses input '
'is required as well')
f'is required as well{said}')
return ('path rules (shadow)', True, f'the path rules would have said: {judgement.text()}; the required-lenses '
'input decides (mode shadow)')
f'input decides (mode shadow){said}')


def decide(repo, n, config_path, checkout, required, pass_drafts, gh, sleep, out, problems):
Expand All @@ -311,10 +317,14 @@ def decide(repo, n, config_path, checkout, required, pass_drafts, gh, sleep, out
if problems:
out('::error::head check refused to start: ' + escape('; '.join(problems)))
return REFUSED, False, '; '.join(problems), []
enforced = False
if paths is not None:
row = path_row(cfg.review.mode, paths)
row = path_row(cfg.review.mode, paths, cfg.review.warnings)
for w in cfg.review.warnings:
out('::warning::the review rules of the config: ' + escape(w))
out(f'{row[0]}: {escape(row[2])}')
if cfg.review.mode == 'enforce':
enforced = True
required = dict(required or {})
for name in paths.lenses:
required.setdefault(lens.lens_key(name), name)
Expand All @@ -323,7 +333,7 @@ def decide(repo, n, config_path, checkout, required, pass_drafts, gh, sleep, out
return 1, False, f'could not read the pull request from GitHub after {ATTEMPTS} tries: {why}', []
review = lens.Review(cfg.bot_logins, cfg.override_prefix, cfg.override_association, cfg.lens_author_associations,
log=lambda text: out(escape(text)))
ok, reason, rows = verdict(pr, review, checkout, required, pass_drafts)
ok, reason, rows = verdict(pr, review, checkout, required, pass_drafts, enforced)
if paths is not None:
rows = [row] + rows
return (0 if ok else 1), ok, reason, rows
Expand Down
96 changes: 92 additions & 4 deletions fork-ci/actions/head-check/head_check.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ def commit(self, files, parents=(), msg='c'):
if name != '.git':
p = os.path.join(self.work, name)
shutil.rmtree(p) if os.path.isdir(p) else os.remove(p)
for path, text in files.items():
os.makedirs(os.path.dirname(os.path.join(self.work, path)) or self.work, exist_ok=True)
with open(os.path.join(self.work, path), 'w') as f:
for path, text in files.items(): # a bytes path: a file name that need not be UTF-8
where = os.path.join(os.fsencode(self.work) if isinstance(path, bytes) else self.work, path)
os.makedirs(os.path.dirname(where), exist_ok=True)
with open(where, 'w') as f:
f.write(text)
self.git(self.work, 'add', '-A')
tree = self.git(self.work, 'write-tree')
Expand Down Expand Up @@ -769,6 +770,93 @@ def test_a_huge_change_is_judged_whole(self): # no API page cap: every path is
self.assertEqual(code, 1, out)
self.assertIn('critical, lenses data-1, data-2 (3501 changed paths', out)

def test_a_file_name_that_is_not_utf8_is_judged_by_its_folder(self): # #348 item 1: no UnicodeDecodeError
for mode in ('enforce', 'shadow'):
with self.subTest(mode=mode):
base = self.base(mode)
tree = base_files(cfg_raw=review_cfg(mode), **{**PROMPTS, **self.STORE})
tree[b'src/storage/bad\xff.py'] = 'b\n'
tree[b'docs/bad\xfe.md'] = 'd\n'
head = self.w.commit(tree, [base])
code, out = self.check(base, head, ['code'])
self.assertIn('critical, lenses data-1, data-2 (2 changed paths', out)
self.assertIn('src/storage/bad\\xff.py', out)
if mode == 'enforce':
self.assertEqual(code, 1, out)
self.assertEqual(self.check(base, head, ['data-1', 'data-2'])[0], 0)
else: # shadow: what main says (every lens read the head), the row only reports
self.assertEqual(code, 0, out)
light = dict(base_files(cfg_raw=review_cfg(mode), **{**PROMPTS, **self.STORE}))
light[b'docs/bad\xfe.md'] = 'd\n'
code, out = self.check(base, self.w.commit(light, [base]), comments=[])
self.assertEqual(code, 0, out)
self.assertIn('light, lenses none (1 changed path; docs (light): docs/bad\\xfe.md', out)

def test_a_lens_name_with_capitals_is_required_by_its_key(self): # #348 item 5: reads are keyed by lens_key
block = json.loads(json.dumps(REVIEW_BLOCK))
block['lenses'] = {('Data-One' if n == 'data-1' else 'Data-Two' if n == 'data-2' else n): x
for n, x in block['lenses'].items()}
block['rules'][0]['lenses'] = ['Data-One', 'Data-Two']
cfg = {**CFG_RAW, 'review': block}
base = self.w.commit(base_files(cfg_raw=cfg, **{**PROMPTS, **self.STORE}), [self.m0])
head = self.head(base, cfg=cfg, **{'src/storage/s.py': lines(40, ['x'], 3)})
code, out = self.check(base, head, ['code'])
self.assertEqual(code, 1, out)
self.assertIn('required lens without a read: Data-One, Data-Two', out)
for names in (['Data-One', 'Data-Two'], ['data-one', 'DATA-TWO']):
with self.subTest(names=names):
code, out = self.check(base, head, names)
self.assertEqual(code, 0, out)

def test_the_summary_row_says_what_the_path_rules_decide(self): # #348 item 5: the row itself, in the table
for mode, says in (('enforce', '| path rules (enforce) | ok | the path rules decide: critical, lenses '
'data-1, data-2 (1 changed path'),
('shadow', '| path rules (shadow) | ok | the path rules would have said: critical, lenses '
'data-1, data-2 (1 changed path')):
with self.subTest(mode=mode):
base = self.base(mode)
head = self.head(base, mode, **{'src/storage/s.py': lines(40, ['x'], 3)})
path = self.w.checkout(base, head)
summary = os.path.join(self.w.dir, f'summary-{mode}.md')
gh = fake_gh((0, json.dumps(pr([self.p.lens(n, head) for n in ('data-1', 'data-2')], head=head))))
code, out = run_cli(path, gh, env={'GITHUB_STEP_SUMMARY': summary})
self.assertEqual(code, 0, out)
with open(summary, encoding='utf-8') as f:
table = f.read().splitlines()
rows = [x for x in table if x.startswith('| ') and not x.startswith(('| lens ', '| --- '))]
self.assertTrue(rows[0].startswith(says), rows)

def test_a_rule_order_warning_of_a_shadow_config_is_in_the_row(self): # #348 item 3: enforce refuses it instead
rules = [dict(REVIEW_BLOCK['rules'][1], paths=['**/*.md']), REVIEW_BLOCK['rules'][0], REVIEW_BLOCK['rules'][2]]
for mode in ('shadow', 'enforce'):
with self.subTest(mode=mode):
cfg = review_cfg(mode, rules=rules)
base = self.w.commit(base_files(cfg_raw=cfg, **{**PROMPTS, **self.STORE}), [self.m0])
head = self.head(base, cfg=cfg, **{'src/storage/notes.md': 'n\n'})
code, out = self.check(base, head, ['code'])
if mode == 'shadow':
self.assertEqual(code, 0, out)
self.assertIn("::warning::the review rules of the config: rule 'docs' (light) comes before the "
"heavier rule 'data' (critical)", out)
self.assertIn("would have said: light, lenses none", out)
self.assertIn("config warning: rule 'docs' (light) comes before", out)
else:
self.assertEqual(code, H.REFUSED, out)
self.assertIn("rule 'docs' (light) comes before the heavier rule 'data' (critical)", out)

def test_without_a_review_block_a_missing_lens_reads_as_before(self): # #348 item 6: byte for byte
base = self.base(mode=None)
head = self.head(base, mode=None, **{'src/app.py': 'a\n'})
code, out = self.check(base, head, ['correctness'], required='design')
self.assertEqual(code, 1, out)
self.assertIn('head check failed: required lens without a read: design. No counted lens comment has that '
'name: check it in required-lenses (one name per line); lenses found: correctness', out)
self.assertNotIn('review rules', out)
base = self.base('enforce') # the path rules can name a missing lens: the reason says where to look
head = self.head(base, **{'src/app.py': 'a\n'})
code, out = self.check(base, head, ['correctness'])
self.assertIn('check it in required-lenses (one name per line) or the review rules of the config;', out)

def test_a_rerun_gives_the_same_answer(self): # runs again: nothing stored between runs
base = self.base()
head = self.head(base, **{'src/storage/s.py': lines(40, ['x'], 3)})
Expand All @@ -788,7 +876,7 @@ def fake(self, *answers):
answers = list(answers)

class Checkout:
def git(self, *a):
def git(self, *a, **kw):
code, out = answers.pop(0)
return types.SimpleNamespace(returncode=code, stdout=out, stderr='boom' if code else '')

Expand Down
18 changes: 18 additions & 0 deletions fork-ci/actions/head-check/head_gate.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,24 @@ def test_an_empty_lens_list_under_enforce_still_needs_the_path_rules_lenses(self
required_lenses='')
self.assertEqual(self.verdict()['conclusion'], 'success', self.verdict()['summary'])

def test_an_empty_lens_list_never_stands_on_a_config_that_cannot_be_read(self): # #348 item 5: this step alone
bare = self.w.commit({'other.py': 'o\n'}, [])
bad = self.w.commit(T.base_files(cfg_raw={**T.CFG_RAW, 'review': {**T.REVIEW_BLOCK, 'mode': 'loud'}},
**T.PROMPTS), [self.m0])
good = self.review_base('enforce')
for base, says in ((bare, 'has no config'), (bad, 'mode'), (good, None)):
with self.subTest(says=says):
path, why = G.bare_repository(self.work, 'gate')
self.assertIsNone(why)
checkout = H.Checkout(path, bare=(base, self.read), remote=self.w.remote)
self.assertIsNone(G.fetch(checkout, base, self.read))
problem = G.empty_lenses_problem(checkout, T.CONFIG_PATH)
if says is None:
self.assertIsNone(problem)
else:
self.assertIsNotNone(problem)
self.assertIn(says, problem)

def test_a_review_block_the_base_tip_cannot_parse_is_a_failure(self): # fail closed, whatever the input says
cfg = {**T.CFG_RAW, 'review': {**T.REVIEW_BLOCK, 'mode': 'loud'}}
base = self.w.commit(T.base_files(cfg_raw=cfg, **T.PROMPTS), [self.m0])
Expand Down
5 changes: 5 additions & 0 deletions fork-ci/actions/pr-queue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ The config is refused when:
catch-all rule (it could never match): a new folder must never fall to no rule
or to no lens;
- there is no `critical` rule (the built-in protected paths below take its lenses);
- in `enforce`, a lighter rule comes before a heavier rule and some path matches
a glob of both (`**/*.md` light above `src/**` critical: `src/notes.md` would
belong to the light rule and need no lens). Put heavier rules first. The
catch-all rule is exempt: a lighter rule above it is the normal case. In
`shadow` the same order is a warning in the gate's summary row, not a refusal;
- a glob is not in the dialect below, or a rule name or glob is written twice.

**Globs.** Paths are repo-relative and use `/`. `*` is any characters inside one
Expand Down
Loading
Loading