4b6817381b
Benchmark image — build + push to ECR (any adapter) / build + push (push) Waiting to run
CI / quality (ubuntu-latest) (push) Waiting to run
CI / test (tools-runtime) (push) Waiting to run
CI / test (e2e-general) (push) Waiting to run
CI / test (cli-runtime) (push) Waiting to run
CI / test (e2e-provider-and-openclaw) (push) Waiting to run
CI / test (integrations-and-misc) (push) Waiting to run
CI / coverage-report (push) Blocked by required conditions
CI / test-kubernetes (push) Waiting to run
CI / should-run-thorough (push) Waiting to run
CI / test-thorough (cloudwatch-demo) (push) Blocked by required conditions
CI / test-thorough (flink-ecs) (push) Blocked by required conditions
CI / test-thorough (upstream-lambda) (push) Blocked by required conditions
CI / test-thorough (prefect-ecs-fargate) (push) Blocked by required conditions
CodeQL / Analyze (python) (push) Waiting to run
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Blocked by required conditions
Release / publish-release (push) Blocked by required conditions
Release / publish-main-release (push) Blocked by required conditions
Release / prepare (push) Waiting to run
Release / verify (push) Blocked by required conditions
Release / build-python-dist (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Blocked by required conditions
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Waiting to run
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Waiting to run
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Waiting to run
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
115 lines
3.7 KiB
Python
115 lines
3.7 KiB
Python
"""Tests for the GitLab MR write-back helper."""
|
|
|
|
import os
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from tools.investigation.reporting.gitlab_writeback import (
|
|
_build_mr_note,
|
|
post_gitlab_mr_writeback,
|
|
)
|
|
|
|
|
|
@pytest.fixture()
|
|
def state_with_gitlab():
|
|
return {
|
|
"available_sources": {
|
|
"gitlab": {
|
|
"merge_request_iid": "42",
|
|
"project_id": "99",
|
|
"gitlab_url": "https://gitlab.example.com",
|
|
"gitlab_token": "glpat-test",
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
def test_build_mr_note_short_message():
|
|
note = _build_mr_note("Hello world")
|
|
assert "Hello world" in note
|
|
assert "<details>" in note
|
|
|
|
|
|
def test_build_mr_note_truncates_long_message():
|
|
long_msg = "x" * 5000
|
|
note = _build_mr_note(long_msg)
|
|
assert "x" * 3997 + "..." in note
|
|
assert "x" * 3998 not in note
|
|
|
|
|
|
def test_build_mr_note_body_capped_at_4000_chars():
|
|
long_msg = "x" * 5000
|
|
note = _build_mr_note(long_msg)
|
|
body = note.split("<summary>Investigation summary</summary>\n\n")[1].split("\n\n</details>")[0]
|
|
assert len(body) == 4000
|
|
assert body.endswith("...")
|
|
|
|
|
|
def test_no_op_when_env_flag_off(state_with_gitlab):
|
|
with (
|
|
patch.dict(os.environ, {"GITLAB_MR_WRITEBACK": "false"}),
|
|
patch("tools.investigation.reporting.gitlab_writeback.post_gitlab_mr_note") as mock_post,
|
|
):
|
|
post_gitlab_mr_writeback(state_with_gitlab, "report")
|
|
mock_post.assert_not_called()
|
|
|
|
|
|
def test_no_op_when_mr_iid_missing():
|
|
state = {"available_sources": {"gitlab": {"project_id": "99"}}}
|
|
with (
|
|
patch.dict(os.environ, {"GITLAB_MR_WRITEBACK": "true"}),
|
|
patch("tools.investigation.reporting.gitlab_writeback.post_gitlab_mr_note") as mock_post,
|
|
):
|
|
post_gitlab_mr_writeback(state, "report")
|
|
mock_post.assert_not_called()
|
|
|
|
|
|
def test_no_op_when_project_id_missing():
|
|
state = {"available_sources": {"gitlab": {"merge_request_iid": "42"}}}
|
|
with (
|
|
patch.dict(os.environ, {"GITLAB_MR_WRITEBACK": "true"}),
|
|
patch("tools.investigation.reporting.gitlab_writeback.post_gitlab_mr_note") as mock_post,
|
|
):
|
|
post_gitlab_mr_writeback(state, "report")
|
|
mock_post.assert_not_called()
|
|
|
|
|
|
def test_failure_does_not_propagate(state_with_gitlab):
|
|
with (
|
|
patch.dict(os.environ, {"GITLAB_MR_WRITEBACK": "true"}),
|
|
patch(
|
|
"tools.investigation.reporting.gitlab_writeback.post_gitlab_mr_note",
|
|
side_effect=RuntimeError("network error"),
|
|
),
|
|
patch(
|
|
"tools.investigation.reporting.gitlab_writeback.build_gitlab_config",
|
|
return_value=MagicMock(),
|
|
),
|
|
patch("tools.investigation.reporting.gitlab_writeback.logger") as mock_logger,
|
|
):
|
|
post_gitlab_mr_writeback(state_with_gitlab, "report")
|
|
mock_logger.warning.assert_called_once()
|
|
|
|
|
|
def test_happy_path_calls_post_mr_note(state_with_gitlab):
|
|
mock_config = MagicMock()
|
|
with (
|
|
patch.dict(os.environ, {"GITLAB_MR_WRITEBACK": "true"}),
|
|
patch(
|
|
"tools.investigation.reporting.gitlab_writeback.build_gitlab_config",
|
|
return_value=mock_config,
|
|
) as mock_build,
|
|
patch("tools.investigation.reporting.gitlab_writeback.post_gitlab_mr_note") as mock_post,
|
|
):
|
|
post_gitlab_mr_writeback(state_with_gitlab, "the report")
|
|
|
|
mock_build.assert_called_once_with(
|
|
{"base_url": "https://gitlab.example.com", "auth_token": "glpat-test"}
|
|
)
|
|
mock_post.assert_called_once()
|
|
call_kwargs = mock_post.call_args.kwargs
|
|
assert call_kwargs["project_id"] == "99"
|
|
assert call_kwargs["mr_iid"] == "42"
|
|
assert "the report" in call_kwargs["body"]
|