OXYGENHUB
# 27322cd2755a oxygenhub / crates / hub / src / web / flow.rs
45.7 KiB · Rust · last changed Run a pipeline's WASM jobs in the browser tab (refs andymccall/oxygenflow#80); runner builds shared with the swarm (refs andymccall/oxygenflow#84); 0.9.15 7 days ago source blame history raw
42fa9554 Andy McCall ↶ 1
//! The Flow tab (hub spec §13.2): runs of a repository, and a run's job
2
//! graph with live status and logs.
3
1edac4b6 Andy McCall ↶ 4
use super::settings::{csrf, require};
5
use super::{filters, lookup, lookup_role, Error, Page, RepoShell, Shell};
6
use crate::auth::{Access, Role, Viewer};
42fa9554 Andy McCall ↶ 7
use crate::db::{self, flow as fdb};
8
use crate::Shared;
9
use askama::Template;
1edac4b6 Andy McCall ↶ 10
use axum::extract::{Form, Path, Query, State};
11
use axum::response::{IntoResponse, Redirect, Response};
42fa9554 Andy McCall ↶ 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)>,
bf3d0754 Andy McCall ↶ 19
    /// "3rd in line" for a run still waiting for a runner.
20
    pub place: Option<String>,
42fa9554 Andy McCall ↶ 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>,
1edac4b6 Andy McCall ↶ 31
    /// Writers may start runs: the bookmarks to offer, default first.
32
    can_run: bool,
33
    bookmarks: Vec<String>,
34
    error: Option<String>,
4f692533 Andy McCall ↶ 35
    /// Why this viewer is not offered "Run pipeline", when they are not.
36
    why_not: Option<String>,
42fa9554 Andy McCall ↶ 37
}
38
39
#[derive(Deserialize)]
40
pub struct RunsQuery {
41
    before: Option<i64>,
1edac4b6 Andy McCall ↶ 42
    /// Why a "Run pipeline" or "Re-run" did not start.
43
    error: Option<String>,
42fa9554 Andy McCall ↶ 44
}
45
4f692533 Andy McCall ↶ 46
/// Why `viewer` may not start runs here, or `None` when they may.
47
pub fn why_not_run(enabled: bool, signed_in: bool, role: Role) -> Option<String> {
48
    if !enabled {
c7067c7b Andy McCall 49
        Some("Flow is off for this repository; a site administrator can turn it on in Settings › Pipelines.".into())
4f692533 Andy McCall ↶ 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
42fa9554 Andy McCall ↶ 59
const PER_PAGE: i64 = 30;
60
b644e175 Andy McCall ↶ 61
#[derive(Template)]
62
#[template(path = "deployments.html")]
63
pub struct DeploymentsPage {
64
    shell: Shell,
65
    repo: RepoShell,
66
    /// Each environment's newest deployment, by name.
67
    current: Vec<fdb::Deployment>,
68
    /// Every recent deployment, newest first.
69
    history: Vec<fdb::Deployment>,
70
}
71
72
/// `GET /{owner}/{name}/deployments`: what each environment runs
73
/// (oxygenflow#59).
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
42fa9554 Andy McCall ↶ 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);
bf3d0754 Andy McCall ↶ 102
    let queue = if list.iter().any(|r| r.live()) { fdb::queue(&app.db).await.unwrap_or_default() } else { vec![] };
42fa9554 Andy McCall ↶ 103
    let mut runs = Vec::new();
104
    for r in list.into_iter().take(PER_PAGE as usize) {
bf3d0754 Andy McCall ↶ 105
        let place = if r.live() { crate::flow::queue_info(&queue, row.id, r.id).place() } else { None };
42fa9554 Andy McCall ↶ 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
        };
bf3d0754 Andy McCall ↶ 112
        runs.push(RunRow { jobs: jobs.into_iter().map(|j| (j.name, j.status)).collect(), duration, r, place });
42fa9554 Andy McCall ↶ 113
    }
9008bf6c Andy McCall ↶ 114
    let enabled = crate::flow::enabled(&app, &row);
4f692533 Andy McCall ↶ 115
    let why_not = why_not_run(enabled, viewer.account().is_some(), role);
116
    let can_run = why_not.is_none();
1edac4b6 Andy McCall ↶ 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,
4f692533 Andy McCall ↶ 127
        why_not,
1edac4b6 Andy McCall ↶ 128
    }))
129
}
130
c26267b3 Andy McCall ↶ 131
/// A WASM job as the tab runner runs it (oxygenflow#80).
132
#[derive(serde::Serialize)]
133
pub struct TabJob {
134
    name: String,
135
    exec: Vec<String>,
136
    env: std::collections::BTreeMap<String, String>,
137
    memory_mib: u32,
138
}
139
140
#[derive(Template)]
141
#[template(path = "flow_tab.html")]
142
pub struct TabPage {
143
    shell: Shell,
144
    repo: RepoShell,
145
    bookmark: String,
146
    bookmarks: Vec<String>,
147
    /// The pipeline's WASM jobs, as JSON for the page's script.
148
    jobs_json: String,
149
    jobs: usize,
150
    /// Why there is nothing to run: no pipeline, one that does not read,
151
    /// or no WASM job in it.
152
    problem: Option<String>,
153
    archive: String,
154
}
155
156
#[derive(Deserialize)]
157
pub struct TabQuery {
158
    bookmark: Option<String>,
159
}
160
161
/// The WASM jobs of a pipeline (`runtime "wasm"`), each with the
162
/// environment its sandbox and itself give it.
163
pub fn wasm_jobs(p: &flow_core::Pipeline) -> Vec<TabJob> {
164
    p.jobs
165
        .values()
166
        .filter_map(|j| {
167
            let sb = p.sandboxes.get(&j.sandbox)?;
168
            (sb.runtime == flow_core::Runtime::Wasm && !j.exec.is_empty()).then(|| {
169
                let mut env = sb.env.clone();
170
                env.extend(j.env.clone());
171
                TabJob { name: j.name.clone(), exec: j.exec.clone(), env, memory_mib: sb.memory_mib }
172
            })
173
        })
174
        .collect()
175
}
176
177
/// `GET /{owner}/{name}/flow/tab`: run the pipeline's WASM jobs in this
178
/// browser tab (oxygenflow#80, first part): a worker unpacks the
179
/// bookmark's files and runs each job's modules with a WASI shim, as a
180
/// runner's WASM tier does. Nothing is claimed or reported: it shows the
181
/// tier works in a tab, before the tab becomes a runner.
182
pub async fn tab(
183
    State(app): State<Shared>,
184
    viewer: Viewer,
185
    Path((owner, name)): Path<(String, String)>,
186
    Query(q): Query<TabQuery>,
187
) -> Result<Page<TabPage>, Error> {
188
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/tab"), &viewer);
189
    let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
190
    let bookmarks = bookmark_names(&app, &row).await;
191
    let bookmark = q.bookmark.filter(|b| bookmarks.contains(b)).unwrap_or_else(|| row.default_bookmark.clone());
192
    let b = bookmark.clone();
193
    let text = app
194
        .repos
195
        .read(&repo, move |r, cache| {
196
            let Some(st) = r.view()?.bookmarks.get(&b).copied() else { return Ok(None) };
197
            let heads = r.store().get_state(&st)?.heads;
198
            let loaded = cache.load(r, &heads)?;
199
            Ok(match loaded.snapshot.files.get(flow_core::PIPELINE_PATH).map(|f| &f.content) {
200
                Some(oxy_core::state::FileContent::Text { bytes, .. }) => Some(String::from_utf8_lossy(bytes).into_owned()),
201
                _ => None,
202
            })
203
        })
204
        .await
205
        .unwrap_or(None);
206
    let (jobs, problem) = match text.map(|t| flow_core::Pipeline::parse(&t)) {
207
        None => (vec![], Some(format!("{bookmark} has no {}.", flow_core::PIPELINE_PATH))),
208
        Some(Err(e)) => (vec![], Some(format!("The pipeline on {bookmark} does not read: {e}"))),
209
        Some(Ok(p)) => {
210
            let jobs = wasm_jobs(&p);
211
            let problem =
212
                jobs.is_empty().then(|| format!("The pipeline on {bookmark} has no WASM jobs (a sandbox with runtime \"wasm\")."));
213
            (jobs, problem)
214
        }
215
    };
216
    Ok(Page(TabPage {
217
        shell,
218
        repo: RepoShell::new(&app, &row, "flow").with_role(role),
219
        archive: format!("/{owner}/{name}/archive?ref={}&format=zip", urlencoding(&bookmark)),
220
        bookmark,
221
        bookmarks,
222
        jobs: jobs.len(),
223
        jobs_json: serde_json::to_string(&jobs).unwrap_or_else(|_| "[]".into()),
224
        problem,
225
    }))
226
}
227
1edac4b6 Andy McCall ↶ 228
/// The repository's bookmarks, its default first.
229
async fn bookmark_names(app: &Shared, row: &db::repos::RepoRow) -> Vec<String> {
230
    let Some(repo) = crate::repos::RepoName::new(&row.owner, &row.name) else { return vec![] };
231
    let mut names: Vec<String> = app.repos.read(&repo, |r, _| Ok(r.view()?.bookmarks.keys().cloned().collect())).await.unwrap_or_default();
232
    names.sort_by_key(|n| (n != &row.default_bookmark, n.clone()));
233
    names
234
}
235
236
#[derive(Deserialize)]
237
pub struct StartForm {
238
    #[serde(rename = "_csrf")]
239
    csrf_token: String,
4f692533 Andy McCall ↶ 240
    #[serde(default)]
1edac4b6 Andy McCall ↶ 241
    bookmark: String,
4f692533 Andy McCall ↶ 242
    /// A change's full hash: run the state whose only head it is.
243
    #[serde(default)]
244
    change: String,
1edac4b6 Andy McCall ↶ 245
}
246
4f692533 Andy McCall ↶ 247
/// `POST /{owner}/{name}/flow/start`: "Run pipeline" on a bookmark's
248
/// current state, or on one committed change.
1edac4b6 Andy McCall ↶ 249
pub async fn start(
250
    State(app): State<Shared>,
251
    viewer: Viewer,
252
    Path((owner, name)): Path<(String, String)>,
253
    Form(f): Form<StartForm>,
254
) -> Result<Response, Error> {
255
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow"), &viewer);
256
    let me = require(&viewer, &shell)?;
257
    let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?;
258
    csrf(&viewer, &f.csrf_token, &shell)?;
4f692533 Andy McCall ↶ 259
    let (started, what) = if f.change.trim().is_empty() {
0650d94e Andy McCall ↶ 260
        (crate::flow::run_bookmark(&app, &row, f.bookmark.trim(), &me.account.username, &[]).await, f.bookmark.trim().to_string())
4f692533 Andy McCall ↶ 261
    } else {
0650d94e Andy McCall ↶ 262
        (crate::flow::run_change(&app, &row, &f.change, &me.account.username, &[]).await, format!("change {}", f.change.trim()))
4f692533 Andy McCall ↶ 263
    };
264
    Ok(match started {
1edac4b6 Andy McCall ↶ 265
        Ok(n) => {
4f692533 Andy McCall ↶ 266
            db::accounts::audit(&app.db, Some(&me.account), "flow.run", &format!("{owner}/{name}"), &format!("#{n} {what}")).await.ok();
1edac4b6 Andy McCall ↶ 267
            Redirect::to(&format!("/{owner}/{name}/flow/{n}")).into_response()
268
        }
269
        Err(msg) => Redirect::to(&format!("/{owner}/{name}/flow?error={}", urlencoding(&msg))).into_response(),
270
    })
271
}
272
273
#[derive(Deserialize)]
274
pub struct RerunForm {
275
    #[serde(rename = "_csrf")]
276
    csrf_token: String,
277
}
278
279
/// `POST /{owner}/{name}/flow/{number}/rerun`.
280
pub async fn rerun(
281
    State(app): State<Shared>,
282
    viewer: Viewer,
283
    Path((owner, name, number)): Path<(String, String, i64)>,
284
    Form(f): Form<RerunForm>,
285
) -> Result<Response, Error> {
286
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{number}"), &viewer);
287
    let me = require(&viewer, &shell)?;
288
    let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?;
289
    csrf(&viewer, &f.csrf_token, &shell)?;
290
    let run = fdb::run(&app.db, row.id, number)
291
        .await
292
        .map_err(|e| Error::from_db(shell.clone(), e))?
293
        .ok_or_else(|| Error::not_found(shell.clone()))?;
294
    Ok(match crate::flow::rerun(&app, &row, &run, &me.account.username).await {
295
        Ok(n) => {
296
            db::accounts::audit(&app.db, Some(&me.account), "flow.rerun", &format!("{owner}/{name}"), &format!("#{n} of #{number}"))
297
                .await
298
                .ok();
299
            Redirect::to(&format!("/{owner}/{name}/flow/{n}")).into_response()
300
        }
301
        Err(msg) => Redirect::to(&format!("/{owner}/{name}/flow?error={}", urlencoding(&msg))).into_response(),
302
    })
303
}
304
e8fa5369 Andy McCall ↶ 305
/// `POST /{owner}/{name}/flow/{number}/jobs/{job}/rerun` (oxygenflow#31).
306
pub async fn rerun_job(
307
    State(app): State<Shared>,
308
    viewer: Viewer,
309
    Path((owner, name, number, job)): Path<(String, String, i64, String)>,
310
    Form(f): Form<RerunForm>,
311
) -> Result<Response, Error> {
312
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{number}"), &viewer);
313
    let me = require(&viewer, &shell)?;
314
    let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?;
315
    csrf(&viewer, &f.csrf_token, &shell)?;
316
    let run = fdb::run(&app.db, row.id, number)
317
        .await
318
        .map_err(|e| Error::from_db(shell.clone(), e))?
319
        .ok_or_else(|| Error::not_found(shell.clone()))?;
320
    Ok(match crate::flow::rerun_job(&app, &row, &run, &job, &me.account.username).await {
321
        Ok(_) => {
322
            db::accounts::audit(&app.db, Some(&me.account), "flow.rerun", &format!("{owner}/{name}"), &format!("{job} of #{number}"))
323
                .await
324
                .ok();
325
            Redirect::to(&format!("/{owner}/{name}/flow/{number}")).into_response()
326
        }
327
        Err(msg) => Redirect::to(&format!("/{owner}/{name}/flow?error={}", urlencoding(&msg))).into_response(),
328
    })
329
}
330
745d225d Andy McCall ↶ 331
/// `POST /{owner}/{name}/flow/{number}/cancel` (oxygenflow#32).
332
pub async fn cancel(
333
    State(app): State<Shared>,
334
    viewer: Viewer,
335
    Path((owner, name, number)): Path<(String, String, i64)>,
336
    Form(f): Form<RerunForm>,
337
) -> Result<Response, Error> {
338
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{number}"), &viewer);
339
    let me = require(&viewer, &shell)?;
340
    let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?;
341
    csrf(&viewer, &f.csrf_token, &shell)?;
342
    let run = fdb::run(&app.db, row.id, number)
343
        .await
344
        .map_err(|e| Error::from_db(shell.clone(), e))?
345
        .ok_or_else(|| Error::not_found(shell.clone()))?;
346
    Ok(match crate::flow::cancel(&app, &row, &run, &me.account.username).await {
347
        Ok(()) => {
348
            db::accounts::audit(&app.db, Some(&me.account), "flow.cancel", &format!("{owner}/{name}"), &format!("#{number}")).await.ok();
349
            Redirect::to(&format!("/{owner}/{name}/flow/{number}")).into_response()
350
        }
351
        Err(msg) => Redirect::to(&format!("/{owner}/{name}/flow?error={}", urlencoding(&msg))).into_response(),
352
    })
353
}
354
107eaba5 Andy McCall ↶ 355
#[derive(Deserialize)]
356
pub struct ExpediteForm {
357
    #[serde(rename = "_csrf")]
358
    csrf_token: String,
359
    #[serde(default)]
360
    reason: String,
361
}
362
363
/// `POST /{owner}/{repo}/flow/{n}/expedite` (oxygenhub#244): a maintainer
364
/// raises a running or queued run to urgent, saying why (an incident's
365
/// fix, say). Audited and shown on the run; it lapses when the run ends.
366
pub async fn expedite(
367
    State(app): State<Shared>,
368
    viewer: Viewer,
369
    Path((owner, name, number)): Path<(String, String, i64)>,
370
    Form(f): Form<ExpediteForm>,
371
) -> Result<Response, Error> {
372
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{number}"), &viewer);
373
    let me = require(&viewer, &shell)?;
374
    let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?;
375
    csrf(&viewer, &f.csrf_token, &shell)?;
376
    let run = fdb::run(&app.db, row.id, number)
377
        .await
378
        .map_err(|e| Error::from_db(shell.clone(), e))?
379
        .ok_or_else(|| Error::not_found(shell.clone()))?;
b1860d60 Andy McCall ↶ 380
    if let Err(msg) = crate::flow::expedite(&app, &row, &run, &me.account, &f.reason).await {
76f93eae Andy McCall ↶ 381
        return Ok(Redirect::to(&format!("/{owner}/{name}/flow?error={}", urlencoding(&msg))).into_response());
107eaba5 Andy McCall ↶ 382
    }
383
    Ok(Redirect::to(&format!("/{owner}/{name}/flow/{number}")).into_response())
384
}
385
1edac4b6 Andy McCall ↶ 386
/// Percent-encode a message for a query string.
387
fn urlencoding(s: &str) -> String {
388
    s.bytes()
389
        .map(|b| match b {
390
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => (b as char).to_string(),
391
            _ => format!("%{b:02X}"),
392
        })
393
        .collect()
42fa9554 Andy McCall ↶ 394
}
395
396
/// A job placed in the graph.
397
pub struct Node {
398
    pub j: fdb::Job,
399
    pub x: i64,
400
    pub y: i64,
401
    pub duration: String,
402
}
403
404
pub struct Edge {
405
    pub d: String,
406
    pub status: String,
407
}
408
409
pub struct Graph {
410
    pub nodes: Vec<Node>,
411
    pub edges: Vec<Edge>,
412
    pub width: i64,
413
    pub height: i64,
414
}
415
416
pub const NODE_W: i64 = 200;
417
pub const NODE_H: i64 = 48;
418
const COL_GAP: i64 = 72;
419
const ROW_GAP: i64 = 18;
420
const PAD: i64 = 12;
421
422
/// Columns by dependency depth, jobs in each column in name order.
423
pub fn layout(jobs: Vec<fdb::Job>) -> Graph {
424
    let mut by_depth: std::collections::BTreeMap<i64, Vec<fdb::Job>> = Default::default();
425
    for j in jobs {
426
        by_depth.entry(j.depth).or_default().push(j);
427
    }
428
    let rows = by_depth.values().map(Vec::len).max().unwrap_or(0) as i64;
429
    let height = PAD * 2 + rows * NODE_H + (rows - 1).max(0) * ROW_GAP;
430
    let mut nodes = Vec::new();
431
    for (col, (_, mut js)) in by_depth.into_iter().enumerate() {
432
        js.sort_by(|a, b| a.name.cmp(&b.name));
433
        let n = js.len() as i64;
434
        let column_h = n * NODE_H + (n - 1).max(0) * ROW_GAP;
435
        let top = (height - column_h) / 2;
436
        for (i, j) in js.into_iter().enumerate() {
437
            let duration = crate::flow::duration(&j);
438
            nodes.push(Node { x: PAD + col as i64 * (NODE_W + COL_GAP), y: top + i as i64 * (NODE_H + ROW_GAP), duration, j });
439
        }
440
    }
441
    let mut edges = Vec::new();
442
    for n in &nodes {
443
        for d in n.j.depends_on() {
444
            if let Some(from) = nodes.iter().find(|m| m.j.name == d) {
445
                let (x1, y1) = (from.x + NODE_W, from.y + NODE_H / 2);
446
                let (x2, y2) = (n.x, n.y + NODE_H / 2);
447
                let mid = (x1 + x2) / 2;
448
                edges.push(Edge { d: format!("M{x1},{y1} C{mid},{y1} {mid},{y2} {x2},{y2}"), status: from.j.status.clone() });
449
            }
450
        }
451
    }
452
    let cols = nodes.iter().map(|n| n.x).max().map(|x| x + NODE_W + PAD).unwrap_or(PAD * 2);
453
    Graph { nodes, edges, width: cols, height: height.max(NODE_H + PAD * 2) }
454
}
455
456
pub fn glyph(status: &str) -> &'static str {
457
    match status {
458
        "passed" => "✓",
459
        "failed" => "✕",
460
        "running" => "●",
461
        "skipped" => "⤼",
745d225d Andy McCall ↶ 462
        "cancelled" => "⊘",
42fa9554 Andy McCall ↶ 463
        "error" => "!",
464
        _ => "○",
465
    }
466
}
467
0b203bbf Andy McCall ↶ 468
/// Failed tests a run page lists per job; the rest are counted.
469
const SHOWN_FAILURES: i64 = 50;
470
42fa9554 Andy McCall ↶ 471
pub struct JobLog {
472
    pub j: fdb::Job,
a3c8288b Andy McCall ↶ 473
    pub artifacts: Vec<fdb::Artifact>,
42fa9554 Andy McCall ↶ 474
    pub lines: Vec<fdb::LogLine>,
475
    pub duration: String,
476
    /// The last line shown; the live view asks for what follows.
477
    pub last_seq: i64,
554ccacf Andy McCall ↶ 478
    /// The job's coverage report, when it uploaded one (#113): its id and percentage.
479
    pub coverage: Option<(i64, String)>,
0b203bbf Andy McCall ↶ 480
    /// The job's test report, when it uploaded one (#114), and its first
481
    /// failures.
482
    pub tests: Option<crate::junit::Report>,
483
    pub failures: Vec<crate::junit::Case>,
42fa9554 Andy McCall ↶ 484
}
485
486
#[derive(Template)]
487
#[template(path = "flow_run.html")]
488
pub struct RunPage {
489
    shell: Shell,
490
    repo: RepoShell,
491
    run: fdb::Run,
492
    graph: Graph,
493
    logs: Vec<JobLog>,
494
    duration: String,
1edac4b6 Andy McCall ↶ 495
    /// Whether the viewer may re-run it.
496
    can_rerun: bool,
745d225d Andy McCall ↶ 497
    /// Whether the viewer may cancel it.
498
    can_cancel: bool,
107eaba5 Andy McCall ↶ 499
    /// May raise the run to urgent (oxygenhub#244): maintainers, while it runs.
500
    can_expedite: bool,
bf3d0754 Andy McCall ↶ 501
    /// Where a live run stands among the runners' work.
502
    queue: Option<crate::flow::QueueInfo>,
10708ab8 Andy McCall ↶ 503
    /// Releases a writer may attach this run's artifacts to.
504
    releases: Vec<db::releases::Release>,
417184c7 Andy McCall ↶ 505
    /// Jobs that wait, or waited, for a person (oxygenflow#57).
506
    approvals: Vec<ApprovalView>,
42fa9554 Andy McCall ↶ 507
}
508
417184c7 Andy McCall ↶ 509
/// A job's approval as the run page shows it.
510
pub struct ApprovalView {
511
    pub job: String,
512
    pub by: String,
513
    pub others_only: bool,
514
    /// `waiting`, `approved`, `rejected` or `expired`.
515
    pub state: &'static str,
516
    pub who: String,
517
    pub when_ms: i64,
518
    /// Whether the viewer may approve or reject it now, else why not.
519
    pub may: Result<(), String>,
47e37402 Andy McCall ↶ 520
    /// It takes an approval signed with the approver's key (#235): the
521
    /// command that signs it, and the approval's hash once sealed.
522
    pub sealed: bool,
523
    pub command: String,
524
    pub seal: String,
417184c7 Andy McCall ↶ 525
}
526
527
impl ApprovalView {
528
    pub fn may_decide(&self) -> bool {
529
        self.may.is_ok()
530
    }
531
532
    pub fn why_not(&self) -> String {
533
        self.may.clone().err().unwrap_or_default()
534
    }
535
}
536
42fa9554 Andy McCall ↶ 537
/// Lines of a job's log a page shows before "the rest is loaded as it comes".
538
const PAGE_LINES: i64 = 5000;
539
540
pub async fn run(
541
    State(app): State<Shared>,
542
    viewer: Viewer,
543
    Path((owner, name, id)): Path<(String, String, i64)>,
544
) -> Result<Page<RunPage>, Error> {
545
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{id}"), &viewer);
546
    let (_repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
547
    let run = fdb::run(&app.db, row.id, id)
548
        .await
549
        .map_err(|e| Error::from_db(shell.clone(), e))?
550
        .ok_or_else(|| Error::not_found(shell.clone()))?;
551
    let jobs = fdb::jobs(&app.db, run.id).await.map_err(|e| Error::from_db(shell.clone(), e))?;
554ccacf Andy McCall ↶ 552
    let reports = crate::coverage::for_run(&app.db, run.id).await.unwrap_or_default();
0b203bbf Andy McCall ↶ 553
    let test_reports = crate::junit::for_run(&app.db, run.id).await.unwrap_or_default();
42fa9554 Andy McCall ↶ 554
    let mut logs = Vec::new();
555
    for j in &jobs {
556
        let lines = fdb::log(&app.db, j.id, 0, PAGE_LINES).await.unwrap_or_default();
557
        let last_seq = lines.last().map(|l| l.seq).unwrap_or(0);
a3c8288b Andy McCall ↶ 558
        let artifacts = fdb::artifacts(&app.db, j.id).await.unwrap_or_default();
554ccacf Andy McCall ↶ 559
        let coverage = reports
560
            .iter()
40dd3f86 Andy McCall ↶ 561
            .find(|r| r.job_id == Some(j.id))
554ccacf Andy McCall ↶ 562
            .map(|r| (r.id, r.percent().map(|p| format!("{p:.1}%")).unwrap_or_else(|| "no lines".into())));
0b203bbf Andy McCall ↶ 563
        let tests = test_reports.iter().find(|r| r.job_id == j.id).cloned();
564
        let failures = match &tests {
565
            Some(r) if r.broken() > 0 => crate::junit::failures(&app.db, r.id, SHOWN_FAILURES).await.unwrap_or_default(),
566
            _ => vec![],
567
        };
568
        logs.push(JobLog { duration: crate::flow::duration(j), j: j.clone(), artifacts, lines, last_seq, coverage, tests, failures });
42fa9554 Andy McCall ↶ 569
    }
570
    let duration = match run.finished_ms {
571
        Some(f) => crate::flow::human_ms(f - run.created_ms),
572
        None => crate::flow::human_ms(db::now_ms() - run.created_ms),
573
    };
417184c7 Andy McCall ↶ 574
    let mut approvals = Vec::new();
575
    for j in jobs.iter().filter(|j| !j.approval.is_empty()) {
576
        let Some(a) = serde_json::from_str::<flow_core::Job>(&j.spec).ok().and_then(|s| s.approval) else { continue };
577
        let state = match (j.approval.as_str(), j.status.as_str()) {
578
            ("waiting", "queued") => "waiting",
579
            ("approved", _) => "approved",
580
            (_, "cancelled") if !j.approved_by.is_empty() => "rejected",
581
            (_, "cancelled") if j.error.starts_with("no approval within") => "expired",
582
            _ => continue,
583
        };
584
        let may = match (state, viewer.account()) {
585
            ("waiting", Some(me)) => crate::flow::may_decide(&app, &row, &run, j, me).await.map(|_| ()),
586
            ("waiting", None) => Err("sign in to approve".into()),
587
            _ => Err(String::new()),
588
        };
589
        approvals.push(ApprovalView {
47e37402 Andy McCall ↶ 590
            sealed: a.sealed,
591
            command: format!("oxy seal --flow {}/{owner}/{name}/flow/{} --job {}", app.config.base_url(), run.number, j.name),
592
            seal: j.approval_seal.clone(),
417184c7 Andy McCall ↶ 593
            job: j.name.clone(),
594
            by: a.by,
595
            others_only: a.others_only,
596
            state,
05f1a85a Andy McCall ↶ 597
            who: if j.approved_via.is_empty() { j.approved_by.clone() } else { format!("{} via {}", j.approved_by, j.approved_via) },
417184c7 Andy McCall ↶ 598
            when_ms: j.approved_ms.unwrap_or(0),
599
            may,
600
        });
601
    }
42fa9554 Andy McCall ↶ 602
    let graph = layout(jobs);
745d225d Andy McCall ↶ 603
    let can_write = viewer.account().is_some() && role >= Role::Write;
604
    let can_rerun = !run.live() && can_write && crate::flow::enabled(&app, &row);
605
    let can_cancel = run.live() && can_write;
107eaba5 Andy McCall ↶ 606
    let can_expedite = run.live() && run.priority.is_empty() && viewer.account().is_some() && role >= Role::Maintain;
bf3d0754 Andy McCall ↶ 607
    let queue = if run.live() {
608
        let q = fdb::queue(&app.db).await.unwrap_or_default();
609
        Some(crate::flow::queue_info(&q, row.id, run.id)).filter(|i| i.position.is_some())
610
    } else {
611
        None
10708ab8 Andy McCall ↶ 612
    };
613
    let releases = if viewer.account().is_some() && role >= Role::Write && logs.iter().any(|l| !l.artifacts.is_empty()) {
614
        db::releases::list(&app.db, row.id).await.unwrap_or_default()
615
    } else {
616
        vec![]
bf3d0754 Andy McCall ↶ 617
    };
10708ab8 Andy McCall ↶ 618
    Ok(Page(RunPage {
619
        shell,
620
        repo: RepoShell::new(&app, &row, "flow").with_role(role),
621
        run,
622
        graph,
623
        logs,
624
        duration,
625
        can_rerun,
745d225d Andy McCall ↶ 626
        can_cancel,
107eaba5 Andy McCall ↶ 627
        can_expedite,
10708ab8 Andy McCall ↶ 628
        queue,
629
        releases,
417184c7 Andy McCall ↶ 630
        approvals,
10708ab8 Andy McCall ↶ 631
    }))
417184c7 Andy McCall ↶ 632
}
633
634
#[derive(Deserialize)]
635
pub struct DecideForm {
636
    #[serde(rename = "_csrf")]
637
    csrf_token: String,
638
    /// `approve` or `reject`.
639
    action: String,
640
}
641
642
/// `POST /{owner}/{name}/flow/{id}/jobs/{job}/decide`: approve or reject
643
/// a job that waits for a person (oxygenflow#57).
644
pub async fn decide(
645
    State(app): State<Shared>,
646
    viewer: Viewer,
647
    Path((owner, name, number, job)): Path<(String, String, i64, String)>,
648
    Form(f): Form<DecideForm>,
649
) -> Result<Response, Error> {
650
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{number}"), &viewer);
651
    let me = require(&viewer, &shell)?;
652
    let (_, row, _) = lookup(&app, &shell, &viewer, &owner, &name).await?;
653
    csrf(&viewer, &f.csrf_token, &shell)?;
654
    let run = fdb::run(&app.db, row.id, number)
655
        .await
656
        .map_err(|e| Error::from_db(shell.clone(), e))?
657
        .ok_or_else(|| Error::not_found(shell.clone()))?;
658
    let approve = match f.action.as_str() {
659
        "approve" => true,
660
        "reject" => false,
661
        _ => return Err(Error::bad(shell, "approve or reject")),
662
    };
05f1a85a Andy McCall ↶ 663
    match crate::flow::decide(&app, &row, &run, &job, &me.account, approve, None).await {
417184c7 Andy McCall ↶ 664
        Ok(_) => Ok(Redirect::to(&format!("/{owner}/{name}/flow/{number}")).into_response()),
665
        Err(why) => Err(Error::bad(shell, why)),
666
    }
42fa9554 Andy McCall ↶ 667
}
668
669
#[derive(Deserialize)]
670
pub struct LiveQuery {
671
    /// `job:seq,job:seq`: the last log line each open job already shows.
672
    after: Option<String>,
673
}
674
675
/// `GET /{owner}/{name}/flow/{id}/live`: statuses and new log lines, for the
676
/// run page while it is live.
677
pub async fn live(
678
    State(app): State<Shared>,
679
    viewer: Viewer,
680
    Path((owner, name, id)): Path<(String, String, i64)>,
681
    Query(q): Query<LiveQuery>,
682
) -> Result<Response, Error> {
683
    let shell = Shell::new(&app, "", &viewer);
684
    let (_repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
685
    let run = fdb::run(&app.db, row.id, id)
686
        .await
687
        .map_err(|e| Error::from_db(shell.clone(), e))?
688
        .ok_or_else(|| Error::not_found(shell.clone()))?;
689
    let jobs = fdb::jobs(&app.db, run.id).await.map_err(|e| Error::from_db(shell.clone(), e))?;
690
    let after: std::collections::HashMap<i64, i64> = q
691
        .after
692
        .unwrap_or_default()
693
        .split(',')
694
        .filter_map(|p| p.split_once(':'))
695
        .filter_map(|(a, b)| Some((a.parse().ok()?, b.parse().ok()?)))
696
        .collect();
697
    let mut out = Vec::new();
698
    for j in &jobs {
699
        let lines = match after.get(&j.id) {
700
            Some(seq) => fdb::log(&app.db, j.id, *seq, 1000).await.unwrap_or_default(),
701
            None => vec![],
702
        };
703
        out.push(serde_json::json!({
704
            "id": j.id,
705
            "name": j.name,
706
            "status": j.status,
707
            "glyph": glyph(&j.status),
708
            "duration": crate::flow::duration(j),
709
            "exit_code": j.exit_code,
710
            "lines": lines.iter().map(|l| serde_json::json!({ "seq": l.seq, "t": l.t, "stream": l.stream, "text": l.text })).collect::<Vec<_>>(),
711
        }));
712
    }
713
    Ok(Json(serde_json::json!({ "status": run.status, "live": run.live(), "jobs": out })).into_response())
714
}
736f6813 Andy McCall ↶ 715
716
// ------------------------------------------------------------ state stamps
717
718
/// In rendered Markdown, the image path that is the viewed state's stamp.
719
pub const STAMP_IMAGE: &str = ".flow/stamp";
720
/// In rendered Markdown, the link path to the viewed state's run.
721
pub const RUN_LINK: &str = ".flow/run";
722
723
/// Resolve a ref to its state address, and whether the ref can move.
724
async fn state_of(app: &Shared, owner: &str, name: &str, r: &str) -> Result<(String, bool), crate::repos::RepoError> {
725
    let repo = crate::repos::RepoName::new(owner, name).ok_or(crate::repos::RepoError::NotFound)?;
726
    let spec = r.to_string();
727
    app.repos
728
        .read(&repo, move |repo, cache| {
729
            let t = crate::repos::view::resolve(repo, cache, &spec)?;
730
            let moves = matches!(t.kind, crate::repos::view::RefKind::Bookmark | crate::repos::view::RefKind::Tag);
731
            Ok((t.permalink().unwrap_or_default(), moves))
732
        })
733
        .await
734
}
735
736
/// `GET /{owner}/{name}/flow/stamp/{ref}` (hub spec §13.2).
737
pub async fn stamp(
738
    State(app): State<Shared>,
739
    viewer: Viewer,
740
    Path((owner, name, r)): Path<(String, String, String)>,
741
) -> Result<Response, Error> {
742
    let shell = Shell::new(&app, "", &viewer);
743
    let (_repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
744
    let (heads, moves) = state_of(&app, &owner, &name, &r).await.map_err(|e| Error::from_repo(shell.clone(), e))?;
745
    if moves && !heads.is_empty() {
746
        let mut res = axum::response::Redirect::to(&format!("/{owner}/{name}/flow/stamp/{heads}")).into_response();
747
        res.headers_mut().insert(axum::http::header::CACHE_CONTROL, axum::http::HeaderValue::from_static("no-cache"));
748
        return Ok(res);
749
    }
750
    let run = fdb::latest_for_heads(&app.db, row.id, &heads).await.map_err(|e| Error::from_db(shell.clone(), e))?;
751
    let jobs = match &run {
752
        Some(r) => fdb::jobs(&app.db, r.id).await.map_err(|e| Error::from_db(shell.clone(), e))?,
753
        None => vec![],
754
    };
755
    let svg = stamp_svg(&heads, run.as_ref(), &jobs);
756
    let cache = if run.as_ref().is_some_and(|r| !r.live()) { "public, max-age=3600" } else { "public, max-age=60" };
757
    Ok((
758
        [
759
            (axum::http::header::CONTENT_TYPE, "image/svg+xml; charset=utf-8"),
760
            (axum::http::header::CACHE_CONTROL, cache),
761
            (axum::http::header::CONTENT_SECURITY_POLICY, "default-src 'none'; style-src 'unsafe-inline'"),
762
        ],
763
        svg,
764
    )
765
        .into_response())
766
}
767
768
/// `GET /{owner}/{name}/flow/at/{ref}`: the run of the ref's state, or the Flow tab.
769
pub async fn at(
770
    State(app): State<Shared>,
771
    viewer: Viewer,
772
    Path((owner, name, r)): Path<(String, String, String)>,
773
) -> Result<Response, Error> {
774
    let shell = Shell::new(&app, "", &viewer);
775
    let (_repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
776
    let (heads, _) = state_of(&app, &owner, &name, &r).await.map_err(|e| Error::from_repo(shell.clone(), e))?;
777
    let run = fdb::latest_for_heads(&app.db, row.id, &heads).await.map_err(|e| Error::from_db(shell.clone(), e))?;
778
    let to = match run {
a0d4bbfb Andy McCall ↶ 779
        Some(r) => format!("/{owner}/{name}/flow/{}", r.number),
736f6813 Andy McCall ↶ 780
        None => format!("/{owner}/{name}/flow"),
781
    };
782
    Ok(axum::response::Redirect::to(&to).into_response())
783
}
784
785
fn xml(s: &str) -> String {
786
    s.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;").replace('"', "&quot;")
787
}
788
789
/// The stamp: the mark, one glyph per job, the status, the state and the
790
/// date; the same facts machine-readable in `<metadata>`. Fixed colours,
791
/// because an image does not follow the page's theme.
792
pub fn stamp_svg(heads: &str, run: Option<&fdb::Run>, jobs: &[fdb::Job]) -> String {
793
    const CH: f64 = 6.6; // IBM Plex Mono at 11px
794
    let colour = |s: &str| match s {
795
        "passed" => "#5fe3a1",
796
        "failed" | "error" => "#ff5e7a",
797
        "running" => "#00e0f0",
798
        _ => "#8592ad",
799
    };
800
    let short: String = heads.chars().take(12).collect();
801
    let (status, date) = match run {
802
        Some(r) => (r.status.as_str(), crate::render::time::absolute(r.created_ms as u64).chars().take(10).collect::<String>()),
803
        None => ("no run", String::new()),
804
    };
805
    let mut ordered: Vec<&fdb::Job> = jobs.iter().collect();
806
    ordered.sort_by(|a, b| (a.depth, &a.name).cmp(&(b.depth, &b.name)));
807
    let label = "OXYGENFLOW";
808
    let label_w = 14.0 + label.len() as f64 * 6.2 + 8.0;
809
    let glyphs_w = if ordered.is_empty() { 0.0 } else { ordered.len() as f64 * 10.0 + 6.0 };
810
    let tail = if date.is_empty() { format!("{status} · {short}") } else { format!("{status} · {short} · {date}") };
811
    let right_w = 8.0 + glyphs_w + tail.chars().count() as f64 * CH + 8.0;
812
    let w = (label_w + right_w).ceil();
813
    let mut glyphs = String::new();
814
    for (i, j) in ordered.iter().enumerate() {
815
        let x = label_w + 8.0 + i as f64 * 10.0 + 4.0;
816
        glyphs.push_str(&format!(
817
            r#"<text x="{x:.1}" y="15" fill="{}" text-anchor="middle">{}</text>"#,
818
            colour(&j.status),
819
            glyph(&j.status)
820
        ));
821
    }
822
    let meta = serde_json::json!({
823
        "heads": heads.split('+').filter(|h| !h.is_empty()).collect::<Vec<_>>(),
a0d4bbfb Andy McCall ↶ 824
        "run": run.map(|r| r.number),
736f6813 Andy McCall ↶ 825
        "status": status,
826
        "jobs": ordered.iter().map(|j| serde_json::json!({ "name": j.name, "status": j.status, "receipt": j.receipt_hash })).collect::<Vec<_>>(),
827
    });
828
    let title = match run {
a0d4bbfb Andy McCall ↶ 829
        Some(r) => format!("OxygenFlow run #{} {status} on state {short}", r.number),
736f6813 Andy McCall ↶ 830
        None => format!("OxygenFlow: no run for state {short}"),
831
    };
832
    format!(
833
        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>"##,
834
        t = xml(&title),
835
        m = xml(&meta.to_string()),
836
        lw = label_w,
837
        lx = label_w - 4.0,
838
        tx = label_w + 8.0 + glyphs_w,
839
        sc = colour(status),
840
        tail = xml(&tail),
841
        wi = w - 1.0,
842
    )
843
}
cc19697f Andy McCall ↶ 844
845
/// A receipt kept in a repository (Oxygen spec §14.6), as pages show it.
846
#[derive(Clone, Debug)]
847
pub struct ReceiptRow {
848
    pub attest: String,
849
    pub job: String,
850
    pub exit_code: u32,
851
    pub duration: String,
852
    pub runner: String,
853
    pub run: String,
854
    /// Why it does not verify, or that its runner's key was revoked.
855
    pub problem: String,
856
    pub made_ms: i64,
857
}
858
859
impl ReceiptRow {
860
    pub fn passed(&self) -> bool {
861
        self.exit_code == 0
862
    }
863
}
864
865
/// The receipts kept for the state of `heads`.
866
pub async fn receipts_of(app: &Shared, name: &crate::repos::RepoName, heads: Vec<oxy_core::hash::ChangeHash>) -> Vec<ReceiptRow> {
867
    let base = app.config.base_url();
868
    let found = app.repos.read(name, move |repo, _| Ok(repo.receipts(&heads)?)).await.unwrap_or_default();
869
    found
870
        .into_iter()
871
        .map(|i| {
872
            let r = &i.signed.receipt;
873
            let run = i.signed.run().to_string();
874
            ReceiptRow {
875
                attest: i.attest.to_hex(),
876
                job: r.job_name.clone(),
877
                exit_code: r.exit_code,
878
                duration: crate::flow::human_ms(r.duration_ms as i64),
879
                runner: i.signed.runner.name.clone(),
880
                // Only links to this hub are followed from the page.
881
                run: if run.starts_with(base) { run } else { String::new() },
882
                problem: match (i.problem, i.revoked) {
883
                    (Some(p), _) => p,
884
                    (None, true) => "the runner's key was revoked".into(),
885
                    (None, false) => String::new(),
886
                },
887
                made_ms: i.signed.attest.hlc.wall_ms as i64,
888
            }
889
        })
890
        .collect()
891
}
892
893
/// `GET /{owner}/{name}/receipts/{attest}`: a signed receipt, for
894
/// `oxy receipt verify` or `oxy receipt import`.
895
pub async fn receipt_file(
896
    State(app): State<Shared>,
897
    viewer: Viewer,
898
    Path((owner, name, attest)): Path<(String, String, String)>,
899
) -> Result<Response, Error> {
900
    let shell = Shell::new(&app, &format!("/{owner}/{name}/receipts/{attest}"), &viewer);
901
    let (repo, _row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
902
    let Some(r) = oxy_core::hash::ObjectRef::from_hex(attest.trim_end_matches(".receipt")) else {
903
        return Err(Error::not_found(shell));
904
    };
905
    let (job, bytes) = app
906
        .repos
907
        .read(&repo, move |repo, _| {
908
            let i = repo.receipt(&r)?;
909
            Ok((i.signed.receipt.job_name.clone(), i.signed.encode()))
910
        })
911
        .await
912
        .map_err(|_| Error::not_found(shell.clone()))?;
913
    let file: String = job.chars().filter(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.')).collect();
914
    Ok((
915
        [
916
            (axum::http::header::CONTENT_TYPE, "application/oxy-receipt".to_string()),
917
            (axum::http::header::CONTENT_DISPOSITION, format!("attachment; filename=\"{file}-{}.receipt\"", r.short(12))),
a3c8288b Andy McCall ↶ 918
        ],
919
        bytes,
920
    )
921
        .into_response())
922
}
923
924
/// `GET /{owner}/{name}/flow/{id}/artifacts/{job}/{artifact}`: an artifact
925
/// of a run's job, read from the repository.
926
pub async fn artifact_file(
927
    State(app): State<Shared>,
928
    viewer: Viewer,
929
    Path((owner, name, id, job, artifact)): Path<(String, String, i64, String, String)>,
930
) -> Result<Response, Error> {
931
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{id}/artifacts/{job}/{artifact}"), &viewer);
932
    let (repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
933
    let run = fdb::run(&app.db, row.id, id)
934
        .await
935
        .map_err(|e| Error::from_db(shell.clone(), e))?
936
        .ok_or_else(|| Error::not_found(shell.clone()))?;
937
    let jobs = fdb::jobs(&app.db, run.id).await.map_err(|e| Error::from_db(shell.clone(), e))?;
938
    let Some(j) = jobs.iter().find(|j| j.name == job) else { return Err(Error::not_found(shell)) };
939
    let arts = fdb::artifacts(&app.db, j.id).await.map_err(|e| Error::from_db(shell.clone(), e))?;
940
    let Some(a) = arts.into_iter().find(|a| a.name == artifact) else { return Err(Error::not_found(shell)) };
941
    let Some(blob) = oxy_core::hash::ObjectRef::from_hex(&a.blob) else { return Err(Error::not_found(shell)) };
942
    let bytes = app.repos.read(&repo, move |r, _| Ok(r.read_blob(&blob)?)).await.map_err(|_| Error::not_found(shell.clone()))?;
943
    let file = a.path.rsplit('/').next().unwrap_or(&a.name).to_string();
944
    Ok((
945
        [
946
            (axum::http::header::CONTENT_TYPE, "application/octet-stream".to_string()),
947
            (axum::http::header::CONTENT_DISPOSITION, format!("attachment; filename=\"{}\"", file.replace('"', ""))),
cc19697f Andy McCall ↶ 948
        ],
949
        bytes,
950
    )
951
        .into_response())
10708ab8 Andy McCall ↶ 952
}
953
076c7a7c Andy McCall ↶ 954
/// `GET …/artifacts/{job}/{artifact}/provenance` (oxygenhub#239): the
955
/// artifact's SLSA provenance in a DSSE envelope signed by the hub, for
956
/// `cosign verify-blob-attestation --key oxyhub-signing.pem --type
957
/// slsaprovenance1`. Only for a job whose signed receipt the hub kept.
958
pub async fn provenance(
959
    State(app): State<Shared>,
960
    viewer: Viewer,
961
    Path((owner, name, id, job, artifact)): Path<(String, String, i64, String, String)>,
962
) -> Result<Response, Error> {
963
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{id}/artifacts/{job}/{artifact}/provenance"), &viewer);
964
    let (repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
965
    let run = fdb::run(&app.db, row.id, id)
966
        .await
967
        .map_err(|e| Error::from_db(shell.clone(), e))?
968
        .ok_or_else(|| Error::not_found(shell.clone()))?;
969
    let jobs = fdb::jobs(&app.db, run.id).await.map_err(|e| Error::from_db(shell.clone(), e))?;
970
    let Some(j) = jobs.iter().find(|j| j.name == job) else { return Err(Error::not_found(shell)) };
971
    if j.receipt_attest.is_empty() {
972
        return Err(Error::bad(shell, "This job has no signed receipt the hub kept, so the hub states no provenance for it.".to_string()));
973
    }
974
    let arts = fdb::artifacts(&app.db, j.id).await.map_err(|e| Error::from_db(shell.clone(), e))?;
975
    let Some(a) = arts.into_iter().find(|a| a.name == artifact) else { return Err(Error::not_found(shell)) };
976
    let Some(blob) = oxy_core::hash::ObjectRef::from_hex(&a.blob) else { return Err(Error::not_found(shell)) };
977
    let bytes = app.repos.read(&repo, move |r, _| Ok(r.read_blob(&blob)?)).await.map_err(|_| Error::not_found(shell.clone()))?;
978
    let file = a.path.rsplit('/').next().unwrap_or(&a.name).to_string();
979
    let base = app.config.base_url();
980
    let subject = crate::provenance::Subject {
981
        name: &file,
982
        bytes: &bytes,
983
        repo: &format!("{owner}/{name}"),
984
        run_url: &format!("{base}/{owner}/{name}/flow/{id}"),
985
        bookmark: &run.bookmark,
986
        state: &run.state,
987
        heads: &run.heads,
988
        pipeline_hash: &run.pipeline_hash,
989
        job: &j.name,
990
        runner: &j.runner,
991
        receipt_signer: &j.receipt_signer,
992
        receipt_attest: &j.receipt_attest,
993
        started_ms: j.started_ms,
994
        finished_ms: j.finished_ms,
995
    };
996
    let statement = crate::provenance::statement(base, &subject);
997
    let envelope = crate::provenance::envelope(&app, &statement).map_err(|e| Error::bad(shell.clone(), e.to_string()))?;
998
    Ok((
999
        [
1000
            (axum::http::header::CONTENT_TYPE, "application/json".to_string()),
1001
            (axum::http::header::CONTENT_DISPOSITION, format!("attachment; filename=\"{}.intoto.json\"", file.replace('"', ""))),
1002
        ],
1003
        serde_json::to_string(&envelope).unwrap_or_default(),
1004
    )
1005
        .into_response())
1006
}
1007
1008
/// `GET /.well-known/oxyhub-signing.pem`: the hub's Ed25519 public key, as
1009
/// cosign and other verifiers take it (oxygenhub#239).
1010
pub async fn signing_key(State(app): State<Shared>) -> Response {
1011
    ([(axum::http::header::CONTENT_TYPE, "application/x-pem-file")], crate::provenance::public_pem(&app)).into_response()
1012
}
1013
10708ab8 Andy McCall ↶ 1014
#[derive(Deserialize)]
1015
pub struct AttachForm {
1016
    _csrf: String,
1017
    release: i64,
1018
}
1019
1020
/// `POST /{owner}/{name}/flow/{id}/artifacts/{job}/{artifact}/attach`: add
1021
/// an artifact to a release as an asset, named after its path; the release
1022
/// page then shows the run and receipt that built it.
1023
pub async fn attach(
1024
    State(app): State<Shared>,
1025
    viewer: Viewer,
1026
    Path((owner, name, id, job, artifact)): Path<(String, String, i64, String, String)>,
1027
    Form(f): Form<AttachForm>,
1028
) -> Result<Response, Error> {
1029
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{id}"), &viewer);
1030
    require(&viewer, &shell)?;
1031
    csrf(&viewer, &f._csrf, &shell)?;
1032
    let (repo, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?;
1033
    let run = fdb::run(&app.db, row.id, id)
1034
        .await
1035
        .map_err(|e| Error::from_db(shell.clone(), e))?
1036
        .ok_or_else(|| Error::not_found(shell.clone()))?;
1037
    let jobs = fdb::jobs(&app.db, run.id).await.map_err(|e| Error::from_db(shell.clone(), e))?;
1038
    let Some(j) = jobs.iter().find(|j| j.name == job) else { return Err(Error::not_found(shell)) };
6f52a041 Andy McCall ↶ 1039
    // A failing job's `when "always"` artifacts are not in its receipt, so
1040
    // they cannot stand for a release (oxygenflow#37).
1041
    if j.status != "passed" {
1042
        return Err(Error::bad(shell, "only a passing job's artifacts can be attached to a release"));
1043
    }
10708ab8 Andy McCall ↶ 1044
    let arts = fdb::artifacts(&app.db, j.id).await.map_err(|e| Error::from_db(shell.clone(), e))?;
1045
    let Some(a) = arts.into_iter().find(|a| a.name == artifact) else { return Err(Error::not_found(shell)) };
1046
    let releases = db::releases::list(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?;
1047
    let Some(rel) = releases.into_iter().find(|r| r.id == f.release) else { return Err(Error::not_found(shell)) };
1048
    let Some(blob) = oxy_core::hash::ObjectRef::from_hex(&a.blob) else { return Err(Error::not_found(shell)) };
1049
    let bytes = app.repos.read(&repo, move |r, _| Ok(r.read_blob(&blob)?)).await.map_err(|e| Error::from_repo(shell.clone(), e))?;
1050
    let file = a.path.rsplit('/').next().unwrap_or(&a.name).to_string();
1051
    super::releases::store_asset(&app, rel.id, &file, "application/octet-stream", &bytes)
1052
        .await
1053
        .map_err(|m| Error::bad(shell.clone(), m))?;
1054
    Ok(Redirect::to(&format!("/{owner}/{name}/releases/tag/{}", rel.tag)).into_response())
cc19697f Andy McCall ↶ 1055
}
8933faff Andy McCall ↶ 1056
1057
/// `/{owner}/{name}/flow/badge.svg`: the latest run on the default
1058
/// bookmark as a small SVG for a README, for whoever may read the
1059
/// repository. `passed` green, `failed` or `error` red, a run still going
1060
/// blue, none grey.
1061
pub async fn badge(
1062
    State(app): State<Shared>,
1063
    viewer: Viewer,
1064
    Path((owner, name)): Path<(String, String)>,
1065
) -> Result<axum::response::Response, Error> {
1066
    use axum::response::IntoResponse;
1067
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow"), &viewer);
1068
    let (_repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
1069
    let latest = fdb::latest_on(&app.db, row.id, &row.default_bookmark).await.map_err(|e| Error::from_db(shell.clone(), e))?;
1070
    let (text, colour) = match latest.as_ref().map(|r| r.status.as_str()) {
1071
        Some("passed") => ("passing", "#2da44e"),
1072
        Some("failed") | Some("error") => ("failing", "#cf222e"),
1073
        Some("cancelled") => ("cancelled", "#6e7781"),
1074
        Some(_) => ("running", "#0969da"),
1075
        None => ("no runs", "#6e7781"),
1076
    };
1077
    let label = "OxygenFlow";
1078
    let (lw, tw) = (label.len() as u32 * 7 + 10, text.len() as u32 * 7 + 10);
1079
    let svg = format!(
1080
        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>"##,
1081
        w = lw + tw,
1082
        lx = lw / 2,
1083
        tx = lw + tw / 2,
1084
    );
1085
    Ok((
1086
        [(axum::http::header::CONTENT_TYPE, "image/svg+xml; charset=utf-8"), (axum::http::header::CACHE_CONTROL, "no-cache, max-age=60")],
1087
        svg,
1088
    )
1089
        .into_response())
1090
}