refactor: open run log file when creating Runner

Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
Lars Wirzenius
2024-01-10 17:38:05 +02:00
parent 5266ca1cb9
commit 31e39d5a9b
+13 -11
View File
@@ -157,21 +157,21 @@ struct Runner<'a> {
commit: Oid,
src: PathBuf,
log: &'a mut LogFile,
run_log: PathBuf,
run_log: LogFile,
builder: &'a mut RunInfoBuilder,
}
impl<'a> Runner<'a> {
/// Perform the CI run.
fn run(&mut self) -> Result<(), NativeError> {
let mut run_log = LogFile::open(&self.run_log)?;
self.log
.write(format!("CI run on {}, {}\n", self.repo, self.commit))?;
run_log.write_str("# Log from Radicle native CI\n\n")?;
run_log.write(format!("* Repository id: `{}`\n", self.repo))?;
run_log.write(format!("* Commit: `{}`\n\n", self.commit))?;
self.run_log.write_str("# Log from Radicle native CI\n\n")?;
self.run_log
.write(format!("* Repository id: `{}`\n", self.repo))?;
self.run_log
.write(format!("* Commit: `{}`\n\n", self.commit))?;
write_response(&Response::triggered(RunId::from(
self.run_id.to_string().as_str(),
@@ -183,7 +183,7 @@ impl<'a> Runner<'a> {
debug!("cloning repository to {}", self.src.display());
self.log.write_str("clone repository\n")?;
runcmd(
&mut run_log,
&mut self.run_log,
&[
"git",
"clone",
@@ -196,7 +196,7 @@ impl<'a> Runner<'a> {
debug!("checking out commit {}", self.commit);
self.log.write_str("check out commit\n")?;
runcmd(
&mut run_log,
&mut self.run_log,
&["git", "checkout", &self.commit.to_string()],
&self.src,
)?;
@@ -207,13 +207,13 @@ impl<'a> Runner<'a> {
debug!("running CI in cloned repository");
self.log.write_str("run shell snippet in repository\n")?;
let snippet = format!("set -xeuo pipefail\n{}", &runspec.shell);
runcmd(&mut run_log, &["bash", "-c", &snippet], &self.src)?;
runcmd(&mut self.run_log, &["bash", "-c", &snippet], &self.src)?;
let result = RunResult::Success;
write_response(&Response::finished(result.clone()))?;
run_log.write_str("CI run finished successfully\n")?;
self.run_log.write_str("CI run finished successfully\n")?;
std::fs::remove_dir_all(&self.src)
.map_err(|e| NativeError::RemoveDir(self.src.clone(), e))?;
@@ -278,6 +278,8 @@ impl<'a> RunnerBuilder<'a> {
}
fn build(self) -> Result<Runner<'a>, NativeError> {
let run_log = self.run_log.ok_or(NativeError::Unset("run_log"))?;
let run_log = LogFile::open(&run_log)?;
Ok(Runner {
run_id: self.run_id.ok_or(NativeError::Unset("run_id"))?,
storage: self.storage.ok_or(NativeError::Unset("storage"))?,
@@ -285,7 +287,7 @@ impl<'a> RunnerBuilder<'a> {
commit: self.commit.ok_or(NativeError::Unset("commit"))?,
src: self.src.ok_or(NativeError::Unset("src"))?,
log: self.log.ok_or(NativeError::Unset("log"))?,
run_log: self.run_log.ok_or(NativeError::Unset("run_log"))?,
run_log,
builder: self.builder.ok_or(NativeError::Unset("builder"))?,
})
}