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
3 changes: 2 additions & 1 deletion fork-ci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ Two habits every action expects:
- **A job that others `need` must not skip them on failure:** GitHub counts a skipped required job as passing. Give
the dependent job `if: ${{ !cancelled() }}` and a fallback for an empty output (fleet-route's README shows one).

To move to a new release, change the SHA and the comment together; read the new tag's release notes first.
To move to a new release, change the SHA and the comment together; read the new tag's release notes first
([RELEASE-NOTES.md](RELEASE-NOTES.md): what changed, and what to do before or after the move).

### Outside a workflow

Expand Down
50 changes: 50 additions & 0 deletions fork-ci/RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# fork-ci release notes

Read this before you move a pin to a new `ci/vN` (see [Pinning](README.md#pinning)). Newest first. Every release
commit holds this file as main had it, so the entries in the new tag's copy that your pinned tag's copy lacks are what
the move brings:

```sh
git diff ci/vOLD ci/vNEW -- fork-ci/RELEASE-NOTES.md
```

An entry says what changed, and under **Upgrade** what to do before or after the move. An entry without one needs
nothing.

## After ci/v14

### head-check: the config a pull request would merge must parse (#388)

The head check (and the gate) now reads the queue config as the merge would leave it: GitHub's merge commit in
checkout mode, `git merge-tree` of the base and the head in bare mode. A pull request is refused (exit 2, with the
reason) when that config:

- does not parse (`config.parse`: not JSON, empty, an unknown or bad field, a key written twice, a bad `review` block
in any mode), or is in `enforce` with a rule order `enforce` refuses;
- is deleted, or is no longer a file;
- is over the caps on a config a pull request writes: 32 KiB, 128 characters per glob in `review.rules[].paths`, and
2,048 glob characters in all;
- cannot be read or checked at all (any error is a refusal that names it, never a crash or a pass).

Merged, any of these would leave a base config that refuses every pull request after it, the fix too. A pull request
that leaves the config exactly as the base has it is not read again, so the caps never apply to a base config that is
already bigger. In bare mode, a head that does not merge cleanly into the base is refused too (GitHub cannot merge it
either).

**Upgrade:**

- To delete the queue config, first take its checks out of the branch ruleset; a pull request that deletes it is
refused.
- A config change that needs more than the caps has to be pushed by an owner.
- After an owner fixes a refused base config by hand, open pull requests no longer need to merge the base first: they
are judged on what they would merge.

### pr-queue and head-check: stricter rule order in `review` (#378)

A rule of the same weight above another rule that lacks one of its lenses is now a rule-order problem, and only the
match-every globs of the catch-all rule are exempt (its explicit globs, such as `src/**` in `["src/**", "**"]`, are
compared like any other rule's). Shadow warns, `enforce` refuses.

**Upgrade:** a base config already in `enforce` with such an order is refused after the move, and so is every pull
request, the one that fixes it too. Before you move the pin, check the config with the new release (or switch it to
`shadow`); otherwise an owner has to push the fix by hand.
155 changes: 129 additions & 26 deletions fork-ci/actions/head-check/head_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
reads the inline review comments (on a line of the diff), which count like any other comment.
- pr_queue.holds: the head is that commit, or a clean merge of the base into it, byte for byte (a queue update).
The config is the queue's own JSON file (config.py), read from the base commit, so the check and the queue read the
same people and override, and a pull request cannot change them for its own check.
same people and override, and a pull request cannot change them for its own check. The config the merge would leave is
read for one thing only: it must parse, under size caps, or the head is refused (merged_config_problem), since merged
it would refuse every head after it.
`--required-lenses` (the project's, per pull request if it likes) adds lenses that must be among the reads.
The config's optional `review` block (../pr-queue/README.md, "Review rules") names lenses by changed path: the paths
are `git diff --name-status -M` from the merge base to the head (changed_paths), judged by review_judge.judge. In
Expand Down Expand Up @@ -312,7 +314,7 @@ def decide(repo, n, config_path, checkout, required, pass_drafts, gh, sleep, out
if not problems:
cfg = base_config(checkout, config_path, problems)
if cfg is not None:
why = head_order_problem(checkout, config_path)
why = merged_config_problem(checkout, config_path)
if why:
problems.append(why)
if cfg is not None and cfg.review is not None:
Expand Down Expand Up @@ -388,6 +390,24 @@ def cli(argv=None, env=None, gh=gh_run, sleep=time.sleep, git=None, out=print):
return code


# Caps on a config a pull request writes, checked before it is parsed: the rule order check searches every pair of
# globs (review_config._shadowed), so its work grows with the square of the glob characters. Measured: a 20 KB config
# of two long globs took 2.9 GB, 100 rules took 15.6 s. Under these caps the worst shapes take about a second
# (head_check.test.py, HostileConfig). The base's own config is never capped: a pull request that leaves it as it is
# is not read again (merged_config_problem).
MAX_CONFIG_BYTES = 32 * 1024
MAX_GLOB_LENGTH = 128 # characters of one glob in review.rules[].paths
MAX_GLOB_TOTAL = 2048 # characters of all of them together
LOCKS = ('the base config would refuse every pull request after it, the one that fixes it too, and the queue would '
'stop')


def repo_path(path):
"""The config's path in a tree: `git show rev:./x` resolves against the work tree, and a bare repository has
none."""
return path[2:] if path.startswith('./') else path


def base_config(checkout, path, problems):
"""The queue's config as the base commit holds it (the merge commit's first parent), never the pull request's own
copy: a pull request cannot widen whose comments count for its own check. None with a problem added."""
Expand All @@ -397,8 +417,7 @@ def base_config(checkout, path, problems):
return None
# Bare, the base is the tip the gate read from GitHub's API, fetched by sha: what the pull request's branch says
# about its own config is never read.
rel = path[2:] if path.startswith('./') else path
r = checkout.git('show', f'{parents[0]}:{rel}')
r = checkout.git('show', f'{parents[0]}:{repo_path(path)}')
if r.returncode != 0:
problems.append(f'the base commit {parents[0][:12]} has no config {path}')
return None
Expand All @@ -409,35 +428,119 @@ def base_config(checkout, path, problems):
return None


def head_order_problem(checkout, path):
"""Why the head's own config must not merge, or None: its `review` block is in enforce with a rule order enforce
refuses (review_config.enforce_order_problems). The config that judges a head is always the base's, so such a
head is judged by the old block and could merge; after it, the base config refuses every head, the one that fixes
the order too, and the queue stops. The only thing read from the head's config: it is never parsed for anything
else, and a head config that is unreadable, not JSON, without a `review` block or with another problem is left to
the rules of the base (that last one also stops every head once merged: a check of its own, not this one)."""
def merged_config_problem(checkout, path):
"""Why the config this pull request would merge must not merge, or None. The config that judges a head is the
base's, so a head is judged by the old config and could merge a new one that does not parse (or is in enforce with
a rule order enforce refuses, or is deleted); after it, the base config refuses every head, the one that fixes it
too, and the queue stops. So the config the merge leaves must parse (config.parse), under the caps above. It is
the merge's, never the head's own copy: two pull requests that each leave a good config can merge into a bad one,
and an older head's stale copy says nothing once an owner fixed the base. Checkout mode reads GitHub's merge commit
(HEAD), bare mode `git merge-tree` of the base and the head (a head that does not merge cleanly is refused: the
merged config is unknown, and GitHub cannot merge it either). The merged config is never used for anything else:
the rules that judge the head stay the base's. Any exception is a refusal that says why, never a crash or a pass."""
parents = checkout.merge_parents()
if not parents:
return None # base_config refused it already
rel = path[2:] if path.startswith('./') else path
# errors='replace': only the rule order is read, and a name with a broken byte is still a name. A head that leaves
# the config as the base has it gets nothing new here: base_config already refused an enforce block with this order.
r = checkout.git('show', f'{parents[1]}:{rel}', errors='replace')
if r.returncode != 0:
return None # the head deletes the config: judged by the base's rules, as every change to it
try:
raw = json.loads(r.stdout)
except ValueError:
return None
return _merged_config_problem(checkout, path, *parents)
except Exception as e: # unknown never passes
return (f'the config {path} this pull request would merge could not be checked, so it is not passed: '
f'{type(e).__name__}: {review_judge.shown(str(e))[:300]}')


def _merged_config_problem(checkout, path, base, head):
rel = repo_path(path)
merged = 'HEAD'
if checkout.bare:
m = checkout.git('-c', 'merge.directoryRenames=true', 'merge-tree', '--write-tree', base, head,
errors='surrogateescape')
tree = (m.stdout or '').split()[:1]
if m.returncode == 1:
return (f'the head {head[:12]} does not merge cleanly into the base {base[:12]}, so the config it would '
'merge is unknown: merge the base into the branch and resolve the conflict')
if m.returncode != 0 or not tree or not SHA.fullmatch(tree[0]):
said = review_judge.shown((m.stderr or m.stdout or '').strip()[:200])
return f'git merge-tree of the base {base[:12]} and the head {head[:12]} failed: {said}'
merged = tree[0]
new = checkout.git('rev-parse', '--verify', '-q', f'{merged}:{rel}')
if new.returncode != 0:
return (f'merged, this pull request deletes {path}: {LOCKS}. To stop using the queue, take its checks out of '
'the branch ruleset first')
old = checkout.git('rev-parse', '--verify', '-q', f'{base}:{rel}')
if old.returncode == 0 and old.stdout.strip() == new.stdout.strip():
return None # the merge leaves the base's own config, which base_config parsed
oid = new.stdout.strip()
kind = checkout.git('cat-file', '-t', oid).stdout.strip()
if kind != 'blob':
what = review_judge.shown(kind[:20]) or 'thing'
return f'merged, this pull request leaves {path} as a {what}, not a file: {LOCKS}'
size = checkout.git('cat-file', '-s', oid).stdout.strip()
if not size.isdigit() or int(size) > MAX_CONFIG_BYTES:
return config_size_problem(path, size)
r = checkout.git('cat-file', 'blob', oid)
if r.returncode != 0:
said = review_judge.shown(r.stderr or '')[:200]
return f'the config {path} this pull request would merge could not be read: {said}'
return config_text_problem(r.stdout, path)


def config_size_problem(path, size):
return (f'merged, this pull request leaves {path} at {size} bytes, over the {MAX_CONFIG_BYTES} bytes the check '
'reads of a config a pull request writes: keep it smaller')


def glob_cap_problem(raw):
"""Why the raw config's review globs are over the caps, or None. Only what config.parse would search is counted;
anything of another shape is left to config.parse, which refuses it without a search."""
block = raw.get('review') if isinstance(raw, dict) else None
if not isinstance(block, dict) or block.get('mode') != 'enforce':
rules = block.get('rules') if isinstance(block, dict) else None
if not isinstance(rules, list):
return None
order = review_config.enforce_order_problems(block)
if not order:
total = 0
for i, rule in enumerate(rules):
globs = rule.get('paths') if isinstance(rule, dict) else None
for g in globs if isinstance(globs, list) else ():
if isinstance(g, str):
if len(g) > MAX_GLOB_LENGTH:
return (f'rules[{i}].paths has a glob of {len(g)} characters, over the {MAX_GLOB_LENGTH} the check '
'reads in a config a pull request writes')
total += len(g)
if total > MAX_GLOB_TOTAL:
return (f'globs of {total} characters in all, over the {MAX_GLOB_TOTAL} the check reads in a config a pull '
'request writes')
return None


def config_text_problem(text, path):
"""Why the config text a merge would leave must not merge, or None: over the caps, or it does not parse
(config.parse: an enforce block with a rule order enforce refuses is one such). Any exception is a refusal."""
try:
size = len(text.encode('utf-8', 'surrogateescape'))
if size > MAX_CONFIG_BYTES:
return config_size_problem(path, size)
try:
raw = json.loads(text)
except ValueError:
raw = None # config.parse names it
why = glob_cap_problem(raw)
if why:
return f'merged, this pull request leaves {path} with {why}: keep the rules shorter'
try:
config.parse(text, f'{path} after the merge')
except config.ConfigError as e:
block = raw.get('review') if isinstance(raw, dict) else None
order = (review_config.enforce_order_problems(block)
if isinstance(block, dict) and block.get('mode') == 'enforce' else [])
if order:
return (f'merged, this pull request leaves {path} in mode enforce with a rule order enforce refuses: '
f'{"; ".join(order)}. {LOCKS[0].upper()}{LOCKS[1:]}: fix the rule order first (or keep mode '
'shadow until it is fixed)')
return (f'merged, this pull request leaves {path} a config that does not parse: {e}. Then {LOCKS}: fix '
'it first')
return None
return (f'this pull request leaves {path} in mode enforce with a rule order enforce refuses: {"; ".join(order)}. '
'Merged, the base config would refuse every pull request after it, the one that fixes the order too: fix '
'the rule order first (or keep mode shadow until it is fixed)')
except Exception as e: # RecursionError, MemoryError, ...: unknown never passes
return (f'the config {path} this pull request would merge could not be checked, so it is not passed: '
f'{type(e).__name__}: {review_judge.shown(str(e))[:300]}')


if __name__ == '__main__':
Expand Down
Loading
Loading