From 2d0d98ea2f1dd9e3d39418246b796a6aca7f07fe Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 31 Mar 2024 15:47:16 +0300 Subject: [PATCH] feat: add branch or patch information to run log Signed-off-by: Lars Wirzenius --- src/bin/run_log.rs | 5 +++++ src/engine.rs | 54 ++++++++++++++++++++++++++++++++++++++++------ src/runlog.rs | 26 ++++++++++++++++++++++ 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/src/bin/run_log.rs b/src/bin/run_log.rs index 5965ca7..91c6f07 100644 --- a/src/bin/run_log.rs +++ b/src/bin/run_log.rs @@ -10,6 +10,11 @@ fn main() { run_log.title("Some Title"); run_log.rid(RepoId::from_urn("rad:z3qg5TKmN83afz2fj9z3fQjU8vaYE").expect("rid")); run_log.commit(Oid::try_from("b788f7ffd38572614457adb1656c0b4575b941dd").expect("commit")); + run_log.branch("xyzzy"); + run_log.patch( + Oid::try_from("e676fde7bfc7f8d433b50d73a236601d1f63cec3").unwrap(), + "This be my patch", + ); run_log.runspec(RunSpec { shell: "echo hello, world".into(), }); diff --git a/src/engine.rs b/src/engine.rs index 2b1dd18..28d142b 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,7 +3,9 @@ use std::path::PathBuf; use uuid::Uuid; use radicle::prelude::Profile; -use radicle_ci_broker::msg::{Oid, RepoId, Request, RunId, RunResult}; +use radicle_ci_broker::msg::{ + Oid, Patch, PatchEvent, PushEvent, RepoId, Request, RunId, RunResult, +}; use crate::{ config::{Config, ConfigError}, @@ -64,12 +66,40 @@ impl Engine { // Check that we got the right kind of request. let mut success = false; - match req { - Request::Trigger { .. } => { + match &req { + Request::Trigger { + push: Some(PushEvent { branch, .. }), + .. + } => { let repo = req.repo(); let commit = req.commit(); - match self.run_helper(repo, commit) { + match self.run_helper(repo, commit, Some(branch), None) { + Ok(true) => success = true, + Ok(false) => (), + Err(e) => { + // If the run helper return an error, something + // went wrong in that is not due to the repository + // under test. So we don't put it in the run log, + // but the admin log and return it to the caller. + self.adminlog.writeln(&format!("Error running CI: {}", e))?; + return Err(e); + } + } + } + Request::Trigger { + common: _, + push: _, + patch: + Some(PatchEvent { + patch: Patch { id, title, .. }, + .. + }), + } => { + let repo = req.repo(); + let commit = req.commit(); + + match self.run_helper(repo, commit, None, Some((*id, title))) { Ok(true) => success = true, Ok(false) => (), Err(e) => { @@ -134,7 +164,13 @@ impl Engine { // Execute the CI run. Log any problems to a log for this run, and // persist that. Update the run info builder as needed. #[allow(clippy::result_large_err)] - fn run_helper(&mut self, rid: RepoId, commit: Oid) -> Result { + fn run_helper( + &mut self, + rid: RepoId, + commit: Oid, + branch: Option<&str>, + patch: Option<(Oid, &str)>, + ) -> Result { // Pick a run id and create a directory for files related to // the run. let (run_id, run_dir) = mkdir_run(&self.config)?; @@ -163,7 +199,13 @@ impl Engine { // Actually run. Examine the run log to decide if the run // succeeded or failed. let result = run.run(); - if let Ok(run_log) = result { + if let Ok(mut run_log) = result { + if let Some(branch) = branch { + run_log.branch(branch); + } + if let Some((patch, title)) = patch { + run_log.patch(patch, title); + } self.result = if run_log.all_commands_succeeded() { Some(RunResult::Success) } else { diff --git a/src/runlog.rs b/src/runlog.rs index cd510ed..de0ff61 100644 --- a/src/runlog.rs +++ b/src/runlog.rs @@ -15,6 +15,8 @@ pub struct RunLog { title: Option, rid: Option, commit: Option, + branch: Option, + patch: Option<(Oid, String)>, commands: Vec, timeout: bool, runspec: Option, @@ -41,6 +43,14 @@ impl RunLog { self.commit = Some(commit); } + pub fn branch(&mut self, branch: &str) { + self.branch = Some(branch.into()); + } + + pub fn patch(&mut self, patch: Oid, title: &str) { + self.patch = Some((patch, title.into())); + } + pub fn all_commands_succeeded(&self) -> bool { self.commands.iter().all(|c| c.exit == 0) && !self.timeout && self.runspec_error.is_none() } @@ -88,6 +98,22 @@ impl RunLog { .with_text("Commit: ") .with_child(Element::new(Tag::Code).with_text(&commit.to_string())), ); + if let Some(branch) = &self.branch { + ul.push_child( + &Element::new(Tag::Li) + .with_text("Branch: ") + .with_child(Element::new(Tag::Code).with_text(branch)), + ); + } + if let Some((patch, title)) = &self.patch { + ul.push_child( + &Element::new(Tag::Li) + .with_text("Patch: ") + .with_child(Element::new(Tag::Code).with_text(&patch.to_string())) + .with_text(" ") + .with_text(title), + ); + } ul.push_child( &Element::new(Tag::Li) .with_text("Result: ")