From 057bc60b0ec88c728a779750b2e59a46be25f3bb Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Tue, 17 Nov 2026 23:57:09 +0100 Subject: [PATCH] pr-queue: read the config with git; head check refuses case-only twins (#424) The queue read its config from the checkout's file, which a .gitattributes encoding (working-tree-encoding=UTF-16) could rewrite without changing the config's tree entry: the head check passed it and the queue crashed with UnicodeDecodeError. It now reads the blob of the commit checked out at the config's path (ls-tree + cat-file), refuses a config that is not committed there as a regular file, and refuses non-UTF-8 text instead of crashing. The head check refuses a merge that adds a path equal to the config path ignoring case. RELEASE-NOTES now says a symlink or submodule base config locks the fix too unless its link text parses as a config. Co-Authored-By: Claude Opus 5.5 --- fork-ci/RELEASE-NOTES.md | 27 +++- fork-ci/actions/head-check/head_check.py | 36 ++++- fork-ci/actions/head-check/head_check.test.py | 34 ++++- fork-ci/actions/pr-queue/README.md | 14 +- fork-ci/actions/pr-queue/action.yml | 4 +- fork-ci/actions/pr-queue/config.py | 16 +-- fork-ci/actions/pr-queue/lens.test.py | 5 - fork-ci/actions/pr-queue/pr_queue.py | 55 +++++++- fork-ci/actions/pr-queue/pr_queue.test.py | 126 ++++++++++++++++-- 9 files changed, 280 insertions(+), 37 deletions(-) diff --git a/fork-ci/RELEASE-NOTES.md b/fork-ci/RELEASE-NOTES.md index 946d9bf853bf7..78c4aa1f97294 100644 --- a/fork-ci/RELEASE-NOTES.md +++ b/fork-ci/RELEASE-NOTES.md @@ -13,6 +13,25 @@ nothing. ## After ci/v14 +### pr-queue: reads its config with git, never from the checkout's file (#424) + +The queue now reads its config as the commit checked out at `config` holds it (HEAD): the blob, with `git ls-tree` and +`git cat-file`, never the file on disk. A checkout writes that file through `.gitattributes` (a +`working-tree-encoding=UTF-16` made it UTF-16 and stopped the queue with a `UnicodeDecodeError`; `ident` rewrote it), +and a pull request could add such an attribute without changing the config's own entry, so the head check passed it. +No checkout filter or attribute changes what the queue reads now. A config that is not UTF-8 is a refusal that says so +(exit 2), never a crash. + +The head check (and the gate) also refuses a pull request that adds a name differing from the config's path only in +case (`.github/PR-QUEUE.json`, `.GitHub/pr-queue.json`): a checkout on a case-insensitive file system keeps one of the +two, and which one is up to git. A base that already holds one is not a lock: pull requests that leave or delete it +pass. + +**Upgrade:** the action's `config` must be a path inside a git checkout whose HEAD commit holds the config as a regular +file (the reusable workflow's checkout does). The queue refuses to start (exit 2) when the path is not in a git +checkout, when the checked-out commit has no config (an edit or a file that is not committed is not read), or when the +config there is a symlink, a submodule entry or a folder. + ### 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 @@ -22,8 +41,7 @@ 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 regular file (git mode 100644 or 100755): a folder, a symlink or a submodule entry - is refused, even one whose object is the config's own (the queue opens the config from a checkout, where none of - them is a file); + is refused, even one whose object is the config's own (the queue reads only a regular file's config); - 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). @@ -48,7 +66,10 @@ then behind, and the update that brings it up to date is a new head, judged on t new run judges them on what they would merge. In checkout mode that takes a new event (a push, a reopen, a title or body edit): a re-run of the old `pull_request` job checks out the old merge commit again, which holds the old base. - A base whose config is a symlink or a submodule entry (only an owner's push can leave one now) refuses every pull - request until one makes it a regular file again; that one is judged as a new config. + request. A pull request that makes it a regular file again passes (judged as a new config) only when the link's text + is itself a config that parses. A real symlink's text is a path and a submodule entry names a commit, so the check + refuses the fix too ("not JSON"), and an owner has to push the fix by hand, as for any base config that does not + parse. ### pr-queue and head-check: stricter rule order in `review` (#378) diff --git a/fork-ci/actions/head-check/head_check.py b/fork-ci/actions/head-check/head_check.py index f92330211e9de..cac5e584641e5 100644 --- a/fork-ci/actions/head-check/head_check.py +++ b/fork-ci/actions/head-check/head_check.py @@ -400,8 +400,8 @@ def cli(argv=None, env=None, gh=gh_run, sleep=time.sleep, git=None, out=print): 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 -# The queue opens the config from a checkout (config.load): only a regular file is one there. git keeps a symlink -# (120000) as a blob of its target text, and a submodule (160000) can name any object, so the object alone says nothing. +# The queue reads the config only from a regular file's blob (pr_queue.committed_config). git keeps a symlink (120000) +# as a blob of its target text, and a submodule (160000) can name any object, so the object alone says nothing. FILE_MODES = ('100644', '100755') MERGE_TREE_SECONDS = 300 # a merge that takes longer is refused; the gate's job has 15 minutes in all LOCKS = ('the base config would refuse every pull request after it, the one that fixes it too, and the queue would ' @@ -439,7 +439,7 @@ def merged_config_problem(checkout, path): 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, and stay - a regular file (FILE_MODES: the queue opens it from a checkout). It is the merge's, never the head's own copy: two + a regular file (FILE_MODES: the queue reads no other). 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 @@ -471,6 +471,28 @@ def tree_entry(checkout, tree, rel): return tuple(fields) +def case_matches(checkout, tree, rel): + """The paths in `tree` that name `rel` ignoring case, `rel` itself too: at each level the entries whose name + casefolds to the same as `rel`'s, the last of any type (a folder there collides too). Raises ValueError when git + fails (the caller refuses).""" + parts = rel.casefold().split('/') + level = [('', tree)] + for i, want in enumerate(parts): + found = [] + for prefix, oid in level: + r = checkout.git('ls-tree', '-z', oid, errors='surrogateescape') + if r.returncode != 0: + said = review_judge.shown((r.stderr or '').strip()[:200]) + raise ValueError(f'git ls-tree {oid[:12]} failed: {said}') + for record in filter(None, (r.stdout or '').split('\0')): + meta, _, name = record.partition('\t') + fields = meta.split(' ') + if name.casefold() == want and (i == len(parts) - 1 or fields[1:2] == ['tree']): + found.append((prefix + name, fields[-1])) + level = [(p + '/', oid) for p, oid in found] if i < len(parts) - 1 else found + return {p for p, _ in level} + + def _merged_config_problem(checkout, path, base, head): rel = repo_path(path) merged = 'HEAD' @@ -499,8 +521,12 @@ def _merged_config_problem(checkout, path, base, head): if mode not in FILE_MODES or kind != 'blob': # even when the base holds the same: unknown never passes what = {'120000': 'symlink', '160000': 'submodule'}.get(mode) or review_judge.shown(kind[:20]) return (f'merged, this pull request leaves {path} as a {what} (mode {mode}), not a file (mode 100644 or ' - '100755): the queue reads the config from a checkout, where it is no file, and would stop. Make it a ' - 'file again') + '100755): the queue reads only a regular file\'s config and would stop. Make it a file again') + added = sorted(case_matches(checkout, merged, rel) - case_matches(checkout, base, rel)) + if added: # checked before the skip below: a twin leaves the config's own entry as it was + return (f'merged, this pull request adds {review_judge.shown(added[0])[:200]}, which differs from {path} only ' + 'in case: a checkout on a case-insensitive file system (macOS, Windows) keeps one of the two, and ' + 'which one is up to git. Rename or delete it') if tree_entry(checkout, base, rel) == new: return None # the merge leaves the base's own config, mode and text, which base_config parsed size = checkout.git('cat-file', '-s', oid).stdout.strip() diff --git a/fork-ci/actions/head-check/head_check.test.py b/fork-ci/actions/head-check/head_check.test.py index 329be38b4b1b0..882e64bb9d349 100644 --- a/fork-ci/actions/head-check/head_check.test.py +++ b/fork-ci/actions/head-check/head_check.test.py @@ -1009,7 +1009,7 @@ def test_a_config_that_is_no_regular_file_is_refused(self): # #413 item 1: the code, out = self.check(base, self.entry(docs, '100755', oid, base), EVERY_LENS) self.assertEqual(code, 0, out) - def test_a_base_that_holds_a_symlink_config_refuses_heads_until_one_makes_it_a_file(self): # old state: no lock + def test_a_base_that_holds_a_symlink_config_refuses_heads_until_one_makes_it_a_file(self): # old state m = self.base(mode=None) oid = self.w.git(self.w.work, 'rev-parse', f'{m}:{CONFIG_PATH}') base = self.entry(m, '120000', oid, m) # the gate reads the link's text; the queue's open() cannot @@ -1022,6 +1022,14 @@ def test_a_base_that_holds_a_symlink_config_refuses_heads_until_one_makes_it_a_f fix = self.w.commit(base_files(cfg_raw=cfg, **PROMPTS, **self.STORE), [base]) code, out = self.check(base, fix, EVERY_LENS) self.assertEqual(code, 0, out) + for name, text in (('a link to a path', 'pr-queue.real.json'), ('a submodule', m)): # #424 item 2: a lock + with self.subTest('the fix is refused too when the base holds ' + name): + mode = '160000' if text == m else '120000' + locked = self.entry(m, mode, text if text == m else self.blob(text), m) + fix = self.w.commit(base_files(**PROMPTS, **self.STORE), [locked]) + code, out = self.check(locked, fix, EVERY_LENS) + self.assertEqual(code, H.REFUSED, out) # only an owner's push makes it a file again + self.assertIn(f'{CONFIG_PATH} at the base {locked[:12]}: not JSON', out) big = json.dumps({**CFG_RAW, 'lens_format_doc': 'd' * H.MAX_CONFIG_BYTES}) # the gate reads it, uncapped base = self.entry(m, '120000', self.blob(big), m) fix = self.w.commit(base_files(cfg_raw=json.loads(big), **PROMPTS, **self.STORE), [base]) @@ -1030,6 +1038,30 @@ def test_a_base_that_holds_a_symlink_config_refuses_heads_until_one_makes_it_a_f self.assertEqual(code, H.REFUSED, out) self.assertIn('bytes the check reads', out) + def test_a_name_that_differs_from_the_config_only_in_case_is_refused(self): # #424 item 3 + # A case-insensitive checkout (a macOS or Windows runner) keeps one of the two, and which one is up to git. + base = self.base(mode=None) + for twin in ('.github/PR-QUEUE.json', '.GitHub/pr-queue.json', '.GITHUB/Pr-Queue.JSON/x.json'): + with self.subTest(twin): + code, out = self.check(base, self.head(base, mode=None, **{twin: 'not json\n'}), EVERY_LENS) + self.assertEqual(code, H.REFUSED, out) + self.assertIn(f'merged, this pull request adds {twin.split("/x.json")[0]}, which differs from ' + f'{CONFIG_PATH} only in case', out) + for name in ('.github/pr-queue.json.bak', '.github/other.json', 'pr-queue.json', '.GITHUB'): # others pass + with self.subTest(name): + self.assertEqual(self.check(base, self.head(base, mode=None, **{name: 'x\n'}), EVERY_LENS)[0], 0) + old = self.base(mode=None, **{'.github/PR-QUEUE.json': 'x\n'}) # old state: a base that holds one already + for name, files, drop in (('a head that leaves it', {'docs/a.md': 'a\n'}, ()), + ('a head that deletes it', {}, ('.github/PR-QUEUE.json',))): + with self.subTest(name): + head = self.head(old, mode=None, drop=drop, **{'.github/PR-QUEUE.json': 'x\n', **files}) + code, out = self.check(old, head, EVERY_LENS) + self.assertEqual(code, 0, out) + code, out = self.check(old, self.head(old, mode=None, **{'.github/PR-QUEUE.json': 'x\n', + '.github/pr-QUEUE.json': 'y\n'}), EVERY_LENS) + self.assertEqual(code, H.REFUSED, out) # a second one is still new + self.assertIn('adds .github/pr-QUEUE.json, which differs', out) + def test_the_caps_hold_at_their_edge(self): # #413 item 4: the documented values, exactly at and one over base = self.base('shadow') at = 'd/' * 62 + '*.py' # 128 characters diff --git a/fork-ci/actions/pr-queue/README.md b/fork-ci/actions/pr-queue/README.md index 3907933611296..6c8c2ec223ebe 100644 --- a/fork-ci/actions/pr-queue/README.md +++ b/fork-ci/actions/pr-queue/README.md @@ -7,10 +7,17 @@ only for a head that holds exactly the change the review read. ## Config -One JSON file in the project's repository. Unknown keys, a key written twice, a +One JSON file in the project's repository, UTF-8. Unknown keys, a key written twice, a missing required key, an empty `required_checks` or a value of the wrong type is an error: the queue then exits non-zero and touches no pull request. +The queue reads the config with git, as the commit checked out at its path +(HEAD) holds it, never the file on disk: a checkout writes that file through +`.gitattributes` (an encoding, `ident`), which a pull request can change +without touching the config itself. So the path must be inside a git checkout, +and the config must be committed there as a regular file: an edit that is not +committed is not read, and a symlink, a submodule entry or a folder is refused. + ```json { "required_checks": ["build", "test", "review-check"], @@ -161,8 +168,9 @@ head, the one that fixes it too, and the queue stops (an owner then has to push the fix by hand). The gate therefore refuses a pull request whose merged config (GitHub's merge commit, or `git merge-tree` of base and head in the gate's bare mode) does not parse, is deleted, is no regular file (a folder, a symlink or a -submodule entry), or is in `enforce` with a rule order `enforce` refuses (the -order checks above), and caps what it reads of a config a pull request changes +submodule entry), gains a twin whose name differs from it only in case, or is +in `enforce` with a rule order `enforce` refuses (the order checks above), and +caps what it reads of a config a pull request changes (32 KiB, 128 characters per glob, 2,048 in all). That one check reads the merged config; everything else, the rules that judge the head too, the base's. It judges the merge with the base of its run: two pull requests that each pass diff --git a/fork-ci/actions/pr-queue/action.yml b/fork-ci/actions/pr-queue/action.yml index d68564c37d494..98bc08d89c47e 100644 --- a/fork-ci/actions/pr-queue/action.yml +++ b/fork-ci/actions/pr-queue/action.yml @@ -9,7 +9,9 @@ inputs: required: false default: ${{ github.repository }} config: - description: The queue's JSON config (README.md, "Config"). + description: >- + Path of the queue's JSON config inside a git checkout; the queue reads it as the checked-out commit (HEAD) holds + it, never the file on disk (README.md, "Config"). required: true state-file: description: Absolute path, outside the job's workspace, of the plan a stopped run leaves for the next one. diff --git a/fork-ci/actions/pr-queue/config.py b/fork-ci/actions/pr-queue/config.py index 6f40f2b66ddb1..0479325186642 100644 --- a/fork-ci/actions/pr-queue/config.py +++ b/fork-ci/actions/pr-queue/config.py @@ -104,7 +104,13 @@ def _review(v): def parse(text, where='config'): - """A Config from the JSON text, or ConfigError naming every problem at once.""" + """A Config from the JSON text (str, or bytes that must be UTF-8), or ConfigError naming every problem at once.""" + if isinstance(text, bytes): + try: + text = text.decode('utf-8') + except UnicodeDecodeError as e: # a ValueError, but not JSON's: say what it is, never crash on it + raise ConfigError(f'{where}: not UTF-8 text ({e.reason} at byte {e.start}): commit the config as UTF-8 ' + 'JSON') from None try: raw = json.loads(text, object_pairs_hook=_no_duplicates) except ConfigError as e: @@ -140,11 +146,3 @@ def parse(text, where='config'): lens_author_associations=tuple(raw.pop('lens_author_associations')), override_prefix=override['prefix'], override_association=override['author_association'], **raw) - -def load(path): - try: - with open(path, encoding='utf-8') as f: - text = f.read() - except OSError as e: - raise ConfigError(f'{path}: cannot read it: {e}') from None - return parse(text, path) diff --git a/fork-ci/actions/pr-queue/lens.test.py b/fork-ci/actions/pr-queue/lens.test.py index 70d790c9c7fad..d2ed01c94248f 100644 --- a/fork-ci/actions/pr-queue/lens.test.py +++ b/fork-ci/actions/pr-queue/lens.test.py @@ -664,11 +664,6 @@ def test_not_json_or_not_an_object(self): with self.assertRaises(config.ConfigError): config.parse(text) - def test_an_unreadable_file_is_an_error(self): - with self.assertRaises(config.ConfigError) as e: - config.load(os.path.join(HERE, 'testdata', 'no-such-config.json')) - self.assertIn('cannot read it', str(e.exception)) - if __name__ == '__main__': unittest.main() diff --git a/fork-ci/actions/pr-queue/pr_queue.py b/fork-ci/actions/pr-queue/pr_queue.py index e0f26efe6526e..c26470e2d64af 100755 --- a/fork-ci/actions/pr-queue/pr_queue.py +++ b/fork-ci/actions/pr-queue/pr_queue.py @@ -309,6 +309,59 @@ def git_problem(run=subprocess.run): return f'git {".".join(map(str, GIT_MIN))} or newer is needed (merge-tree --write-tree); found: {out[:80] or "none"}' +def committed_config(path, run=subprocess.run): + """The config at `path` as the commit checked out there (HEAD) holds it: its blob, read with git, never the file. A + checkout writes that file through filters and `.gitattributes` (a `working-tree-encoding` makes it UTF-16, an + `ident` rewrites it), which a pull request can change without changing the config's own entry, so the head check + would pass it. A path outside a git checkout, a commit without the config, an entry that is no regular file + (a symlink, a submodule, a folder) or a blob that is not UTF-8 JSON is a ConfigError, never a crash.""" + full = os.path.abspath(path) + where = os.path.dirname(full) + while not os.path.isdir(where): # the folder is missing when the commit has no config: git still says so + where = os.path.dirname(where) + + def git(cwd, *a): + try: + r = run(['git', *GIT_SAFE, '-C', cwd, *a], capture_output=True, + env={**no_token_env(), 'GIT_TERMINAL_PROMPT': '0', **GIT_ENV}) + except OSError as e: + raise config.ConfigError(f'{path}: cannot run git to read it: {e}') from None + return r.returncode, r.stdout, os.fsdecode(r.stderr or b'').strip()[:200] + code, out, err = git(where, 'rev-parse', '--show-toplevel') + if code != 0: + raise config.ConfigError(f'{path}: not in a git checkout ({err}): the queue reads the config as the ' + 'checked-out commit holds it, never the file') + top, head, rest = os.path.realpath(os.fsdecode(out.rstrip(b'\n'))), full, [] + while os.path.realpath(head) != top: # the path's own names below the top, links in them not followed + head, name = os.path.split(head) + if not name: + raise config.ConfigError(f'{path}: not inside the checkout {top}') + rest.insert(0, name) + rel = '/'.join(rest) + code, out, err = git(top, 'rev-parse', '--verify', '-q', 'HEAD^{commit}') + sha = os.fsdecode(out).strip() + if code != 0 or not re.fullmatch(r'[0-9a-f]{40}', sha): + raise config.ConfigError(f'{path}: the checkout {top} has no commit checked out') + code, out, err = git(top, 'ls-tree', '-z', sha, '--', rel) + records = [x for x in out.split(b'\0') if x] + if code != 0 or not records: + raise config.ConfigError(f'{path}: the commit checked out ({sha[:12]}) has no {rel}' + + (f': {err}' if err else '')) + meta, _, name = records[0].partition(b'\t') + fields = meta.decode('ascii', 'replace').split(' ') + if len(records) != 1 or name != os.fsencode(rel) or len(fields) != 3: + raise config.ConfigError(f'{path}: git ls-tree {sha[:12]} -- {rel} did not list one entry') + mode, kind, oid = fields + if mode not in ('100644', '100755'): # git lists a blob in no other mode as a file + what = {'120000': 'symlink', '160000': 'submodule', '040000': 'tree'}.get(mode, kind[:20]) + raise config.ConfigError(f'{path}: at {sha[:12]} {rel} is a {what} (mode {mode}), not a file (mode 100644 or ' + '100755): make it a regular file') + code, out, err = git(top, 'cat-file', 'blob', oid) + if code != 0: + raise config.ConfigError(f'{path}: cannot read {rel} at {sha[:12]}: {err}') + return config.parse(out, f'{path} at {sha[:12]}') + + class Queue: """One repository's queue. gh (reads), gh_write (writes), git and sleep are attributes, so tests replace them.""" @@ -1145,7 +1198,7 @@ def cli(argv=None, env=None): elif work and (os.path.realpath(path) + os.sep).startswith(work + os.sep): problems.append(f'{flag} {path} is inside the job\'s workspace, which the next checkout empties') try: - cfg = config.load(a.config) + cfg = committed_config(a.config) except config.ConfigError as e: problems.append(str(e)) read, write = env.get(READ_TOKEN) or '', env.get(WRITE_TOKEN) or '' diff --git a/fork-ci/actions/pr-queue/pr_queue.test.py b/fork-ci/actions/pr-queue/pr_queue.test.py index cae753b903a69..808d95067bb91 100644 --- a/fork-ci/actions/pr-queue/pr_queue.test.py +++ b/fork-ci/actions/pr-queue/pr_queue.test.py @@ -3018,11 +3018,33 @@ class Cli(unittest.TestCase): def setUp(self): self.d = tempfile.mkdtemp(prefix='pr-queue-cli-') self.addCleanup(shutil.rmtree, self.d, True) - self.config = os.path.join(self.d, 'queue.json') - with open(self.config, 'w') as f: - json.dump(CFG_RAW, f) + self.repo = os.path.join(self.d, 'caller') # the caller's checkout, as the reusable workflow makes it + self.git('init', '-q', '-b', 'main') + self.config = os.path.join(self.repo, '.github', 'pr-queue.json') + self.commit({'.github/pr-queue.json': json.dumps(CFG_RAW)}) self.built, self.tokens, self.steps = [], [], [] + def git(self, *a): + os.makedirs(self.repo, exist_ok=True) + return subprocess.run(['git', '-C', self.repo, *a], check=True, capture_output=True, + env={**os.environ, **GIT_ENV}).stdout + + def write(self, rel, data): + where = os.path.join(self.repo, rel) + os.makedirs(os.path.dirname(where), exist_ok=True) + with open(where, 'wb') as f: + f.write(data if isinstance(data, bytes) else data.encode()) + + def commit(self, files): + """Commit `files` (path: text or bytes; None deletes it) on the checkout's HEAD; the work tree follows.""" + for rel, data in files.items(): + if data is None: + self.git('rm', '-q', '--', rel) + else: + self.write(rel, data) + self.git('add', '--', rel) + self.git('commit', '-q', '--allow-empty', '-m', 'c') + def cli(self, *extra, repo=REPO, state=None, cache=None, env=None, git=None): test = self @@ -3123,19 +3145,105 @@ def test_paths_in_the_job_workspace_are_refused(self): # the next checkout emp self.assertEqual(code, 0, 'a sibling folder whose name starts the same is not inside: ' + out) def test_a_config_error_is_refused(self): - with open(self.config, 'w') as f: - json.dump({**CFG_RAW, 'required_check': ['build']}, f) + self.commit({'.github/pr-queue.json': json.dumps({**CFG_RAW, 'required_check': ['build']})}) self.refused("unknown key 'required_check'") + self.commit({'.github/pr-queue.json': None}) + self.refused('has no .github/pr-queue.json') + + def test_the_config_is_read_as_committed_never_from_the_checkouts_file(self): # #424 item 1 + # A .gitattributes encoding leaves the config's blob as it was (the head check skips it), but a checkout writes + # the file as UTF-16: the queue must read what git holds, which no checkout filter or attribute changes. + self.commit({'.gitattributes': '.github/pr-queue.json working-tree-encoding=UTF-16\n'}) os.remove(self.config) - self.refused('cannot read it') + self.git('checkout', '--', '.github/pr-queue.json') + with open(self.config, 'rb') as f: + self.assertEqual(f.read(2), b'\xff\xfe', 'the checkout wrote UTF-16') + code, out = self.cli() + self.assertEqual(code, 0, out) + self.assertEqual(self.built[-1][0][1], CFG) + self.write('.github/pr-queue.json', 'not json') # an edit in the work tree, not committed: never read + code, out = self.cli() + self.assertEqual((code, self.built[-1][0][1]), (0, CFG), out) + os.remove(self.config) # nor a file missing from the work tree + code, out = self.cli() + self.assertEqual((code, self.built[-1][0][1]), (0, CFG), out) + + def test_a_config_changed_between_runs_is_read_anew_and_a_rerun_reads_the_same(self): # state: nothing kept + self.cli() + self.commit({'.github/pr-queue.json': json.dumps({**CFG_RAW, 'merge_method': 'merge'})}) + self.cli() + self.cli() + self.assertEqual([b[0][1].merge_method for b in self.built], ['squash', 'merge', 'merge']) + + def test_the_config_read_ignores_global_and_system_git_config(self): # an earlier job's ~/.gitconfig, say + broken = os.path.join(self.d, 'broken.gitconfig') + with open(broken, 'w') as f: + f.write('[core\n') + with unittest.mock.patch.dict(os.environ, {'GIT_CONFIG_GLOBAL': broken, 'GIT_CONFIG_SYSTEM': broken, + 'GIT_CONFIG_NOSYSTEM': '0'}): + code, out = self.cli() + self.assertEqual((code, self.built[-1][0][1]), (0, CFG), out) + + def test_a_config_that_is_not_utf8_is_refused_never_a_crash(self): # #424 item 1: was a UnicodeDecodeError + for name, data in (('UTF-16', json.dumps(CFG_RAW).encode('utf-16')), ('Latin-1', b'{"push_command": "\xe9"}')): + with self.subTest(name): + self.commit({'.github/pr-queue.json': data}) + self.refused('.github/pr-queue.json at ', 'not UTF-8 text') + + def test_a_config_that_is_empty_missing_or_no_file_at_head_is_refused(self): # old, empty or missing state + self.commit({'.github/pr-queue.json': ''}) + self.refused('not JSON') + self.commit({'.github/pr-queue.json': None}) + self.write('.github/pr-queue.json', json.dumps(CFG_RAW)) # on disk, never committed: not read + self.refused('has no .github/pr-queue.json') + self.commit({'.github/real.json': json.dumps(CFG_RAW)}) + os.symlink('real.json', self.config + '.tmp') + os.replace(self.config + '.tmp', self.config) + self.git('add', '--', '.github/pr-queue.json') + self.git('commit', '-q', '-m', 'symlink') + self.refused('is a symlink (mode 120000), not a file') + os.remove(self.config) + self.commit({'.github/pr-queue.json/x.json': json.dumps(CFG_RAW)}) + self.refused('is a tree (mode 040000), not a file') + + def test_a_tree_that_lists_the_config_twice_is_refused(self): # git mktree makes one; fsck would flag it + def hashed(*a, data): + return subprocess.run(['git', '-C', self.repo, *a], input=data, check=True, capture_output=True, + env={**os.environ, **GIT_ENV}).stdout.decode().strip() + good = hashed('hash-object', '-w', '--stdin', data=json.dumps(CFG_RAW).encode()) + bad = hashed('hash-object', '-w', '--stdin', data=b'not json') + for first, second in ((good, bad), (bad, good)): + with self.subTest(first=first[:7]): + inner = hashed('mktree', data=f'100644 blob {first}\tpr-queue.json\n' + f'100644 blob {second}\tpr-queue.json\n'.encode()) + root = hashed('mktree', data=f'040000 tree {inner}\t.github\n'.encode()) + self.git('reset', '-q', '--soft', hashed('commit-tree', root, '-m', 'twice', data=b'')) + self.refused('did not list one entry') + + def test_a_config_outside_a_git_checkout_or_before_its_first_commit_is_refused(self): + outside = os.path.join(self.d, 'outside', 'queue.json') + os.makedirs(os.path.dirname(outside)) + with open(outside, 'w') as f: + json.dump(CFG_RAW, f) + self.config = outside + self.refused('not in a git checkout') + os.symlink(os.path.join(self.repo, '.github'), os.path.join(self.d, 'outside', 'link')) + self.config = os.path.join(self.d, 'outside', 'link', 'pr-queue.json') # into the checkout from outside + self.refused('not inside the checkout') + shutil.rmtree(self.repo) + self.git('init', '-q', '-b', 'main') + self.write('.github/pr-queue.json', json.dumps(CFG_RAW)) + self.config = os.path.join(self.repo, '.github', 'pr-queue.json') + self.refused('has no commit checked out') def test_an_old_git_is_refused(self): self.refused('git 2.38 or newer', git='git 2.38 or newer is needed (merge-tree --write-tree); found: git 2.34') def test_every_problem_is_named_at_once(self): - os.remove(self.config) - self.refused('GitHub-hosted', 'is not owner/name', 'not an absolute path', 'cannot read it', 'git 2.38', - repo='x', state='p', env={'RUNNER_ENVIRONMENT': 'github-hosted'}, git='git 2.38 or newer') + self.commit({'.github/pr-queue.json': None}) + self.refused('GitHub-hosted', 'is not owner/name', 'not an absolute path', 'has no .github/pr-queue.json', + 'git 2.38', repo='x', state='p', env={'RUNNER_ENVIRONMENT': 'github-hosted'}, + git='git 2.38 or newer') class GateApp(unittest.TestCase):