| # Licensed to the Apache Software Foundation (ASF) under one | |
| # or more contributor license agreements. See the NOTICE file | |
| # distributed with this work for additional information | |
| # regarding copyright ownership. The ASF licenses this file | |
| # to you under the Apache License, Version 2.0 (the | |
| # "License"); you may not use this file except in compliance | |
| # with the License. You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, | |
| # software distributed under the License is distributed on an | |
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| # KIND, either express or implied. See the License for the | |
| # specific language governing permissions and limitations | |
| # under the License. | |
| # Comes from apache/tooling-devkit (devkit.files): change it there, not in a copy. | |
| """What a branch may ask of the behaviour comparison (`scripts/ci/behaviour.py`), read from git. | |
| The comparison runs the branch's own code, so whatever the branch says about itself is checked against | |
| what git says instead. Whether the base has a harness is read from the base commit's tree, not from a | |
| path the branch's script names. Which commits argue for a change is read from the branch's own line, | |
| not from a merge's message or a branch merged in. And a branch that changes what the record is made | |
| with says so in a trailer of its own, and changes nothing of the app in the same breath: a harness change | |
| and an app change together would compare a new app, recorded by a new harness, with an old one, and a | |
| difference could come from either. | |
| """ | |
| from __future__ import annotations | |
| import fnmatch | |
| import subprocess | |
| from typing import TYPE_CHECKING, Final | |
| import trailers | |
| if TYPE_CHECKING: | |
| import pathlib | |
| ALLOW: Final = "Behaviour-Allow" | |
| HARNESS: Final = "Behaviour-Harness" | |
| HARNESS_FILES: Final = ( | |
| "tests/behaviour/*", | |
| "tests/conftest.py", | |
| "scripts/ci/behaviour*.py", | |
| "scripts/ci/trailers.py", | |
| ) | |
| """What the record is made with: a change here changes what is recorded without the app changing. | |
| A repository adds the data its record reads in `scripts/local/behaviour.toml` (`behaviour_rules.py`). | |
| Name only what the record reads: what only pytest reads moves with the app change that moves it, and a | |
| branch changing both has to be compared, not refused as a harness change beside one to the app.""" | |
| HARNESS_MARKS: Final = ("scripts/ci/behaviour.py", "tests/behaviour") | |
| """What a commit holds where it can record itself.""" | |
| SUMMARY_BYTES: Final = 512 * 1024 | |
| """GitHub refuses a step summary over 1 MiB; a diff stops well short of that, and the artifact has it all.""" | |
| def has_harness(commit: str, cwd: pathlib.Path) -> bool: | |
| """Whether `commit`'s own tree holds the harness.""" | |
| return all( | |
| subprocess.run(["git", "cat-file", "-e", f"{commit}:{mark}"], cwd=cwd, capture_output=True).returncode == 0 | |
| for mark in HARNESS_MARKS | |
| ) | |
| def branch_tip(base: str, cwd: pathlib.Path) -> str: | |
| """The branch's own last commit: HEAD, or - where HEAD is the merge CI makes of the branch into its | |
| base - the branch's side of it.""" | |
| parents = subprocess.run( | |
| ["git", "rev-list", "--parents", "-n", "1", "HEAD"], cwd=cwd, capture_output=True, text=True, check=True | |
| ).stdout.split() | |
| based = subprocess.run(["git", "rev-parse", base], cwd=cwd, capture_output=True, text=True).stdout.strip() | |
| return parents[2] if len(parents) == 3 and parents[1] == based else "HEAD" | |
| def claims(key: str, base: str, cwd: pathlib.Path, *, head: str | None = None) -> list[tuple[str, str]]: | |
| """`(value, subject)` for every `key:` trailer on the branch's own commits since `base`: the branch | |
| ending at `head`, or at this checkout's tip. A merged pull request's head is read by the same rule | |
| (`behaviour_pulls.py`).""" | |
| tip = head or branch_tip(base, cwd) | |
| return [(value, subject) for subject, value in trailers.values(key, base, cwd, head=tip, own=True)] | |
| def too_broad(glob: str, recorded: list[str]) -> bool: | |
| """A glob that allows every difference there could be: one that names no part of a path, or a pattern | |
| that matches every path the base record holds - `*.txt` where every file is text - whatever it names. | |
| A plain path is never one: naming the only file a record holds is naming it, not allowing everything.""" | |
| if not glob.strip("*?/"): | |
| return True | |
| return _pattern(glob) and bool(recorded) and all(fnmatch.fnmatchcase(path, glob) for path in recorded) | |
| def unanchored(glob: str) -> bool: | |
| """A pattern whose first path segment is not written out: `*.txt`, `*/x.txt`, `**/foo`. Each reaches | |
| into every directory of the record, so one written for a few files allows a difference anywhere.""" | |
| if not _pattern(glob): | |
| return False | |
| first, slash, _ = glob.partition("/") | |
| return not slash or not first or _pattern(first) | |
| def _pattern(glob: str) -> bool: | |
| return any(char in glob for char in "*?[") | |
| def touched(changed: list[str], globs: tuple[str, ...]) -> list[str]: | |
| return [path for path in changed if any(fnmatch.fnmatchcase(path, glob) for glob in globs)] | |
| def refused(allowances: list[tuple[str, str]], recorded: list[str]) -> list[tuple[tuple[str, ...], str]]: | |
| """The allowances the gate refuses, each with why: one too broad or unanchored by itself, and the | |
| patterns among the rest when together they match every path the base record holds - `http/*` and | |
| `tasks/*` where that is all there is. Plain paths count towards the whole but are never refused.""" | |
| found: list[tuple[tuple[str, ...], str]] = [] | |
| kept = [] | |
| for glob, subject in allowances: | |
| if too_broad(glob, recorded): | |
| found.append( | |
| ((glob,), f"`{ALLOW}: {glob}` ('{subject}') allows every difference; name the paths that change") | |
| ) | |
| elif unanchored(glob): | |
| found.append( | |
| ( | |
| (glob,), | |
| f"`{ALLOW}: {glob}` ('{subject}') reaches into every directory of the record; " | |
| "name the directory: http/*.txt, not *.txt", | |
| ) | |
| ) | |
| else: | |
| kept.append(glob) | |
| patterns = tuple(dict.fromkeys(glob for glob in kept if _pattern(glob))) | |
| if patterns and recorded and all(any(fnmatch.fnmatchcase(path, glob) for glob in kept) for path in recorded): | |
| named = ", ".join(patterns) | |
| found.append((patterns, f"`{ALLOW}: {named}` together allow every difference; name the paths that change")) | |
| return found | |
| def refusals( | |
| changed: list[str], | |
| allowances: list[tuple[str, str]], | |
| harness: list[tuple[str, str]], | |
| recorded: list[str], | |
| harness_files: tuple[str, ...] = HARNESS_FILES, | |
| ) -> list[str]: | |
| """Why the branch may not be compared at all, or nothing. `recorded` is every path the base record holds.""" | |
| reasons = [reason for _, reason in refused(allowances, recorded)] | |
| tools = touched(changed, harness_files) | |
| if not tools: | |
| return reasons | |
| named = ", ".join(tools[:5]) + (f" and {len(tools) - 5} more" if len(tools) > 5 else "") | |
| app = [path for path in changed if path.startswith("app/")] | |
| if app: | |
| reasons.append( | |
| f"this branch changes what the record is made with ({named}) and the app itself ({', '.join(app[:5])}): " | |
| "split it - the harness first, recorded against the app it has; then the change to the app, recorded " | |
| "by the harness the first one left" | |
| ) | |
| if not harness: | |
| reasons.append(f"this branch changes what the record is made with ({named}); say why with `{HARNESS}: <why>`") | |
| return reasons | |
| def summary(heading: str, lines: list[str], diff: list[str], limit: int = SUMMARY_BYTES) -> str: | |
| """The step summary, `limit` bytes at most: the heading, then each line, then the diff, until it is | |
| full, and where it was cut how much more the artifact holds.""" | |
| parts = [f"### What the app answers\n\n{heading}\n\n"] | |
| used = len(parts[0].encode()) | |
| closing = "\n```\n" | |
| rest = [f"- {line}\n" for line in lines] + (["\n```diff\n"] + [f"{line}\n" for line in diff] if diff else []) | |
| for index, part in enumerate(rest): | |
| size = len(part.encode()) | |
| if used + size + len(closing) + 200 > limit: # room for the closing fence and the note | |
| if diff and index > len(lines): | |
| parts.append(closing) | |
| parts.append(f"\n{len(rest) - index} more lines in the `behaviour` artifact.\n") | |
| return "".join(parts) | |
| parts.append(part) | |
| used += size | |
| if diff: | |
| parts.append(closing) | |
| return "".join(parts) |