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