Radish alpha
r
rad:z3qg5TKmN83afz2fj9z3fQjU8vaYE
Radicle CI adapter for native CI
Radicle
Git
refactor: drop use of RunInfo
Lars Wirzenius committed 2 years ago
commit 8bd8b9b21b3b088738ede51967374153b0c43612
parent 2fa62ab
5 files changed +12 -160
modified src/engine.rs
@@ -13,7 +13,6 @@ use crate::{
        NativeMessageError,
    },
    run::{Run, RunError},
-
    runinfo::{RunInfo, RunInfoBuilder, RunInfoError},
    runlog::RunLogError,
    runspec::RunSpecError,
};
@@ -23,7 +22,7 @@ use crate::{
pub struct Engine {
    config: Config,
    adminlog: AdminLog,
-
    run_info_builder: RunInfoBuilder,
+
    result: Option<RunResult>,
}

impl Engine {
@@ -45,7 +44,7 @@ impl Engine {
        Ok(Self {
            config,
            adminlog,
-
            run_info_builder: RunInfo::builder(),
+
            result: None,
        })
    }

@@ -70,8 +69,6 @@ impl Engine {
                let repo = req.repo();
                let commit = req.commit();

-
                self.run_info_builder.repo(repo);
-
                self.run_info_builder.commit(commit);
                match self.run_helper(repo, commit) {
                    Ok(true) => success = true,
                    Ok(false) => (),
@@ -121,18 +118,11 @@ impl Engine {
    #[allow(clippy::result_large_err)]
    fn finish(&mut self) -> Result<(), EngineError> {
        // Write response message indicating the run has finished.
-
        let ri = self.run_info_builder.build()?;
-
        match &ri.result {
-
            RunResult::Success => write_succeeded()?,
-
            RunResult::Failure => write_failed()?,
-
            RunResult::Error(s) => write_errored(s)?,
-
            _ => write_errored(&format!("unknown result {}", ri.result))?,
-
        }
-

-
        // Persist the run info. If this fails, it's a node admin problem.
-
        if let Err(e) = ri.write() {
-
            self.adminlog
-
                .writeln(&format!("failed to write run info: {}", e))?;
+
        match &self.result {
+
            Some(RunResult::Success) => write_succeeded()?,
+
            Some(RunResult::Failure) => write_failed()?,
+
            Some(RunResult::Error(s)) => write_errored(s)?,
+
            _ => write_errored(&format!("unknown result {:?}", self.result))?,
        }

        // Log that we've reached the end successfully.
@@ -153,10 +143,6 @@ impl Engine {
        // Set the file where the run info should be written, now that
        // we have the run directory.
        let run_log_filename = run_dir.join("log.html");
-
        self.run_info_builder
-
            .log(&self.config.state, run_log_filename.clone());
-
        self.run_info_builder.run_info(run_dir.join("run.yaml"));
-
        self.run_info_builder.run_id(run_id.clone());

        // Get node git storage. We do this before responding to the
        // trigger, so that if this fails, we haven't said that a run
@@ -178,16 +164,15 @@ impl Engine {
        // succeeded or failed.
        let result = run.run();
        if let Ok(run_log) = result {
-
            let result = if run_log.all_commands_succeeded() {
-
                RunResult::Success
+
            self.result = if run_log.all_commands_succeeded() {
+
                Some(RunResult::Success)
            } else {
-
                RunResult::Failure
+
                Some(RunResult::Failure)
            };
-
            self.run_info_builder.result(result);
            let all = run_log.all_commands_succeeded();
            Ok(all)
        } else {
-
            self.run_info_builder.result(RunResult::Failure);
+
            self.result = Some(RunResult::Failure);
            Ok(false)
        }
    }
@@ -239,9 +224,6 @@ pub enum EngineError {
    Message(#[from] NativeMessageError),

    #[error(transparent)]
-
    RunInfo(#[from] RunInfoError),
-

-
    #[error(transparent)]
    RunLog(#[from] RunLogError),

    #[error(transparent)]
modified src/lib.rs
@@ -3,6 +3,5 @@ pub mod engine;
pub mod logfile;
pub mod msg;
pub mod run;
-
pub mod runinfo;
pub mod runlog;
pub mod runspec;
modified src/msg.rs
@@ -2,7 +2,7 @@ use std::path::PathBuf;

use radicle_ci_broker::msg::{MessageError, Request, Response, RunId, RunResult};

-
use crate::{config::ConfigError, logfile::LogError, runinfo::RunInfoError, runspec::RunSpecError};
+
use crate::{config::ConfigError, logfile::LogError, runspec::RunSpecError};

/// Read a request from stdin.
pub fn read_request() -> Result<Request, NativeMessageError> {
@@ -73,8 +73,5 @@ pub enum NativeMessageError {
    Log(#[from] LogError),

    #[error(transparent)]
-
    RunInfo(#[from] RunInfoError),
-

-
    #[error(transparent)]
    RunSpec(#[from] RunSpecError),
}
modified src/run.rs
@@ -7,7 +7,6 @@ use radicle_ci_broker::msg::{Oid, RepoId};

use crate::{
    msg::NativeMessageError,
-
    runinfo::RunInfoError,
    runlog::{RunLog, RunLogError},
    runspec::{RunSpec, RunSpecError},
};
@@ -221,9 +220,6 @@ pub enum RunError {
    Message(#[from] NativeMessageError),

    #[error(transparent)]
-
    RunInfo(#[from] RunInfoError),
-

-
    #[error(transparent)]
    RunLog(#[from] RunLogError),

    #[error(transparent)]
deleted src/runinfo.rs
@@ -1,122 +0,0 @@
-
use std::path::{Path, PathBuf};
-

-
use serde::{Deserialize, Serialize};
-
use time::{macros::format_description, OffsetDateTime};
-

-
use radicle_ci_broker::msg::{Oid, RepoId, RunId, RunResult};
-

-
/// Metadata about a run.
-
#[derive(Debug, Serialize, Deserialize)]
-
#[serde(deny_unknown_fields)]
-
#[allow(dead_code)]
-
pub struct RunInfo {
-
    /// Repository ID.
-
    pub repo: String,
-

-
    /// Commit ID.
-
    pub commit: String,
-

-
    /// Identifier for the CI run.
-
    pub id: String,
-

-
    /// Result of the CI run.
-
    pub result: RunResult,
-

-
    /// Name of log file.
-
    pub log: PathBuf,
-

-
    /// Name of run info file.
-
    #[serde(skip)]
-
    pub run_info: Option<PathBuf>,
-

-
    /// Timestamp of when the run ended (the value was created).
-
    /// ISO8601 format.
-
    pub timestamp: String,
-
}
-

-
impl RunInfo {
-
    pub fn builder() -> RunInfoBuilder {
-
        RunInfoBuilder::default()
-
    }
-

-
    pub fn write(&self) -> Result<(), RunInfoError> {
-
        if let Some(filename) = &self.run_info {
-
            let yaml = serde_yaml::to_string(&self).map_err(RunInfoError::SerializeRunInfo)?;
-
            std::fs::write(filename, yaml.as_bytes())
-
                .map_err(|e| RunInfoError::WriteRunInfo(filename.into(), e))?;
-
        }
-
        Ok(())
-
    }
-
}
-

-
#[derive(Debug, Default)]
-
#[allow(dead_code)]
-
pub struct RunInfoBuilder {
-
    repo: Option<RepoId>,
-
    commit: Option<Oid>,
-
    run_id: Option<String>,
-
    result: Option<RunResult>,
-
    log: Option<PathBuf>,
-
    run_info: Option<PathBuf>,
-
}
-

-
impl RunInfoBuilder {
-
    pub fn repo(&mut self, repo: RepoId) {
-
        self.repo = Some(repo);
-
    }
-

-
    pub fn commit(&mut self, commit: Oid) {
-
        self.commit = Some(commit);
-
    }
-

-
    pub fn run_id(&mut self, id: RunId) {
-
        self.run_id = Some(format!("{}", id));
-
    }
-

-
    pub fn result(&mut self, result: RunResult) {
-
        self.result = Some(result);
-
    }
-

-
    pub fn log(&mut self, state: &Path, log: PathBuf) {
-
        self.log = Some(log.strip_prefix(state).unwrap().into());
-
    }
-

-
    pub fn run_info(&mut self, filename: PathBuf) {
-
        self.run_info = Some(filename);
-
    }
-

-
    pub fn build(&self) -> Result<RunInfo, RunInfoError> {
-
        let fmt = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]");
-
        let now = OffsetDateTime::now_utc()
-
            .format(fmt)
-
            .map_err(RunInfoError::TimeFormat)?;
-

-
        Ok(RunInfo {
-
            repo: self.repo.map(|x| x.to_string()).unwrap_or("".into()),
-
            commit: self.commit.map(|x| x.to_string()).unwrap_or("".into()),
-
            id: self.run_id.as_deref().unwrap_or("<unknown>").into(),
-
            result: self
-
                .result
-
                .clone()
-
                .ok_or(RunInfoError::MissingInfo("result".into()))?,
-
            log: self.log.as_deref().unwrap_or(Path::new("<unknown>")).into(),
-
            run_info: self.run_info.clone(),
-
            timestamp: now,
-
        })
-
    }
-
}
-

-
#[derive(Debug, thiserror::Error)]
-
pub enum RunInfoError {
-
    #[error("programming error: field {0} is not set")]
-
    MissingInfo(String),
-

-
    #[error("failed to write file for run metadata: {0}")]
-
    WriteRunInfo(PathBuf, #[source] std::io::Error),
-

-
    #[error("failed to serialize run metadata: programming error")]
-
    SerializeRunInfo(#[source] serde_yaml::Error),
-

-
    #[error("failed to format current time as a string")]
-
    TimeFormat(#[source] time::error::Format),
-
}