Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
feat(src/bin/cibtoolcmd/run.rs)! simplify run add options
Lars Wirzenius committed 1 year ago
commit 5676cf8607f0f7616c2fac4b95a9654f70247f0b
parent 05ab47af7416aefa2213efa66741054fc16cc636
4 files changed +67 -44
modified src/bin/cibtoolcmd/run.rs
@@ -6,21 +6,18 @@ pub struct AddRun {
    #[clap(long)]
    id: Option<RunId>,

-
    /// Set alias of node that performed the CI run.
-
    #[clap(long)]
-
    alias: String,
-

    /// Set optional URL to information about the CI run.
    #[clap(long)]
    url: Option<String>,

-
    /// Set the repository ID that the CI run for.
+
    /// Set the repository the event refers to. Can be a RID, or the
+
    /// repository name.
    #[clap(long)]
-
    repo: RepoId,
+
    repo: String,

    /// Set timestamp of the CI run.
    #[clap(long)]
-
    timestamp: String,
+
    timestamp: Option<String>,

    /// Set the Git branch used by the CI run.
    #[clap(long)]
@@ -28,7 +25,7 @@ pub struct AddRun {

    /// Set the commit SHA ID used by the CI run.
    #[clap(long)]
-
    commit: Oid,
+
    commit: String,

    /// Set the author of the commit used by the CI run.
    #[clap(long)]
@@ -56,12 +53,17 @@ impl Leaf for AddRun {
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        let db = args.open_db()?;

+
        let profile = util::load_profile()?;
+
        let (rid, repo_name) = util::lookup_repo(&profile, &self.repo)?;
+
        let oid = util::oid_from_cli_arg(&profile, rid, &self.commit)?;
+
        let ts = self.timestamp.clone().unwrap_or(util::now()?);
+

        let whence = Whence::Branch {
            name: self.branch.clone(),
-
            commit: self.commit,
+
            commit: oid,
            who: self.who.clone(),
        };
-
        let mut run = Run::new(self.repo, &self.alias, whence, self.timestamp.clone());
+
        let mut run = Run::new(rid, &repo_name, whence, ts);

        let id = self.id.clone().unwrap_or_default();
        run.set_adapter_run_id(id);
modified src/bin/cibtoolcmd/trigger.rs
@@ -28,7 +28,7 @@ impl Leaf for TriggerCmd {
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        let profile = util::load_profile()?;
        let nid = util::lookup_nid(&profile)?;
-
        let rid = util::rid_from_cli_arg(&profile, &self.repo)?;
+
        let (rid, _repo_name) = util::lookup_repo(&profile, &self.repo)?;
        let oid = util::oid_from_cli_arg(&profile, rid, &self.commit)?;

        let base = util::lookup_commit(&profile, rid, &format!("{oid}^")).unwrap_or(oid);
modified src/run.rs
@@ -13,7 +13,7 @@ pub struct Run {
    adapter_run_id: Option<RunId>,
    adapter_info_url: Option<String>,
    repo_id: RepoId,
-
    repo_alias: String,
+
    repo_name: String,
    timestamp: String,
    whence: Whence,
    state: RunState,
@@ -22,13 +22,13 @@ pub struct Run {

impl Run {
    /// Create a new run.
-
    pub fn new(repo_id: RepoId, alias: &str, whence: Whence, timestamp: String) -> Self {
+
    pub fn new(repo_id: RepoId, name: &str, whence: Whence, timestamp: String) -> Self {
        Self {
            broker_run_id: RunId::default(),
            adapter_run_id: None,
            adapter_info_url: None,
            repo_id,
-
            repo_alias: alias.into(),
+
            repo_name: name.into(),
            timestamp,
            whence,
            state: RunState::Triggered,
@@ -38,7 +38,7 @@ impl Run {

    /// Return the repo alias.
    pub fn repo_alias(&self) -> &str {
-
        &self.repo_alias
+
        &self.repo_name
    }

    /// Return the repo id.
modified src/util.rs
@@ -1,5 +1,7 @@
use std::str::FromStr;

+
use time::{macros::format_description, OffsetDateTime};
+

use radicle::{
    prelude::{NodeId, RepoId},
    storage::ReadStorage,
@@ -7,11 +9,46 @@ use radicle::{
};
use radicle_git_ext::Oid;

-
pub fn rid_from_cli_arg(profile: &Profile, repo: &str) -> Result<RepoId, UtilError> {
-
    if let Ok(rid) = RepoId::from_urn(repo) {
+
pub fn lookup_repo(profile: &Profile, wanted: &str) -> Result<(RepoId, String), UtilError> {
+
    let storage = Storage::open(profile.storage(), profile.info()).map_err(UtilError::Storage)?;
+

+
    let repos = storage.repositories().map_err(UtilError::Repositories)?;
+
    let mut rid = None;
+

+
    if let Ok(wanted_rid) = RepoId::from_urn(wanted) {
+
        for ri in repos {
+
            let project = ri
+
                .doc
+
                .project()
+
                .map_err(|e| UtilError::Project(ri.rid, e))?;
+

+
            if ri.rid == wanted_rid {
+
                if rid.is_some() {
+
                    return Err(UtilError::DuplicateRepositories(wanted.into()));
+
                }
+
                rid = Some((ri.rid, project.name().to_string()));
+
            }
+
        }
+
    } else {
+
        for ri in repos {
+
            let project = ri
+
                .doc
+
                .project()
+
                .map_err(|e| UtilError::Project(ri.rid, e))?;
+

+
            if project.name() == wanted {
+
                if rid.is_some() {
+
                    return Err(UtilError::DuplicateRepositories(wanted.into()));
+
                }
+
                rid = Some((ri.rid, project.name().to_string()));
+
            }
+
        }
+
    }
+

+
    if let Some(rid) = rid {
        Ok(rid)
    } else {
-
        Ok(lookup_rid(profile, repo)?)
+
        Err(UtilError::NotFound(wanted.into()))
    }
}

@@ -31,32 +68,6 @@ pub fn lookup_nid(profile: &Profile) -> Result<NodeId, UtilError> {
    Ok(*profile.id())
}

-
pub fn lookup_rid(profile: &Profile, wanted: &str) -> Result<RepoId, UtilError> {
-
    let storage = Storage::open(profile.storage(), profile.info()).map_err(UtilError::Storage)?;
-

-
    let mut rid = None;
-
    let repo_infos = storage.repositories().map_err(UtilError::Repositories)?;
-
    for ri in repo_infos {
-
        let project = ri
-
            .doc
-
            .project()
-
            .map_err(|e| UtilError::Project(ri.rid, e))?;
-

-
        if project.name() == wanted {
-
            if rid.is_some() {
-
                return Err(UtilError::DuplicateRepositories(wanted.into()));
-
            }
-
            rid = Some(ri.rid);
-
        }
-
    }
-

-
    if let Some(rid) = rid {
-
        Ok(rid)
-
    } else {
-
        Err(UtilError::NotFound(wanted.into()))
-
    }
-
}
-

pub fn lookup_commit(profile: &Profile, rid: RepoId, gitref: &str) -> Result<Oid, UtilError> {
    let storage = Storage::open(profile.storage(), profile.info()).map_err(UtilError::Storage)?;
    let repo = storage
@@ -70,6 +81,13 @@ pub fn lookup_commit(profile: &Profile, rid: RepoId, gitref: &str) -> Result<Oid
    Ok(object.id().into())
}

+
pub fn now() -> Result<String, UtilError> {
+
    let fmt = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]Z");
+
    OffsetDateTime::now_utc()
+
        .format(fmt)
+
        .map_err(UtilError::TimeFormat)
+
}
+

#[derive(Debug, thiserror::Error)]
#[allow(clippy::large_enum_variant)]
pub enum UtilError {
@@ -96,4 +114,7 @@ pub enum UtilError {

    #[error("failed to parse git ref as a commit id: {0}")]
    RevParse(String, #[source] radicle::git::raw::Error),
+

+
    #[error("failed to format time stamp")]
+
    TimeFormat(#[source] time::error::Format),
}