mirror of
https://github.com/rennf93/roboco.git
synced 2026-08-03 07:23:24 +02:00
* feat(forge): Phase 2 — Gitea provider, per-call routing, host registry Gitea support lands behind the Phase-1 seam: - GiteaProvider (services/forge/gitea.py): Gitea v1 transport addressed by instance host (api base from the project's git_url). Where Gitea's wire contract diverges from GitHub's, the provider adapts responses back into the shapes GitService already classifies (ShapedResponse): `token` auth scheme, duplicate-PR 409→422 with the "already exists" text GitService keys on, commit statuses reshaped into check_runs / workflow_runs envelopes, APPROVE→APPROVED review mapping, Do-keyed POST merge, merge-method repo keys, label-color '#' prefix, client-side head/base PR filtering. Deliberate postures per the spec: zero-workflows fail-open (statuses-free repo → no_ci_configured) and merge_branch as a shaped 501 (env-sync cascade lands on missing_ref; the shared local-git fallback is Phase-2.1). - ForgeRouter (services/forge/router.py): GitService._forge now routes per call from RepoRef.host — every existing call site unchanged in shape. RepoRef gains an optional host; _parse_git_url returns the host-stamped ref and it is threaded through GitService/release executor instead of being rebuilt from strings (helpers re-signatured to take RepoRef). - Host registry (services/forge/registry.py): in-memory host→provider map, self-healing — ProjectService.get/get_by_slug re-register on every read; provider_for resolves gitea projects by git_url host. - Registration validation now accepts git_provider="gitea"; GitLab remains recognized-but-rejected. Panel: the read-only Forge badge becomes a real picker (Auto-detect / GitHub-GHE / Gitea / GitLab disabled). Plain git (clone/fetch/push) needs no changes — the Basic-auth extraheader works on Gitea unchanged. Gates: mypy 392 files, xenon A, full unit suite 6356 green, integration suite 2257 green. * feat(forge): live-Gitea contract suite + scheme support + slash-safe refs Hardening from running the provider against a real dockerized Gitea 1.22.6 (the spec's Phase-2 contract suite, now committed as the env-gated tests/e2e_smoke/test_gitea_live.py — self-seeding: creates its own repo, pushes real commits, and drives PR open → duplicate reshape → list/filter → diff → review → labels → commit-status CI reshapes → squash merge → branch delete → release, plus a live verification of the x-access-token Basic-auth git-CLI claim). Two real findings fixed: - Branch refs weren't URL-encoded — every RoboCo branch carries slashes (feature/backend/...), and Gitea's router 404s on the extra path segments. list_ci_runs + delete_branch_ref now quote the ref (regression-pinned in the unit suite). - The API base hardcoded https; a LAN instance serving plain http is a real deployment shape. GiteaProvider gains a scheme (recorded per host by the registry from the project's git_url). ShapedResponse moves to forge/shaping.py (shared by the upcoming GitLab transport, which needs its text override for diff reassembly). * feat(forge): Phase 3 GitLab provider + Phase 2.1 local-merge fallback GitLabProvider (services/forge/gitlab.py): GitLab v4 transport addressed by host+scheme, subgroup-safe (the MR project path packs into RepoRef.owner, URL-encoded per call). Adapters translate MR semantics into the GitHub shapes GitService classifies: iid→number, source/target_branch→head/base with a merged bool, per-file diffs reassembled into unified-diff text (ShapedResponse text override, 3-page cap), approve-vs-note review routing (GitLab has no request-changes verb), pipelines/statuses reshaped into workflow_runs/check_runs, merge-method repo-key mapping, duplicate-MR 409→422. Reviewer mirroring is skipped (needs numeric ids RoboCo doesn't store); provisioning stays Phase 4. gitlab.com now auto-detects at registration like github.com; self-hosted GitLab sets the provider explicitly (panel picker enabled). Phase 2.1: neither Gitea nor GitLab has GitHub's server-side merges API — their merge_branch returns a shaped 501 and GitService.sync_env_branch now runs the shared local-git fallback (_local_merge_branch: throwaway clone → ancestor check → merge → push; a conflict aborts with the remote untouched; same status vocabulary as the merges-API path). Also aligns the whole tree with the full gate's tests/-scoped mypy (provider-test responder typing, e2e_smoke's stale owner/repo shapes). Gates: mypy 1229 files clean, xenon A, unit suite 6393 green, forge suites 85 green, panel typecheck/lint clean. --------- Co-authored-by: Renn F <rennf93@users.noreply.github.com>
105 lines
4.0 KiB
Python
105 lines
4.0 KiB
Python
"""Provider resolution + the host↔provider map behind per-call routing.
|
|
|
|
``provider_for(project)`` maps a project's ``git_provider`` (Phase 0's
|
|
``projects.git_provider`` column) onto a concrete :class:`GitProvider`.
|
|
Phase 2 adds gitea: the provider is addressed by the instance host, derived
|
|
from the project's ``git_url``.
|
|
|
|
The module also keeps the process-wide **host map** the
|
|
:class:`~roboco.services.forge.router.ForgeRouter` consults per call:
|
|
``register_project_forge`` is invoked at the project chokepoints every git
|
|
flow already crosses (``ProjectService.create``/``update`` and the decrypted
|
|
token reads), so by the time any REST call happens for a gitea project its
|
|
host is registered. In-memory and per-process by design — a restart forgets
|
|
it and the very next project/token read re-registers (the same self-healing
|
|
posture as the read-clone sync throttle).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from roboco.exceptions import GitError
|
|
from roboco.foundation.policy.forge import extract_host
|
|
from roboco.services.forge.gitea import GiteaProvider
|
|
from roboco.services.forge.github import GitHubProvider
|
|
from roboco.services.forge.gitlab import GitLabProvider
|
|
|
|
if TYPE_CHECKING:
|
|
from roboco.services.forge.base import GitProvider
|
|
|
|
# host (lowercase) → provider name ("github" | "gitea"). github.com is
|
|
# implicit and never needs registering. _HOST_SCHEMES remembers a plain-http
|
|
# host (LAN instance with no TLS terminator) so the API base matches the
|
|
# git_url's own scheme; absent = https.
|
|
_HOST_PROVIDERS: dict[str, str] = {}
|
|
_HOST_SCHEMES: dict[str, str] = {}
|
|
|
|
|
|
def host_of(git_url: str | None) -> str | None:
|
|
"""The forge host of a git URL (https/ssh/scp forms), or None."""
|
|
if not git_url:
|
|
return None
|
|
return extract_host(git_url)
|
|
|
|
|
|
def _scheme_of(git_url: str) -> str:
|
|
return "http" if git_url.strip().lower().startswith("http://") else "https"
|
|
|
|
|
|
def register_project_forge(git_url: str | None, git_provider: str | None) -> None:
|
|
"""Record a project's host→provider mapping for per-call routing.
|
|
|
|
Called from the ProjectService chokepoints; a github.com host or a
|
|
missing provider records nothing (GitHub is the router's default).
|
|
"""
|
|
host = host_of(git_url)
|
|
if host is None or host == "github.com" or git_url is None:
|
|
return
|
|
if git_provider in ("github", "gitea", "gitlab"):
|
|
_HOST_PROVIDERS[host] = git_provider
|
|
_HOST_SCHEMES[host] = _scheme_of(git_url)
|
|
|
|
|
|
def provider_name_for_host(host: str) -> str | None:
|
|
"""The registered provider name for a host — None when unregistered."""
|
|
if host == "github.com":
|
|
return "github"
|
|
return _HOST_PROVIDERS.get(host.lower())
|
|
|
|
|
|
def scheme_for_host(host: str) -> str:
|
|
"""The registered scheme for a host — https unless the project's git_url
|
|
said otherwise."""
|
|
return _HOST_SCHEMES.get(host.lower(), "https")
|
|
|
|
|
|
def provider_for(project: Any | None = None) -> GitProvider:
|
|
"""Resolve the :class:`GitProvider` for a project (or the system default).
|
|
|
|
``project`` is duck-typed (only ``.git_provider`` / ``.git_url`` are
|
|
read) so this module never needs to import a concrete Project model.
|
|
``None`` — no project in scope, or a project whose ``git_provider`` is
|
|
unset — resolves to GitHub, matching
|
|
``roboco.foundation.policy.forge.detect_provider``'s default.
|
|
"""
|
|
provider_name = (
|
|
getattr(project, "git_provider", None) if project is not None else None
|
|
)
|
|
if provider_name in (None, "github"):
|
|
return GitHubProvider()
|
|
host = host_of(getattr(project, "git_url", None))
|
|
if host is None:
|
|
raise GitError(
|
|
f"{provider_name} project has no parseable git_url host",
|
|
{"git_provider": provider_name},
|
|
)
|
|
if provider_name == "gitea":
|
|
return GiteaProvider(host, scheme=scheme_for_host(host))
|
|
if provider_name == "gitlab":
|
|
return GitLabProvider(host, scheme=scheme_for_host(host))
|
|
raise GitError(
|
|
f"Unsupported git_provider {provider_name!r}.",
|
|
{"git_provider": provider_name},
|
|
)
|