Skip to content

Commit ddd0cf9

Browse files
committed
WIP: environment_info_for, blocked notice on build responses, approved_elsewhere, MCP args
1 parent e15817f commit ddd0cf9

4 files changed

Lines changed: 549 additions & 3 deletions

File tree

‎app/src/domain/baselines.rs‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,69 @@ pub async fn local_notes(ids: &[i64]) -> laravel::Result<HashMap<i64, String>> {
476476
}
477477
Ok(out)
478478
}
479+
480+
/// Where a snapshot's new version was already approved: the same name and platform, the same content (its
481+
/// fingerprint), approved in a review on another branch. A hint for the reviewer: it approves nothing.
482+
#[derive(Clone, Debug, PartialEq, serde::Serialize)]
483+
pub struct ApprovedElsewhere {
484+
pub branch: String,
485+
pub pr_number: Option<i32>,
486+
/// The build it was approved in, and the snapshot there.
487+
pub build: i32,
488+
pub snapshot_id: laravel::str::Uuid,
489+
/// Who approved it ("Claude Code for Ada Lovelace").
490+
pub approved_by: Option<String>,
491+
pub approved_at: String,
492+
}
493+
494+
/// For a build's changed and added snapshots, and those that passed by matching an approval elsewhere, the newest
495+
/// matching approval on another branch, by snapshot id. Changes are always the build's own rows.
496+
///
497+
/// # Errors
498+
/// On a database error.
499+
pub async fn approved_elsewhere(build_id: i64) -> laravel::Result<HashMap<i64, ApprovedElsewhere>> {
500+
let rows = db::query!(
501+
"SELECT DISTINCT ON (s.id) s.id AS snapshot_id, bl.branch, b2.pr_number, b2.number, s2.pid AS other, \
502+
u.name AS approver, r.source, r.client_name, r.agent, r.token_name, bl.created_at \
503+
FROM snapshots s \
504+
JOIN builds b ON b.id = s.build_id \
505+
JOIN baselines bl ON bl.project_id = b.project_id AND bl.name = s.name AND bl.platform = s.platform \
506+
AND bl.approval = 'review' AND bl.branch <> b.branch AND bl.snapshot_id IS NOT NULL \
507+
JOIN snapshots s2 ON s2.id = bl.snapshot_id AND s2.source_fingerprint = s.source_fingerprint \
508+
JOIN builds b2 ON b2.id = bl.build_id \
509+
LEFT JOIN users u ON u.id = bl.approved_by \
510+
LEFT JOIN reviews r ON r.id = bl.review_id \
511+
WHERE s.build_id = {build_id} AND (s.state IN ('changed', 'added') \
512+
OR EXISTS (SELECT 1 FROM snapshot_pages p WHERE p.snapshot_id = s.id AND p.state = 'variant')) \
513+
ORDER BY s.id, bl.created_at DESC"
514+
)
515+
.select()
516+
.await?;
517+
let mut out = HashMap::new();
518+
for r in rows {
519+
let at: laravel::time::Timestamp = r.try_get("created_at")?;
520+
let person: Option<String> = r.try_get("approver")?;
521+
let approved_by = match r.try_get::<Option<String>>("source")? {
522+
Some(source) => Some(super::reviews::by(
523+
&source,
524+
r.try_get::<Option<String>>("client_name")?.as_deref(),
525+
r.try_get::<Option<String>>("agent")?.as_deref(),
526+
r.try_get::<Option<String>>("token_name")?.as_deref(),
527+
person.as_deref(),
528+
)),
529+
None => person,
530+
};
531+
out.insert(
532+
r.try_get::<i64>("snapshot_id")?,
533+
ApprovedElsewhere {
534+
branch: r.try_get("branch")?,
535+
pr_number: r.try_get("pr_number")?,
536+
build: r.try_get("number")?,
537+
snapshot_id: r.try_get("other")?,
538+
approved_by,
539+
approved_at: at.to_string(),
540+
},
541+
);
542+
}
543+
Ok(out)
544+
}

‎app/src/domain/builds.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,20 @@ pub async fn finalize_shard(build: Build, shard: Option<i32>, allow_empty: bool)
363363
.await;
364364
result.or_else(server)
365365
}
366+
367+
/// Where the build ran, as someone outside the organization may see it on a public project: the CI provider and the
368+
/// links to the job and the repository, never a local run's user name or machine.
369+
#[must_use]
370+
pub fn environment_info_for(build: &Build, member: bool) -> serde_json::Value {
371+
let info = build.environment_info.clone();
372+
if member {
373+
return info;
374+
}
375+
let keep = ["provider", "job_url", "repository_url"];
376+
match info {
377+
serde_json::Value::Object(map) => {
378+
serde_json::Value::Object(map.into_iter().filter(|(k, _)| keep.contains(&k.as_str())).collect())
379+
}
380+
_ => serde_json::Value::Null,
381+
}
382+
}

‎app/src/http/api/builds.rs‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,31 @@ impl BuildResponse {
8383
}
8484
}
8585

86+
impl BuildResponse {
87+
/// The response with `blocked` filled in: for a build stopped for credits, and for one still receiving files while
88+
/// its organization has none. A stopped build whose organization has credits again says so.
89+
///
90+
/// # Errors
91+
/// On a database error.
92+
pub async fn with_blocked(mut self, build: &Build, project: &Project, organization: &Organization) -> laravel::Result<Self> {
93+
use crate::domain::{credits, processing::OUT_OF_CREDITS};
94+
let stopped = build.error_code.as_deref() == Some(OUT_OF_CREDITS);
95+
if !stopped && build.state != "receiving" {
96+
return Ok(self);
97+
}
98+
self.blocked = match credits::blocked(organization, &app_url(), Some(credits::Spender::of(project))).await? {
99+
Some(b) => Some(b),
100+
None if stopped => Some(serde_json::json!({
101+
"reason": OUT_OF_CREDITS,
102+
"resolved": true,
103+
"message": format!("{} has VisualCI credits again. Run CI again to compare this commit.", organization.name),
104+
})),
105+
None => None,
106+
};
107+
Ok(self)
108+
}
109+
}
110+
86111
fn respond<T: Serialize>(result: Result<T, ApiError>) -> Response {
87112
match result {
88113
Ok(body) => axum::response::IntoResponse::into_response(axum::Json(body)),
@@ -113,7 +138,7 @@ pub async fn create(auth: Result<ApiProject, ApiError>, Body(params): Body<Creat
113138
// task), so they don't sit "receiving" beside this one.
114139
crate::domain::processing::expire_abandoned(Some(project.id)).await?;
115140
let build = domain::create(&project, &params, creator).await.map_err(ApiError::from)?;
116-
Ok(BuildResponse::new(&build, &project, &organization))
141+
Ok(BuildResponse::new(&build, &project, &organization).with_blocked(&build, &project, &organization).await?)
117142
}
118143
.await,
119144
)
@@ -125,7 +150,7 @@ pub async fn show(auth: Result<ApiProject, ApiError>, Params(pid): Params<Uuid>)
125150
async {
126151
let ApiProject { project, organization, .. } = auth?;
127152
let build = own_build(&project, pid).await?;
128-
Ok(BuildResponse::new(&build, &project, &organization))
153+
Ok(BuildResponse::new(&build, &project, &organization).with_blocked(&build, &project, &organization).await?)
129154
}
130155
.await,
131156
)
@@ -239,7 +264,7 @@ pub async fn finalize(
239264
}
240265
// The job may have run already (a synchronous queue): answer with the build as it is now.
241266
let build = Build::find(build.id).await?.unwrap_or(build);
242-
Ok(BuildResponse::new(&build, &project, &organization))
267+
Ok(BuildResponse::new(&build, &project, &organization).with_blocked(&build, &project, &organization).await?)
243268
}
244269
.await,
245270
)

0 commit comments

Comments
 (0)