Files
roboco/tests/unit/services/test_git_pr_update.py
T
96401f4c10 feat(forge): Phases 2+2.1+3 — Gitea + GitLab providers, per-call routing, local-merge fallback (#575)
* 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>
2026-07-19 08:12:34 +02:00

311 lines
11 KiB
Python

"""Unit tests for GitService.update_pr_for_task.
Covers PATCH (title/body) + POST reviewers (requested_reviewers) round-trips
against GitHub's REST API. httpx is fully mocked — no real network.
"""
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING, Any
from unittest.mock import AsyncMock, MagicMock, patch
from uuid import UUID, uuid4
import pytest
from roboco.exceptions import GitError
from roboco.services.base import NotFoundError
from roboco.services.forge import RepoRef
from roboco.services.git import GitService
if TYPE_CHECKING:
from contextlib import AbstractContextManager
_PR_NUMBER = 42
_HTTP_NOT_FOUND = 404
_HTTP_UNPROCESSABLE = 422
def _make_session() -> MagicMock:
session = MagicMock()
session.execute = AsyncMock()
session.commit = AsyncMock()
session.rollback = AsyncMock()
return session
def _service() -> GitService:
return GitService(_make_session())
def _patch_project_service(project: object | None) -> AbstractContextManager[object]:
fake_service = MagicMock()
fake_service.get = AsyncMock(return_value=project)
fake_service.get_by_slug = AsyncMock(return_value=project)
return patch("roboco.services.git.get_project_service", return_value=fake_service)
def _bind(svc: GitService, name: str, value: object) -> None:
object.__setattr__(svc, name, value)
def _task_with_pr(pr_number: int | None = _PR_NUMBER, pr_url: str = "") -> MagicMock:
"""Build a TaskTable-shaped MagicMock with a pr_number set."""
project_id = uuid4()
return MagicMock(
id=uuid4(),
project_id=project_id,
pr_number=pr_number,
pr_url=pr_url or f"https://github.com/acme/repo/pull/{pr_number}",
assigned_to=uuid4(),
created_by=uuid4(),
)
def _make_http_response(
*, status_code: int, json_payload: dict[str, Any] | None = None, text: str = ""
) -> MagicMock:
resp = MagicMock()
resp.status_code = status_code
resp.is_success = 200 <= status_code < 300 # noqa: PLR2004
resp.json.return_value = json_payload or {}
resp.text = text
return resp
def _make_async_client(
*,
patch_resp: MagicMock | None = None,
post_resp: MagicMock | None = None,
) -> MagicMock:
client = MagicMock()
client.__aenter__ = AsyncMock(return_value=client)
client.__aexit__ = AsyncMock(return_value=False)
client.patch = AsyncMock(return_value=patch_resp)
client.post = AsyncMock(return_value=post_resp)
return client
async def _stub_task_get(svc: GitService, task: object | None) -> None:
task_service = MagicMock()
task_service.get = AsyncMock(return_value=task)
_bind(svc, "_task_service_for_pr_update", task_service)
def _wire_service(svc: GitService, task: MagicMock) -> MagicMock:
"""Apply common bindings: workspace, remote parse, token resolution."""
_bind(svc, "get_workspace", AsyncMock(return_value=Path("/tmp/ws")))
_bind(svc, "_parse_github_remote", MagicMock(return_value=RepoRef("acme", "repo")))
_bind(svc, "_get_project_token_or_raise", AsyncMock(return_value="tok"))
# update_pr_for_task fetches the task via get_task_service; we patch it
# at the module level.
fake_task_service = MagicMock()
fake_task_service.get = AsyncMock(return_value=task)
return fake_task_service
@pytest.mark.asyncio
async def test_update_pr_title_only_calls_patch_with_title() -> None:
"""title-only call PATCHes /pulls/{n} with {title: ...} and no reviewers POST."""
svc = _service()
task = _task_with_pr()
fake_task_service = _wire_service(svc, task)
fake_project = MagicMock(slug="roboco")
patch_resp = _make_http_response(
status_code=200,
json_payload={
"number": _PR_NUMBER,
"html_url": task.pr_url,
"title": "new title",
},
)
fake_client = _make_async_client(patch_resp=patch_resp)
with (
patch("roboco.services.git.get_task_service", return_value=fake_task_service),
_patch_project_service(fake_project),
patch("roboco.services.git.httpx.AsyncClient", return_value=fake_client),
):
out = await svc.update_pr_for_task(
UUID(str(task.id)), title="new title", body=None, reviewers=None
)
fake_client.patch.assert_awaited_once()
fake_client.post.assert_not_awaited()
call = fake_client.patch.await_args
assert f"/pulls/{_PR_NUMBER}" in call.args[0]
assert call.kwargs["json"] == {"title": "new title"}
assert out["pr_number"] == _PR_NUMBER
assert out["updated_fields"] == ["title"]
@pytest.mark.asyncio
async def test_update_pr_title_and_body_calls_patch_with_both() -> None:
"""Both title and body in the same PATCH payload."""
svc = _service()
task = _task_with_pr()
fake_task_service = _wire_service(svc, task)
fake_project = MagicMock(slug="roboco")
patch_resp = _make_http_response(
status_code=200,
json_payload={"number": _PR_NUMBER, "html_url": task.pr_url},
)
fake_client = _make_async_client(patch_resp=patch_resp)
with (
patch("roboco.services.git.get_task_service", return_value=fake_task_service),
_patch_project_service(fake_project),
patch("roboco.services.git.httpx.AsyncClient", return_value=fake_client),
):
out = await svc.update_pr_for_task(
UUID(str(task.id)), title="t", body="b", reviewers=None
)
fake_client.patch.assert_awaited_once()
call = fake_client.patch.await_args
assert call.kwargs["json"] == {"title": "t", "body": "b"}
assert set(out["updated_fields"]) == {"title", "body"}
@pytest.mark.asyncio
async def test_update_pr_reviewers_only_calls_post_reviewers() -> None:
"""reviewers-only call POSTs to /pulls/{n}/requested_reviewers, skips PATCH."""
svc = _service()
task = _task_with_pr()
fake_task_service = _wire_service(svc, task)
fake_project = MagicMock(slug="roboco")
post_resp = _make_http_response(
status_code=201,
json_payload={"number": _PR_NUMBER, "html_url": task.pr_url},
)
fake_client = _make_async_client(post_resp=post_resp)
with (
patch("roboco.services.git.get_task_service", return_value=fake_task_service),
_patch_project_service(fake_project),
patch("roboco.services.git.httpx.AsyncClient", return_value=fake_client),
):
out = await svc.update_pr_for_task(
UUID(str(task.id)),
title=None,
body=None,
reviewers=["be-dev-2", "be-qa"],
)
fake_client.patch.assert_not_awaited()
fake_client.post.assert_awaited_once()
call = fake_client.post.await_args
assert f"/pulls/{_PR_NUMBER}/requested_reviewers" in call.args[0]
assert call.kwargs["json"] == {"reviewers": ["be-dev-2", "be-qa"]}
assert out["updated_fields"] == ["reviewers"]
@pytest.mark.asyncio
async def test_update_pr_all_three_fields_forwarded() -> None:
"""title + body + reviewers → one PATCH + one POST, both fields reported."""
svc = _service()
task = _task_with_pr()
fake_task_service = _wire_service(svc, task)
fake_project = MagicMock(slug="roboco")
patch_resp = _make_http_response(
status_code=200, json_payload={"number": _PR_NUMBER, "html_url": task.pr_url}
)
post_resp = _make_http_response(
status_code=201, json_payload={"number": _PR_NUMBER, "html_url": task.pr_url}
)
fake_client = _make_async_client(patch_resp=patch_resp, post_resp=post_resp)
with (
patch("roboco.services.git.get_task_service", return_value=fake_task_service),
_patch_project_service(fake_project),
patch("roboco.services.git.httpx.AsyncClient", return_value=fake_client),
):
out = await svc.update_pr_for_task(
UUID(str(task.id)), title="t", body="b", reviewers=["be-dev-2"]
)
fake_client.patch.assert_awaited_once()
fake_client.post.assert_awaited_once()
assert set(out["updated_fields"]) == {"title", "body", "reviewers"}
@pytest.mark.asyncio
async def test_update_pr_404_raises_pr_not_found() -> None:
"""HTTP 404 from PATCH → GitError mentioning PR not found."""
svc = _service()
task = _task_with_pr()
fake_task_service = _wire_service(svc, task)
fake_project = MagicMock(slug="roboco")
patch_resp = _make_http_response(status_code=_HTTP_NOT_FOUND, text="Not Found")
fake_client = _make_async_client(patch_resp=patch_resp)
with (
patch("roboco.services.git.get_task_service", return_value=fake_task_service),
_patch_project_service(fake_project),
patch("roboco.services.git.httpx.AsyncClient", return_value=fake_client),
pytest.raises(GitError, match="PR not found"),
):
await svc.update_pr_for_task(
UUID(str(task.id)), title="t", body=None, reviewers=None
)
@pytest.mark.asyncio
async def test_update_pr_422_raises_with_validation_message() -> None:
"""HTTP 422 from PATCH → GitError surfacing the validation text."""
svc = _service()
task = _task_with_pr()
fake_task_service = _wire_service(svc, task)
fake_project = MagicMock(slug="roboco")
patch_resp = _make_http_response(
status_code=_HTTP_UNPROCESSABLE, text="Validation Failed: body too long"
)
fake_client = _make_async_client(patch_resp=patch_resp)
with (
patch("roboco.services.git.get_task_service", return_value=fake_task_service),
_patch_project_service(fake_project),
patch("roboco.services.git.httpx.AsyncClient", return_value=fake_client),
pytest.raises(GitError, match="Validation Failed"),
):
await svc.update_pr_for_task(
UUID(str(task.id)), title=None, body="long body", reviewers=None
)
@pytest.mark.asyncio
async def test_update_pr_task_missing_raises_not_found() -> None:
"""Unknown task_id → NotFoundError (caller maps to invalid_state envelope)."""
svc = _service()
fake_task_service = MagicMock()
fake_task_service.get = AsyncMock(return_value=None)
with (
patch("roboco.services.git.get_task_service", return_value=fake_task_service),
pytest.raises(NotFoundError),
):
await svc.update_pr_for_task(uuid4(), title="t", body=None, reviewers=None)
@pytest.mark.asyncio
async def test_update_pr_no_pr_number_raises_git_error() -> None:
"""Task exists but has no pr_number → GitError ('no PR open')."""
svc = _service()
task = _task_with_pr(pr_number=None)
fake_task_service = MagicMock()
fake_task_service.get = AsyncMock(return_value=task)
with (
patch("roboco.services.git.get_task_service", return_value=fake_task_service),
pytest.raises(GitError, match="no PR"),
):
await svc.update_pr_for_task(
UUID(str(task.id)), title="t", body=None, reviewers=None
)