Repository navigation
ci: a kickoff issue is validated before a worker sees it #723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| #!/usr/bin/env python3 | ||
| """Validate kickoff files or fizzyedit/work issues against the documented template.""" | ||
|
|
||
| import re | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| TEMPLATE = Path(__file__).resolve().parents[1] / "docs/AGENT_WORK.md" | ||
| MAX_CHARACTERS = 1600 | ||
|
|
||
|
|
||
| def template_fields(): | ||
| text = TEMPLATE.read_text(encoding="utf-8") | ||
| section = text.split("## Kickoff template", 1)[1].split("```", 2)[1] | ||
| fields = re.findall(r"^([^:\n]+):", section, re.MULTILINE) | ||
| if not fields or len(fields) != len(set(fields)): | ||
| raise ValueError("kickoff template has no fields or duplicate fields") | ||
| return fields | ||
|
|
||
|
|
||
| def validate(body, fields): | ||
| values = {} | ||
| current = None | ||
| for line in body.splitlines(): | ||
| # GitHub issue forms emit headings; pasted kickoffs use Label: value. | ||
| heading = re.fullmatch(r"### (.+?)\s*", line) | ||
| label, separator, value = line.partition(":") | ||
| name = heading[1] if heading else label | ||
| if name in fields and (heading or separator): | ||
| current = name | ||
| values.setdefault(name, []).append("" if heading else value) | ||
| elif current: | ||
| values[current].append(line) | ||
| values = {name: "\n".join(lines).strip() for name, lines in values.items()} | ||
| failures = [] | ||
| for name in fields: | ||
| if not values.get(name) or values[name] == "_No response_": | ||
| failures.append(f"{name}: missing field or empty value") | ||
| if len(body) > MAX_CHARACTERS: | ||
| failures.append(f"Body: {len(body)} characters exceeds {MAX_CHARACTERS}") | ||
| if values.get("Branch") and not re.fullmatch(r"[a-z0-9-]+/[a-z0-9._-]+", values["Branch"]): | ||
| failures.append("Branch: must match ^[a-z0-9-]+/[a-z0-9._-]+$") | ||
| if re.search(r"scripts/zb\s+zig\s+build", values.get("Gates", "")): | ||
| failures.append("Gates: use scripts/zb build, not scripts/zb zig build") | ||
| if not re.search(r"fizzyedit/work#(?:[1-9][0-9]*|N)\b", values.get("Rules", "")): | ||
| failures.append("Rules: must require a PR reference to fizzyedit/work#N") | ||
| return failures | ||
|
|
||
|
|
||
| def main(): | ||
| if len(sys.argv) != 2: | ||
| print("usage: scripts/check-kickoff <file|issue-number>", file=sys.stderr) | ||
| return 2 | ||
| source = sys.argv[1] | ||
| try: | ||
| fields = template_fields() | ||
| if re.fullmatch(r"[1-9][0-9]*", source): | ||
| body = subprocess.run( | ||
| ["gh", "issue", "view", source, "--repo", "fizzyedit/work", "--json", "body", "--jq", ".body"], | ||
| check=True, capture_output=True, text=True, | ||
| ).stdout.removesuffix("\n") | ||
| else: | ||
| body = Path(source).read_text(encoding="utf-8") | ||
| failures = validate(body, fields) | ||
| except (OSError, ValueError, IndexError, subprocess.CalledProcessError) as error: | ||
| print(f"check-kickoff: {source}: {error}", file=sys.stderr) | ||
| if isinstance(error, subprocess.CalledProcessError) and error.stderr: | ||
| print(error.stderr.strip(), file=sys.stderr) | ||
| return 2 | ||
| for failure in failures: | ||
| print(f"{source}: {failure}", file=sys.stderr) | ||
| if not failures: | ||
| print(f"{source}: kickoff passes") | ||
| return bool(failures) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| #!/usr/bin/env python3 | ||
| """Exercise kickoff validation and its file and issue-number interfaces.""" | ||
|
|
||
| import importlib.machinery | ||
| import importlib.util | ||
| import os | ||
| from pathlib import Path | ||
| import subprocess | ||
| import tempfile | ||
| import unittest | ||
|
|
||
| SCRIPT = Path(__file__).with_name("check-kickoff") | ||
| loader = importlib.machinery.SourceFileLoader("kickoff", str(SCRIPT)) | ||
| spec = importlib.util.spec_from_loader(loader.name, loader) | ||
| kickoff = importlib.util.module_from_spec(spec) | ||
| loader.exec_module(kickoff) | ||
|
|
||
|
|
||
| class KickoffTests(unittest.TestCase): | ||
| def setUp(self): | ||
| self.fields = kickoff.template_fields() | ||
| self.values = {name: "example" for name in self.fields} | ||
| self.values.update(Branch="ci/check-kickoff", Gates="scripts/zb build", | ||
| Rules="PRs say Part of fizzyedit/work#42") | ||
|
|
||
| def body(self): | ||
| return "\n".join(f"{name}: {value}" for name, value in self.values.items()) | ||
|
|
||
| def test_valid_and_form(self): | ||
| self.assertEqual(kickoff.validate(self.body(), self.fields), []) | ||
| form = "\n\n".join(f"### {name}\n\n{value}" for name, value in self.values.items()) | ||
| self.assertEqual(kickoff.validate(form, self.fields), []) | ||
|
|
||
| def test_each_missing_or_empty_field(self): | ||
| for name in self.fields: | ||
| for value in (None, "", "_No response_"): | ||
| with self.subTest(name=name, value=value): | ||
| saved = self.values.pop(name) | ||
| if value is not None: | ||
| self.values[name] = value | ||
| failures = kickoff.validate(self.body(), self.fields) | ||
| self.assertTrue(any(f.startswith(name + ":") for f in failures)) | ||
| self.values[name] = saved | ||
|
|
||
| def test_all_failures_reported(self): | ||
| self.values.update(Branch="Bad branch", Gates="scripts/zb zig build", Rules="no reference") | ||
| failures = kickoff.validate(self.body() + "x" * 1600, self.fields) | ||
| self.assertEqual({f.split(":")[0] for f in failures}, {"Branch", "Gates", "Rules", "Body"}) | ||
|
|
||
| def test_length_boundary_and_unicode(self): | ||
| body = self.body() + "\né" * 10 | ||
| body += "x" * (1600 - len(body)) | ||
| self.assertEqual(kickoff.validate(body, self.fields), []) | ||
| self.assertTrue(any(f.startswith("Body:") for f in kickoff.validate(body + "x", self.fields))) | ||
|
|
||
| def test_branch_and_multiline_rules(self): | ||
| for branch in ("ci/a._-9", "ci/A", "ci/a/b", "ci/", "ci/a extra"): | ||
| self.values["Branch"] = branch | ||
| failures = kickoff.validate(self.body(), self.fields) | ||
| self.assertEqual(bool(failures), branch != "ci/a._-9") | ||
| self.values["Branch"] = "ci/check" | ||
| self.values["Rules"] = '\n- PRs say "Part of fizzyedit/work#N".' | ||
| self.assertEqual(kickoff.validate(self.body(), self.fields), []) | ||
|
|
||
| def test_template_fields_are_live(self): | ||
| with tempfile.TemporaryDirectory() as directory: | ||
| template = Path(directory) / "AGENT_WORK.md" | ||
| template.write_text("## Kickoff template\n```\nExtra: value\n```\n") | ||
| original = kickoff.TEMPLATE | ||
| try: | ||
| kickoff.TEMPLATE = template | ||
| self.assertEqual(kickoff.template_fields(), ["Extra"]) | ||
| self.assertIn("Extra: missing field or empty value", | ||
| kickoff.validate("", kickoff.template_fields())) | ||
| finally: | ||
| kickoff.TEMPLATE = original | ||
|
|
||
| def test_cli_file_and_issue(self): | ||
| with tempfile.TemporaryDirectory() as directory: | ||
| body = Path(directory) / "body" | ||
| body.write_text(self.body()) | ||
| result = subprocess.run([str(SCRIPT), str(body)], capture_output=True, text=True) | ||
| self.assertEqual(result.returncode, 0, result.stderr) | ||
| gh = Path(directory) / "gh" | ||
| gh.write_text('#!/bin/sh\n[ "$*" = "issue view 7 --repo fizzyedit/work --json body --jq .body" ] || exit 9\ncat "$KICKOFF_BODY"\n') | ||
| gh.chmod(0o755) | ||
| env = dict(os.environ, PATH=directory + os.pathsep + os.environ["PATH"], KICKOFF_BODY=str(body)) | ||
| result = subprocess.run([str(SCRIPT), "7"], env=env, capture_output=True, text=True) | ||
| self.assertEqual(result.returncode, 0, result.stderr) | ||
| body.write_text("Task: incomplete") | ||
| result = subprocess.run([str(SCRIPT), str(body)], capture_output=True, text=True) | ||
| self.assertEqual(result.returncode, 1) | ||
| self.assertIn("Branch:", result.stderr) | ||
| gh.write_text('#!/bin/sh\nprintf "lookup failed\\n" >&2\nexit 1\n') | ||
| result = subprocess.run([str(SCRIPT), "7"], env=env, capture_output=True, text=True) | ||
| self.assertEqual(result.returncode, 2) | ||
| self.assertIn("lookup failed", result.stderr) | ||
| result = subprocess.run([str(SCRIPT), str(body) + ".missing"], capture_output=True, text=True) | ||
| self.assertEqual(result.returncode, 2) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The custom regex accepts names such as
ci/..,ci/.hidden,ci/foo., andci/foo.lock, even thoughgit check-ref-format --branchrejects each as an invalid branch name. A kickoff using one of these values therefore passes validation but leaves the worker unable to create the prescribed branch; validate with Git's ref rules or add the missing component restrictions.Useful? React with 👍 / 👎.