Skip to content
Merged
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
88 changes: 87 additions & 1 deletion docs/api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3111,6 +3111,10 @@
"minimum": 0,
"type": "integer"
},
"trigger": {
"$ref": "#/components/schemas/RunTrigger",
"description": "How the run was started and by whom (as in the runs list)."
},
"updated_at": {
"description": "When it last changed (RFC 3339).",
"examples": [
Expand All @@ -3132,7 +3136,8 @@
"applied_standards",
"sign_in_prompts",
"questions",
"answers"
"answers",
"trigger"
],
"type": "object"
},
Expand Down Expand Up @@ -3461,8 +3466,66 @@
"integer",
"null"
]
},
"trigger": {
"$ref": "#/components/schemas/RunTrigger",
"description": "How the run was started and by whom, worked out from the fields\nabove and what started it. Show this instead of an internal\n`ticket_id` such as `E2E-FIX-…`."
}
},
"required": [
"trigger"
],
"type": "object"
},
"RunTrigger": {
"description": "How a run was started, in words, with a link to what started it.",
"properties": {
"by": {
"description": "The person who started it, when a person did: a name, GitHub login\nor email. Null for CoderHelm's own work.",
"examples": [
"Dana Reyes"
],
"type": [
"string",
"null"
]
},
"kind": {
"$ref": "#/components/schemas/TriggerKind",
"description": "The group: `jira`, `github`, `coderhelm`, `alert`, `plan`,\n`dashboard` or `chat`. Treat a value you don't know as `coderhelm`."
},
"label": {
"description": "What started it, to show as is: `Jira`, `GitHub issue`, `Alert`,\n`Plan`, `Started by Coderhelm · flaky test`, `Dashboard · log finding`.",
"examples": [
"Jira"
],
"type": "string"
},
"ref": {
"description": "The thing that started it, when it has a name people know: a ticket\nkey, `#42`, a plan's or an alert's title, a log group.",
"examples": [
"WEB-42"
],
"type": [
"string",
"null"
]
},
"ref_url": {
"description": "Where that thing is: a full URL (GitHub, Jira) or a dashboard path\nstarting with `/` (a test, a log finding, an alert, a plan).",
"examples": [
"https://acme.atlassian.net/browse/WEB-42"
],
"type": [
"string",
"null"
]
}
},
"required": [
"kind",
"label"
],
"type": "object"
},
"RunsPage": {
Expand Down Expand Up @@ -3845,6 +3908,19 @@
],
"type": "object"
},
"TriggerKind": {
"description": "The group a run's start belongs to, which the runs list filters on.",
"enum": [
"jira",
"github",
"coderhelm",
"alert",
"plan",
"dashboard",
"chat"
],
"type": "string"
},
"UpdateEndpointBody": {
"description": "Changes to an endpoint. Only the fields sent change.",
"properties": {
Expand Down Expand Up @@ -5788,6 +5864,16 @@
"schema": {
"type": "string"
}
},
{
"description": "Comma-separated ways the run was started (`trigger.kind`): `jira`,\n`github`, `coderhelm`, `alert`, `plan`, `dashboard`, `chat`.",
"example": "coderhelm,alert",
"in": "query",
"name": "started_by",
"required": false,
"schema": {
"type": "string"
}
}
],
"responses": {
Expand Down
39 changes: 34 additions & 5 deletions services/gateway/src/routes/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::Arc;
use tracing::{error, info, warn};

use super::run_search;
use super::run_trigger;
use crate::auth::jwt;
use crate::middleware::auth::SESSION_TTL_SECS;
use crate::models::{
Expand Down Expand Up @@ -678,6 +679,10 @@ pub struct RunsQuery {
/// One repo, `owner/name` (exact, case-insensitive).
#[param(example = "acme/web")]
repo: Option<String>,
/// Comma-separated ways the run was started (`trigger.kind`): `jira`,
/// `github`, `coderhelm`, `alert`, `plan`, `dashboard`, `chat`.
#[param(example = "coderhelm,alert")]
started_by: Option<String>,
}

/// One run in a list.
Expand Down Expand Up @@ -728,6 +733,10 @@ pub struct RunSummary {
/// When the run started (RFC 3339).
#[schema(example = "2026-10-22T14:03:11Z")]
pub created_at: Option<String>,
/// How the run was started and by whom, worked out from the fields
/// above and what started it. Show this instead of an internal
/// `ticket_id` such as `E2E-FIX-…`.
pub trigger: run_trigger::RunTrigger,
}

/// A page of runs.
Expand Down Expand Up @@ -776,7 +785,8 @@ pub async fn list_runs(
params.status.as_deref(),
params.source.as_deref(),
)
.with_repo(params.repo.as_deref());
.with_repo(params.repo.as_deref())
.with_started_by(params.started_by.as_deref());
let mut start = params.cursor.as_deref().filter(|c| c.len() <= 64).map(|c| {
std::collections::HashMap::from([
("team_id".to_string(), attr_s(&claims.team_id)),
Expand Down Expand Up @@ -844,9 +854,11 @@ pub async fn list_runs(
.and_then(|v| v.as_n().ok())
.and_then(|n| n.parse::<u64>().ok())
};
let lookups = run_trigger::load_lookups(&state, &claims.team_id, matched.iter()).await;
let runs: Vec<RunSummary> = matched
.iter()
.map(|item| RunSummary {
trigger: run_trigger::trigger(item, &lookups),
run_id: str_of(item, "run_id"),
status: str_of(item, "status"),
ticket_source: str_of(item, "ticket_source"),
Expand Down Expand Up @@ -1030,6 +1042,8 @@ pub struct Run {
/// The run that picked the answers up and continued the work.
#[schema(example = json!(null))]
pub continued_run_id: Option<String>,
/// How the run was started and by whom (as in the runs list).
pub trigger: run_trigger::RunTrigger,
}

/// A question a run asked a person.
Expand Down Expand Up @@ -1196,6 +1210,7 @@ pub async fn get_run(
(tokens_in, tokens_out, cache_read, cache_write, cost_usd)
};

let lookups = run_trigger::load_lookups(&state, &claims.team_id, [item]).await;
Ok(Json(run_of(
item,
&run_id,
Expand All @@ -1206,6 +1221,7 @@ pub async fn get_run(
cache_write,
cost_usd,
},
&lookups,
)))
}

Expand All @@ -1219,7 +1235,12 @@ struct RunUsage {
}

/// The run `get_run` answers with, from its row in the runs table.
fn run_of(item: &run_search::Item, run_id: &str, usage: RunUsage) -> Run {
fn run_of(
item: &run_search::Item,
run_id: &str,
usage: RunUsage,
lookups: &run_trigger::Lookups,
) -> Run {
let RunUsage {
tokens_in,
tokens_out,
Expand Down Expand Up @@ -1258,6 +1279,7 @@ fn run_of(item: &run_search::Item, run_id: &str, usage: RunUsage) -> Run {
};
let asked = run_asked(item);
Run {
trigger: run_trigger::trigger(item, lookups),
run_id: run_id.to_string(),
status: st("status"),
ticket_source: st("ticket_source"),
Expand Down Expand Up @@ -5513,15 +5535,21 @@ mod tests {
cache_write: 0,
cost_usd: 0.0,
};
let run =
serde_json::to_value(run_of(&item, "01JB8X6F4M2C9QZ3T7V5W1K0HA", usage())).unwrap();
let run = serde_json::to_value(run_of(
&item,
"01JB8X6F4M2C9QZ3T7V5W1K0HA",
usage(),
&Default::default(),
))
.unwrap();
assert_eq!(run["run_id"], "01JB8X6F4M2C9QZ3T7V5W1K0HA");
assert_eq!(run["status"], "completed");
// The id comes from the request, so a row without the attribute
// still answers with it.
let bare =
std::collections::HashMap::from([("status".to_string(), A::S("running".into()))]);
let run = serde_json::to_value(run_of(&bare, "01JB8Y", usage())).unwrap();
let run =
serde_json::to_value(run_of(&bare, "01JB8Y", usage(), &Default::default())).unwrap();
assert_eq!(run["run_id"], "01JB8Y");
assert_eq!(run["questions"], json!([]));
assert_eq!(run["answers"], json!([]));
Expand Down Expand Up @@ -5564,6 +5592,7 @@ mod tests {
cache_write: 0,
cost_usd: 0.0,
},
&Default::default(),
))
.unwrap();
assert_eq!(
Expand Down
1 change: 1 addition & 0 deletions services/gateway/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub mod roles;
pub mod run_answer;
pub mod run_close;
pub mod run_search;
pub mod run_trigger;
pub mod search;
pub mod setup;
pub mod setup_checks;
Expand Down
57 changes: 57 additions & 0 deletions services/gateway/src/routes/run_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ pub const LIST_ATTRS: &[&str] = &[
"duration_s",
"current_pass",
"created_at",
// How the run started (see `run_trigger`).
"issue_number",
"sender",
"requester_key",
"requester_name",
"requester_email",
"plan_id",
"plan_title",
"plan_by",
"plugin_id",
];

#[derive(Debug, Default)]
Expand All @@ -45,6 +55,8 @@ pub struct RunFilter {
source: Option<String>,
/// One repo to keep, lowercased (exact match).
repo: Option<String>,
/// How the run started; empty keeps every kind.
started_by: Vec<super::run_trigger::TriggerKind>,
}

impl RunFilter {
Expand All @@ -71,9 +83,22 @@ impl RunFilter {
statuses,
source,
repo: None,
started_by: Vec::new(),
}
}

/// Keep only runs started one of these ways (comma-separated
/// `run_trigger::TriggerKind` words). Unknown words are ignored; when
/// none is known, every run is kept.
pub fn with_started_by(mut self, started_by: Option<&str>) -> Self {
self.started_by = started_by
.unwrap_or("")
.split(',')
.filter_map(super::run_trigger::TriggerKind::parse)
.collect();
self
}

/// Keep only runs of one repo (`owner/name`, any case). Empty keeps all.
pub fn with_repo(mut self, repo: Option<&str>) -> Self {
self.repo = repo
Expand Down Expand Up @@ -102,6 +127,10 @@ impl RunFilter {
return false;
}
}
if !self.started_by.is_empty() && !self.started_by.contains(&super::run_trigger::kind(item))
{
return false;
}
if self.terms.is_empty() {
return true;
}
Expand Down Expand Up @@ -345,4 +374,32 @@ mod tests {
assert_eq!(found.len(), 5);
assert_eq!(read, 10);
}

#[test]
fn keeps_runs_started_the_chosen_ways() {
// `run` makes Jira runs.
let jira = run("1", "completed", "ABC-1", "Login", "acme/web", None);
let mut flaky = run(
"2",
"completed",
"E2E-FIX-abc",
"Fix flaky test: x",
"acme/web",
None,
);
flaky.insert("ticket_source".into(), AttributeValue::S("alert".into()));
let mut plan = run("3", "completed", "ABC-3", "Task", "acme/web", None);
plan.insert("plan_id".into(), AttributeValue::S("p1".into()));

let f = RunFilter::new(None, None, None).with_started_by(Some("coderhelm,plans"));
assert!(!f.matches(&jira));
assert!(f.matches(&flaky));
assert!(f.matches(&plan));
let j = RunFilter::new(None, None, None).with_started_by(Some("jira"));
assert!(j.matches(&jira));
assert!(!j.matches(&plan), "a plan's Jira ticket is the plan's");
// Unknown words keep everything.
let any = RunFilter::new(None, None, None).with_started_by(Some("nope"));
assert!(any.matches(&jira) && any.matches(&flaky));
}
}
Loading
Loading