Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
fix(src/pages.rs): use broker run id, always there
Merged liw opened 1 year ago

Co-worker reported that the unwrap of the optional adapter run id triggered a panic for them.

Each run has a unique broker run id, and an optional, non-unique adapter run id. Use the broker run id instead.

Actually, the whole PageBuilder type is now useless, but I’ll tackle that separately.

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

refactor: drop the PageBuilder type

After various previous changes, the PageBuilder::build now only calls StatusPage::new, so there’s no point in the type. Drop it.

Also replace StatusPage::new with StatusPage::default, for better style.

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

4 files changed +8 -69 84029fe1 e5873fd0
modified src/bin/cib.rs
@@ -19,7 +19,7 @@ use radicle_ci_broker::{
    db::{Db, DbError},
    logger,
    notif::NotificationChannel,
-
    pages::{PageBuilder, PageError},
+
    pages::StatusPage,
    queueadd::{AdderError, QueueAdderBuilder},
    queueproc::{QueueError, QueueProcessorBuilder},
};
@@ -164,12 +164,7 @@ impl QueuedCmd {
            .map_err(CibError::process_queue)?;

        let db = args.open_db(config)?;
-
        let mut page = PageBuilder::default()
-
            .node_alias(&profile.config.node.alias)
-
            .runs(db.get_all_runs().map_err(CibError::db)?)
-
            .build()
-
            .map_err(CibError::page_updater)?;
-

+
        let mut page = StatusPage::default();
        if let Some(dirname) = &config.report_dir {
            page.set_output_dir(dirname);
        }
@@ -204,12 +199,7 @@ impl ProcessEventsCmd {

        let db = args.open_db(config)?;

-
        let mut page = PageBuilder::default()
-
            .node_alias(&profile.config.node.alias)
-
            .runs(db.get_all_runs().map_err(CibError::db)?)
-
            .build()
-
            .map_err(CibError::page_updater)?;
-

+
        let mut page = StatusPage::default();
        if let Some(dirname) = &config.report_dir {
            page.set_output_dir(dirname);
        }
@@ -269,9 +259,6 @@ enum CibError {
    #[error("failed to use SQLite database")]
    Db(#[source] DbError),

-
    #[error("failed to create report page")]
-
    PageUpdater(#[source] PageError),
-

    #[error("failed create broker data type")]
    NewBroker(#[source] BrokerError),

@@ -309,10 +296,6 @@ impl CibError {
        Self::Db(e)
    }

-
    fn page_updater(e: PageError) -> Self {
-
        Self::PageUpdater(e)
-
    }
-

    fn new_broker(e: BrokerError) -> Self {
        Self::NewBroker(e)
    }
modified src/bin/cibtool.rs
@@ -33,7 +33,7 @@ use radicle_ci_broker::{
    event::BrokerEvent,
    msg::{RunId, RunResult},
    notif::NotificationChannel,
-
    pages::{PageBuilder, PageError},
+
    pages::PageError,
    run::{Run, RunState, Whence},
    util::{self, UtilError},
};
modified src/bin/cibtoolcmd/report.rs
@@ -1,3 +1,5 @@
+
use radicle_ci_broker::pages::StatusPage;
+

use super::*;

/// Produce HTML reports based on database contents.
@@ -15,11 +17,7 @@ impl Leaf for ReportCmd {

        let db = args.open_db()?;

-
        let mut page = PageBuilder::default()
-
            .node_alias(&profile.config.node.alias)
-
            .runs(db.get_all_runs()?)
-
            .build()?;
-

+
        let mut page = StatusPage::default();
        page.set_output_dir(&self.output_dir);

        let mut run_notification = NotificationChannel::default();
modified src/pages.rs
@@ -66,42 +66,6 @@ pub enum PageError {
    StatusToJson(#[source] serde_json::Error),
}

-
/// A builder for constructing a [`StatusPage`] value. It will only
-
/// construct a valid value.
-
#[derive(Default)]
-
pub struct PageBuilder {
-
    node_alias: Option<String>,
-
    runs: Vec<Run>,
-
    events: Vec<QueuedEvent>,
-
}
-

-
impl PageBuilder {
-
    pub fn node_alias(mut self, alias: &str) -> Self {
-
        self.node_alias = Some(alias.into());
-
        self
-
    }
-

-
    pub fn runs(mut self, runs: Vec<Run>) -> Self {
-
        self.runs = runs;
-
        self
-
    }
-

-
    pub fn events(mut self, events: Vec<QueuedEvent>) -> Self {
-
        self.events = events;
-
        self
-
    }
-

-
    pub fn build(self) -> Result<StatusPage, PageError> {
-
        let mut runs = HashMap::new();
-
        for run in self.runs.iter() {
-
            runs.insert(run.adapter_run_id().unwrap().clone(), run.clone());
-
        }
-
        logger::debug2(format!("broker database has {} CI runs", runs.len()));
-

-
        Ok(StatusPage::new())
-
    }
-
}
-

fn now() -> Result<String, time::error::Format> {
    let fmt = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]Z");
    OffsetDateTime::now_utc().format(fmt)
@@ -501,19 +465,13 @@ impl PageData {
/// of repositories for which the broker has run CI. Then there is a
/// page per such repository, with a list of CI runs for that
/// repository.
+
#[derive(Default)]
pub struct StatusPage {
    node_alias: String,
    dirname: Option<PathBuf>,
}

impl StatusPage {
-
    fn new() -> Self {
-
        Self {
-
            node_alias: String::new(),
-
            dirname: None,
-
        }
-
    }
-

    pub fn set_output_dir(&mut self, dirname: &Path) {
        self.dirname = Some(dirname.into());
    }