Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions services/hackbot-api/app/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 17 additions & 1 deletion services/hackbot-api/app/routers/runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from app.schemas import (
AgentDescriptor,
ArtifactRef,
BugRuns,
IdentityProvider,
RunActionDoc,
RunDoc,
Expand Down Expand Up @@ -164,14 +165,16 @@ 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)
.values(
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,
Expand Down Expand Up @@ -296,6 +299,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()])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed that we return the runs as they are: we might want to accommodate the expected data here, especially origin. See https://github.com/mozilla/bmo/pull/2829/changes#diff-ab09301ff998f2ccac548b78b3934dcc02b3faf312415ecbdf09aaf850994946R180-R192

The origin is guessed on Bugzilla side: https://github.com/mozilla/bmo/pull/2829/changes#diff-ab09301ff998f2ccac548b78b3934dcc02b3faf312415ecbdf09aaf850994946R160-R172
I think we should do it here instead.



@router.get("/runs/{run_id}")
async def get_run(
run_id: uuid.UUID, db: Annotated[AsyncSession, Depends(get_db)]
Expand Down
5 changes: 5 additions & 0 deletions services/hackbot-api/app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -104,6 +105,10 @@ class RunDoc(BaseModel):
error: str | None = None


class BugRuns(BaseModel):
runs: list[RunDoc]


# --- Per-agent input schemas ---


Expand Down
17 changes: 16 additions & 1 deletion services/hackbot-api/tests/test_list_runs_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -149,3 +149,18 @@ 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_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


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