diff --git a/cmapi/src/cmapi/core/git/base.py b/cmapi/src/cmapi/core/git/base.py index 90aeff9b..d461d88f 100644 --- a/cmapi/src/cmapi/core/git/base.py +++ b/cmapi/src/cmapi/core/git/base.py @@ -355,7 +355,7 @@ def open_pr(self, branch: str, head: str, title: str, body: str) -> PrRef: """Open it, or return the one that exists for the branch. Its head must be head.""" raise NotImplementedError - def pr_status(self, pr: PrRef) -> PrStatus: + def pr_status(self, pr: PrRef, *, ci: bool = True) -> PrStatus: raise NotImplementedError def merge(self, pr: PrRef, expected_head: str) -> str: diff --git a/cmapi/src/cmapi/core/git/gitea.py b/cmapi/src/cmapi/core/git/gitea.py index 0bfa9e3f..c86cdc31 100644 --- a/cmapi/src/cmapi/core/git/gitea.py +++ b/cmapi/src/cmapi/core/git/gitea.py @@ -259,17 +259,18 @@ def find_pr(self, branch: str) -> PrRef | None: return self._pr_ref(p) return None - def pr_status(self, pr: PrRef) -> PrStatus: + def pr_status(self, pr: PrRef, *, ci: bool = True) -> PrStatus: r = self._call("pr_status", "GET", f"{self._repo()}/pulls/{pr.number}") if r.status_code != 200: raise GitUnavailable(f"PR {pr.number}: {r.status_code}") p = r.json() head = p["head"]["sha"] checks: dict[str, CheckState] = {} - s = self._call("pr_checks", "GET", f"{self._repo()}/commits/{head}/status", ok=(200, 404)) - if s.status_code == 200: - for st in s.json().get("statuses") or []: - checks[st["context"]] = {"success": "success", "pending": "pending"}.get(st["status"], "failure") + if ci: + s = self._call("pr_checks", "GET", f"{self._repo()}/commits/{head}/status", ok=(200, 404)) + if s.status_code == 200: + for st in s.json().get("statuses") or []: + checks[st["context"]] = {"success": "success", "pending": "pending"}.get(st["status"], "failure") state = "merged" if p.get("merged") else ("open" if p["state"] == "open" else "closed") return PrStatus(state=state, checks=checks, merge_sha=p.get("merge_commit_sha") if p.get("merged") else None, head_sha=head) diff --git a/cmapi/src/cmapi/core/git/github.py b/cmapi/src/cmapi/core/git/github.py index 32be3f09..844b91dc 100644 --- a/cmapi/src/cmapi/core/git/github.py +++ b/cmapi/src/cmapi/core/git/github.py @@ -269,7 +269,7 @@ def list_open(self, prefix: str) -> list[PrRef]: return [self._pr_ref(p) for p in self._pages("pr_list", f"{self._repo()}/pulls", None, state="open") if p["head"]["ref"].startswith(prefix)] - def pr_status(self, pr: PrRef) -> PrStatus: + def pr_status(self, pr: PrRef, *, ci: bool = True) -> PrStatus: r = self._call("pr_state", "GET", f"{self._repo()}/pulls/{pr.number}", ok=(200,)) if r.status_code != 200: text = self.host_text(r) @@ -282,7 +282,7 @@ def pr_status(self, pr: PrRef) -> PrStatus: head_sha=head) # CI counts only for the head this request approved (pr.head_sha, the head the PR was opened on). A PR the host reports # on another head, or on none, gives no CI: the worker fails its cmapi check on the head_sha returned here. - checks = self._actions_checks(pr) if self.external_ci and head and head == pr.head_sha else {} + checks = self._actions_checks(pr) if ci and self.external_ci and head and head == pr.head_sha else {} return PrStatus(state="open", checks=checks, head_sha=head) def _actions_checks(self, pr: PrRef) -> dict[str, CheckState]: diff --git a/cmapi/src/cmapi/core/ports.py b/cmapi/src/cmapi/core/ports.py index 1e99cdf3..9df630b1 100644 --- a/cmapi/src/cmapi/core/ports.py +++ b/cmapi/src/cmapi/core/ports.py @@ -119,7 +119,7 @@ def read_folder(self, path: str, ref: str | None = None) -> tuple[dict[str, str] def list_folder(self, path: str, ref: str | None = None) -> tuple[dict[str, str], str]: ... # {path: blob|commit}, sha def push_request_branch(self, req: Any) -> Any: ... # BranchRequest -> PushResult def open_pr(self, branch: str, head: str, title: str, body: str) -> PrRef: ... - def pr_status(self, pr: PrRef) -> PrStatus: ... + def pr_status(self, pr: PrRef, *, ci: bool = True) -> PrStatus: ... def merge_check(self, head: str, folder: str, prev: Mapping[str, str], files: Mapping[str, str | None], *, whole_folder: bool = False) -> Any: ... def merge(self, pr: PrRef, expected_head: str) -> str: ... # merge sha, pinned to expected_head diff --git a/cmapi/src/cmapi/jobs/apply.py b/cmapi/src/cmapi/jobs/apply.py index 80aafb46..a7db2714 100644 --- a/cmapi/src/cmapi/jobs/apply.py +++ b/cmapi/src/cmapi/jobs/apply.py @@ -143,7 +143,7 @@ def _apply(ctx: AppContext, repo: RequestRepo, jobs: JobRepo, row, job: JobRow) def _adopt_merged(ctx: AppContext, repo: RequestRepo, jobs: JobRepo, row, job: JobRow) -> bool: """A person merged the PR: adopted through the merge job's post-merge check (merged, then watch_sync applies from main), and True. False while the PR is open or closed unmerged, and the caller's refusal stands.""" - state = ctx.git.pr_status(pr_of(row)) + state = ctx.git.pr_status(pr_of(row), ci=False) if state.state != "merged": return False from cmapi.jobs.merge import _merged diff --git a/cmapi/src/cmapi/jobs/merge.py b/cmapi/src/cmapi/jobs/merge.py index 0d786c94..7e6abad2 100644 --- a/cmapi/src/cmapi/jobs/merge.py +++ b/cmapi/src/cmapi/jobs/merge.py @@ -57,7 +57,7 @@ def _merge(ctx: AppContext, repo: RequestRepo, jobs: JobRepo, row, job: JobRow) after_apply = row.status in AFTER_APPLY pinned = row.pr.get("applied_head") if after_apply else ((job.payload or {}).get("head") or row.pr["head"]) pr = pr_of(row) - state = ctx.git.pr_status(pr) + state = ctx.git.pr_status(pr, ci=False) if state.state == "merged": return _merged(ctx, repo, jobs, row, job, state.merge_sha or "", adopted=True) if state.state == "closed": diff --git a/cmapi/tests/service/conftest.py b/cmapi/tests/service/conftest.py index 1ce62b96..40e7558f 100644 --- a/cmapi/tests/service/conftest.py +++ b/cmapi/tests/service/conftest.py @@ -272,7 +272,7 @@ def request_of(self, sha): branch = next((b for b, h in self.heads.items() if h == sha and b in self.branches), None) return (self.owners.get(branch) or (None,))[0] - def pr_status(self, pr): + def pr_status(self, pr, *, ci=True): self._check() p = self.prs[pr.number] head = self.heads[pr.branch] if pr.branch in self.heads else pr.head_sha diff --git a/cmapi/tests/service/test_github_actions_ci.py b/cmapi/tests/service/test_github_actions_ci.py index 5a20a892..ed774ad4 100644 --- a/cmapi/tests/service/test_github_actions_ci.py +++ b/cmapi/tests/service/test_github_actions_ci.py @@ -73,6 +73,15 @@ def checks(**kw): return dict(provider(**kw).pr_status(PR).checks) +def test_state_only_read_skips_the_actions_api(): + seen = [] + + status = provider(seen=seen).pr_status(PR, ci=False) + + assert status.state == "open" and status.checks == {} + assert [r.url.path for r in seen] == ["/repos/o/r/pulls/7"] + + def test_host_ci_is_on_by_default_and_the_worker_posts_cmapi_check_alone(): git = GitHubProvider(API, "o/r", "token") assert (git.external_ci, git.post_statuses, git.posts_ci) == (True, True, False)