Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
feat: add --json to cibtool event list
Merged liw opened 1 year ago

Add a –json option to allow “cibtool event list” to output JSON, for easier post-processing by other programs. Keep the –verbose, to avoid making a breaking change, but hide it from –help output.

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

4 files changed +36 -12 2c70b462 dd043e01
modified ci-broker.md
@@ -632,10 +632,10 @@ given file broker.yaml
when I try to run bash radenv.sh env RAD_SOCKET=synt.sock cib --config broker.yaml insert
then command is successful

-
when I run cibtool --db ci-broker.db event list --verbose
+
when I run cibtool --db ci-broker.db event list --json
then stdout contains "RefChanged"
-
then stdout contains "oid: Oid(0000000000000000000000000000000000000000)"
-
then stdout contains "old: Some(Oid(0000000000000000000000000000000000000000))"
+
then stdout contains ""oid": "0000000000000000000000000000000000000000""
+
then stdout contains ""old": "0000000000000000000000000000000000000000""
~~~

## Insert many events into queue
modified src/bin/cibtool.rs
@@ -29,7 +29,7 @@ use radicle_git_ext::Oid;

use radicle_ci_broker::{
    broker::BrokerError,
-
    db::{Db, DbError, QueueId},
+
    db::{Db, DbError, QueueId, QueuedEvent},
    event::BrokerEvent,
    msg::{RunId, RunResult},
    notif::NotificationChannel,
@@ -265,6 +265,9 @@ enum CibToolError {
    #[error("failed to serialize broker event to JSON: {0:#?}")]
    EventToJson(BrokerEvent, #[source] serde_json::Error),

+
    #[error("failed to serialize list of stored broker event to JSON")]
+
    EventListToJson(#[source] serde_json::Error),
+

    #[error("failed to serialize CI run information to JSON")]
    RunToJson(#[source] serde_json::Error),

modified src/bin/cibtoolcmd/event.rs
@@ -3,22 +3,42 @@ use super::*;
/// List events in the queue, waiting to be processed.
#[derive(Parser)]
pub struct ListEvents {
-
    /// Show more details about the event.
-
    #[clap(long)]
+
    /// Show more details about the event using Rust debug formatting.
+
    #[clap(long, hide = true)]
    verbose: bool,
+

+
    /// List events as JSON.
+
    #[clap(long)]
+
    json: bool,
}

impl Leaf for ListEvents {
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        let db = args.open_db()?;
-
        for id in db.queued_events()? {
-
            if self.verbose {
+
        let event_ids = db.queued_events()?;
+
        if self.json {
+
            let events: Result<Vec<QueuedEvent>, DbError> = event_ids
+
                .iter()
+
                .filter_map(|id| match db.get_queued_event(id) {
+
                    Ok(Some(event)) => Some(Ok(event)),
+
                    Err(e) => Some(Err(e)),
+
                    _ => None,
+
                })
+
                .collect();
+
            let events = events?;
+
            let json =
+
                serde_json::to_string_pretty(&events).map_err(CibToolError::EventListToJson)?;
+
            println!("{}", json);
+
        } else if self.verbose {
+
            for id in event_ids {
                if let Some(e) = db.get_queued_event(&id)? {
                    println!("{id}: {:?}", e);
                } else {
                    println!("{id}: No such event");
                }
-
            } else {
+
            }
+
        } else {
+
            for id in event_ids {
                println!("{id}");
            }
        }
modified src/db.rs
@@ -16,6 +16,7 @@ use std::{
    time::Duration,
};

+
use serde::{Deserialize, Serialize};
use sqlite::{Connection, State, Statement};
use time::{macros::format_description, OffsetDateTime};
use uuid::Uuid;
@@ -168,7 +169,7 @@ impl Db {
        Ok(counter)
    }

-
    /// Return list of broker events currently in the queue.
+
    /// Return list of identifiers for broker events currently in the queue.
    pub fn queued_events(&self) -> Result<Vec<QueueId>, DbError> {
        let mut select = self.prepare("SELECT id FROM event_queue")?;

@@ -485,7 +486,7 @@ impl<'a> Stmt<'a> {
}

/// An identifier for an event in the event queue in the database.
-
#[derive(Clone, Debug)]
+
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct QueueId {
    id: String,
}
@@ -522,7 +523,7 @@ impl QueueId {
    }
}

-
#[derive(Clone, Debug)]
+
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct QueuedEvent {
    id: QueueId,
    ts: String,