Answer stability round 1: 1 🔴, 1 🟡 (0 misread); wait for a pull request at another head, and only where a review is owed
This commit is contained in:
+38
-29
@@ -43,9 +43,9 @@ ready marks the pull request ready for review. On Gitea a draft is a pull req
|
||||
as the pull request's title first; where that fails, the pull request stays a draft.
|
||||
push pushes the branch, setting its upstream. Where its pull request is open and goes into an
|
||||
epic's working branch, it then asks for a review as ready does. Use it for each push
|
||||
after ready there, and wait for that review as for any other. Where the pull request
|
||||
was open at the branch's old head, it waits up to a minute for the forge to show the
|
||||
new head on it, and fails, naming the pull request, where the forge never does.
|
||||
after ready there, and wait for that review as for any other. Before it asks, it waits up
|
||||
to a minute for the forge to show the pushed commit as the head of the branch's open
|
||||
pull request, and fails, naming the pull request, where the forge never does.
|
||||
merge merges the pull request in its turn: the chunks of one project on this machine take
|
||||
turns, one at a time. Run it in the background. In its turn it:
|
||||
1. fetches origin and merges its default branch into the branch, as sync does;
|
||||
@@ -122,7 +122,7 @@ import time
|
||||
import typing
|
||||
|
||||
from chunk_pr.approval import approval, approved, awaited, left_to_a_person, left_to_park, PARKS, unanswered
|
||||
from forge.pulls import checks_passed, merged_pull, open_pull, other_base, required_checks
|
||||
from forge.pulls import checks_passed, merged_pull, open_pull, other_base, pull_from, required_checks
|
||||
from git_cli.git_repo import CHUNK_BASE, Git
|
||||
from git_cli import epic_branch
|
||||
from failure import Failure
|
||||
@@ -240,7 +240,7 @@ class Base:
|
||||
|
||||
|
||||
def base_of(branch: str, run: Run) -> Base | Failure:
|
||||
"""The Base of the pull request from branch: the epic's working branch the checkout records for it (`git_repo.record_chunk_base`), else origin's default branch. A recorded base that is origin's default branch is no epic's: the pull request merges into the default branch, where a person may have to merge it."""
|
||||
"""The Base of the pull request from branch: the epic's working branch the checkout records for it (`git_repo.record_chunk_base`), else origin's default branch. A recorded base equal to origin's default branch isn't an epic's working branch: the pull request merges into the default branch, so `merge = "human"` and `wait-for-approval` apply to it."""
|
||||
default = default_branch(run)
|
||||
if isinstance(default, Failure):
|
||||
return default
|
||||
@@ -375,36 +375,49 @@ def pushed(run: Run, config: Config, sleep: typing.Callable[[float], None]) -> s
|
||||
if isinstance(found, Failure):
|
||||
return found
|
||||
forge, branch, head = found
|
||||
old = run(["git", "ls-remote", "origin", f"refs/heads/{branch}"])
|
||||
if isinstance(old, Failure):
|
||||
return old
|
||||
sent = run(["git", "push", "--quiet", "--set-upstream", "origin", branch])
|
||||
number = sent if isinstance(sent, Failure) else shown_pull(forge, branch, head, old.split()[0] if old.strip() else "", run, sleep)
|
||||
if isinstance(number, Failure):
|
||||
return number
|
||||
asked = review_asked(forge, number, run, config) if number is not None else None
|
||||
if isinstance(asked, Failure):
|
||||
return Failure(f"pushed {branch}, but could not ask for a review: {asked.reason}")
|
||||
return f"Pushed {branch}, and asked for a review of pull request #{number} with “{asked}”." if asked else f"Pushed {branch}."
|
||||
if isinstance(sent, Failure):
|
||||
return sent
|
||||
comment = review_comment(run, config)
|
||||
number = None if comment is None or isinstance(comment, Failure) else shown_pull(forge, branch, head, run, sleep)
|
||||
if isinstance(comment, Failure):
|
||||
return Failure(f"pushed {branch}, but could not ask for a review: {comment.reason}")
|
||||
if isinstance(number, Failure) or number is None or comment is None:
|
||||
return number if isinstance(number, Failure) else f"Pushed {branch}."
|
||||
made = comment_on(forge, number, comment, run)
|
||||
return Failure(f"pushed {branch}, but could not ask for a review: {made.reason}") if isinstance(made, Failure) else f"Pushed {branch}, and asked for a review of pull request #{number} with “{comment}”."
|
||||
|
||||
|
||||
def shown_pull(forge: Forge, branch: str, head: str, old: str, run: Run, sleep: typing.Callable[[float], None]) -> str | None | Failure:
|
||||
"""The number of branch's open pull request at head, just pushed over old, the head origin had before. The forge may keep showing old for up to PUSH_SECONDS, so while it does, this polls; a Failure naming the pull request where it still does. None where no pull request is open at head or at old: `push` before `open` is normal."""
|
||||
stale = None
|
||||
def shown_pull(forge: Forge, branch: str, head: str, run: Run, sleep: typing.Callable[[float], None]) -> str | None | Failure:
|
||||
"""The number of branch's open pull request at head, just pushed. Right after a push the forge may still list the pull request at an older head, so this polls up to PUSH_SECONDS while it lists one at another head, and then fails naming it. None where no pull request from branch is open: `push` before `open` is normal."""
|
||||
other = None
|
||||
for waited in range(0, PUSH_SECONDS + 1, PUSH_POLL_SECONDS):
|
||||
number = open_pull(forge, branch, head, run)
|
||||
if number is not None or not old or old == head:
|
||||
if number is not None:
|
||||
return number
|
||||
stale = open_pull(forge, branch, old, run)
|
||||
if not isinstance(stale, str):
|
||||
return stale
|
||||
other = pull_from(forge, branch, run)
|
||||
if not isinstance(other, str):
|
||||
return other
|
||||
if waited < PUSH_SECONDS:
|
||||
sleep(PUSH_POLL_SECONDS)
|
||||
return Failure(f"pushed {branch}, but the forge still shows pull request #{stale} at {old[:12]}, not at {head[:12]}, after {PUSH_SECONDS} seconds, so no review was asked for; run chunk-pr push again")
|
||||
return Failure(f"pushed {branch}, but pull request #{other} still doesn't show {head[:12]} as its head after {PUSH_SECONDS} seconds, so no review was asked for; once it does, run chunk-pr push again")
|
||||
|
||||
|
||||
def review_asked(forge: Forge, number: str, run: Run, config: Config) -> str | None | Failure:
|
||||
"""Comment on the pull request numbered number with the config's `review-request-comment`, where the pull request goes into an epic's working branch: a forge's auto-reviewer may review only pull requests into the default branch unless asked. The comment made; None where the config names none or the pull request goes into the default branch."""
|
||||
"""Comment on the pull request numbered number as `review_comment` says. The comment made; None where it says none."""
|
||||
comment = review_comment(run, config)
|
||||
if comment is None or isinstance(comment, Failure):
|
||||
return comment
|
||||
made = comment_on(forge, number, comment, run)
|
||||
return made if isinstance(made, Failure) else comment
|
||||
|
||||
|
||||
def comment_on(forge: Forge, number: str, comment: str, run: Run) -> str | Failure:
|
||||
return run(["gh", "pr", "comment", number, "--body", comment] if forge == "gh" else ["tea", "comment", number, comment])
|
||||
|
||||
|
||||
def review_comment(run: Run, config: Config) -> str | None | Failure:
|
||||
"""The config's `review-request-comment` that a pull request from the branch checked out is owed, where it goes into an epic's working branch: a forge's auto-reviewer may review only pull requests into the default branch unless asked. None where the config names none or the pull request goes into the default branch."""
|
||||
branch = answers(run, ["symbolic-ref", "--short", "HEAD"])
|
||||
base = branch if isinstance(branch, Failure) else base_of(branch[0], run)
|
||||
if isinstance(base, Failure):
|
||||
@@ -412,11 +425,7 @@ def review_asked(forge: Forge, number: str, run: Run, config: Config) -> str | N
|
||||
if not base.to_epic:
|
||||
return None
|
||||
loaded = config()
|
||||
if isinstance(loaded, Failure) or loaded.review_request_comment is None:
|
||||
return loaded if isinstance(loaded, Failure) else None
|
||||
comment = loaded.review_request_comment
|
||||
made = run(["gh", "pr", "comment", number, "--body", comment] if forge == "gh" else ["tea", "comment", number, comment])
|
||||
return made if isinstance(made, Failure) else comment
|
||||
return loaded if isinstance(loaded, Failure) else loaded.review_request_comment
|
||||
|
||||
|
||||
def epic_opened(epic: str, title: str, body: str, run: Run, config: Config) -> str | Failure:
|
||||
|
||||
@@ -25,7 +25,6 @@ GITEA = "ssh://git@gitea.example.org:22/me/project.git"
|
||||
HEAD = "c0ffee" * 7
|
||||
PUSH = ["git", "push", "--quiet", "--set-upstream", "origin", "item-7"]
|
||||
OLD = "badc0de" * 6
|
||||
OLD_HEAD = ["git", "ls-remote", "origin", "refs/heads/item-7"]
|
||||
GH_LIST = ["gh", "pr", "list", "--head", "item-7", "--json", "number,headRefOid,isCrossRepository", "--template", "{{range .}}{{.number}}\t{{.headRefOid}}\t{{.isCrossRepository}}\n{{end}}"]
|
||||
TEA_LIST = ["tea", "api", "repos/{owner}/{repo}/pulls?state=open&limit=50"]
|
||||
GH_SETTINGS = ["gh", "repo", "view", "--json", "mergeCommitAllowed,rebaseMergeAllowed,squashMergeAllowed"]
|
||||
@@ -127,7 +126,7 @@ class Checkout:
|
||||
|
||||
def acted(self) -> list[list[str]]:
|
||||
"""What it ran beyond reading the checkout, the pull requests and the repository's settings."""
|
||||
reads = [OLD_HEAD, GH_LIST, GH_DECISION, GH_BASE, TEA_LIST, TEA_PULL, GH_SETTINGS, TEA_SETTINGS, OWED, GH_CHECKS, TEA_BRANCH, TEA_STATUS, GH_THREADS]
|
||||
reads = [GH_LIST, GH_DECISION, GH_BASE, TEA_LIST, TEA_PULL, GH_SETTINGS, TEA_SETTINGS, OWED, GH_CHECKS, TEA_BRANCH, TEA_STATUS, GH_THREADS]
|
||||
return [command for command in self.ran if tuple(command) not in self.answers and command not in reads]
|
||||
|
||||
|
||||
@@ -1092,6 +1091,8 @@ class EpicBranch(unittest.TestCase):
|
||||
|
||||
def test_a_branch_whose_base_git_does_not_record_merges_into_the_default_branch(self):
|
||||
self.assertEqual(base_of("item-7", self.unrecorded), Base("main", False))
|
||||
|
||||
def test_a_recorded_base_is_an_epics_working_branch_unless_it_is_the_default_branch(self):
|
||||
self.assertEqual(base_of("item-7", self.recorded("main")), Base("main", False))
|
||||
self.assertEqual(base_of("item-7", self.recorded("ABC-1")), Base("ABC-1", True))
|
||||
|
||||
@@ -1210,31 +1211,31 @@ class EpicBranch(unittest.TestCase):
|
||||
|
||||
def test_push_waits_for_the_forge_to_show_the_new_head_before_it_asks_for_a_review(self):
|
||||
checkout, slept = self.checkout(), []
|
||||
checkout.answers[tuple(OLD_HEAD)] = f"{OLD}\trefs/heads/item-7\n"
|
||||
checkout.listed_heads = [OLD, OLD, OLD, OLD, HEAD]
|
||||
self.assertEqual(step(["push"], checkout, checkout.config, sleep=slept.append), "Pushed item-7, and asked for a review of pull request #12 with “@reviewer review”.")
|
||||
self.assertEqual(slept, [5, 5])
|
||||
|
||||
def test_push_fails_naming_the_pull_request_where_the_forge_never_shows_the_new_head(self):
|
||||
checkout, slept = self.checkout(), []
|
||||
checkout.answers[tuple(OLD_HEAD)] = f"{OLD}\trefs/heads/item-7\n"
|
||||
checkout.listed_heads = [OLD]
|
||||
done = step(["push"], checkout, checkout.config, sleep=slept.append)
|
||||
self.assertEqual(done, Failure(f"pushed item-7, but the forge still shows pull request #12 at {OLD[:12]}, not at {HEAD[:12]}, after 60 seconds, so no review was asked for; run chunk-pr push again"))
|
||||
self.assertIsInstance(done, Failure)
|
||||
for fact in ("pushed item-7", "pull request #12", HEAD[:12], "60 seconds"):
|
||||
self.assertIn(fact, done.reason)
|
||||
self.assertEqual(sum(slept), 60)
|
||||
self.assertNotIn(self.COMMENT_GH, checkout.ran)
|
||||
|
||||
def test_push_waits_for_no_pull_request_that_was_not_open_at_the_old_head(self):
|
||||
checkout, slept = self.checkout(open=False), []
|
||||
checkout.answers[tuple(OLD_HEAD)] = f"{OLD}\trefs/heads/item-7\n"
|
||||
def test_push_does_not_wait_where_the_pull_request_goes_into_the_default_branch(self):
|
||||
checkout, slept = self.checkout(), []
|
||||
checkout.chunk_base = ""
|
||||
checkout.listed_heads = [OLD]
|
||||
self.assertEqual(step(["push"], checkout, checkout.config, sleep=slept.append), "Pushed item-7.")
|
||||
self.assertEqual(slept, [])
|
||||
|
||||
def test_a_push_whose_old_head_cannot_be_read_pushes_nothing(self):
|
||||
checkout = self.checkout()
|
||||
checkout.answers[tuple(OLD_HEAD)] = Failure("git ls-remote: no network")
|
||||
self.assertEqual(step(["push"], checkout, checkout.config), Failure("git ls-remote: no network"))
|
||||
self.assertNotIn(PUSH, checkout.ran)
|
||||
def test_push_does_not_wait_where_no_pull_request_is_open(self):
|
||||
checkout, slept = self.checkout(open=False), []
|
||||
self.assertEqual(step(["push"], checkout, checkout.config, sleep=slept.append), "Pushed item-7.")
|
||||
self.assertEqual(slept, [])
|
||||
|
||||
def test_a_failed_push_asks_for_no_review(self):
|
||||
checkout = self.checkout()
|
||||
|
||||
+8
-3
@@ -22,26 +22,31 @@ def open_pull(forge: Forge, branch: str, head: str, run: Run) -> str | None | Fa
|
||||
return pull_at(forge, branch, head, "open", run)
|
||||
|
||||
|
||||
def pull_from(forge: Forge, branch: str, run: Run) -> str | None | Failure:
|
||||
"""The number of the open pull request from origin's own branch, whatever its head is; None where there is none."""
|
||||
return pull_at(forge, branch, None, "open", run)
|
||||
|
||||
|
||||
def merged_pull(forge: Forge, branch: str, head: str, run: Run) -> str | None | Failure:
|
||||
"""The number of the merged pull request from origin's own branch whose head was head; None where there is none. A rebase or a squash merge counts: the pull request keeps the head it had."""
|
||||
return pull_at(forge, branch, head, "merged", run)
|
||||
|
||||
|
||||
def pull_at(forge: Forge, branch: str, head: str, state: typing.Literal["open", "merged"], run: Run) -> str | None | Failure:
|
||||
def pull_at(forge: Forge, branch: str, head: str | None, state: typing.Literal["open", "merged"], run: Run) -> str | None | Failure:
|
||||
if forge == "gh":
|
||||
# gh lists the open pull requests unless told another state.
|
||||
listed = run(["gh", "pr", "list", "--head", branch, *(["--state", "merged"] if state == "merged" else []), "--json", "number,headRefOid,isCrossRepository", "--template", "{{range .}}{{.number}}\t{{.headRefOid}}\t{{.isCrossRepository}}\n{{end}}"])
|
||||
if isinstance(listed, Failure):
|
||||
return listed
|
||||
rows = [line.split("\t") for line in listed.splitlines()]
|
||||
return next((row[0] for row in rows if row[1:] == [head, "false"]), None)
|
||||
return next((row[0] for row in rows if row[1:] == ([head, "false"] if head is not None else row[1:2] + ["false"])), None)
|
||||
# Gitea lists at most 50 a page; the pull request sought is among the newest. Gitea has no merged state: a merged pull request is a closed one marked merged.
|
||||
pulls = gitea(run, f"repos/{{owner}}/{{repo}}/pulls?state={'open' if state == 'open' else 'closed'}&limit=50")
|
||||
if isinstance(pulls, Failure):
|
||||
return pulls
|
||||
try:
|
||||
# Match the label, not the ref: once origin deletes a merged branch, Gitea reports its ref as refs/pull/<N>/head.
|
||||
return next((str(pull["number"]) for pull in pulls if pull["head"]["label"] == branch and pull["head"]["sha"] == head and pull["head"]["repo"]["full_name"] == pull["base"]["repo"]["full_name"] and (state == "open" or pull["merged"] is True)), None)
|
||||
return next((str(pull["number"]) for pull in pulls if pull["head"]["label"] == branch and (head is None or pull["head"]["sha"] == head) and pull["head"]["repo"]["full_name"] == pull["base"]["repo"]["full_name"] and (state == "open" or pull["merged"] is True)), None)
|
||||
except (KeyError, TypeError):
|
||||
return Failure(f"tea api listed pull requests in a shape that can't be read: {str(pulls)[:200]}")
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from forge.pulls import BEHIND, CheckFailed, checks_now, checks_passed, CONFLICTS, open_pull, other_base, Pending, POLL_SECONDS, ready_pulls, ReadyPull, required_checks, unmergeable, WAIT_SECONDS
|
||||
from forge.pulls import BEHIND, CheckFailed, checks_now, checks_passed, CONFLICTS, open_pull, other_base, Pending, POLL_SECONDS, pull_from, ready_pulls, ReadyPull, required_checks, unmergeable, WAIT_SECONDS
|
||||
from failure import Failure
|
||||
from forge.commands import forge_of, gitea, runner, url_beside
|
||||
|
||||
@@ -46,6 +46,14 @@ class Gitea(unittest.TestCase):
|
||||
|
||||
|
||||
class OpenPull(unittest.TestCase):
|
||||
def test_finds_the_open_pull_request_from_a_branch_whatever_its_head_is(self):
|
||||
gh = "12\told\tfalse\n13\tfork\ttrue\n"
|
||||
tea = json.dumps([{"number": 14, "head": {"label": "item-3", "sha": "old", "repo": {"full_name": "o/r"}}, "base": {"repo": {"full_name": "o/r"}}}, {"number": 15, "head": {"label": "item-3", "sha": "fork", "repo": {"full_name": "f/r"}}, "base": {"repo": {"full_name": "o/r"}}}])
|
||||
self.assertEqual(pull_from("gh", "item-3", lambda command: gh), "12")
|
||||
self.assertEqual(pull_from("tea", "item-3", lambda command: tea), "14")
|
||||
self.assertEqual(open_pull("gh", "item-3", "new", lambda command: gh), None)
|
||||
self.assertIsNone(pull_from("gh", "item-3", lambda command: "13\tfork\ttrue\n"))
|
||||
|
||||
def test_fails_quoting_the_first_200_characters_where_tea_lists_pull_requests_in_another_shape(self):
|
||||
self.assertEqual(open_pull("tea", "item-3", "abc", lambda command: LONG).reason, f"tea api listed pull requests in a shape that can't be read: {LONG[:200]}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user