Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmapi/src/cmapi/core/git/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 6 additions & 5 deletions cmapi/src/cmapi/core/git/gitea.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions cmapi/src/cmapi/core/git/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]:
Expand Down
2 changes: 1 addition & 1 deletion cmapi/src/cmapi/core/ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmapi/src/cmapi/jobs/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmapi/src/cmapi/jobs/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion cmapi/tests/service/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions cmapi/tests/service/test_github_actions_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down