refactor: don't write run info file unless requested

Also, rename id to run_id for clarity. Found this out the hard way
while debugging.

Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
Lars Wirzenius
2024-01-10 17:38:05 +02:00
parent 6083f087d7
commit ddfedf5985
3 changed files with 15 additions and 13 deletions
+1 -1
View File
@@ -95,7 +95,7 @@ fn fallible_main_inner(
let req = read_request()?;
logfile.writeln(&format!("request: {:#?}", req))?;
builder.id(run_id.clone());
builder.run_id(run_id.clone());
builder.log(&config.state, run_log.clone());
builder.run_info(run_info_file.clone());
+1 -1
View File
@@ -76,7 +76,7 @@ fn list_runs(repos: &[&str], run_infos: &[RunInfo]) -> Document {
let run_id = Element::new(Tag::Code)
.with_class("run_id")
.with_text(&ri.id);
.with_text(&ri.run_id);
let timestamp = Element::new(Tag::Span)
.with_class("timestamp")
+13 -11
View File
@@ -18,7 +18,7 @@ pub struct RunInfo {
pub commit: String,
/// Identifier for the CI run.
pub id: String,
pub run_id: String,
/// Result of the CI run.
pub result: RunResult,
@@ -28,7 +28,7 @@ pub struct RunInfo {
/// Name of run info file.
#[serde(skip)]
pub run_info: PathBuf,
pub run_info: Option<PathBuf>,
/// Timestamp of when the run ended (the value was created).
/// ISO8601 format.
@@ -41,10 +41,12 @@ impl RunInfo {
}
pub fn write(&self) -> Result<(), RunInfoError> {
info!("Writing run info to {}", self.run_info.display());
let yaml = serde_yaml::to_string(&self).map_err(RunInfoError::SerializeRunInfo)?;
std::fs::write(&self.run_info, yaml.as_bytes())
.map_err(|e| RunInfoError::WriteRunInfo(self.run_info.clone(), e))?;
if let Some(filename) = &self.run_info {
info!("Writing run info to {}", filename.display());
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(())
}
}
@@ -54,7 +56,7 @@ impl RunInfo {
pub struct RunInfoBuilder {
repo: Option<Id>,
commit: Option<Oid>,
id: Option<String>,
run_id: Option<String>,
result: Option<RunResult>,
log: Option<PathBuf>,
run_info: Option<PathBuf>,
@@ -69,8 +71,8 @@ impl RunInfoBuilder {
self.commit = Some(commit);
}
pub fn id(&mut self, id: RunId) {
self.id = Some(format!("{}", id));
pub fn run_id(&mut self, id: RunId) {
self.run_id = Some(format!("{}", id));
}
pub fn result(&mut self, result: RunResult) {
@@ -94,12 +96,12 @@ impl RunInfoBuilder {
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.id.unwrap_or("<unknown>".into()),
run_id: self.run_id.unwrap_or("<unknown>".into()),
result: self
.result
.ok_or(RunInfoError::MissingInfo("result".into()))?,
log: self.log.unwrap_or("<unknown>".into()),
run_info: self.run_info.unwrap_or("<unknown>".into()),
run_info: self.run_info,
timestamp: now,
})
}