Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
feat(src/db.rs): Db::get_all_runs returns runs in timestamp order
Merged liw opened 1 year ago

Signed-off-by: Lars Wirzenius liw@liw.fi

feat! “cibtool run list” output changes

The default output now only lists the run id. With the –json option, more information about each run is returned, as JSON.

This is a breaking change because the output content and format of the command changes.

Signed-off-by: Lars Wirzenius liw@liw.fi

3 files changed +55 -10 285047d5 7a1f7a7a
modified ci-broker.md
@@ -229,8 +229,8 @@ given an installed cibtool
when I run cibtool --db ci-broker.db event list
then stdout is exactly ""

-
when I run cibtool --db ci-broker.db run list
-
then stdout contains "id: "xyzzy""
+
when I run cibtool --db ci-broker.db run list --json
+
then stdout contains ""run_id": "xyzzy""
~~~


@@ -657,8 +657,8 @@ then stderr contains "Action: shutdown"
when I run cibtool --db ci-broker.db event list
then stdout is exactly ""

-
when I run cibtool --db ci-broker.db run list
-
then stdout contains "Success"
+
when I run cibtool --db ci-broker.db run list --json
+
then stdout contains "success"
~~~


@@ -819,6 +819,6 @@ when I run cibtool --db x.db run list
then stdout is exactly ""

when I run cibtool --db x.db run add --id x --repo rad:zwTxygwuz5LDGBq255RA2CbNGrz8 --alias x --url https://x/1 --branch main --commit f1815dde6ae406d8fe3cec0b96c4486766342716 --who x --finished --failure --timestamp 2024-07-09T02:00:00
-
when I run cibtool --db x.db run list
+
when I run cibtool --db x.db run list --json
then stdout contains "rad:zwTxygwuz5LDGBq255RA2CbNGrz8"
~~~
modified src/bin/cibtool.rs
@@ -15,6 +15,7 @@ use std::{
};

use clap::Parser;
+
use serde::Serialize;

use radicle::{
    git::RefString,
@@ -30,7 +31,7 @@ use radicle_ci_broker::{
    event::BrokerEvent,
    msg::{RunId, RunResult},
    pages::{PageBuilder, PageError},
-
    run::{Run, Whence},
+
    run::{Run, RunState, Whence},
};

fn main() {
@@ -626,21 +627,64 @@ impl AddRun {
}

#[derive(Parser)]
-
struct ListRuns {}
+
struct ListRuns {
+
    #[clap(long)]
+
    json: bool,
+
}

impl ListRuns {
    #[allow(clippy::result_large_err)]
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        let db = args.open_db()?;

-
        for run in db.get_all_runs()? {
-
            println!("{} {run:#?}", run.adapter_run_id().unwrap());
+
        if self.json {
+
            let runs: Vec<RunInfo> = db.get_all_runs()?.iter().map(RunInfo::from).collect();
+
            println!("{}", serde_json::to_string_pretty(&runs).expect("JSON"));
+
        } else {
+
            for run in db.get_all_runs()? {
+
                println!(
+
                    "{}",
+
                    run.adapter_run_id()
+
                        .map(|id| id.to_string())
+
                        .unwrap_or("unknown".into())
+
                );
+
            }
        }

        Ok(())
    }
}

+
#[derive(Serialize)]
+
struct RunInfo {
+
    run_id: String,
+
    timestamp: String,
+
    repo_id: String,
+
    repo_alias: String,
+
    whence: Whence,
+
    info_url: Option<String>,
+
    state: RunState,
+
    result: Option<RunResult>,
+
}
+

+
impl From<&Run> for RunInfo {
+
    fn from(run: &Run) -> Self {
+
        RunInfo {
+
            run_id: run
+
                .adapter_run_id()
+
                .map(|id| id.to_string())
+
                .unwrap_or("unknown".into()),
+
            timestamp: run.timestamp().into(),
+
            repo_id: run.repo_id().to_string(),
+
            repo_alias: run.repo_alias().into(),
+
            whence: run.whence().clone(),
+
            info_url: run.adapter_info_url().map(|url| url.into()),
+
            state: run.state(),
+
            result: run.result().cloned(),
+
        }
+
    }
+
}
+

#[derive(Parser)]
struct ReportCmd {
    /// Write HTML files to this directory. The directory must exist:
modified src/db.rs
@@ -320,7 +320,7 @@ impl Db {
                        .stmt
                        .read("json")
                        .map_err(|e| DbError::get_run(&select.sql, e))?;
-
                    let run = serde_json::from_str(&json)
+
                    let run: Run = serde_json::from_str(&json)
                        .map_err(|e| DbError::run_from_json(&json, e))?;
                    runs.push(run);
                }
@@ -333,6 +333,7 @@ impl Db {
            }
        }

+
        runs.sort_by_cached_key(|run| run.timestamp().to_string());
        Ok(runs)
    }