Publish curated ITWorx_MCP_Hub source from 7fde2770ba344c684b7e7a6b74f763dc19e7c560
This commit is contained in:
1 parent
18cf749bf5
commit
89703431af
11 files changed
+1247
-114
No files matched your search
@@ -1,9 +1,9 @@
|
||||
"""Compatibility hardening for Gitea validation discovery and request latency.
|
||||
|
||||
Some Gitea versions expose pull-request check results as commit statuses while
|
||||
returning an empty branch-filtered Actions run wrapper. The base client treats
|
||||
collections strictly; this subclass preserves that strictness while translating
|
||||
a fully-green exact-head status set into one synthetic completed validation run.
|
||||
Native Gitea PR runs can omit their head branch. This client reads actual
|
||||
exact-head runs without a branch filter and checks available PR identity.
|
||||
Older status-only adapters retain an explicitly labeled legacy fallback;
|
||||
exact-head native reads never synthesize a workflow run or merge proof.
|
||||
|
||||
Large repositories can require longer than the historical 15-second client
|
||||
budget to commit files, apply changesets, or materialize a server-side squash
|
||||
@@ -15,6 +15,8 @@ base client and are not duplicated here.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
@@ -40,6 +42,174 @@ class GiteaClient(BaseGiteaClient):
|
||||
)
|
||||
super().__init__(resilient_settings, token_provider, transport=transport)
|
||||
|
||||
@staticmethod
|
||||
def matches_exact_head_action_run(
|
||||
run: dict[str, Any],
|
||||
*,
|
||||
branch: str,
|
||||
head_sha: str,
|
||||
pull_request_number: int | None = None,
|
||||
) -> bool:
|
||||
if run.get("head_sha") != head_sha:
|
||||
return False
|
||||
head_branch = run.get("head_branch", "")
|
||||
event = run.get("event")
|
||||
if head_branch is None and event == "pull_request":
|
||||
head_branch = ""
|
||||
if event == "workflow_dispatch" and head_branch == f"refs/heads/{branch}":
|
||||
head_branch = branch
|
||||
if not isinstance(head_branch, str) or head_branch not in {"", branch}:
|
||||
return False
|
||||
if not head_branch and event != "pull_request":
|
||||
return False
|
||||
path = run.get("path")
|
||||
path_number: int | None = None
|
||||
if path is not None:
|
||||
if not isinstance(path, str) or len(path) > 512 or path.count("@") > 1:
|
||||
return False
|
||||
if "@" in path:
|
||||
ref = path.split("@", 1)[1]
|
||||
match = re.fullmatch(r"refs/pull/([1-9][0-9]*)/head", ref)
|
||||
if match:
|
||||
path_number = int(match[1])
|
||||
if event != "pull_request" or path_number > 2_147_483_647:
|
||||
return False
|
||||
elif ref != f"refs/heads/{branch}" or event == "pull_request":
|
||||
return False
|
||||
raw_references = run.get("pull_requests")
|
||||
references = [] if raw_references is None else raw_references
|
||||
if not isinstance(references, list):
|
||||
return False
|
||||
numbers: list[int] = []
|
||||
for reference in references:
|
||||
if not isinstance(reference, dict):
|
||||
return False
|
||||
number = reference.get("number")
|
||||
if number is not None:
|
||||
if type(number) is not int or not 1 <= number <= 2_147_483_647:
|
||||
return False
|
||||
numbers.append(number)
|
||||
head = reference.get("head")
|
||||
if head is not None:
|
||||
if not isinstance(head, dict):
|
||||
return False
|
||||
if head.get("ref") not in (None, "", branch) or head.get("sha") not in (
|
||||
None,
|
||||
"",
|
||||
head_sha,
|
||||
):
|
||||
return False
|
||||
if event == "pull_request" and pull_request_number is not None:
|
||||
if path_number not in {None, pull_request_number} or any(
|
||||
number != pull_request_number for number in numbers
|
||||
):
|
||||
return False
|
||||
# Legacy adapters identify PR runs by a real branch and SHA. Native
|
||||
# blank-branch runs need affirmative PR number/ref evidence.
|
||||
if path_number != pull_request_number and pull_request_number not in numbers:
|
||||
return head_branch == branch and not path
|
||||
return True
|
||||
|
||||
def exact_head_action_runs(
|
||||
self,
|
||||
repository: RegisteredRepository,
|
||||
*,
|
||||
branch: str,
|
||||
head_sha: str,
|
||||
pull_request_number: int | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Read bounded actual run evidence; never synthesize a native validation."""
|
||||
if not re.fullmatch(r"(?:[0-9a-fA-F]{40}|[0-9a-fA-F]{64})", head_sha):
|
||||
raise GiteaError("GITEA_TARGET_INVALID", "exact Actions head SHA is invalid")
|
||||
if pull_request_number is not None and (
|
||||
type(pull_request_number) is not int or not 1 <= pull_request_number <= 2_147_483_647
|
||||
):
|
||||
raise GiteaError("GITEA_TARGET_INVALID", "exact Actions PR number is invalid")
|
||||
output: list[dict[str, Any]] = []
|
||||
seen: set[tuple[int, int]] = set()
|
||||
expected_count: int | None = None
|
||||
deadline = time.monotonic() + 20
|
||||
for page in range(1, 11):
|
||||
if time.monotonic() >= deadline:
|
||||
raise GiteaError(
|
||||
"GITEA_ACTION_RUNS_TRUNCATED", "exact Actions read exceeded its bound"
|
||||
)
|
||||
payload = self._request(
|
||||
self._path("repos", repository.owner, repository.name, "actions", "runs"),
|
||||
{"head_sha": head_sha, "page": page, "limit": 100},
|
||||
timeout_seconds=min(10.0, max(0.1, deadline - time.monotonic())),
|
||||
max_attempts=1,
|
||||
)
|
||||
count = payload.get("total_count") if isinstance(payload, dict) else None
|
||||
if isinstance(payload, dict) and "total_count" in payload:
|
||||
if type(count) is not int or not 0 <= count <= 1000:
|
||||
raise GiteaError("GITEA_ACTION_RUNS_INVALID", "invalid exact Actions count")
|
||||
if expected_count is not None and count != expected_count:
|
||||
raise GiteaError("GITEA_ACTION_RUNS_TRUNCATED", "exact Actions count changed")
|
||||
expected_count = count
|
||||
elif expected_count is not None:
|
||||
raise GiteaError("GITEA_ACTION_RUNS_TRUNCATED", "exact Actions count disappeared")
|
||||
if isinstance(payload, dict):
|
||||
payload = (
|
||||
payload.get("workflow_runs")
|
||||
if "workflow_runs" in payload
|
||||
else payload.get("runs")
|
||||
)
|
||||
runs = self._as_list(payload)
|
||||
if len(runs) > 100:
|
||||
raise GiteaError("GITEA_ACTION_RUNS_INVALID", "excess exact Actions page")
|
||||
for run in runs:
|
||||
identifier, attempt = run.get("id"), run.get("run_attempt", 0)
|
||||
number = run.get("run_number", identifier)
|
||||
if (
|
||||
type(identifier) is not int
|
||||
or identifier < 1
|
||||
or type(attempt) is not int
|
||||
or attempt < 0
|
||||
or ("run_attempt" in run and attempt == 0)
|
||||
or type(number) is not int
|
||||
or number < 1
|
||||
or (identifier, attempt) in seen
|
||||
):
|
||||
raise GiteaError(
|
||||
"GITEA_ACTION_RUNS_INVALID", "invalid or repeated exact Actions run"
|
||||
)
|
||||
seen.add((identifier, attempt))
|
||||
output.append(run)
|
||||
if expected_count is not None:
|
||||
if len(output) > expected_count:
|
||||
raise GiteaError("GITEA_ACTION_RUNS_INVALID", "excess exact Actions runs")
|
||||
complete = len(output) == expected_count
|
||||
else:
|
||||
complete = len(runs) < 100
|
||||
if complete:
|
||||
if time.monotonic() >= deadline:
|
||||
raise GiteaError(
|
||||
"GITEA_ACTION_RUNS_TRUNCATED", "exact Actions read exceeded its bound"
|
||||
)
|
||||
matching = [
|
||||
run
|
||||
for run in output
|
||||
if self.matches_exact_head_action_run(
|
||||
run,
|
||||
branch=branch,
|
||||
head_sha=head_sha,
|
||||
pull_request_number=pull_request_number,
|
||||
)
|
||||
]
|
||||
return sorted(
|
||||
matching,
|
||||
key=lambda run: (
|
||||
run.get("run_number", run["id"]),
|
||||
run.get("run_attempt", 0),
|
||||
run["id"],
|
||||
),
|
||||
reverse=True,
|
||||
)
|
||||
if not runs:
|
||||
raise GiteaError("GITEA_ACTION_RUNS_TRUNCATED", "incomplete exact Actions page")
|
||||
raise GiteaError("GITEA_ACTION_RUNS_TRUNCATED", "exact Actions pages exceeded their bound")
|
||||
|
||||
def action_runs(
|
||||
self,
|
||||
repository: RegisteredRepository,
|
||||
@@ -78,6 +248,11 @@ class GiteaClient(BaseGiteaClient):
|
||||
):
|
||||
return []
|
||||
|
||||
native_runs = self.exact_head_action_runs(repository, branch=branch, head_sha=head_sha)
|
||||
if native_runs:
|
||||
offset = (page - 1) * limit
|
||||
return native_runs[offset : offset + limit]
|
||||
|
||||
statuses = self.commit_statuses(repository, head_sha, 1, 100)
|
||||
if not statuses:
|
||||
return []
|
||||
|
||||
Reference in new issue
Block a user