From 63ad93d909471400cf48942e65b418f2c258ecd3 Mon Sep 17 00:00:00 2001 From: ayoubdiourin7 Date: Mon, 9 Nov 2026 20:05:20 +0100 Subject: [PATCH 1/7] Filter runs by bug id in the hackbot API --- services/hackbot-api/app/routers/runs.py | 6 ++++++ services/hackbot-api/tests/test_list_runs_api.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/services/hackbot-api/app/routers/runs.py b/services/hackbot-api/app/routers/runs.py index 337c68982c3..716c3c64702 100644 --- a/services/hackbot-api/app/routers/runs.py +++ b/services/hackbot-api/app/routers/runs.py @@ -272,6 +272,10 @@ async def list_runs( DedupeKey, Query(description="Only return runs carrying this dedupe key."), ] = None, + bug_id: Annotated[ + int | None, + Query(ge=1, description="Only return runs for this Bugzilla bug."), + ] = None, ) -> list[RunDoc]: stmt = select(Run) if agent is not None: @@ -284,6 +288,8 @@ async def list_runs( # The trigger's own handle on its run: a caller that keyed the work can # find it again without having stored the run id. stmt = stmt.where(Run.dedupe_key == dedupe_key) + if bug_id is not None: + stmt = stmt.where(Run.inputs.contains({"bug_id": bug_id})) # created_at is the sort key; run_id is a deterministic tiebreaker so offset # paging is stable when timestamps collide. (agent/status/requested_by and # created_at are all indexed, so filtering + ordering stay index-backed.) diff --git a/services/hackbot-api/tests/test_list_runs_api.py b/services/hackbot-api/tests/test_list_runs_api.py index 7b2f86b3e4c..2f61b04f976 100644 --- a/services/hackbot-api/tests/test_list_runs_api.py +++ b/services/hackbot-api/tests/test_list_runs_api.py @@ -149,3 +149,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_runs_filters_by_bug_id(): + db = _CapturingDB([]) + await runs_router.list_runs( + limit=50, + offset=0, + agent=None, + status_filter=None, + requested_by=None, + bug_id=1846789, + db=db, + ) + assert "runs.inputs @>" in _sql(db.stmt) + params = db.stmt.compile(dialect=postgresql.dialect()).params + assert {"bug_id": 1846789} in params.values() From d997c07f1f72b42e4846759dea5fc1931d8e9f22 Mon Sep 17 00:00:00 2001 From: ayoubdiourin7 Date: Tue, 10 Nov 2026 08:12:31 +0100 Subject: [PATCH 2/7] Store bug_id as a column on runs --- .../versions/d2e6b8f4a1c9_runs_bug_id.py | 34 +++++++++++++++++++ services/hackbot-api/app/database/models.py | 1 + services/hackbot-api/app/routers/runs.py | 3 +- .../hackbot-api/tests/test_list_runs_api.py | 4 +-- 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 services/hackbot-api/alembic/versions/d2e6b8f4a1c9_runs_bug_id.py 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 00000000000..ac197deae9b --- /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: 78015b96b6db +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] = "78015b96b6db" +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 d4e6f5b9a35..8bb407fe0b8 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 716c3c64702..10315eda17d 100644 --- a/services/hackbot-api/app/routers/runs.py +++ b/services/hackbot-api/app/routers/runs.py @@ -172,6 +172,7 @@ async def create_run( agent=agent.name, status=RunStatus.pending.value, inputs=inputs.model_dump(mode="json"), + bug_id=getattr(inputs, "bug_id", None), requested_by=on_behalf_of, requested_via=requested_via, source=source, @@ -289,7 +290,7 @@ async def list_runs( # find it again without having stored the run id. stmt = stmt.where(Run.dedupe_key == dedupe_key) if bug_id is not None: - stmt = stmt.where(Run.inputs.contains({"bug_id": bug_id})) + stmt = stmt.where(Run.bug_id == bug_id) # created_at is the sort key; run_id is a deterministic tiebreaker so offset # paging is stable when timestamps collide. (agent/status/requested_by and # created_at are all indexed, so filtering + ordering stay index-backed.) diff --git a/services/hackbot-api/tests/test_list_runs_api.py b/services/hackbot-api/tests/test_list_runs_api.py index 2f61b04f976..3da5d87174f 100644 --- a/services/hackbot-api/tests/test_list_runs_api.py +++ b/services/hackbot-api/tests/test_list_runs_api.py @@ -162,6 +162,6 @@ async def test_list_runs_filters_by_bug_id(): bug_id=1846789, db=db, ) - assert "runs.inputs @>" in _sql(db.stmt) + assert "runs.bug_id =" in _sql(db.stmt) params = db.stmt.compile(dialect=postgresql.dialect()).params - assert {"bug_id": 1846789} in params.values() + assert 1846789 in params.values() From b4ea49a11dca50ec03cdfc3294251bde4590aff8 Mon Sep 17 00:00:00 2001 From: ayoubdiourin7 Date: Tue, 10 Nov 2026 08:55:25 +0100 Subject: [PATCH 3/7] Rebase bug_id migration onto requested_via --- .../hackbot-api/alembic/versions/d2e6b8f4a1c9_runs_bug_id.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/hackbot-api/alembic/versions/d2e6b8f4a1c9_runs_bug_id.py b/services/hackbot-api/alembic/versions/d2e6b8f4a1c9_runs_bug_id.py index ac197deae9b..f5f9093d58b 100644 --- a/services/hackbot-api/alembic/versions/d2e6b8f4a1c9_runs_bug_id.py +++ b/services/hackbot-api/alembic/versions/d2e6b8f4a1c9_runs_bug_id.py @@ -1,7 +1,7 @@ """Store the bug a run is for, so runs can be listed by bug id. Revision ID: d2e6b8f4a1c9 -Revises: 78015b96b6db +Revises: d4e7b2a9c1f3 Create Date: 2026-11-10 00:00:00.000000 """ @@ -13,7 +13,7 @@ # revision identifiers, used by Alembic. revision: str = "d2e6b8f4a1c9" -down_revision: Union[str, Sequence[str], None] = "78015b96b6db" +down_revision: Union[str, Sequence[str], None] = "d4e7b2a9c1f3" branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None From d1292c600c030ed43bd3b35a70284959bfd0a4ed Mon Sep 17 00:00:00 2001 From: ayoubdiourin7 Date: Sun, 15 Nov 2026 21:56:49 +0100 Subject: [PATCH 4/7] Move runs by bug id to its own endpoint --- services/hackbot-api/app/routers/runs.py | 20 +++++++++++++------ services/hackbot-api/app/schemas.py | 4 ++++ .../hackbot-api/tests/test_list_runs_api.py | 15 ++++---------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/services/hackbot-api/app/routers/runs.py b/services/hackbot-api/app/routers/runs.py index 10315eda17d..ef551eb28a1 100644 --- a/services/hackbot-api/app/routers/runs.py +++ b/services/hackbot-api/app/routers/runs.py @@ -26,6 +26,7 @@ from app.schemas import ( AgentDescriptor, ArtifactRef, + BugRuns, IdentityProvider, RunActionDoc, RunDoc, @@ -273,10 +274,6 @@ async def list_runs( DedupeKey, Query(description="Only return runs carrying this dedupe key."), ] = None, - bug_id: Annotated[ - int | None, - Query(ge=1, description="Only return runs for this Bugzilla bug."), - ] = None, ) -> list[RunDoc]: stmt = select(Run) if agent is not None: @@ -289,8 +286,6 @@ async def list_runs( # The trigger's own handle on its run: a caller that keyed the work can # find it again without having stored the run id. stmt = stmt.where(Run.dedupe_key == dedupe_key) - if bug_id is not None: - stmt = stmt.where(Run.bug_id == bug_id) # created_at is the sort key; run_id is a deterministic tiebreaker so offset # paging is stable when timestamps collide. (agent/status/requested_by and # created_at are all indexed, so filtering + ordering stay index-backed.) @@ -303,6 +298,19 @@ 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) + .where(Run.bug_id == bug_id) + .order_by(Run.created_at.desc(), Run.run_id.desc()) + ) + result = await db.execute(stmt) + return BugRuns(runs=[RunDoc.model_validate(r) for r in result.scalars()]) + + @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 5e85710ec1f..1138199be50 100644 --- a/services/hackbot-api/app/schemas.py +++ b/services/hackbot-api/app/schemas.py @@ -104,6 +104,10 @@ class RunDoc(BaseModel): error: str | None = None +class BugRuns(BaseModel): + runs: list[RunDoc] + + # --- 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 3da5d87174f..6d0cad18b3c 100644 --- a/services/hackbot-api/tests/test_list_runs_api.py +++ b/services/hackbot-api/tests/test_list_runs_api.py @@ -151,17 +151,10 @@ def test_list_runs_ignores_blank_requested_by(client, db, raw): assert "WHERE" not in _sql(db.stmt) -async def test_list_runs_filters_by_bug_id(): - db = _CapturingDB([]) - await runs_router.list_runs( - limit=50, - offset=0, - agent=None, - status_filter=None, - requested_by=None, - bug_id=1846789, - db=db, - ) +async def test_list_bug_runs_filters_by_bug_id(): + db = _CapturingDB([_fake_run()]) + result = await runs_router.list_bug_runs(bug_id=1846789, db=db) assert "runs.bug_id =" in _sql(db.stmt) params = db.stmt.compile(dialect=postgresql.dialect()).params assert 1846789 in params.values() + assert len(result.runs) == 1 From c48df2b335040b76d722717539ed9e97f7ce1555 Mon Sep 17 00:00:00 2001 From: ayoubdiourin7 Date: Wed, 2 Dec 2026 14:04:59 +0100 Subject: [PATCH 5/7] Read bug_id from the dumped run inputs --- services/hackbot-api/app/routers/runs.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/hackbot-api/app/routers/runs.py b/services/hackbot-api/app/routers/runs.py index ef551eb28a1..0520918ddf5 100644 --- a/services/hackbot-api/app/routers/runs.py +++ b/services/hackbot-api/app/routers/runs.py @@ -165,6 +165,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) @@ -172,8 +173,8 @@ async def create_run( run_id=run_id, agent=agent.name, status=RunStatus.pending.value, - inputs=inputs.model_dump(mode="json"), - bug_id=getattr(inputs, "bug_id", None), + inputs=dumped_inputs, + bug_id=dumped_inputs.get("bug_id"), requested_by=on_behalf_of, requested_via=requested_via, source=source, From 059444fb7c14a56241f71a4ec34ad48068ac7f72 Mon Sep 17 00:00:00 2001 From: ayoubdiourin7 Date: Wed, 2 Dec 2026 15:34:57 +0100 Subject: [PATCH 6/7] Rebase bug_id migration onto accounts --- .../alembic/versions/d2e6b8f4a1c9_runs_bug_id.py | 4 ++-- services/hackbot-api/app/schemas.py | 1 + services/hackbot-api/tests/test_list_runs_api.py | 8 +++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/services/hackbot-api/alembic/versions/d2e6b8f4a1c9_runs_bug_id.py b/services/hackbot-api/alembic/versions/d2e6b8f4a1c9_runs_bug_id.py index f5f9093d58b..53ab2d2a1e4 100644 --- a/services/hackbot-api/alembic/versions/d2e6b8f4a1c9_runs_bug_id.py +++ b/services/hackbot-api/alembic/versions/d2e6b8f4a1c9_runs_bug_id.py @@ -1,7 +1,7 @@ """Store the bug a run is for, so runs can be listed by bug id. Revision ID: d2e6b8f4a1c9 -Revises: d4e7b2a9c1f3 +Revises: e5f1a3c7d9b2 Create Date: 2026-11-10 00:00:00.000000 """ @@ -13,7 +13,7 @@ # revision identifiers, used by Alembic. revision: str = "d2e6b8f4a1c9" -down_revision: Union[str, Sequence[str], None] = "d4e7b2a9c1f3" +down_revision: Union[str, Sequence[str], None] = "e5f1a3c7d9b2" branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None diff --git a/services/hackbot-api/app/schemas.py b/services/hackbot-api/app/schemas.py index 1138199be50..c63281bc365 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 diff --git a/services/hackbot-api/tests/test_list_runs_api.py b/services/hackbot-api/tests/test_list_runs_api.py index 6d0cad18b3c..6057660eecf 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 @@ -158,3 +158,9 @@ async def test_list_bug_runs_filters_by_bug_id(): params = db.stmt.compile(dialect=postgresql.dialect()).params assert 1846789 in params.values() assert len(result.runs) == 1 + + +async def test_list_bug_runs_returns_source(): + db = _CapturingDB([_fake_run(source=Source.bugzilla)]) + result = await runs_router.list_bug_runs(bug_id=1846789, db=db) + assert result.runs[0].source == Source.bugzilla From 05a943eb4a46550563821ff81143070b40681429 Mon Sep 17 00:00:00 2001 From: ayoubdiourin7 Date: Wed, 2 Dec 2026 17:01:59 +0100 Subject: [PATCH 7/7] Return only what Bugzilla shows from GET /bugs/{bug_id}/runs --- services/hackbot-api/app/routers/runs.py | 23 +++++++++++++--- services/hackbot-api/app/schemas.py | 14 +++++++++- .../hackbot-api/tests/test_list_runs_api.py | 26 +++++++++++-------- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/services/hackbot-api/app/routers/runs.py b/services/hackbot-api/app/routers/runs.py index 0520918ddf5..8ac20af4f07 100644 --- a/services/hackbot-api/app/routers/runs.py +++ b/services/hackbot-api/app/routers/runs.py @@ -20,12 +20,13 @@ 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, @@ -304,12 +305,28 @@ async def list_bug_runs( bug_id: int, db: Annotated[AsyncSession, Depends(get_db)] ) -> BugRuns: stmt = ( - select(Run) + 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=[RunDoc.model_validate(r) for r in result.scalars()]) + 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}") diff --git a/services/hackbot-api/app/schemas.py b/services/hackbot-api/app/schemas.py index c63281bc365..7a7f300ec4a 100644 --- a/services/hackbot-api/app/schemas.py +++ b/services/hackbot-api/app/schemas.py @@ -105,8 +105,20 @@ 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[RunDoc] + 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 6057660eecf..346b4fd109b 100644 --- a/services/hackbot-api/tests/test_list_runs_api.py +++ b/services/hackbot-api/tests/test_list_runs_api.py @@ -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.""" @@ -151,16 +154,17 @@ def test_list_runs_ignores_blank_requested_by(client, db, raw): assert "WHERE" not in _sql(db.stmt) -async def test_list_bug_runs_filters_by_bug_id(): - db = _CapturingDB([_fake_run()]) +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) - params = db.stmt.compile(dialect=postgresql.dialect()).params - assert 1846789 in params.values() - assert len(result.runs) == 1 - - -async def test_list_bug_runs_returns_source(): - db = _CapturingDB([_fake_run(source=Source.bugzilla)]) - result = await runs_router.list_bug_runs(bug_id=1846789, db=db) - assert result.runs[0].source == Source.bugzilla + 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"