Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
feat: output trigger event to stdout, file
Merged liw opened 1 year ago

Signed-off-by: Lars Wirzenius liw@liw.fi

4 files changed +62 -6 fe9ffca3 43306e15
modified ci-broker.md
@@ -1789,6 +1789,33 @@ then stdout contains "main"
~~~


+
## Can output trigger message for a CI run
+

+
_Want:_ The `cibtool` command can output the CI event to trigger a CI
+
run to the standard output or a file.
+

+
_Why:_ This is helpful for debugging the CI broker at least.
+

+
_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
+

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

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

+
when I run cibtool --db x.db event list
+
then stdout is empty
+
~~~
+

+

## Add information about triggered run to database

_Want:_ `cibtool` can add information about a triggered CI run.
modified src/bin/cibtool.rs
@@ -288,6 +288,9 @@ enum CibToolError {
    #[error("failed to serialize CI event to JSON: {0:#?}")]
    EventToJson(CiEvent, #[source] serde_json::Error),

+
    #[error(transparent)]
+
    EventToJson2(#[from] CiEventError),
+

    #[error("failed to serialize node event to JSON: {0:#?}")]
    NodeEevnetToJson(radicle::node::Event, #[source] serde_json::Error),

modified src/bin/cibtoolcmd/trigger.rs
@@ -26,6 +26,16 @@ pub struct TriggerCmd {
    /// queue.
    #[clap(long)]
    id_file: Option<PathBuf>,
+

+
    /// Output the event to trigger a CI run to the standard output,
+
    /// instead of adding to the event queue in the database.
+
    #[clap(long)]
+
    stdout: bool,
+

+
    /// Output the event to trigger a CI run to a named file, instead
+
    /// of adding to the event queue in the database.
+
    #[clap(long)]
+
    output: Option<PathBuf>,
}

impl Leaf for TriggerCmd {
@@ -43,13 +53,22 @@ impl Leaf for TriggerCmd {
        let event =
            CiEvent::branch_updated(nid, rid, &name, oid, base).map_err(CibToolError::CiEvent)?;

-
        let db = args.open_db()?;
-
        let id = db.push_queued_ci_event(event)?;
-
        println!("{id}");
+
        if self.stdout {
+
            let json = event.to_pretty_json().map_err(CibToolError::EventToJson2)?;
+
            println!("{json}");
+
        } else if let Some(filename) = &self.output {
+
            let json = event.to_pretty_json().map_err(CibToolError::EventToJson2)?;
+
            std::fs::write(filename, &json)
+
                .map_err(|err| CibToolError::Write(filename.into(), err))?;
+
        } else {
+
            let db = args.open_db()?;
+
            let id = db.push_queued_ci_event(event)?;
+
            println!("{id}");

-
        if let Some(filename) = &self.id_file {
-
            write(filename, id.to_string().as_bytes())
-
                .map_err(|e| CibToolError::WriteEventId(filename.into(), e))?;
+
            if let Some(filename) = &self.id_file {
+
                write(filename, id.to_string().as_bytes())
+
                    .map_err(|e| CibToolError::WriteEventId(filename.into(), e))?;
+
            }
        }

        Ok(())
modified src/ci_event.rs
@@ -174,6 +174,10 @@ impl CiEvent {
            | Event::InventoryAnnounced { .. } => Ok(vec![]),
        }
    }
+

+
    pub fn to_pretty_json(&self) -> Result<String, CiEventError> {
+
        serde_json::to_string_pretty(self).map_err(CiEventError::ToJson)
+
    }
}

fn originator(name: RefString) -> Result<PublicKey, CiEventError> {
@@ -232,6 +236,9 @@ pub enum CiEventError {

    #[error("failed to convert name spaced Git ref into node public key: {0}")]
    KeyFromNamespaced(RefString, #[source] radicle_crypto::PublicKeyError),
+

+
    #[error("failed to encode CI event as JSON")]
+
    ToJson(#[source] serde_json::Error),
}

impl CiEventError {