Stage 1 flags the living documents a pull request makes stale (no model, nothing sent). Stage 2, on repositories that opt in with drafter: claude, has Claude Code draft the edits in a sandboxed copy of the tree and opens them as a pull request into the feature branch. A monthly pass re-reads the top-level documents against the whole repository. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
ade847ba5b
38 files changed
+4286
No files matched your search
@@ -0,0 +1,247 @@
|
||||
import io
|
||||
import json
|
||||
import unittest
|
||||
import urllib.error
|
||||
|
||||
from docs_review.forge import ForgeError, Gitea
|
||||
|
||||
TOKEN = "s3cr3t-token"
|
||||
BASE = "https://gitea.example.org"
|
||||
API = BASE + "/api/v1/repos/o/r"
|
||||
|
||||
|
||||
class Resp:
|
||||
def __init__(self, payload, status=200):
|
||||
self._data = payload if isinstance(payload, bytes) else json.dumps(payload).encode()
|
||||
self.status = status
|
||||
|
||||
def read(self):
|
||||
return self._data
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc):
|
||||
return False
|
||||
|
||||
|
||||
class FakeUrlopen:
|
||||
"""Routes (method, url) to a payload, a callable, or an HTTP error status."""
|
||||
|
||||
def __init__(self, routes):
|
||||
self.routes = routes
|
||||
self.requests = []
|
||||
self.timeouts = []
|
||||
|
||||
def __call__(self, req, timeout=None):
|
||||
self.requests.append(req)
|
||||
self.timeouts.append(timeout)
|
||||
key = (req.get_method(), req.full_url)
|
||||
handler = self.routes[key]
|
||||
if isinstance(handler, int):
|
||||
raise urllib.error.HTTPError(
|
||||
req.full_url, handler, "err", {}, io.BytesIO(b"{}")
|
||||
)
|
||||
return Resp(handler(req) if callable(handler) else handler)
|
||||
|
||||
|
||||
def client(routes, redact_env=None):
|
||||
fake = FakeUrlopen(routes)
|
||||
return Gitea(BASE, "o/r", TOKEN, urlopen=fake, redact_env=redact_env), fake
|
||||
|
||||
|
||||
def comment(cid, login, body):
|
||||
return {"id": cid, "body": body, "user": {"login": login}}
|
||||
|
||||
|
||||
def pr(number, ref, login="bot", repo="o/r"):
|
||||
return {"number": number, "user": {"login": login},
|
||||
"head": {"ref": ref, "repo": {"full_name": repo}}}
|
||||
|
||||
|
||||
USER = {"login": "bot", "email": "bot@example.org"}
|
||||
LIST5 = API + "/issues/5/comments?limit=50&page=1"
|
||||
|
||||
|
||||
class TestComments(unittest.TestCase):
|
||||
def test_upsert_patches_matching_comment(self):
|
||||
g, f = client({
|
||||
("GET", BASE + "/api/v1/user"): USER,
|
||||
("GET", LIST5): [comment(7, "bot", "<!-- m --> old")],
|
||||
("PATCH", API + "/issues/comments/7"): {},
|
||||
})
|
||||
g.upsert_comment(5, "<!-- m -->", "<!-- m --> new")
|
||||
last = f.requests[-1]
|
||||
self.assertEqual(last.get_method(), "PATCH")
|
||||
self.assertEqual(json.loads(last.data), {"body": "<!-- m --> new"})
|
||||
self.assertEqual(f.timeouts[-1], 30)
|
||||
|
||||
def test_upsert_posts_when_none(self):
|
||||
g, f = client({
|
||||
("GET", BASE + "/api/v1/user"): USER,
|
||||
("GET", LIST5): [],
|
||||
("POST", API + "/issues/5/comments"): {},
|
||||
})
|
||||
g.upsert_comment(5, "<!-- m -->", "body")
|
||||
self.assertEqual(f.requests[-1].get_method(), "POST")
|
||||
|
||||
def test_other_users_comment_not_edited(self):
|
||||
g, f = client({
|
||||
("GET", BASE + "/api/v1/user"): USER,
|
||||
("GET", LIST5): [comment(7, "someone", "<!-- m -->")],
|
||||
("POST", API + "/issues/5/comments"): {},
|
||||
})
|
||||
g.upsert_comment(5, "<!-- m -->", "body")
|
||||
self.assertEqual(f.requests[-1].get_method(), "POST")
|
||||
|
||||
def test_newest_matching_comment_wins(self):
|
||||
g, f = client({
|
||||
("GET", BASE + "/api/v1/user"): USER,
|
||||
("GET", LIST5): [comment(7, "bot", "<!-- m -->"),
|
||||
comment(9, "bot", "<!-- m -->")],
|
||||
})
|
||||
self.assertEqual(g.find_comment(5, "<!-- m -->")["id"], 9)
|
||||
|
||||
def test_headers_and_identity_cache(self):
|
||||
g, f = client({("GET", BASE + "/api/v1/user"): USER})
|
||||
self.assertEqual(g.identity(), ("bot", "bot@example.org"))
|
||||
g.identity()
|
||||
self.assertEqual(len(f.requests), 1)
|
||||
req = f.requests[0]
|
||||
self.assertEqual(req.get_header("Authorization"), "token " + TOKEN)
|
||||
self.assertEqual(req.get_header("Content-type"), "application/json")
|
||||
self.assertEqual(req.get_header("Accept"), "application/json")
|
||||
|
||||
def test_403_raises_without_token(self):
|
||||
g, _ = client({("GET", BASE + "/api/v1/user"): 403})
|
||||
with self.assertRaises(ForgeError) as cm:
|
||||
g.identity()
|
||||
self.assertNotIn(TOKEN, str(cm.exception))
|
||||
self.assertIn("403", str(cm.exception))
|
||||
self.assertIn("GET", str(cm.exception))
|
||||
|
||||
def test_pagination_reads_page_two(self):
|
||||
page1 = [comment(i, "x", "n") for i in range(50)]
|
||||
g, f = client({
|
||||
("GET", BASE + "/api/v1/user"): USER,
|
||||
("GET", LIST5): page1,
|
||||
("GET", API + "/issues/5/comments?limit=50&page=2"):
|
||||
[comment(99, "bot", "<!-- m -->")],
|
||||
})
|
||||
self.assertEqual(g.find_comment(5, "<!-- m -->")["id"], 99)
|
||||
|
||||
|
||||
class TestPrsAndIssues(unittest.TestCase):
|
||||
def test_find_open_pr_prefix(self):
|
||||
prs = [pr(1, "feat/x"), pr(2, "docs-review/pr-4")]
|
||||
g, _ = client({
|
||||
("GET", BASE + "/api/v1/user"): USER,
|
||||
("GET", API + "/pulls?state=open&limit=50&page=1"): prs})
|
||||
self.assertEqual(g.find_open_pr_prefix("docs-review/")["number"], 2)
|
||||
self.assertIsNone(g.find_open_pr_prefix("nope"))
|
||||
|
||||
def test_find_open_pr_is_exact(self):
|
||||
prs = [pr(1, "docs-review/pr-123"), pr(2, "docs-review/pr-12")]
|
||||
g, _ = client({
|
||||
("GET", BASE + "/api/v1/user"): USER,
|
||||
("GET", API + "/pulls?state=open&limit=50&page=1"): prs})
|
||||
self.assertEqual(g.find_open_pr("docs-review/pr-12")["number"], 2)
|
||||
|
||||
def test_find_pr_any_state_newest_first(self):
|
||||
prs = [pr(7, "docs-review/pr-12"), pr(9, "docs-review/pr-12"),
|
||||
pr(11, "docs-review/pr-123")]
|
||||
g, _ = client({
|
||||
("GET", BASE + "/api/v1/user"): USER,
|
||||
("GET", API + "/pulls?state=all&limit=50&page=1"): prs})
|
||||
self.assertEqual(g.find_pr("docs-review/pr-12")["number"], 9)
|
||||
self.assertIsNone(g.find_pr("docs-review/pr-1"))
|
||||
|
||||
def test_prs_by_other_users_or_forks_are_ignored(self):
|
||||
spoof = pr(20, "docs-review/pr-12", login="mallory")
|
||||
fork = pr(21, "docs-review/pr-12", repo="mallory/r")
|
||||
mine = pr(5, "docs-review/pr-12")
|
||||
routes = {("GET", BASE + "/api/v1/user"): USER}
|
||||
for state in ("all", "open"):
|
||||
routes[("GET", API + f"/pulls?state={state}&limit=50&page=1")] = \
|
||||
[spoof, fork, mine]
|
||||
g, _ = client(routes)
|
||||
self.assertEqual(g.find_pr("docs-review/pr-12")["number"], 5)
|
||||
self.assertEqual(g.find_open_pr("docs-review/pr-12")["number"], 5)
|
||||
self.assertEqual(g.find_open_pr_prefix("docs-review/")["number"], 5)
|
||||
g, _ = client({**routes, **{
|
||||
(m, u): [spoof, fork] for (m, u) in routes if "/pulls" in u}})
|
||||
self.assertIsNone(g.find_pr("docs-review/pr-12"))
|
||||
self.assertIsNone(g.find_open_pr("docs-review/pr-12"))
|
||||
self.assertIsNone(g.find_open_pr_prefix("docs-review/"))
|
||||
|
||||
def test_open_pr_and_edit(self):
|
||||
g, f = client({
|
||||
("POST", API + "/pulls"): {"number": 12},
|
||||
("PATCH", API + "/pulls/12"): {},
|
||||
})
|
||||
self.assertEqual(g.open_pr("h", "main", "t", "b"), 12)
|
||||
self.assertEqual(json.loads(f.requests[0].data),
|
||||
{"head": "h", "base": "main", "title": "t", "body": "b"})
|
||||
g.edit_pr(12, body="new")
|
||||
self.assertEqual(json.loads(f.requests[1].data), {"body": "new"})
|
||||
|
||||
def test_close_pr_comments_then_closes(self):
|
||||
g, f = client({
|
||||
("POST", API + "/issues/12/comments"): {},
|
||||
("PATCH", API + "/pulls/12"): {},
|
||||
})
|
||||
g.close_pr(12, "bye")
|
||||
self.assertEqual([r.get_method() for r in f.requests], ["POST", "PATCH"])
|
||||
self.assertEqual(json.loads(f.requests[1].data), {"state": "closed"})
|
||||
|
||||
def test_issues(self):
|
||||
g, f = client({
|
||||
("GET", API + "/issues?state=open&type=issues&limit=50&page=1"):
|
||||
[{"number": 3, "body": "x <!-- k -->"}],
|
||||
("POST", API + "/issues"): {"number": 8},
|
||||
("PATCH", API + "/issues/8"): {},
|
||||
})
|
||||
self.assertEqual(g.find_open_issue("<!-- k -->")["number"], 3)
|
||||
self.assertEqual(g.open_issue("t", "b"), 8)
|
||||
g.edit_issue(8, title="t2")
|
||||
self.assertEqual(json.loads(f.requests[-1].data), {"title": "t2"})
|
||||
|
||||
|
||||
class TestRedaction(unittest.TestCase):
|
||||
SECRET = "oauth-value-0123456789"
|
||||
|
||||
def test_secret_values_are_redacted_in_posted_text(self):
|
||||
env = {"CLAUDE_CODE_OAUTH_TOKEN": self.SECRET, "PATH": "/usr/bin"}
|
||||
g, f = client({
|
||||
("POST", API + "/issues"): {"number": 8},
|
||||
("PATCH", API + "/pulls/3"): {},
|
||||
}, redact_env=env)
|
||||
g.open_issue(f"title {self.SECRET}", f"body {self.SECRET} /usr/bin")
|
||||
g.edit_pr(3, body=f"leak {self.SECRET}")
|
||||
self.assertEqual(json.loads(f.requests[0].data),
|
||||
{"title": "title ***", "body": "body *** /usr/bin"})
|
||||
self.assertEqual(json.loads(f.requests[1].data), {"body": "leak ***"})
|
||||
for req in f.requests:
|
||||
self.assertNotIn(self.SECRET.encode(), req.data)
|
||||
|
||||
def test_own_token_is_redacted_without_env(self):
|
||||
g, f = client({("POST", API + "/issues/5/comments"): {}})
|
||||
g._request("POST", "/repos/o/r/issues/5/comments", {"body": f"x {TOKEN}"})
|
||||
self.assertEqual(json.loads(f.requests[0].data), {"body": "x ***"})
|
||||
|
||||
|
||||
class TestResponses(unittest.TestCase):
|
||||
def test_non_json_response_raises_forge_error(self):
|
||||
g, _ = client({("GET", BASE + "/api/v1/user"): b"<html>bad gateway</html>"})
|
||||
with self.assertRaises(ForgeError) as cm:
|
||||
g.identity()
|
||||
self.assertIn("GET /user", str(cm.exception))
|
||||
|
||||
def test_close_issue(self):
|
||||
g, f = client({("PATCH", API + "/issues/8"): {}})
|
||||
g.close_issue(8)
|
||||
self.assertEqual(json.loads(f.requests[0].data), {"state": "closed"})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user