Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
feat: `cibtool trigger --patch` option
Lars Wirzenius committed 8 months ago
commit 80db71a4406d986537159c30c7a7f68560be1e0a
parent a13b5a9
3 files changed +85 -17
modified ci-broker.md
@@ -948,7 +948,7 @@ 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 ./env.sh cibtool --db ci-broker.db trigger --repo xyzzy
+
when I run ./env.sh cibtool --db ci-broker.db trigger --repo xyzzy --commit HEAD
when I run ./env.sh cibtool --db ci-broker.db event list --json

given a directory reports
@@ -1056,8 +1056,8 @@ given file config.yaml from filter-repository.yaml
given file update-repoid.sh
when I run bash update-repoid.sh xyzzy config.yaml

-
when I run cibtool --db ci-broker.db trigger --repo xyzzy
-
when I run cibtool --db ci-broker.db trigger --repo other
+
when I run cibtool --db ci-broker.db trigger --repo xyzzy --commit HEAD
+
when I run cibtool --db ci-broker.db trigger --repo other --commit HEAD

when I run ./env.sh cib --config config.yaml queued

@@ -1105,8 +1105,8 @@ given file config.yaml from filter-node.yaml
given file update-nodeid.sh
when I run bash update-nodeid.sh xyzzy config.yaml

-
when I run cibtool --db ci-broker.db trigger --repo xyzzy
-
when I run cibtool --db ci-broker.db trigger --repo other --node z6MkgEMYod7Hxfy9qCvDv5hYHkZ4ciWmLFgfvm3Wn1b2w2FV
+
when I run cibtool --db ci-broker.db trigger --repo xyzzy --commit HEAD
+
when I run cibtool --db ci-broker.db trigger --repo other --commit HEAD --node z6MkgEMYod7Hxfy9qCvDv5hYHkZ4ciWmLFgfvm3Wn1b2w2FV

when I run ./env.sh cib --config config.yaml queued

@@ -1151,7 +1151,7 @@ given a Git repository xyzzy in the Radicle node

given file config.yaml from filter-tag.yaml

-
when I run cibtool --db ci-broker.db trigger --repo xyzzy
+
when I run cibtool --db ci-broker.db trigger --repo xyzzy --commit HEAD
when I run ./env.sh cib --config config.yaml queued
when I run cibtool --db ci-broker.db run list --json
then stdout doesn't contain ""repo_name": "xyzzy""
@@ -1160,7 +1160,7 @@ when I try to run cibtool --db ci-broker.db trigger --repo xyzzy --commit v1.0
then command fails

when I run env -C xyzzy git tag -am "version 1.0" v1.0
-
when I run cibtool --db ci-broker.db trigger --repo xyzzy
+
when I run cibtool --db ci-broker.db trigger --repo xyzzy --commit HEAD
when I run ./env.sh cib --config config.yaml queued
when I run cibtool --db ci-broker.db run list --json
then stdout doesn't contain ""repo_name": "xyzzy""
@@ -1191,8 +1191,8 @@ given a Git repository other in the Radicle node

given file config.yaml from filter-branch.yaml

-
when I run cibtool --db ci-broker.db trigger --repo xyzzy
-
when I run cibtool --db ci-broker.db trigger --repo other --ref oksa
+
when I run cibtool --db ci-broker.db trigger --repo xyzzy --commit HEAD
+
when I run cibtool --db ci-broker.db trigger --repo other --commit HEAD --ref oksa

when I run ./env.sh cib --config config.yaml queued

@@ -2078,7 +2078,7 @@ _Who:_ `cib-devs`
given a Radicle node, with CI configured with broker.yaml and adapter dummy.sh
given a Git repository xyzzy in the Radicle node

-
when I run ./env.sh cibtool --db x.db trigger --repo xyzzy --id-file id.txt
+
when I run ./env.sh cibtool --db x.db trigger --repo xyzzy --commit HEAD --id-file id.txt

when I run cibtool --db x.db event show --id-file id.txt
then stdout contains "rad:"
@@ -2100,11 +2100,11 @@ _Who:_ `cib-devs`
given a Radicle node, with CI configured with broker.yaml and adapter dummy.sh
given a Git repository xyzzy in the Radicle node

-
when I run ./env.sh cibtool --db x.db trigger --repo xyzzy --stdout
+
when I run ./env.sh cibtool --db x.db trigger --repo xyzzy --commit HEAD --stdout
then stdout contains "rad:"
then stdout contains "main"

-
when I run ./env.sh cibtool --db x.db trigger --repo xyzzy --output trigger.json
+
when I run ./env.sh cibtool --db x.db trigger --repo xyzzy --commit HEAD --output trigger.json
then file trigger.json contains "rad:"
then file trigger.json contains "main"

@@ -2303,11 +2303,11 @@ when I try to run ./env.sh cibtool --db x.db event add --repo rad:z3byzFpcfbMJBp
then command fails
then stderr contains "rad:z3byzFpcfbMJBp4tKYyuuTZiP8WUB"

-
when I try to run ./env.sh cibtool --db x.db trigger --repo missing --id-file id.txt
+
when I try to run ./env.sh cibtool --db x.db trigger --repo missing --commit HEAD --id-file id.txt
then command fails
then stderr contains "missing"

-
when I try to run ./env.sh cibtool --db x.db trigger --repo rad:z3byzFpcfbMJBp4tKYyuuTZiP8WUB --id-file id.txt
+
when I try to run ./env.sh cibtool --db x.db trigger --repo rad:z3byzFpcfbMJBp4tKYyuuTZiP8WUB --commit HEAD --id-file id.txt
then command fails
then stderr contains "rad:z3byzFpcfbMJBp4tKYyuuTZiP8WUB"

modified src/bin/cibtool.rs
@@ -386,4 +386,7 @@ enum CibToolError {

    #[error(transparent)]
    RefError(#[from] radicle_ci_broker::refs::RefError),
+

+
    #[error(transparent)]
+
    Trigger(#[from] cibtoolcmd::TriggerError),
}
modified src/bin/cibtoolcmd/trigger.rs
@@ -1,3 +1,10 @@
+
use radicle::{
+
    cob::patch::{cache::Patches, Patch, PatchId},
+
    identity::RepoId,
+
    profile::Profile,
+
    storage::ReadStorage,
+
};
+

use radicle_ci_broker::refs::{branch_ref, ref_string};

use super::*;
@@ -21,8 +28,12 @@ pub struct TriggerCmd {
    /// Set the commit the event refers to. Can be the SHA1 commit id,
    /// or a symbolic Git revision, as understood by `git rev-parse`.
    /// For example, `HEAD`.
-
    #[clap(long, default_value = "HEAD")]
-
    commit: String,
+
    #[clap(long, required_unless_present = "patch")]
+
    commit: Option<String>,
+

+
    /// Trigger CI to run on this patch.
+
    #[clap(long, required_unless_present = "commit")]
+
    patch: Option<PatchId>,

    /// Write the event ID to this file, after adding the event to the
    /// queue.
@@ -46,7 +57,21 @@ impl Leaf for TriggerCmd {
        let nid = util::lookup_nid(&profile)?;
        let nid = self.node.unwrap_or(nid);
        let (rid, _repo_name) = util::lookup_repo(&profile, &self.repo)?;
-
        let oid = util::oid_from_cli_arg(&profile, rid, &self.commit)?;
+

+
        let oid = if let Some(commit) = &self.commit {
+
            util::oid_from_cli_arg(&profile, rid, commit)?
+
        } else if let Some(wanted) = &self.patch {
+
            let mut oid = None;
+
            for (patch_id, patch) in patches(&profile, &rid)? {
+
                if &patch_id == wanted {
+
                    oid = Some(*patch.head());
+
                    break;
+
                }
+
            }
+
            oid.ok_or(TriggerError::NoSuchPatch(*wanted, rid))?
+
        } else {
+
            util::oid_from_cli_arg(&profile, rid, "HEAD")?
+
        };

        let base = util::lookup_commit(&profile, rid, &format!("{oid}^")).unwrap_or(oid);
        let branch_name = if let Some(name) = &self.name {
@@ -86,3 +111,43 @@ impl Leaf for TriggerCmd {
        Ok(())
    }
}
+

+
pub fn patches(profile: &Profile, repo_id: &RepoId) -> Result<Vec<(PatchId, Patch)>, TriggerError> {
+
    let repo = profile
+
        .storage
+
        .repository(*repo_id)
+
        .map_err(|err| TriggerError::LoadRepo(*repo_id, Box::new(err)))?;
+

+
    let patches = profile
+
        .home
+
        .patches(&repo)
+
        .map_err(|err| TriggerError::LoadPathces(*repo_id, Box::new(err)))?;
+

+
    let mut items = vec![];
+
    let list = patches
+
        .list()
+
        .map_err(|err| TriggerError::ListCache(*repo_id, err))?;
+
    for result in list {
+
        let (id, patch) = result.map_err(|err| TriggerError::CacheListItem(*repo_id, err))?;
+
        items.push((id, patch));
+
    }
+
    Ok(items)
+
}
+

+
#[derive(Debug, thiserror::Error)]
+
pub enum TriggerError {
+
    #[error("failed to load info from Radicle node storage for repository {0}")]
+
    LoadRepo(RepoId, #[source] Box<radicle::storage::RepositoryError>),
+

+
    #[error("failed to load patch list from Radicle node storage for repository {0}")]
+
    LoadPathces(RepoId, #[source] Box<radicle::profile::Error>),
+

+
    #[error("failed to list patches for repository {0}")]
+
    ListCache(RepoId, #[source] radicle::patch::cache::Error),
+

+
    #[error("failed to list info for patch {0}")]
+
    CacheListItem(RepoId, #[source] radicle::patch::cache::Error),
+

+
    #[error("can't find patch {0} in repository {1}")]
+
    NoSuchPatch(PatchId, RepoId),
+
}