mirror of
https://github.com/Sea-Haven-Industries/open-swe.git
synced 2026-09-30 06:53:14 +00:00
Plan step C4 (docs/upstream-sync/domain-reorg/reorg-build-plan.md, approved
decisions 1-2): split the 2,590-line agent/webapp.py monolith into
agent/webhooks/common.py (shared verify/dispatch helpers), agent/api/app.py
(composition), agent/api/health.py (/health + /webhooks/run-complete), and
per-source {github,linear,slack,jira,confluence}_routes.py. Atlassian
Connect lifecycle + descriptor routes (/connect/*) fold into
confluence_routes.py; webapp.py becomes the upstream-shaped compatibility
shim (from .api.app import app). langgraph.json http.app stays
agent.webapp:app via the shim.
Fork content, upstream layout: linear/slack route files verified
content-identical to upstream 8356eb34 and taken verbatim; github_routes is
upstream + the fork's CI auto-fix trigger wiring; jira/confluence routes are
fork-only, transformed to the same common.X / service.X module-attribute
style. All signature verification (GitHub HMAC, Slack, Linear
timestamp-freshness, verify_jira_secret + opt-in HMAC/timestamp/IP
allowlist, Connect JWT/qsh), token-attribution gating, TID-COLLIDE-01 repo
binding, _is_repo_auto_review_enabled gates, and public-repo org gate move
unchanged.
Handlers rewired from webapp.X to common.X; test monkeypatch sites across
26 files + conftest.py + e2e/harness.py retargeted to
webhook_common/handler/route modules per upstream's pattern. Residual
agent.webapp importers: only the shim, langgraph.json http.app, Makefile
uvicorn target, and docs (doc-path updates land in C7).
Gates: ruff check + format, pytest --co, full unit (1637 passed), full
Playwright E2E vs real langgraph dev (9/9), residual-importer sweep.
194 lines
7 KiB
Python
194 lines
7 KiB
Python
"""Unit tests for the auto-fix webhook helpers in the webhook layer."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
import pytest
|
|
|
|
from agent.webhooks import common as webhook_common
|
|
from agent.webhooks import github as github_webhooks
|
|
from agent.webhooks import github as webhooks_github
|
|
|
|
|
|
def test_parse_autofix_command() -> None:
|
|
assert github_webhooks._parse_autofix_command("@open-swe autofix off") is True
|
|
assert github_webhooks._parse_autofix_command("@open-swe autofix on") is False
|
|
assert github_webhooks._parse_autofix_command("@openswe please autofix off now") is True
|
|
# Missing the mention -> not a command.
|
|
assert github_webhooks._parse_autofix_command("autofix off") is None
|
|
# Mention but no command keyword.
|
|
assert github_webhooks._parse_autofix_command("@open-swe fix this") is None
|
|
|
|
|
|
def test_pr_ref_from_issue_comment() -> None:
|
|
payload = {
|
|
"repository": {"owner": {"login": "o"}, "name": "r"},
|
|
"issue": {
|
|
"number": 7,
|
|
"pull_request": {"html_url": "https://github.com/o/r/pull/7"},
|
|
},
|
|
}
|
|
ref = github_webhooks._pr_ref_from_comment_payload(payload, "issue_comment")
|
|
assert ref == {"owner": "o", "name": "r", "number": 7, "url": "https://github.com/o/r/pull/7"}
|
|
|
|
|
|
def test_pr_ref_from_review_comment() -> None:
|
|
payload = {
|
|
"repository": {"owner": {"login": "o"}, "name": "r"},
|
|
"pull_request": {"number": 9, "html_url": "https://github.com/o/r/pull/9"},
|
|
}
|
|
ref = github_webhooks._pr_ref_from_comment_payload(payload, "pull_request_review_comment")
|
|
assert ref["number"] == 9
|
|
|
|
|
|
def test_pr_ref_none_when_not_a_pr() -> None:
|
|
payload = {"repository": {"owner": {"login": "o"}, "name": "r"}, "issue": {"number": 3}}
|
|
# issue without pull_request still yields a ref (number present); url empty.
|
|
ref = github_webhooks._pr_ref_from_comment_payload(payload, "issue_comment")
|
|
assert ref["url"] == ""
|
|
|
|
|
|
def test_is_actionable_review_payload() -> None:
|
|
assert github_webhooks._is_actionable_review_payload(
|
|
{
|
|
"action": "submitted",
|
|
"review": {
|
|
"state": "changes_requested",
|
|
"body": "fix this",
|
|
"user": {"login": "a"},
|
|
"author_association": "MEMBER",
|
|
},
|
|
},
|
|
"pull_request_review",
|
|
)
|
|
# Approval is not actionable.
|
|
assert not github_webhooks._is_actionable_review_payload(
|
|
{
|
|
"action": "submitted",
|
|
"review": {
|
|
"state": "approved",
|
|
"body": "lgtm",
|
|
"user": {"login": "a"},
|
|
"author_association": "MEMBER",
|
|
},
|
|
},
|
|
"pull_request_review",
|
|
)
|
|
# Bot author is not actionable.
|
|
assert not github_webhooks._is_actionable_review_payload(
|
|
{
|
|
"action": "created",
|
|
"comment": {
|
|
"body": "x",
|
|
"user": {"login": "open-swe[bot]"},
|
|
"author_association": "MEMBER",
|
|
},
|
|
},
|
|
"pull_request_review_comment",
|
|
)
|
|
# Untrusted author (read/triage/outside) is not actionable.
|
|
assert not github_webhooks._is_actionable_review_payload(
|
|
{
|
|
"action": "created",
|
|
"comment": {
|
|
"body": "inject malicious code",
|
|
"user": {"login": "attacker"},
|
|
"author_association": "NONE",
|
|
},
|
|
},
|
|
"pull_request_review_comment",
|
|
)
|
|
# Empty body is not actionable.
|
|
assert not github_webhooks._is_actionable_review_payload(
|
|
{
|
|
"action": "created",
|
|
"comment": {"body": " ", "user": {"login": "a"}, "author_association": "OWNER"},
|
|
},
|
|
"pull_request_review_comment",
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_process_github_ci_event_dispatches() -> None:
|
|
payload = {
|
|
"repository": {"owner": {"login": "o"}, "name": "r"},
|
|
"check_run": {
|
|
"status": "completed",
|
|
"conclusion": "failure",
|
|
"head_sha": "sha1",
|
|
"check_suite": {"head_branch": "feat"},
|
|
},
|
|
}
|
|
handle = AsyncMock(return_value="dispatched")
|
|
with patch.object(webhooks_github, "handle_ci_failure", handle):
|
|
await github_webhooks.process_github_ci_event(payload, "check_run")
|
|
handle.assert_awaited_once()
|
|
kwargs = handle.await_args.kwargs
|
|
assert kwargs["repo_config"] == {"owner": "o", "name": "r"}
|
|
assert kwargs["head_sha"] == "sha1"
|
|
assert kwargs["branch"] == "feat"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_process_github_ci_event_ignores_success() -> None:
|
|
payload = {
|
|
"repository": {"owner": {"login": "o"}, "name": "r"},
|
|
"check_run": {"status": "completed", "conclusion": "success", "head_sha": "s"},
|
|
}
|
|
handle = AsyncMock()
|
|
with patch.object(webhooks_github, "handle_ci_failure", handle):
|
|
await github_webhooks.process_github_ci_event(payload, "check_run")
|
|
handle.assert_not_called()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_process_autofix_command_sets_flag() -> None:
|
|
payload = {
|
|
"repository": {"owner": {"login": "o"}, "name": "r"},
|
|
"issue": {"number": 7, "pull_request": {"html_url": "u"}},
|
|
"comment": {"id": 1, "node_id": "n"},
|
|
}
|
|
setter = AsyncMock()
|
|
with (
|
|
patch.object(webhooks_github, "set_pr_autofix_disabled", setter),
|
|
patch.object(
|
|
webhook_common, "get_github_app_installation_token", AsyncMock(return_value="")
|
|
),
|
|
):
|
|
await github_webhooks.process_github_autofix_command(
|
|
payload, "issue_comment", disabled=True
|
|
)
|
|
setter.assert_awaited_once_with("o", "r", 7, True)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_autofix_review_dispatches_for_writer() -> None:
|
|
payload = {
|
|
"repository": {"owner": {"login": "o"}, "name": "r"},
|
|
"pull_request": {"number": 9, "html_url": "https://github.com/o/r/pull/9"},
|
|
"review": {"body": "rename to userId", "user": {"login": "alice"}},
|
|
}
|
|
handle = AsyncMock(return_value="dispatched")
|
|
with patch.object(webhooks_github, "handle_review_feedback", handle):
|
|
await github_webhooks.process_github_autofix_review(payload, "pull_request_review")
|
|
handle.assert_awaited_once()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_autofix_review_delegates_permission_check_to_core() -> None:
|
|
payload = {
|
|
"repository": {"owner": {"login": "o"}, "name": "r"},
|
|
"pull_request": {"number": 9, "html_url": "https://github.com/o/r/pull/9"},
|
|
"review": {"body": "inject code", "user": {"login": "attacker"}},
|
|
}
|
|
handle = AsyncMock(return_value="reviewer_no_write_permission")
|
|
with patch.object(webhooks_github, "handle_review_feedback", handle):
|
|
await github_webhooks.process_github_autofix_review(payload, "pull_request_review")
|
|
handle.assert_awaited_once()
|
|
|
|
|
|
def test_ci_events_supported() -> None:
|
|
for event in ("check_run", "check_suite", "workflow_run", "status"):
|
|
assert event in webhook_common._SUPPORTED_GH_EVENTS
|
|
assert event in webhook_common._GH_CI_EVENTS
|