Treat a recorded base that is origin's default branch as the default branch, and wait for the forge to show a pushed head before push asks for a review
This commit is contained in:
+36
-14
@@ -43,7 +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.
|
||||
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.
|
||||
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;
|
||||
@@ -139,6 +141,9 @@ QUEUE = "chunk-pr-merge-queue"
|
||||
# A turn lasts as long as its checks, which chunk-pr waits half an hour for on Gitea, so a queue still held after that is reported, not waited out.
|
||||
QUEUE_SECONDS = 30 * 60
|
||||
QUEUE_POLL_SECONDS = 10
|
||||
# How long push waits for the forge to show the pushed commit as the head of the branch's open pull request, polling every PUSH_POLL_SECONDS: GitHub may list the old head for a while after a push.
|
||||
PUSH_SECONDS = 60
|
||||
PUSH_POLL_SECONDS = 5
|
||||
# The line record-ruling writes into the comprehension history, as git's -G reads it.
|
||||
RULING = "^Ruling: "
|
||||
# The project's config, loaded once per step, on the first call that needs it.
|
||||
@@ -164,8 +169,8 @@ def main(argv: list[str]) -> int:
|
||||
return 0
|
||||
|
||||
|
||||
def step(argv: list[str], run: Run, config: Config, parks: bool = False) -> str | Failure:
|
||||
"""Run the step argv names: the line to print, or a Failure. A malformed argv doesn't run anything. parks is whether a merge fails at once where it would wait for an approval (`merged`)."""
|
||||
def step(argv: list[str], run: Run, config: Config, parks: bool = False, sleep: typing.Callable[[float], None] = time.sleep) -> str | Failure:
|
||||
"""Run the step argv names: the line to print, or a Failure. A malformed argv doesn't run anything. parks is whether a merge fails at once where it would wait for an approval (`merged`). sleep is how push and merge wait."""
|
||||
match argv:
|
||||
case ["open", title, *body] if len(body) <= 1 and not title.startswith("-"):
|
||||
return Failure("give a title of one line") if not one_line(title) else misfit(title, run, config) or opened(title, body[0] if body else "", run)
|
||||
@@ -175,12 +180,12 @@ def step(argv: list[str], run: Run, config: Config, parks: bool = False) -> str
|
||||
return Failure("give a title of one line") if title and not one_line(title[0]) else (misfit(title[0], run, config) if title else None) or readied(run, title[0] if title else None, config)
|
||||
# Three readers, seated as those who run `chunk-pr`, picked the spellings `push`, 3-0, and `open-epic`, 2-1.
|
||||
case ["push"]:
|
||||
return pushed(run, config)
|
||||
return pushed(run, config, sleep)
|
||||
case ["open-epic", epic, title, *body] if len(body) <= 1 and not any(given.startswith("-") for given in (epic, title)):
|
||||
return Failure("give a title of one line") if not one_line(title) else epic_opened(epic, title, body[0] if body else "", run, config)
|
||||
case ["merge", *given]:
|
||||
finishes = finishes_of(given)
|
||||
return Failure(USAGE) if finishes is None else merged(run, config, time.sleep, finishes=finishes, parks=parks)
|
||||
return Failure(USAGE) if finishes is None else merged(run, config, sleep, finishes=finishes, parks=parks)
|
||||
case ["clean"]:
|
||||
return cleaned(run)
|
||||
return Failure(USAGE)
|
||||
@@ -235,12 +240,13 @@ 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."""
|
||||
recorded = run(["git", "config", "--get", f"branch.{branch}.{CHUNK_BASE}"])
|
||||
if not isinstance(recorded, Failure) and recorded.strip():
|
||||
return Base(recorded.strip(), True)
|
||||
"""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."""
|
||||
default = default_branch(run)
|
||||
return default if isinstance(default, Failure) else Base(default, False)
|
||||
if isinstance(default, Failure):
|
||||
return default
|
||||
recorded = run(["git", "config", "--get", f"branch.{branch}.{CHUNK_BASE}"])
|
||||
recorded = "" if isinstance(recorded, Failure) else recorded.strip()
|
||||
return Base(recorded, True) if recorded and recorded != default else Base(default, False)
|
||||
|
||||
|
||||
def pull(run: Run) -> tuple[Forge, str, str] | Failure:
|
||||
@@ -363,14 +369,15 @@ def marked_ready(run: Run, title: str | None) -> str | Failure:
|
||||
return f"Marked pull request #{number} ready, titled “{title}”." if title is not None else f"Marked pull request #{number} ready."
|
||||
|
||||
|
||||
def pushed(run: Run, config: Config) -> str | Failure:
|
||||
"""Push the branch checked out, and ask for a review of its open pull request as `review_asked` does."""
|
||||
def pushed(run: Run, config: Config, sleep: typing.Callable[[float], None]) -> str | Failure:
|
||||
"""Push the branch checked out, and ask for a review of its open pull request as `review_asked` does, once the forge shows the pushed commit as that pull request's head."""
|
||||
found = checkout(run)
|
||||
if isinstance(found, Failure):
|
||||
return found
|
||||
forge, branch, head = found
|
||||
sent = run(["git", "push", "--quiet", "--set-upstream", "origin", branch])
|
||||
number = sent if isinstance(sent, Failure) else open_pull(forge, branch, head, run)
|
||||
old = run(["git", "ls-remote", "origin", f"refs/heads/{branch}"])
|
||||
sent = old if isinstance(old, Failure) else 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
|
||||
@@ -379,6 +386,21 @@ def pushed(run: Run, config: Config) -> str | Failure:
|
||||
return f"Pushed {branch}, and asked for a review of pull request #{number} with “{asked}”." if asked else f"Pushed {branch}."
|
||||
|
||||
|
||||
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
|
||||
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:
|
||||
return number
|
||||
stale = open_pull(forge, branch, old, run)
|
||||
if not isinstance(stale, str):
|
||||
return stale
|
||||
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")
|
||||
|
||||
|
||||
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."""
|
||||
branch = answers(run, ["symbolic-ref", "--short", "HEAD"])
|
||||
|
||||
Reference in New Issue
Block a user