Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
feat: create and update job COBs when adapter runs
Lars Wirzenius committed 9 months ago
commit c55375e1303e628530e362df4620e2af12dd8ea2
parent 6a4c026
3 files changed +61 -6
modified src/adapter.rs
@@ -18,12 +18,14 @@ use std::{

use serde::Serialize;
use tempfile::{tempdir, TempDir};
+
use url::Url;

use crate::{
+
    cob::{create_run, failed, succeeded},
    config::AdapterSpec,
    db::{Db, DbError},
    logger,
-
    msg::{MessageError, Request, Response},
+
    msg::{MessageError, Request, Response, RunResult},
    notif::NotificationSender,
    run::{Run, RunState},
    sensitive::Sensitive,
@@ -237,6 +239,9 @@ impl Adapter {
        run_notification: &NotificationSender,
        mut stdout: RealtimeLines,
    ) -> Result<(), AdapterError> {
+
        #[allow(clippy::unwrap_used)]
+
        let no_url = Url::parse("https://no.url.example.com").unwrap();
+

        if let Some(line) = stdout.line() {
            let resp = Response::from_str(&line).map_err(AdapterError::ParseResponse)?;
            run_notification.notify()?;
@@ -244,10 +249,28 @@ impl Adapter {
                Response::Triggered { run_id, info_url } => {
                    run.set_state(RunState::Running);
                    run.set_adapter_run_id(run_id);
-
                    if let Some(url) = info_url {
+

+
                    let url = if let Some(url) = info_url {
                        run.set_adapter_info_url(&url);
-
                    }
+
                        Url::parse(&url)
+
                    } else {
+
                        Ok(no_url.clone())
+
                    };
+

                    db.update_run(run).map_err(AdapterError::UpdateRun)?;
+

+
                    // Try to add this CI run tothe job COB. If that fails, the
+
                    // function logs it. We won't do anything about a failure, as
+
                    // there's nothing useful we can do about it, as long as we
+
                    // let CI run, which want to do.
+
                    let url = url.unwrap_or(no_url);
+
                    create_run(
+
                        run.repo_id(),
+
                        run.whence().oid(),
+
                        run.broker_run_id().clone(),
+
                        &url,
+
                    )
+
                    .ok();
                }
                _ => {
                    return Err(AdapterError::NotTriggered(resp));
@@ -264,8 +287,21 @@ impl Adapter {
            run_notification.notify()?;
            match resp {
                Response::Finished { result } => {
-
                    run.set_result(result);
+
                    run.set_result(result.clone());
                    db.update_run(run).map_err(AdapterError::UpdateRun)?;
+

+
                    // Try to mark this CI run in job COB as finished.
+
                    // If that fails, the function logs it. We won't
+
                    // do anything about a failure, as there's nothing
+
                    // useful we can do about it.
+
                    let repo_id = run.repo_id();
+
                    let oid = run.whence().oid();
+
                    let run_id = run.broker_run_id().clone();
+
                    match &result {
+
                        RunResult::Success => succeeded(repo_id, oid, run_id),
+
                        RunResult::Failure => failed(repo_id, oid, run_id),
+
                    }
+
                    .ok();
                }
                _ => {
                    return Err(AdapterError::NotFinished(resp));
modified src/broker.rs
@@ -15,6 +15,7 @@ use radicle::prelude::RepoId;

use crate::{
    adapter::Adapter,
+
    cob::create_job,
    db::{Db, DbError},
    logger,
    msg::{PatchEvent, PushEvent, Request, RunId},
@@ -72,7 +73,7 @@ impl Broker {
        run_notification: &NotificationSender,
    ) -> Result<Run, BrokerError> {
        logger::broker_start_run(trigger);
-
        let (common, whence) = match &trigger {
+
        let (common, whence, oid) = match &trigger {
            Request::Trigger {
                common,
                push:
@@ -85,7 +86,11 @@ impl Broker {
                patch: None,
            } => {
                let who = pusher.to_string();
-
                (common, Whence::branch(branch, *after, Some(who.as_str())))
+
                (
+
                    common,
+
                    Whence::branch(branch, *after, Some(who.as_str())),
+
                    *after,
+
                )
            }
            Request::Trigger {
                common,
@@ -101,6 +106,7 @@ impl Broker {
                (
                    common,
                    Whence::patch(patch.id, patch.after, revision, Some(who.as_str())),
+
                    patch.after,
                )
            }
            _ => panic!("neither a push nor a patch event"),
@@ -115,6 +121,12 @@ impl Broker {
            .build();
        self.db.push_run(&run)?;

+
        // Try to create a job COB. If that fails, the function logs
+
        // it. We won't do anything about a failure, as there's
+
        // nothing useful we can do about it, as long as we let CI
+
        // run, which want to do.
+
        create_job(trigger.repo(), oid).ok();
+

        // We run the adapter, but if that fails, we just
        // log the error. The `Run` value records the
        // result of the run.
modified src/run.rs
@@ -217,6 +217,13 @@ impl Whence {
}

impl Whence {
+
    pub fn oid(&self) -> Oid {
+
        match self {
+
            Self::Branch { commit, .. } => *commit,
+
            Self::Patch { commit, .. } => *commit,
+
        }
+
    }
+

    pub fn who(&self) -> Option<&str> {
        match self {
            Self::Branch {