From fe9ffca3d27a486b321756fe932294225519cb27 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 4 Mar 2025 17:33:01 +0200 Subject: [PATCH] feat: cibool run remove Also fix Db::remove_run, which has never actually worked, because the run id column was named wrongly. Oops. Signed-off-by: Lars Wirzenius --- ci-broker.md | 28 ++++++++++++++++++++++++++++ src/bin/cibtool.rs | 4 ++++ src/bin/cibtoolcmd/run.rs | 32 ++++++++++++++++++++++++++++++++ src/db.rs | 2 +- 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/ci-broker.md b/ci-broker.md index e49afe81..b7bf21f8 100644 --- a/ci-broker.md +++ b/ci-broker.md @@ -1883,6 +1883,34 @@ when I run cibtool --db x.db run list --adapter-run-id abracadabra then stdout is empty ~~~ +## Remove information about a run from the database + +_Want:_ `cibtool` can removed information about a CI run. + +_Why:_ This is primarily for completeness. + +_Who:_ `cib-devs` + +~~~scenario +given a Radicle node, with CI configured with broker.yaml and adapter dummy.sh +given a Git repository xyzzy in the Radicle node +given the Radicle node emits a refsUpdated event for xyzzy +when I run ./env.sh synthetic-events synt.sock event.json --log log.txt + +when I run cibtool --db x.db run list +then stdout is empty + +when I run ./env.sh cibtool --db x.db run add --id runny --repo xyzzy --branch main --commit main --triggered +when I run cibtool --db x.db run list --json +then stdout contains "runny" +then stdout contains ""state": "triggered"" + +when I run cibtool --db x.db run remove runny + +when I run cibtool --db x.db run list +then stdout is empty +~~~ + ## Update and show information about run to running _Want:_ `cibtool` can update information about a CI run. diff --git a/src/bin/cibtool.rs b/src/bin/cibtool.rs index 7545c034..72d87f0f 100644 --- a/src/bin/cibtool.rs +++ b/src/bin/cibtool.rs @@ -224,6 +224,7 @@ impl Subcommand for RunCmd { RunSubCmd::Add(x) => x.run(args)?, RunSubCmd::List(x) => x.run(args)?, RunSubCmd::Update(x) => x.run(args)?, + RunSubCmd::Remove(x) => x.run(args)?, RunSubCmd::Show(x) => x.run(args)?, } Ok(()) @@ -239,6 +240,9 @@ enum RunSubCmd { /// Update information about a CI run to the database. Update(cibtoolcmd::UpdateRun), + /// Remove information about a CI run to the database. + Remove(cibtoolcmd::RemoveRun), + /// Show a CI run as JSON. Show(cibtoolcmd::ShowRun), diff --git a/src/bin/cibtoolcmd/run.rs b/src/bin/cibtoolcmd/run.rs index ede83e74..39e114d5 100644 --- a/src/bin/cibtoolcmd/run.rs +++ b/src/bin/cibtoolcmd/run.rs @@ -1,3 +1,5 @@ +use std::collections::HashSet; + use radicle_ci_broker::run::RunBuilder; use super::*; @@ -172,6 +174,36 @@ impl Leaf for ShowRun { } } +#[derive(Parser)] +pub struct RemoveRun { + /// Broker or adapter run IDs of runs to remove. + ids: Vec, +} + +impl Leaf for RemoveRun { + fn run(&self, args: &Args) -> Result<(), CibToolError> { + let db = args.open_db()?; + let unwanted: HashSet = HashSet::from_iter(self.ids.iter().cloned()); + let remove: Vec = db + .get_all_runs()? + .iter() + .filter(|run| { + if let Some(id) = run.adapter_run_id() { + unwanted.contains(id) + } else { + unwanted.contains(run.broker_run_id()) + } + }) + .cloned() + .collect(); + + for run in remove.iter() { + db.remove_run(run.broker_run_id())?; + } + Ok(()) + } +} + #[derive(Parser)] pub struct ListRuns { #[clap(long)] diff --git a/src/db.rs b/src/db.rs index 9bd606e2..9c7e8d22 100644 --- a/src/db.rs +++ b/src/db.rs @@ -450,7 +450,7 @@ impl Db { /// Remove a CI run from database, given its id. It's OK if the run is /// not in the database, that is just silently ignored. pub fn remove_run(&self, id: &RunId) -> Result<(), DbError> { - let mut remove = self.prepare("DELETE FROM ci_runs WHERE id = :id")?; + let mut remove = self.prepare("DELETE FROM ci_runs WHERE broker_run_id = :id")?; remove .stmt .bind((":id", id.to_string().as_str()))