diff --git a/services/hackbot-api/alembic/versions/d2e6b8f4a1c9_runs_bug_id.py b/services/hackbot-api/alembic/versions/d2e6b8f4a1c9_runs_bug_id.py new file mode 100644 index 0000000000..53ab2d2a1e --- /dev/null +++ b/services/hackbot-api/alembic/versions/d2e6b8f4a1c9_runs_bug_id.py @@ -0,0 +1,34 @@ +"""Store the bug a run is for, so runs can be listed by bug id. + +Revision ID: d2e6b8f4a1c9 +Revises: e5f1a3c7d9b2 +Create Date: 2026-11-10 00:00:00.000000 + +""" + +from typing import Sequence, Union + +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision: str = "d2e6b8f4a1c9" +down_revision: Union[str, Sequence[str], None] = "e5f1a3c7d9b2" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + """Upgrade schema.""" + op.add_column("runs", sa.Column("bug_id", sa.Integer(), nullable=True)) + op.execute( + "UPDATE runs SET bug_id = (inputs ->> 'bug_id')::integer " + "WHERE jsonb_typeof(inputs -> 'bug_id') = 'number'" + ) + op.create_index(op.f("ix_runs_bug_id"), "runs", ["bug_id"]) + + +def downgrade() -> None: + """Downgrade schema.""" + op.drop_index(op.f("ix_runs_bug_id"), table_name="runs") + op.drop_column("runs", "bug_id") diff --git a/services/hackbot-api/app/database/models.py b/services/hackbot-api/app/database/models.py index d4e6f5b9a3..8bb407fe0b 100644 --- a/services/hackbot-api/app/database/models.py +++ b/services/hackbot-api/app/database/models.py @@ -32,6 +32,7 @@ class Run(Base): agent: Mapped[str] = mapped_column(String, nullable=False, index=True) status: Mapped[str] = mapped_column(String, nullable=False, index=True) inputs: Mapped[dict] = mapped_column(JSONB, nullable=False) + bug_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True) requested_by: Mapped[str | None] = mapped_column(String, nullable=True, index=True) requested_via: Mapped[str | None] = mapped_column(String, nullable=True) source: Mapped[Source | None] = mapped_column( diff --git a/services/hackbot-api/app/routers/runs.py b/services/hackbot-api/app/routers/runs.py index 337c68982c..8ac20af4f0 100644 --- a/services/hackbot-api/app/routers/runs.py +++ b/services/hackbot-api/app/routers/runs.py @@ -20,12 +20,14 @@ from app.auth import require_api_key from app.config import settings from app.database.connection import get_db -from app.database.models import Run, RunAction +from app.database.models import Account, Run, RunAction from app.hackbot_client import get_hackbot_client from app.jobs import ExecutionStatus from app.schemas import ( AgentDescriptor, ArtifactRef, + BugRun, + BugRuns, IdentityProvider, RunActionDoc, RunDoc, @@ -164,6 +166,7 @@ async def create_run( run_id = uuid.uuid7() results_prefix = gcs.run_prefix(str(run_id)) + dumped_inputs = inputs.model_dump(mode="json") claim = ( pg_insert(Run) @@ -171,7 +174,8 @@ async def create_run( run_id=run_id, agent=agent.name, status=RunStatus.pending.value, - inputs=inputs.model_dump(mode="json"), + inputs=dumped_inputs, + bug_id=dumped_inputs.get("bug_id"), requested_by=on_behalf_of, requested_via=requested_via, source=source, @@ -296,6 +300,35 @@ async def list_runs( return [RunDoc.model_validate(r) for r in result.scalars()] +@router.get("/bugs/{bug_id}/runs") +async def list_bug_runs( + bug_id: int, db: Annotated[AsyncSession, Depends(get_db)] +) -> BugRuns: + stmt = ( + select(Run, Account.username) + .outerjoin(Account, Account.id == Run.account_id) + .where(Run.bug_id == bug_id) + .order_by(Run.created_at.desc(), Run.run_id.desc()) + ) + result = await db.execute(stmt) + return BugRuns( + runs=[ + BugRun( + run_id=run.run_id, + agent=run.agent, + status=run.status, + source=run.source, + bug_id=run.bug_id, + revision_id=run.inputs.get("revision_id"), + requester=username, + created_at=run.created_at, + updated_at=run.updated_at, + ) + for run, username in result.all() + ] + ) + + @router.get("/runs/{run_id}") async def get_run( run_id: uuid.UUID, db: Annotated[AsyncSession, Depends(get_db)] diff --git a/services/hackbot-api/app/schemas.py b/services/hackbot-api/app/schemas.py index 5e85710ec1..7a7f300ec4 100644 --- a/services/hackbot-api/app/schemas.py +++ b/services/hackbot-api/app/schemas.py @@ -94,6 +94,7 @@ class RunDoc(BaseModel): inputs: dict[str, Any] requested_by: str | None = None requested_via: str | None = None + source: Source | None = None dedupe_key: str | None = None created_at: datetime updated_at: datetime @@ -104,6 +105,22 @@ class RunDoc(BaseModel): error: str | None = None +class BugRun(BaseModel): + run_id: UUID + agent: str + status: RunStatus + source: Source | None = None + bug_id: int + revision_id: int | None = None + requester: str | None = None + created_at: datetime + updated_at: datetime + + +class BugRuns(BaseModel): + runs: list[BugRun] + + # --- Per-agent input schemas --- diff --git a/services/hackbot-api/tests/test_list_runs_api.py b/services/hackbot-api/tests/test_list_runs_api.py index 7b2f86b3e4..346b4fd109 100644 --- a/services/hackbot-api/tests/test_list_runs_api.py +++ b/services/hackbot-api/tests/test_list_runs_api.py @@ -11,7 +11,7 @@ import pytest from app.routers import runs as runs_router -from app.schemas import RunStatus +from app.schemas import RunStatus, Source from sqlalchemy.dialects import postgresql @@ -22,6 +22,9 @@ def __init__(self, rows): def scalars(self): return self._rows + def all(self): + return self._rows + class _CapturingDB: """Records the statement passed to execute() and returns canned rows.""" @@ -149,3 +152,19 @@ def test_list_runs_ignores_blank_requested_by(client, db, raw): # A blank param means "no filter", not "runs with an empty requester". assert client.get("/runs", params={"requested_by": raw}).status_code == 200 assert "WHERE" not in _sql(db.stmt) + + +async def test_list_bug_runs_returns_what_bugzilla_shows(): + run = _fake_run( + bug_id=1846789, + source=Source.phabricator, + inputs={"bug_id": 1846789, "revision_id": 218404}, + ) + db = _CapturingDB([(run, "someone@mozilla.com")]) + result = await runs_router.list_bug_runs(bug_id=1846789, db=db) + assert "runs.bug_id =" in _sql(db.stmt) + assert 1846789 in db.stmt.compile(dialect=postgresql.dialect()).params.values() + (bug_run,) = result.runs + assert bug_run.source == Source.phabricator + assert bug_run.revision_id == 218404 + assert bug_run.requester == "someone@mozilla.com"