| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use super::settings::{csrf, require}; |
| 5 |
use super::{filters, lookup, lookup_role, Error, Page, RepoShell, Shell}; |
| 6 |
use crate::auth::{Access, Role, Viewer}; |
| 7 |
use crate::db::{self, flow as fdb}; |
| 8 |
use crate::Shared; |
| 9 |
use askama::Template; |
| 10 |
use axum::extract::{Form, Path, Query, State}; |
| 11 |
use axum::response::{IntoResponse, Redirect, Response}; |
| 12 |
use axum::Json; |
| 13 |
use serde::Deserialize; |
| 14 |
|
| 15 |
pub struct RunRow { |
| 16 |
pub r: fdb::Run, |
| 17 |
pub duration: String, |
| 18 |
pub jobs: Vec<(String, String)>, |
| 19 |
|
| 20 |
pub place: Option<String>, |
| 21 |
} |
| 22 |
|
| 23 |
#[derive(Template)] |
| 24 |
#[template(path = "flow_runs.html")] |
| 25 |
pub struct RunsPage { |
| 26 |
shell: Shell, |
| 27 |
repo: RepoShell, |
| 28 |
runs: Vec<RunRow>, |
| 29 |
enabled: bool, |
| 30 |
older: Option<i64>, |
| 31 |
|
| 32 |
can_run: bool, |
| 33 |
bookmarks: Vec<String>, |
| 34 |
error: Option<String>, |
| 35 |
|
| 36 |
why_not: Option<String>, |
| 37 |
} |
| 38 |
|
| 39 |
#[derive(Deserialize)] |
| 40 |
pub struct RunsQuery { |
| 41 |
before: Option<i64>, |
| 42 |
|
| 43 |
error: Option<String>, |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
pub fn why_not_run(enabled: bool, signed_in: bool, role: Role) -> Option<String> { |
| 48 |
if !enabled { |
| 49 |
Some("Flow is off for this repository; a site administrator can turn it on in Settings › Pipelines.".into()) |
| 50 |
} else if !signed_in { |
| 51 |
Some("Sign in with write access to run the pipeline.".into()) |
| 52 |
} else if role < Role::Write { |
| 53 |
Some("Running the pipeline needs write access to this repository.".into()) |
| 54 |
} else { |
| 55 |
None |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
const PER_PAGE: i64 = 30; |
| 60 |
|
| 61 |
#[derive(Template)] |
| 62 |
#[template(path = "deployments.html")] |
| 63 |
pub struct DeploymentsPage { |
| 64 |
shell: Shell, |
| 65 |
repo: RepoShell, |
| 66 |
|
| 67 |
current: Vec<fdb::Deployment>, |
| 68 |
|
| 69 |
history: Vec<fdb::Deployment>, |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
pub async fn deployments( |
| 75 |
State(app): State<Shared>, |
| 76 |
viewer: Viewer, |
| 77 |
Path((owner, name)): Path<(String, String)>, |
| 78 |
) -> Result<Page<DeploymentsPage>, Error> { |
| 79 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/deployments"), &viewer); |
| 80 |
let (_repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 81 |
let history = fdb::deployments(&app.db, row.id, 50).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 82 |
let mut current: Vec<fdb::Deployment> = Vec::new(); |
| 83 |
for d in &history { |
| 84 |
if !current.iter().any(|c| c.environment == d.environment) { |
| 85 |
current.push(d.clone()); |
| 86 |
} |
| 87 |
} |
| 88 |
current.sort_by(|a, b| a.environment.cmp(&b.environment)); |
| 89 |
Ok(Page(DeploymentsPage { shell, repo: RepoShell::new(&app, &row, "flow").with_role(role), current, history })) |
| 90 |
} |
| 91 |
|
| 92 |
pub async fn runs( |
| 93 |
State(app): State<Shared>, |
| 94 |
viewer: Viewer, |
| 95 |
Path((owner, name)): Path<(String, String)>, |
| 96 |
Query(q): Query<RunsQuery>, |
| 97 |
) -> Result<Page<RunsPage>, Error> { |
| 98 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/flow"), &viewer); |
| 99 |
let (_repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 100 |
let list = fdb::runs(&app.db, row.id, PER_PAGE + 1, q.before).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 101 |
let older = (list.len() as i64 > PER_PAGE).then(|| list[PER_PAGE as usize - 1].id); |
| 102 |
let queue = if list.iter().any(|r| r.live()) { fdb::queue(&app.db).await.unwrap_or_default() } else { vec![] }; |
| 103 |
let mut runs = Vec::new(); |
| 104 |
for r in list.into_iter().take(PER_PAGE as usize) { |
| 105 |
let place = if r.live() { crate::flow::queue_info(&queue, row.id, r.id).place() } else { None }; |
| 106 |
let jobs = fdb::jobs(&app.db, r.id).await.unwrap_or_default(); |
| 107 |
let duration = match r.finished_ms { |
| 108 |
Some(f) => crate::flow::human_ms(f - r.created_ms), |
| 109 |
None if r.live() => crate::flow::human_ms(db::now_ms() - r.created_ms), |
| 110 |
None => "–".into(), |
| 111 |
}; |
| 112 |
runs.push(RunRow { jobs: jobs.into_iter().map(|j| (j.name, j.status)).collect(), duration, r, place }); |
| 113 |
} |
| 114 |
let enabled = crate::flow::enabled(&app, &row); |
| 115 |
let why_not = why_not_run(enabled, viewer.account().is_some(), role); |
| 116 |
let can_run = why_not.is_none(); |
| 117 |
let bookmarks = if can_run { bookmark_names(&app, &row).await } else { vec![] }; |
| 118 |
Ok(Page(RunsPage { |
| 119 |
shell, |
| 120 |
repo: RepoShell::new(&app, &row, "flow").with_role(role), |
| 121 |
runs, |
| 122 |
enabled, |
| 123 |
older, |
| 124 |
can_run, |
| 125 |
bookmarks, |
| 126 |
error: q.error, |
| 127 |
why_not, |
| 128 |
})) |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
async fn bookmark_names(app: &Shared, row: &db::repos::RepoRow) -> Vec<String> { |
| 133 |
let Some(repo) = crate::repos::RepoName::new(&row.owner, &row.name) else { return vec![] }; |
| 134 |
let mut names: Vec<String> = app.repos.read(&repo, |r, _| Ok(r.view()?.bookmarks.keys().cloned().collect())).await.unwrap_or_default(); |
| 135 |
names.sort_by_key(|n| (n != &row.default_bookmark, n.clone())); |
| 136 |
names |
| 137 |
} |
| 138 |
|
| 139 |
#[derive(Deserialize)] |
| 140 |
pub struct StartForm { |
| 141 |
#[serde(rename = "_csrf")] |
| 142 |
csrf_token: String, |
| 143 |
#[serde(default)] |
| 144 |
bookmark: String, |
| 145 |
|
| 146 |
#[serde(default)] |
| 147 |
change: String, |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
pub async fn start( |
| 153 |
State(app): State<Shared>, |
| 154 |
viewer: Viewer, |
| 155 |
Path((owner, name)): Path<(String, String)>, |
| 156 |
Form(f): Form<StartForm>, |
| 157 |
) -> Result<Response, Error> { |
| 158 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/flow"), &viewer); |
| 159 |
let me = require(&viewer, &shell)?; |
| 160 |
let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?; |
| 161 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 162 |
let (started, what) = if f.change.trim().is_empty() { |
| 163 |
(crate::flow::run_bookmark(&app, &row, f.bookmark.trim(), &me.account.username, &[]).await, f.bookmark.trim().to_string()) |
| 164 |
} else { |
| 165 |
(crate::flow::run_change(&app, &row, &f.change, &me.account.username, &[]).await, format!("change {}", f.change.trim())) |
| 166 |
}; |
| 167 |
Ok(match started { |
| 168 |
Ok(n) => { |
| 169 |
db::accounts::audit(&app.db, Some(&me.account), "flow.run", &format!("{owner}/{name}"), &format!("#{n} {what}")).await.ok(); |
| 170 |
Redirect::to(&format!("/{owner}/{name}/flow/{n}")).into_response() |
| 171 |
} |
| 172 |
Err(msg) => Redirect::to(&format!("/{owner}/{name}/flow?error={}", urlencoding(&msg))).into_response(), |
| 173 |
}) |
| 174 |
} |
| 175 |
|
| 176 |
#[derive(Deserialize)] |
| 177 |
pub struct RerunForm { |
| 178 |
#[serde(rename = "_csrf")] |
| 179 |
csrf_token: String, |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
pub async fn rerun( |
| 184 |
State(app): State<Shared>, |
| 185 |
viewer: Viewer, |
| 186 |
Path((owner, name, number)): Path<(String, String, i64)>, |
| 187 |
Form(f): Form<RerunForm>, |
| 188 |
) -> Result<Response, Error> { |
| 189 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{number}"), &viewer); |
| 190 |
let me = require(&viewer, &shell)?; |
| 191 |
let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?; |
| 192 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 193 |
let run = fdb::run(&app.db, row.id, number) |
| 194 |
.await |
| 195 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 196 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 197 |
Ok(match crate::flow::rerun(&app, &row, &run, &me.account.username).await { |
| 198 |
Ok(n) => { |
| 199 |
db::accounts::audit(&app.db, Some(&me.account), "flow.rerun", &format!("{owner}/{name}"), &format!("#{n} of #{number}")) |
| 200 |
.await |
| 201 |
.ok(); |
| 202 |
Redirect::to(&format!("/{owner}/{name}/flow/{n}")).into_response() |
| 203 |
} |
| 204 |
Err(msg) => Redirect::to(&format!("/{owner}/{name}/flow?error={}", urlencoding(&msg))).into_response(), |
| 205 |
}) |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
pub async fn rerun_job( |
| 210 |
State(app): State<Shared>, |
| 211 |
viewer: Viewer, |
| 212 |
Path((owner, name, number, job)): Path<(String, String, i64, String)>, |
| 213 |
Form(f): Form<RerunForm>, |
| 214 |
) -> Result<Response, Error> { |
| 215 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{number}"), &viewer); |
| 216 |
let me = require(&viewer, &shell)?; |
| 217 |
let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?; |
| 218 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 219 |
let run = fdb::run(&app.db, row.id, number) |
| 220 |
.await |
| 221 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 222 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 223 |
Ok(match crate::flow::rerun_job(&app, &row, &run, &job, &me.account.username).await { |
| 224 |
Ok(_) => { |
| 225 |
db::accounts::audit(&app.db, Some(&me.account), "flow.rerun", &format!("{owner}/{name}"), &format!("{job} of #{number}")) |
| 226 |
.await |
| 227 |
.ok(); |
| 228 |
Redirect::to(&format!("/{owner}/{name}/flow/{number}")).into_response() |
| 229 |
} |
| 230 |
Err(msg) => Redirect::to(&format!("/{owner}/{name}/flow?error={}", urlencoding(&msg))).into_response(), |
| 231 |
}) |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
pub async fn cancel( |
| 236 |
State(app): State<Shared>, |
| 237 |
viewer: Viewer, |
| 238 |
Path((owner, name, number)): Path<(String, String, i64)>, |
| 239 |
Form(f): Form<RerunForm>, |
| 240 |
) -> Result<Response, Error> { |
| 241 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{number}"), &viewer); |
| 242 |
let me = require(&viewer, &shell)?; |
| 243 |
let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?; |
| 244 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 245 |
let run = fdb::run(&app.db, row.id, number) |
| 246 |
.await |
| 247 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 248 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 249 |
Ok(match crate::flow::cancel(&app, &row, &run, &me.account.username).await { |
| 250 |
Ok(()) => { |
| 251 |
db::accounts::audit(&app.db, Some(&me.account), "flow.cancel", &format!("{owner}/{name}"), &format!("#{number}")).await.ok(); |
| 252 |
Redirect::to(&format!("/{owner}/{name}/flow/{number}")).into_response() |
| 253 |
} |
| 254 |
Err(msg) => Redirect::to(&format!("/{owner}/{name}/flow?error={}", urlencoding(&msg))).into_response(), |
| 255 |
}) |
| 256 |
} |
| 257 |
|
| 258 |
#[derive(Deserialize)] |
| 259 |
pub struct ExpediteForm { |
| 260 |
#[serde(rename = "_csrf")] |
| 261 |
csrf_token: String, |
| 262 |
#[serde(default)] |
| 263 |
reason: String, |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
pub async fn expedite( |
| 270 |
State(app): State<Shared>, |
| 271 |
viewer: Viewer, |
| 272 |
Path((owner, name, number)): Path<(String, String, i64)>, |
| 273 |
Form(f): Form<ExpediteForm>, |
| 274 |
) -> Result<Response, Error> { |
| 275 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{number}"), &viewer); |
| 276 |
let me = require(&viewer, &shell)?; |
| 277 |
let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 278 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 279 |
let run = fdb::run(&app.db, row.id, number) |
| 280 |
.await |
| 281 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 282 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 283 |
let reason: String = f.reason.chars().filter(|c| !c.is_control()).take(200).collect(); |
| 284 |
let reason = reason.trim(); |
| 285 |
if reason.is_empty() { |
| 286 |
return Ok(Redirect::to(&format!("/{owner}/{name}/flow?error={}", urlencoding("Say why the run is urgent."))).into_response()); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
let cap: i64 = db::accounts::setting(&app.db, "flow_expedite_cap").await.ok().flatten().and_then(|v| v.parse().ok()).unwrap_or(2); |
| 291 |
let live = fdb::expedited_live(&app.db, &owner).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 292 |
if live >= cap { |
| 293 |
let msg = format!("{owner} already has {live} expedited run(s) going, the most at once ({cap}); wait for one to end."); |
| 294 |
return Ok(Redirect::to(&format!("/{owner}/{name}/flow?error={}", urlencoding(&msg))).into_response()); |
| 295 |
} |
| 296 |
let note = format!("expedited by {}: {reason}", me.account.username); |
| 297 |
if fdb::expedite(&app.db, run.id, ¬e).await.map_err(|e| Error::from_db(shell.clone(), e))? { |
| 298 |
db::accounts::audit(&app.db, Some(&me.account), "flow.expedite", &format!("{owner}/{name}"), &format!("#{number}: {reason}")) |
| 299 |
.await |
| 300 |
.ok(); |
| 301 |
} |
| 302 |
Ok(Redirect::to(&format!("/{owner}/{name}/flow/{number}")).into_response()) |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
fn urlencoding(s: &str) -> String { |
| 307 |
s.bytes() |
| 308 |
.map(|b| match b { |
| 309 |
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => (b as char).to_string(), |
| 310 |
_ => format!("%{b:02X}"), |
| 311 |
}) |
| 312 |
.collect() |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
pub struct Node { |
| 317 |
pub j: fdb::Job, |
| 318 |
pub x: i64, |
| 319 |
pub y: i64, |
| 320 |
pub duration: String, |
| 321 |
} |
| 322 |
|
| 323 |
pub struct Edge { |
| 324 |
pub d: String, |
| 325 |
pub status: String, |
| 326 |
} |
| 327 |
|
| 328 |
pub struct Graph { |
| 329 |
pub nodes: Vec<Node>, |
| 330 |
pub edges: Vec<Edge>, |
| 331 |
pub width: i64, |
| 332 |
pub height: i64, |
| 333 |
} |
| 334 |
|
| 335 |
pub const NODE_W: i64 = 200; |
| 336 |
pub const NODE_H: i64 = 48; |
| 337 |
const COL_GAP: i64 = 72; |
| 338 |
const ROW_GAP: i64 = 18; |
| 339 |
const PAD: i64 = 12; |
| 340 |
|
| 341 |
|
| 342 |
pub fn layout(jobs: Vec<fdb::Job>) -> Graph { |
| 343 |
let mut by_depth: std::collections::BTreeMap<i64, Vec<fdb::Job>> = Default::default(); |
| 344 |
for j in jobs { |
| 345 |
by_depth.entry(j.depth).or_default().push(j); |
| 346 |
} |
| 347 |
let rows = by_depth.values().map(Vec::len).max().unwrap_or(0) as i64; |
| 348 |
let height = PAD * 2 + rows * NODE_H + (rows - 1).max(0) * ROW_GAP; |
| 349 |
let mut nodes = Vec::new(); |
| 350 |
for (col, (_, mut js)) in by_depth.into_iter().enumerate() { |
| 351 |
js.sort_by(|a, b| a.name.cmp(&b.name)); |
| 352 |
let n = js.len() as i64; |
| 353 |
let column_h = n * NODE_H + (n - 1).max(0) * ROW_GAP; |
| 354 |
let top = (height - column_h) / 2; |
| 355 |
for (i, j) in js.into_iter().enumerate() { |
| 356 |
let duration = crate::flow::duration(&j); |
| 357 |
nodes.push(Node { x: PAD + col as i64 * (NODE_W + COL_GAP), y: top + i as i64 * (NODE_H + ROW_GAP), duration, j }); |
| 358 |
} |
| 359 |
} |
| 360 |
let mut edges = Vec::new(); |
| 361 |
for n in &nodes { |
| 362 |
for d in n.j.depends_on() { |
| 363 |
if let Some(from) = nodes.iter().find(|m| m.j.name == d) { |
| 364 |
let (x1, y1) = (from.x + NODE_W, from.y + NODE_H / 2); |
| 365 |
let (x2, y2) = (n.x, n.y + NODE_H / 2); |
| 366 |
let mid = (x1 + x2) / 2; |
| 367 |
edges.push(Edge { d: format!("M{x1},{y1} C{mid},{y1} {mid},{y2} {x2},{y2}"), status: from.j.status.clone() }); |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
let cols = nodes.iter().map(|n| n.x).max().map(|x| x + NODE_W + PAD).unwrap_or(PAD * 2); |
| 372 |
Graph { nodes, edges, width: cols, height: height.max(NODE_H + PAD * 2) } |
| 373 |
} |
| 374 |
|
| 375 |
pub fn glyph(status: &str) -> &'static str { |
| 376 |
match status { |
| 377 |
"passed" => "✓", |
| 378 |
"failed" => "✕", |
| 379 |
"running" => "●", |
| 380 |
"skipped" => "⤼", |
| 381 |
"cancelled" => "⊘", |
| 382 |
"error" => "!", |
| 383 |
_ => "○", |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
const SHOWN_FAILURES: i64 = 50; |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
pub enum Segment { |
| 394 |
Line(fdb::LogLine), |
| 395 |
Group(Section), |
| 396 |
} |
| 397 |
|
| 398 |
pub struct Section { |
| 399 |
pub title: String, |
| 400 |
|
| 401 |
pub from_t: i64, |
| 402 |
pub to_t: Option<i64>, |
| 403 |
pub lines: Vec<fdb::LogLine>, |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
pub fn sections(lines: &[fdb::LogLine]) -> Vec<Segment> { |
| 409 |
let mut out: Vec<Segment> = Vec::new(); |
| 410 |
let mut open: Option<Section> = None; |
| 411 |
for l in lines { |
| 412 |
if let Some(title) = l.text.strip_prefix("::group::") { |
| 413 |
if let Some(g) = open.take() { |
| 414 |
out.push(Segment::Group(Section { to_t: Some(l.t), ..g })); |
| 415 |
} |
| 416 |
open = Some(Section { title: title.trim().to_string(), from_t: l.t, to_t: None, lines: Vec::new() }); |
| 417 |
} else if l.text.trim() == "::endgroup::" { |
| 418 |
if let Some(g) = open.take() { |
| 419 |
out.push(Segment::Group(Section { to_t: Some(l.t), ..g })); |
| 420 |
} |
| 421 |
} else { |
| 422 |
match open.as_mut() { |
| 423 |
Some(g) => g.lines.push(l.clone()), |
| 424 |
None => out.push(Segment::Line(l.clone())), |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
if let Some(g) = open { |
| 429 |
out.push(Segment::Group(g)); |
| 430 |
} |
| 431 |
out |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
pub fn secs(ms: impl std::borrow::Borrow<i64>) -> String { |
| 436 |
let ms = *ms.borrow(); |
| 437 |
format!("{}.{}s", ms / 1000, ms % 1000 / 100) |
| 438 |
} |
| 439 |
|
| 440 |
pub struct JobLog { |
| 441 |
pub j: fdb::Job, |
| 442 |
pub artifacts: Vec<fdb::Artifact>, |
| 443 |
pub lines: Vec<fdb::LogLine>, |
| 444 |
|
| 445 |
pub segments: Vec<Segment>, |
| 446 |
pub duration: String, |
| 447 |
|
| 448 |
pub last_seq: i64, |
| 449 |
|
| 450 |
pub coverage: Option<(i64, String)>, |
| 451 |
|
| 452 |
|
| 453 |
pub tests: Option<crate::junit::Report>, |
| 454 |
pub failures: Vec<crate::junit::Case>, |
| 455 |
} |
| 456 |
|
| 457 |
#[derive(Template)] |
| 458 |
#[template(path = "flow_run.html")] |
| 459 |
pub struct RunPage { |
| 460 |
shell: Shell, |
| 461 |
repo: RepoShell, |
| 462 |
run: fdb::Run, |
| 463 |
graph: Graph, |
| 464 |
logs: Vec<JobLog>, |
| 465 |
duration: String, |
| 466 |
|
| 467 |
can_rerun: bool, |
| 468 |
|
| 469 |
can_cancel: bool, |
| 470 |
|
| 471 |
can_expedite: bool, |
| 472 |
|
| 473 |
queue: Option<crate::flow::QueueInfo>, |
| 474 |
|
| 475 |
releases: Vec<db::releases::Release>, |
| 476 |
|
| 477 |
approvals: Vec<ApprovalView>, |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
pub struct ApprovalView { |
| 482 |
pub job: String, |
| 483 |
pub by: String, |
| 484 |
pub others_only: bool, |
| 485 |
|
| 486 |
pub state: &'static str, |
| 487 |
pub who: String, |
| 488 |
pub when_ms: i64, |
| 489 |
|
| 490 |
pub may: Result<(), String>, |
| 491 |
|
| 492 |
|
| 493 |
pub sealed: bool, |
| 494 |
pub command: String, |
| 495 |
pub seal: String, |
| 496 |
} |
| 497 |
|
| 498 |
impl ApprovalView { |
| 499 |
pub fn may_decide(&self) -> bool { |
| 500 |
self.may.is_ok() |
| 501 |
} |
| 502 |
|
| 503 |
pub fn why_not(&self) -> String { |
| 504 |
self.may.clone().err().unwrap_or_default() |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
const PAGE_LINES: i64 = 5000; |
| 510 |
|
| 511 |
pub async fn run( |
| 512 |
State(app): State<Shared>, |
| 513 |
viewer: Viewer, |
| 514 |
Path((owner, name, id)): Path<(String, String, i64)>, |
| 515 |
) -> Result<Page<RunPage>, Error> { |
| 516 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{id}"), &viewer); |
| 517 |
let (_repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 518 |
let run = fdb::run(&app.db, row.id, id) |
| 519 |
.await |
| 520 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 521 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 522 |
let jobs = fdb::jobs(&app.db, run.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 523 |
let reports = crate::coverage::for_run(&app.db, run.id).await.unwrap_or_default(); |
| 524 |
let test_reports = crate::junit::for_run(&app.db, run.id).await.unwrap_or_default(); |
| 525 |
let mut logs = Vec::new(); |
| 526 |
for j in &jobs { |
| 527 |
let lines = fdb::log(&app.db, j.id, 0, PAGE_LINES).await.unwrap_or_default(); |
| 528 |
let last_seq = lines.last().map(|l| l.seq).unwrap_or(0); |
| 529 |
let artifacts = fdb::artifacts(&app.db, j.id).await.unwrap_or_default(); |
| 530 |
let coverage = reports |
| 531 |
.iter() |
| 532 |
.find(|r| r.job_id == Some(j.id)) |
| 533 |
.map(|r| (r.id, r.percent().map(|p| format!("{p:.1}%")).unwrap_or_else(|| "no lines".into()))); |
| 534 |
let tests = test_reports.iter().find(|r| r.job_id == j.id).cloned(); |
| 535 |
let failures = match &tests { |
| 536 |
Some(r) if r.broken() > 0 => crate::junit::failures(&app.db, r.id, SHOWN_FAILURES).await.unwrap_or_default(), |
| 537 |
_ => vec![], |
| 538 |
}; |
| 539 |
let segments = sections(&lines); |
| 540 |
logs.push(JobLog { |
| 541 |
duration: crate::flow::duration(j), |
| 542 |
j: j.clone(), |
| 543 |
artifacts, |
| 544 |
lines, |
| 545 |
segments, |
| 546 |
last_seq, |
| 547 |
coverage, |
| 548 |
tests, |
| 549 |
failures, |
| 550 |
}); |
| 551 |
} |
| 552 |
let duration = match run.finished_ms { |
| 553 |
Some(f) => crate::flow::human_ms(f - run.created_ms), |
| 554 |
None => crate::flow::human_ms(db::now_ms() - run.created_ms), |
| 555 |
}; |
| 556 |
let mut approvals = Vec::new(); |
| 557 |
for j in jobs.iter().filter(|j| !j.approval.is_empty()) { |
| 558 |
let Some(a) = serde_json::from_str::<flow_core::Job>(&j.spec).ok().and_then(|s| s.approval) else { continue }; |
| 559 |
let state = match (j.approval.as_str(), j.status.as_str()) { |
| 560 |
("waiting", "queued") => "waiting", |
| 561 |
("approved", _) => "approved", |
| 562 |
(_, "cancelled") if !j.approved_by.is_empty() => "rejected", |
| 563 |
(_, "cancelled") if j.error.starts_with("no approval within") => "expired", |
| 564 |
_ => continue, |
| 565 |
}; |
| 566 |
let may = match (state, viewer.account()) { |
| 567 |
("waiting", Some(me)) => crate::flow::may_decide(&app, &row, &run, j, me).await.map(|_| ()), |
| 568 |
("waiting", None) => Err("sign in to approve".into()), |
| 569 |
_ => Err(String::new()), |
| 570 |
}; |
| 571 |
approvals.push(ApprovalView { |
| 572 |
sealed: a.sealed, |
| 573 |
command: format!("oxy seal --flow {}/{owner}/{name}/flow/{} --job {}", app.config.base_url(), run.number, j.name), |
| 574 |
seal: j.approval_seal.clone(), |
| 575 |
job: j.name.clone(), |
| 576 |
by: a.by, |
| 577 |
others_only: a.others_only, |
| 578 |
state, |
| 579 |
who: if j.approved_via.is_empty() { j.approved_by.clone() } else { format!("{} via {}", j.approved_by, j.approved_via) }, |
| 580 |
when_ms: j.approved_ms.unwrap_or(0), |
| 581 |
may, |
| 582 |
}); |
| 583 |
} |
| 584 |
let graph = layout(jobs); |
| 585 |
let can_write = viewer.account().is_some() && role >= Role::Write; |
| 586 |
let can_rerun = !run.live() && can_write && crate::flow::enabled(&app, &row); |
| 587 |
let can_cancel = run.live() && can_write; |
| 588 |
let can_expedite = run.live() && run.priority.is_empty() && viewer.account().is_some() && role >= Role::Maintain; |
| 589 |
let queue = if run.live() { |
| 590 |
let q = fdb::queue(&app.db).await.unwrap_or_default(); |
| 591 |
Some(crate::flow::queue_info(&q, row.id, run.id)).filter(|i| i.position.is_some()) |
| 592 |
} else { |
| 593 |
None |
| 594 |
}; |
| 595 |
let releases = if viewer.account().is_some() && role >= Role::Write && logs.iter().any(|l| !l.artifacts.is_empty()) { |
| 596 |
db::releases::list(&app.db, row.id).await.unwrap_or_default() |
| 597 |
} else { |
| 598 |
vec![] |
| 599 |
}; |
| 600 |
Ok(Page(RunPage { |
| 601 |
shell, |
| 602 |
repo: RepoShell::new(&app, &row, "flow").with_role(role), |
| 603 |
run, |
| 604 |
graph, |
| 605 |
logs, |
| 606 |
duration, |
| 607 |
can_rerun, |
| 608 |
can_cancel, |
| 609 |
can_expedite, |
| 610 |
queue, |
| 611 |
releases, |
| 612 |
approvals, |
| 613 |
})) |
| 614 |
} |
| 615 |
|
| 616 |
#[derive(Deserialize)] |
| 617 |
pub struct DecideForm { |
| 618 |
#[serde(rename = "_csrf")] |
| 619 |
csrf_token: String, |
| 620 |
|
| 621 |
action: String, |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
pub async fn decide( |
| 627 |
State(app): State<Shared>, |
| 628 |
viewer: Viewer, |
| 629 |
Path((owner, name, number, job)): Path<(String, String, i64, String)>, |
| 630 |
Form(f): Form<DecideForm>, |
| 631 |
) -> Result<Response, Error> { |
| 632 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{number}"), &viewer); |
| 633 |
let me = require(&viewer, &shell)?; |
| 634 |
let (_, row, _) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 635 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 636 |
let run = fdb::run(&app.db, row.id, number) |
| 637 |
.await |
| 638 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 639 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 640 |
let approve = match f.action.as_str() { |
| 641 |
"approve" => true, |
| 642 |
"reject" => false, |
| 643 |
_ => return Err(Error::bad(shell, "approve or reject")), |
| 644 |
}; |
| 645 |
match crate::flow::decide(&app, &row, &run, &job, &me.account, approve, None).await { |
| 646 |
Ok(_) => Ok(Redirect::to(&format!("/{owner}/{name}/flow/{number}")).into_response()), |
| 647 |
Err(why) => Err(Error::bad(shell, why)), |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
#[derive(Deserialize)] |
| 652 |
pub struct LiveQuery { |
| 653 |
|
| 654 |
after: Option<String>, |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
pub async fn live( |
| 660 |
State(app): State<Shared>, |
| 661 |
viewer: Viewer, |
| 662 |
Path((owner, name, id)): Path<(String, String, i64)>, |
| 663 |
Query(q): Query<LiveQuery>, |
| 664 |
) -> Result<Response, Error> { |
| 665 |
let shell = Shell::new(&app, "", &viewer); |
| 666 |
let (_repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 667 |
let run = fdb::run(&app.db, row.id, id) |
| 668 |
.await |
| 669 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 670 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 671 |
let jobs = fdb::jobs(&app.db, run.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 672 |
let after: std::collections::HashMap<i64, i64> = q |
| 673 |
.after |
| 674 |
.unwrap_or_default() |
| 675 |
.split(',') |
| 676 |
.filter_map(|p| p.split_once(':')) |
| 677 |
.filter_map(|(a, b)| Some((a.parse().ok()?, b.parse().ok()?))) |
| 678 |
.collect(); |
| 679 |
let mut out = Vec::new(); |
| 680 |
for j in &jobs { |
| 681 |
let lines = match after.get(&j.id) { |
| 682 |
Some(seq) => fdb::log(&app.db, j.id, *seq, 1000).await.unwrap_or_default(), |
| 683 |
None => vec![], |
| 684 |
}; |
| 685 |
out.push(serde_json::json!({ |
| 686 |
"id": j.id, |
| 687 |
"name": j.name, |
| 688 |
"status": j.status, |
| 689 |
"glyph": glyph(&j.status), |
| 690 |
"duration": crate::flow::duration(j), |
| 691 |
"exit_code": j.exit_code, |
| 692 |
"lines": lines.iter().map(|l| serde_json::json!({ "seq": l.seq, "t": l.t, "stream": l.stream, "text": l.text })).collect::<Vec<_>>(), |
| 693 |
})); |
| 694 |
} |
| 695 |
Ok(Json(serde_json::json!({ "status": run.status, "live": run.live(), "jobs": out })).into_response()) |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
pub const STAMP_IMAGE: &str = ".flow/stamp"; |
| 702 |
|
| 703 |
pub const RUN_LINK: &str = ".flow/run"; |
| 704 |
|
| 705 |
|
| 706 |
async fn state_of(app: &Shared, owner: &str, name: &str, r: &str) -> Result<(String, bool), crate::repos::RepoError> { |
| 707 |
let repo = crate::repos::RepoName::new(owner, name).ok_or(crate::repos::RepoError::NotFound)?; |
| 708 |
let spec = r.to_string(); |
| 709 |
app.repos |
| 710 |
.read(&repo, move |repo, cache| { |
| 711 |
let t = crate::repos::view::resolve(repo, cache, &spec)?; |
| 712 |
let moves = matches!(t.kind, crate::repos::view::RefKind::Bookmark | crate::repos::view::RefKind::Tag); |
| 713 |
Ok((t.permalink().unwrap_or_default(), moves)) |
| 714 |
}) |
| 715 |
.await |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
pub async fn stamp( |
| 720 |
State(app): State<Shared>, |
| 721 |
viewer: Viewer, |
| 722 |
Path((owner, name, r)): Path<(String, String, String)>, |
| 723 |
) -> Result<Response, Error> { |
| 724 |
let shell = Shell::new(&app, "", &viewer); |
| 725 |
let (_repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 726 |
let (heads, moves) = state_of(&app, &owner, &name, &r).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 727 |
if moves && !heads.is_empty() { |
| 728 |
let mut res = axum::response::Redirect::to(&format!("/{owner}/{name}/flow/stamp/{heads}")).into_response(); |
| 729 |
res.headers_mut().insert(axum::http::header::CACHE_CONTROL, axum::http::HeaderValue::from_static("no-cache")); |
| 730 |
return Ok(res); |
| 731 |
} |
| 732 |
let run = fdb::latest_for_heads(&app.db, row.id, &heads).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 733 |
let jobs = match &run { |
| 734 |
Some(r) => fdb::jobs(&app.db, r.id).await.map_err(|e| Error::from_db(shell.clone(), e))?, |
| 735 |
None => vec![], |
| 736 |
}; |
| 737 |
let svg = stamp_svg(&heads, run.as_ref(), &jobs); |
| 738 |
let cache = if run.as_ref().is_some_and(|r| !r.live()) { "public, max-age=3600" } else { "public, max-age=60" }; |
| 739 |
Ok(( |
| 740 |
[ |
| 741 |
(axum::http::header::CONTENT_TYPE, "image/svg+xml; charset=utf-8"), |
| 742 |
(axum::http::header::CACHE_CONTROL, cache), |
| 743 |
(axum::http::header::CONTENT_SECURITY_POLICY, "default-src 'none'; style-src 'unsafe-inline'"), |
| 744 |
], |
| 745 |
svg, |
| 746 |
) |
| 747 |
.into_response()) |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
pub async fn at( |
| 752 |
State(app): State<Shared>, |
| 753 |
viewer: Viewer, |
| 754 |
Path((owner, name, r)): Path<(String, String, String)>, |
| 755 |
) -> Result<Response, Error> { |
| 756 |
let shell = Shell::new(&app, "", &viewer); |
| 757 |
let (_repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 758 |
let (heads, _) = state_of(&app, &owner, &name, &r).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 759 |
let run = fdb::latest_for_heads(&app.db, row.id, &heads).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 760 |
let to = match run { |
| 761 |
Some(r) => format!("/{owner}/{name}/flow/{}", r.number), |
| 762 |
None => format!("/{owner}/{name}/flow"), |
| 763 |
}; |
| 764 |
Ok(axum::response::Redirect::to(&to).into_response()) |
| 765 |
} |
| 766 |
|
| 767 |
fn xml(s: &str) -> String { |
| 768 |
s.replace('&', "&").replace('<', "<").replace('>', ">").replace('"', """) |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
pub fn stamp_svg(heads: &str, run: Option<&fdb::Run>, jobs: &[fdb::Job]) -> String { |
| 775 |
const CH: f64 = 6.6; |
| 776 |
let colour = |s: &str| match s { |
| 777 |
"passed" => "#5fe3a1", |
| 778 |
"failed" | "error" => "#ff5e7a", |
| 779 |
"running" => "#00e0f0", |
| 780 |
_ => "#8592ad", |
| 781 |
}; |
| 782 |
let short: String = heads.chars().take(12).collect(); |
| 783 |
let (status, date) = match run { |
| 784 |
Some(r) => (r.status.as_str(), crate::render::time::absolute(r.created_ms as u64).chars().take(10).collect::<String>()), |
| 785 |
None => ("no run", String::new()), |
| 786 |
}; |
| 787 |
let mut ordered: Vec<&fdb::Job> = jobs.iter().collect(); |
| 788 |
ordered.sort_by(|a, b| (a.depth, &a.name).cmp(&(b.depth, &b.name))); |
| 789 |
let label = "OXYGENFLOW"; |
| 790 |
let label_w = 18.0 + label.len() as f64 * 7.4 + 8.0; |
| 791 |
let glyphs_w = if ordered.is_empty() { 0.0 } else { ordered.len() as f64 * 10.0 + 6.0 }; |
| 792 |
let tail = if date.is_empty() { format!("{status} · {short}") } else { format!("{status} · {short} · {date}") }; |
| 793 |
let right_w = 8.0 + glyphs_w + tail.chars().count() as f64 * CH + 8.0; |
| 794 |
let w = (label_w + right_w).ceil(); |
| 795 |
let mut glyphs = String::new(); |
| 796 |
for (i, j) in ordered.iter().enumerate() { |
| 797 |
let x = label_w + 8.0 + i as f64 * 10.0 + 4.0; |
| 798 |
glyphs.push_str(&format!( |
| 799 |
r#"<text x="{x:.1}" y="15" fill="{}" text-anchor="middle">{}</text>"#, |
| 800 |
colour(&j.status), |
| 801 |
glyph(&j.status) |
| 802 |
)); |
| 803 |
} |
| 804 |
let meta = serde_json::json!({ |
| 805 |
"heads": heads.split('+').filter(|h| !h.is_empty()).collect::<Vec<_>>(), |
| 806 |
"run": run.map(|r| r.number), |
| 807 |
"status": status, |
| 808 |
"jobs": ordered.iter().map(|j| serde_json::json!({ "name": j.name, "status": j.status, "receipt": j.receipt_hash })).collect::<Vec<_>>(), |
| 809 |
}); |
| 810 |
let title = match run { |
| 811 |
Some(r) => format!("OxygenFlow run #{} {status} on state {short}", r.number), |
| 812 |
None => format!("OxygenFlow: no run for state {short}"), |
| 813 |
}; |
| 814 |
format!( |
| 815 |
r##"<svg xmlns="http://www.w3.org/2000/svg" width="{w}" height="22" viewBox="0 0 {w} 22" role="img" aria-label="{t}"><title>{t}</title><metadata>{m}</metadata><rect width="{w}" height="22" rx="4" fill="#0e1526"/><rect width="{lw:.1}" height="22" rx="4" fill="#0b3f9e"/><rect x="{lx:.1}" width="4" height="22" fill="#0b3f9e"/><circle cx="9" cy="11" r="3.2" fill="none" stroke="#00f0ff" stroke-width="1.6"/><text x="16" y="15" fill="#ffffff" font-family="Barlow, Verdana, sans-serif" font-size="10.5" font-weight="700" letter-spacing=".6">{label}</text><g font-family="IBM Plex Mono, ui-monospace, monospace" font-size="11">{glyphs}<text x="{tx:.1}" y="15" fill="{sc}">{tail}</text></g><rect x=".5" y=".5" width="{wi}" height="21" rx="3.5" fill="none" stroke="#1f2b47"/></svg>"##, |
| 816 |
t = xml(&title), |
| 817 |
m = xml(&meta.to_string()), |
| 818 |
lw = label_w, |
| 819 |
lx = label_w - 4.0, |
| 820 |
tx = label_w + 8.0 + glyphs_w, |
| 821 |
sc = colour(status), |
| 822 |
tail = xml(&tail), |
| 823 |
wi = w - 1.0, |
| 824 |
) |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
#[derive(Clone, Debug)] |
| 829 |
pub struct ReceiptRow { |
| 830 |
pub attest: String, |
| 831 |
pub job: String, |
| 832 |
pub exit_code: u32, |
| 833 |
pub duration: String, |
| 834 |
pub runner: String, |
| 835 |
pub run: String, |
| 836 |
|
| 837 |
pub problem: String, |
| 838 |
pub made_ms: i64, |
| 839 |
} |
| 840 |
|
| 841 |
impl ReceiptRow { |
| 842 |
pub fn passed(&self) -> bool { |
| 843 |
self.exit_code == 0 |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
pub async fn receipts_of(app: &Shared, name: &crate::repos::RepoName, heads: Vec<oxy_core::hash::ChangeHash>) -> Vec<ReceiptRow> { |
| 849 |
let base = app.config.base_url(); |
| 850 |
let found = app.repos.read(name, move |repo, _| Ok(repo.receipts(&heads)?)).await.unwrap_or_default(); |
| 851 |
found |
| 852 |
.into_iter() |
| 853 |
.map(|i| { |
| 854 |
let r = &i.signed.receipt; |
| 855 |
let run = i.signed.run().to_string(); |
| 856 |
ReceiptRow { |
| 857 |
attest: i.attest.to_hex(), |
| 858 |
job: r.job_name.clone(), |
| 859 |
exit_code: r.exit_code, |
| 860 |
duration: crate::flow::human_ms(r.duration_ms as i64), |
| 861 |
runner: i.signed.runner.name.clone(), |
| 862 |
|
| 863 |
run: if run.starts_with(base) { run } else { String::new() }, |
| 864 |
problem: match (i.problem, i.revoked) { |
| 865 |
(Some(p), _) => p, |
| 866 |
(None, true) => "the runner's key was revoked".into(), |
| 867 |
(None, false) => String::new(), |
| 868 |
}, |
| 869 |
made_ms: i.signed.attest.hlc.wall_ms as i64, |
| 870 |
} |
| 871 |
}) |
| 872 |
.collect() |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
pub async fn receipt_file( |
| 878 |
State(app): State<Shared>, |
| 879 |
viewer: Viewer, |
| 880 |
Path((owner, name, attest)): Path<(String, String, String)>, |
| 881 |
) -> Result<Response, Error> { |
| 882 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/receipts/{attest}"), &viewer); |
| 883 |
let (repo, _row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 884 |
let Some(r) = oxy_core::hash::ObjectRef::from_hex(attest.trim_end_matches(".receipt")) else { |
| 885 |
return Err(Error::not_found(shell)); |
| 886 |
}; |
| 887 |
let (job, bytes) = app |
| 888 |
.repos |
| 889 |
.read(&repo, move |repo, _| { |
| 890 |
let i = repo.receipt(&r)?; |
| 891 |
Ok((i.signed.receipt.job_name.clone(), i.signed.encode())) |
| 892 |
}) |
| 893 |
.await |
| 894 |
.map_err(|_| Error::not_found(shell.clone()))?; |
| 895 |
let file: String = job.chars().filter(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.')).collect(); |
| 896 |
Ok(( |
| 897 |
[ |
| 898 |
(axum::http::header::CONTENT_TYPE, "application/oxy-receipt".to_string()), |
| 899 |
(axum::http::header::CONTENT_DISPOSITION, format!("attachment; filename=\"{file}-{}.receipt\"", r.short(12))), |
| 900 |
], |
| 901 |
bytes, |
| 902 |
) |
| 903 |
.into_response()) |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
pub async fn artifact_file( |
| 909 |
State(app): State<Shared>, |
| 910 |
viewer: Viewer, |
| 911 |
Path((owner, name, id, job, artifact)): Path<(String, String, i64, String, String)>, |
| 912 |
) -> Result<Response, Error> { |
| 913 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{id}/artifacts/{job}/{artifact}"), &viewer); |
| 914 |
let (repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 915 |
let run = fdb::run(&app.db, row.id, id) |
| 916 |
.await |
| 917 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 918 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 919 |
let jobs = fdb::jobs(&app.db, run.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 920 |
let Some(j) = jobs.iter().find(|j| j.name == job) else { return Err(Error::not_found(shell)) }; |
| 921 |
let arts = fdb::artifacts(&app.db, j.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 922 |
let Some(a) = arts.into_iter().find(|a| a.name == artifact) else { return Err(Error::not_found(shell)) }; |
| 923 |
let Some(blob) = oxy_core::hash::ObjectRef::from_hex(&a.blob) else { return Err(Error::not_found(shell)) }; |
| 924 |
let bytes = app.repos.read(&repo, move |r, _| Ok(r.read_blob(&blob)?)).await.map_err(|_| Error::not_found(shell.clone()))?; |
| 925 |
let file = a.path.rsplit('/').next().unwrap_or(&a.name).to_string(); |
| 926 |
Ok(( |
| 927 |
[ |
| 928 |
(axum::http::header::CONTENT_TYPE, "application/octet-stream".to_string()), |
| 929 |
(axum::http::header::CONTENT_DISPOSITION, format!("attachment; filename=\"{}\"", file.replace('"', ""))), |
| 930 |
], |
| 931 |
bytes, |
| 932 |
) |
| 933 |
.into_response()) |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
pub async fn provenance( |
| 941 |
State(app): State<Shared>, |
| 942 |
viewer: Viewer, |
| 943 |
Path((owner, name, id, job, artifact)): Path<(String, String, i64, String, String)>, |
| 944 |
) -> Result<Response, Error> { |
| 945 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{id}/artifacts/{job}/{artifact}/provenance"), &viewer); |
| 946 |
let (repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 947 |
let run = fdb::run(&app.db, row.id, id) |
| 948 |
.await |
| 949 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 950 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 951 |
let jobs = fdb::jobs(&app.db, run.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 952 |
let Some(j) = jobs.iter().find(|j| j.name == job) else { return Err(Error::not_found(shell)) }; |
| 953 |
if j.receipt_attest.is_empty() { |
| 954 |
return Err(Error::bad(shell, "This job has no signed receipt the hub kept, so the hub states no provenance for it.".to_string())); |
| 955 |
} |
| 956 |
let arts = fdb::artifacts(&app.db, j.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 957 |
let Some(a) = arts.into_iter().find(|a| a.name == artifact) else { return Err(Error::not_found(shell)) }; |
| 958 |
let Some(blob) = oxy_core::hash::ObjectRef::from_hex(&a.blob) else { return Err(Error::not_found(shell)) }; |
| 959 |
let bytes = app.repos.read(&repo, move |r, _| Ok(r.read_blob(&blob)?)).await.map_err(|_| Error::not_found(shell.clone()))?; |
| 960 |
let file = a.path.rsplit('/').next().unwrap_or(&a.name).to_string(); |
| 961 |
let base = app.config.base_url(); |
| 962 |
let subject = crate::provenance::Subject { |
| 963 |
name: &file, |
| 964 |
bytes: &bytes, |
| 965 |
repo: &format!("{owner}/{name}"), |
| 966 |
run_url: &format!("{base}/{owner}/{name}/flow/{id}"), |
| 967 |
bookmark: &run.bookmark, |
| 968 |
state: &run.state, |
| 969 |
heads: &run.heads, |
| 970 |
pipeline_hash: &run.pipeline_hash, |
| 971 |
job: &j.name, |
| 972 |
runner: &j.runner, |
| 973 |
receipt_signer: &j.receipt_signer, |
| 974 |
receipt_attest: &j.receipt_attest, |
| 975 |
started_ms: j.started_ms, |
| 976 |
finished_ms: j.finished_ms, |
| 977 |
}; |
| 978 |
let statement = crate::provenance::statement(base, &subject); |
| 979 |
let envelope = crate::provenance::envelope(&app, &statement).map_err(|e| Error::bad(shell.clone(), e.to_string()))?; |
| 980 |
Ok(( |
| 981 |
[ |
| 982 |
(axum::http::header::CONTENT_TYPE, "application/json".to_string()), |
| 983 |
(axum::http::header::CONTENT_DISPOSITION, format!("attachment; filename=\"{}.intoto.json\"", file.replace('"', ""))), |
| 984 |
], |
| 985 |
serde_json::to_string(&envelope).unwrap_or_default(), |
| 986 |
) |
| 987 |
.into_response()) |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
pub async fn signing_key(State(app): State<Shared>) -> Response { |
| 993 |
([(axum::http::header::CONTENT_TYPE, "application/x-pem-file")], crate::provenance::public_pem(&app)).into_response() |
| 994 |
} |
| 995 |
|
| 996 |
#[derive(Deserialize)] |
| 997 |
pub struct AttachForm { |
| 998 |
_csrf: String, |
| 999 |
release: i64, |
| 1000 |
} |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
pub async fn attach( |
| 1006 |
State(app): State<Shared>, |
| 1007 |
viewer: Viewer, |
| 1008 |
Path((owner, name, id, job, artifact)): Path<(String, String, i64, String, String)>, |
| 1009 |
Form(f): Form<AttachForm>, |
| 1010 |
) -> Result<Response, Error> { |
| 1011 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{id}"), &viewer); |
| 1012 |
require(&viewer, &shell)?; |
| 1013 |
csrf(&viewer, &f._csrf, &shell)?; |
| 1014 |
let (repo, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?; |
| 1015 |
let run = fdb::run(&app.db, row.id, id) |
| 1016 |
.await |
| 1017 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 1018 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 1019 |
let jobs = fdb::jobs(&app.db, run.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1020 |
let Some(j) = jobs.iter().find(|j| j.name == job) else { return Err(Error::not_found(shell)) }; |
| 1021 |
|
| 1022 |
|
| 1023 |
if j.status != "passed" { |
| 1024 |
return Err(Error::bad(shell, "only a passing job's artifacts can be attached to a release")); |
| 1025 |
} |
| 1026 |
let arts = fdb::artifacts(&app.db, j.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1027 |
let Some(a) = arts.into_iter().find(|a| a.name == artifact) else { return Err(Error::not_found(shell)) }; |
| 1028 |
let releases = db::releases::list(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1029 |
let Some(rel) = releases.into_iter().find(|r| r.id == f.release) else { return Err(Error::not_found(shell)) }; |
| 1030 |
let Some(blob) = oxy_core::hash::ObjectRef::from_hex(&a.blob) else { return Err(Error::not_found(shell)) }; |
| 1031 |
let bytes = app.repos.read(&repo, move |r, _| Ok(r.read_blob(&blob)?)).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 1032 |
let file = a.path.rsplit('/').next().unwrap_or(&a.name).to_string(); |
| 1033 |
super::releases::store_asset(&app, rel.id, &file, "application/octet-stream", &bytes) |
| 1034 |
.await |
| 1035 |
.map_err(|m| Error::bad(shell.clone(), m))?; |
| 1036 |
Ok(Redirect::to(&format!("/{owner}/{name}/releases/tag/{}", rel.tag)).into_response()) |
| 1037 |
} |
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
pub async fn badge( |
| 1044 |
State(app): State<Shared>, |
| 1045 |
viewer: Viewer, |
| 1046 |
Path((owner, name)): Path<(String, String)>, |
| 1047 |
) -> Result<axum::response::Response, Error> { |
| 1048 |
use axum::response::IntoResponse; |
| 1049 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/flow"), &viewer); |
| 1050 |
let (_repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 1051 |
let latest = fdb::latest_on(&app.db, row.id, &row.default_bookmark).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1052 |
let (text, colour) = match latest.as_ref().map(|r| r.status.as_str()) { |
| 1053 |
Some("passed") => ("passing", "#2da44e"), |
| 1054 |
Some("failed") | Some("error") => ("failing", "#cf222e"), |
| 1055 |
Some("cancelled") => ("cancelled", "#6e7781"), |
| 1056 |
Some(_) => ("running", "#0969da"), |
| 1057 |
None => ("no runs", "#6e7781"), |
| 1058 |
}; |
| 1059 |
let label = "OxygenFlow"; |
| 1060 |
let (lw, tw) = (label.len() as u32 * 7 + 10, text.len() as u32 * 7 + 10); |
| 1061 |
let svg = format!( |
| 1062 |
r##"<svg xmlns="http://www.w3.org/2000/svg" width="{w}" height="20" role="img" aria-label="{label}: {text}"><title>{label}: {text}</title><rect width="{lw}" height="20" rx="3" fill="#555"/><rect x="{lw}" width="{tw}" height="20" rx="3" fill="{colour}"/><rect x="{lw}" width="4" height="20" fill="{colour}"/><g fill="#fff" font-family="Verdana,DejaVu Sans,sans-serif" font-size="11" text-anchor="middle"><text x="{lx}" y="14">{label}</text><text x="{tx}" y="14">{text}</text></g></svg>"##, |
| 1063 |
w = lw + tw, |
| 1064 |
lx = lw / 2, |
| 1065 |
tx = lw + tw / 2, |
| 1066 |
); |
| 1067 |
Ok(( |
| 1068 |
[(axum::http::header::CONTENT_TYPE, "image/svg+xml; charset=utf-8"), (axum::http::header::CACHE_CONTROL, "no-cache, max-age=60")], |
| 1069 |
svg, |
| 1070 |
) |
| 1071 |
.into_response()) |
| 1072 |
} |
| 1073 |
|
| 1074 |
#[cfg(test)] |
| 1075 |
mod section_tests { |
| 1076 |
use super::*; |
| 1077 |
|
| 1078 |
fn line(seq: i64, t: i64, text: &str) -> fdb::LogLine { |
| 1079 |
fdb::LogLine { seq, t, stream: "out".into(), text: text.into() } |
| 1080 |
} |
| 1081 |
|
| 1082 |
#[test] |
| 1083 |
fn marked_lines_fold_into_sections() { |
| 1084 |
let lines = vec![ |
| 1085 |
line(1, 0, "booting"), |
| 1086 |
line(2, 100, "::group::cargo build"), |
| 1087 |
line(3, 200, "Compiling a"), |
| 1088 |
line(4, 900, "Compiling b"), |
| 1089 |
line(5, 4_300, "::endgroup::"), |
| 1090 |
line(6, 4_400, "::group::cargo test"), |
| 1091 |
line(7, 4_500, "test x ... ok"), |
| 1092 |
line(8, 5_000, "::group::lint"), |
| 1093 |
line(9, 5_100, "clean"), |
| 1094 |
]; |
| 1095 |
let segs = sections(&lines); |
| 1096 |
assert_eq!(segs.len(), 4); |
| 1097 |
assert!(matches!(&segs[0], Segment::Line(l) if l.text == "booting")); |
| 1098 |
let Segment::Group(build) = &segs[1] else { panic!("a section") }; |
| 1099 |
assert_eq!((build.title.as_str(), build.from_t, build.to_t, build.lines.len()), ("cargo build", 100, Some(4_300), 2)); |
| 1100 |
let Segment::Group(test) = &segs[2] else { panic!("a section") }; |
| 1101 |
assert_eq!(test.to_t, Some(5_000), "a new section ends the one before"); |
| 1102 |
let Segment::Group(lint) = &segs[3] else { panic!("a section") }; |
| 1103 |
assert_eq!((lint.to_t, lint.lines.len()), (None, 1), "still going"); |
| 1104 |
assert_eq!(secs(4_200), "4.2s"); |
| 1105 |
} |
| 1106 |
} |