Radish alpha
r
rad:z3qg5TKmN83afz2fj9z3fQjU8vaYE
Radicle CI adapter for native CI
Radicle
Git
chore: drop unused report module
Lars Wirzenius committed 2 years ago
commit 7746b37c3341781d75f510a6d44279cc007a8ed0
parent b38a6c4
6 files changed +1 -173
deleted src/bin/run_info.rs
@@ -1,14 +0,0 @@
-
use std::path::Path;
-

-
use radicle_native_ci::{report::ReportError, runinfo::RunInfo};
-

-
/// The main program.
-
fn main() {
-
    let x = read_file_info(Path::new("run.yaml")).expect("parse yaml");
-
    println!("{:#?}", x);
-
}
-

-
fn read_file_info(filename: &Path) -> Result<RunInfo, ReportError> {
-
    let yaml = std::fs::read(filename).map_err(|e| ReportError::ReadRunInfo(filename.into(), e))?;
-
    serde_yaml::from_slice(&yaml).map_err(|e| ReportError::DeserializeRunInfo(filename.into(), e))
-
}
modified src/engine.rs
@@ -12,7 +12,6 @@ use crate::{
        read_request, write_errored, write_failed, write_succeeded, write_triggered,
        NativeMessageError,
    },
-
    report,
    run::{Run, RunError},
    runinfo::{RunInfo, RunInfoBuilder, RunInfoError},
    runlog::RunLogError,
@@ -240,9 +239,6 @@ pub enum EngineError {
    Message(#[from] NativeMessageError),

    #[error(transparent)]
-
    Report(#[from] report::ReportError),
-

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

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

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

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

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

    #[error(transparent)]
-
    Report(#[from] report::ReportError),
-

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

    #[error(transparent)]
deleted src/report.rs
@@ -1,144 +0,0 @@
-
use std::{
-
    cmp::Ord,
-
    collections::HashSet,
-
    path::{Path, PathBuf},
-
};
-

-
use html_page::{Document, Element, Tag};
-
use radicle_ci_broker::msg::RunResult;
-
use time::{macros::format_description, OffsetDateTime};
-
use walkdir::WalkDir;
-

-
use crate::runinfo::RunInfo;
-

-
const CSS: &str = include_str!("native-ci.css");
-

-
pub fn build_report(state: &Path) -> Result<(), ReportError> {
-
    let mut run_infos = collect_run_infos(state)?;
-
    run_infos.sort_by(|a, b| a.timestamp.partial_cmp(&b.timestamp).unwrap());
-

-
    let repos = repositories(&run_infos);
-
    let doc = list_runs(&repos, &run_infos);
-

-
    let index = state.join("index.html");
-
    std::fs::write(&index, doc.to_string().as_bytes())
-
        .map_err(|e| ReportError::WriteHtml(index.clone(), e))?;
-

-
    Ok(())
-
}
-

-
fn collect_run_infos(state: &Path) -> Result<Vec<RunInfo>, ReportError> {
-
    let mut infos = vec![];
-
    for entry in WalkDir::new(state) {
-
        let entry = entry?;
-
        if entry.path().ends_with("run.yaml") {
-
            infos.push(read_file_info(entry.path())?);
-
        }
-
    }
-
    Ok(infos)
-
}
-

-
fn repositories(infos: &[RunInfo]) -> Vec<&str> {
-
    let mut ids: Vec<&str> = infos
-
        .iter()
-
        .map(|ri| ri.repo.as_str())
-
        .collect::<HashSet<&str>>()
-
        .iter()
-
        .copied()
-
        .collect();
-
    ids.sort_by(|a, b| a.partial_cmp(b).unwrap());
-
    ids
-
}
-

-
fn runs_for_repo<'a>(repo: &str, infos: &'a [RunInfo]) -> Vec<&'a RunInfo> {
-
    infos.iter().filter(|ri| ri.repo == repo).collect()
-
}
-

-
fn read_file_info(filename: &Path) -> Result<RunInfo, ReportError> {
-
    let yaml = std::fs::read(filename).map_err(|e| ReportError::ReadRunInfo(filename.into(), e))?;
-
    serde_yaml::from_slice(&yaml).map_err(|e| ReportError::DeserializeRunInfo(filename.into(), e))
-
}
-

-
fn list_runs(repos: &[&str], run_infos: &[RunInfo]) -> Document {
-
    let mut doc = Document::default();
-
    doc.push_to_head(&Element::new(Tag::Title).with_text("CI run logs"));
-
    doc.push_to_head(&Element::new(Tag::Style).with_text(CSS));
-

-
    let fmt = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]Z");
-
    let now = OffsetDateTime::now_utc().format(fmt).expect("time format");
-

-
    let timestamp = Element::new(Tag::Div).with_text(&format!("Last updated {}.", now));
-
    doc.push_to_body(&timestamp);
-

-
    for repo in repos {
-
        let section = Element::new(Tag::H1)
-
            .with_text("Repository ")
-
            .with_child(Element::new(Tag::Code).with_text(repo));
-
        doc.push_to_body(&section);
-

-
        let mut list = Element::new(Tag::Ol).with_class("runlist");
-
        let mut runs = runs_for_repo(repo, run_infos);
-
        runs.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));
-
        for ri in runs {
-
            let commit = Element::new(Tag::Code)
-
                .with_class("commit")
-
                .with_text(&ri.commit);
-

-
            let run_id = Element::new(Tag::Code)
-
                .with_class("run_id")
-
                .with_text(&ri.id);
-

-
            let timestamp = Element::new(Tag::Span)
-
                .with_class("timestamp")
-
                .with_text(&ri.timestamp);
-

-
            let (result_text, class) = match &ri.result {
-
                RunResult::Success => ("success".into(), "success"),
-
                RunResult::Failure => ("failure".into(), "failure"),
-
                RunResult::Error(msg) => (format!("error: {}", msg), "failure"),
-
                _ => (format!("unknown: {:?}", ri.result), "unknown"),
-
            };
-
            let result = Element::new(Tag::Span)
-
                .with_class(class)
-
                .with_text(&result_text);
-

-
            let br = Element::new(Tag::Br);
-

-
            let href = format!("{}", ri.log.display());
-
            let link = Element::new(Tag::A)
-
                .with_attribute("href", &href)
-
                .with_text("CI run: ")
-
                .with_child(run_id);
-

-
            let item = Element::new(Tag::Li)
-
                .with_child(timestamp)
-
                .with_text(" ")
-
                .with_child(result)
-
                .with_child(br.clone())
-
                .with_text("Commit: ")
-
                .with_child(commit)
-
                .with_child(br)
-
                .with_child(link);
-

-
            list.push_child(&item);
-
        }
-
        doc.push_to_body(&list);
-
    }
-

-
    doc
-
}
-

-
#[derive(Debug, thiserror::Error)]
-
pub enum ReportError {
-
    #[error(transparent)]
-
    WalkDir(#[from] walkdir::Error),
-

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

-
    #[error("failed to parse run metadata as YAML: {0}")]
-
    DeserializeRunInfo(PathBuf, #[source] serde_yaml::Error),
-

-
    #[error("failed to write HTML file {0}")]
-
    WriteHtml(PathBuf, #[source] std::io::Error),
-
}
modified src/run.rs
@@ -7,7 +7,6 @@ use radicle_ci_broker::msg::{Oid, RepoId};

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

    #[error(transparent)]
-
    Report(#[from] report::ReportError),
-

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

    #[error(transparent)]