Skip to main content

runnerd_core/store/
run.rs

1//! The jobs of one workflow run that the history has, which a job action's confirmation names beside its job
2//! (ADR 0134). Reads only, a bounded number of jobs; it decides nothing about what GitHub's run holds.
3
4use rusqlite::{Connection, params};
5
6use super::read;
7use crate::api;
8
9/// Other jobs of a run the history has.
10#[derive(Debug, Clone, Default, PartialEq, Eq)]
11pub struct RunJobs {
12    /// Newest attempt first, then by ID.
13    pub jobs: Vec<api::JobSummary>,
14    /// Whether the history has more than `jobs`.
15    pub more: bool,
16}
17
18/// Up to `limit` jobs of `repository`'s run `run` but `except`, newest attempt first, and whether there are more.
19pub(super) fn run_jobs(
20    db: &Connection,
21    repository: &str,
22    run: u64,
23    except: u64,
24    limit: usize,
25) -> rusqlite::Result<RunJobs> {
26    let mut statement = db.prepare_cached(
27        "SELECT id FROM jobs WHERE repository = ?1 AND run_id = ?2 AND id != ?3 \
28         ORDER BY run_attempt DESC, id LIMIT ?4",
29    )?;
30    let ids = statement
31        .query_map(
32            params![
33                repository,
34                run,
35                except,
36                i64::try_from(limit).unwrap_or(i64::MAX).saturating_add(1)
37            ],
38            |row| row.get::<_, u64>(0),
39        )?
40        .collect::<rusqlite::Result<Vec<u64>>>()?;
41    let more = ids.len() > limit;
42    let mut jobs = Vec::new();
43    for id in ids.into_iter().take(limit) {
44        jobs.extend(read::summary(db, id)?);
45    }
46    Ok(RunJobs { jobs, more })
47}