Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
refactor: add a utility module
Lars Wirzenius committed 1 year ago
commit 0c6656f701afbfa92d4db35743d1788275601bdd
parent 81a9a7b3b044f2bc3df6cc503c2335be2f7620e3
3 files changed +104 -0
modified src/bin/cibtool.rs
@@ -35,6 +35,7 @@ use radicle_ci_broker::{
    notif::NotificationChannel,
    pages::{PageBuilder, PageError},
    run::{Run, RunState, Whence},
+
    util::{self, UtilError},
};

mod cibtoolcmd;
@@ -290,4 +291,7 @@ enum CibToolError {

    #[error("programming error: confused about state and result of run")]
    AddRunConfusion,
+

+
    #[error(transparent)]
+
    Util(#[from] UtilError),
}
modified src/lib.rs
@@ -18,3 +18,4 @@ pub mod queueproc;
pub mod run;
#[cfg(test)]
pub mod test;
+
pub mod util;
added src/util.rs
@@ -0,0 +1,99 @@
+
use std::str::FromStr;
+

+
use radicle::{
+
    prelude::{NodeId, RepoId},
+
    storage::ReadStorage,
+
    Profile, Storage,
+
};
+
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) {
+
        Ok(rid)
+
    } else {
+
        Ok(lookup_rid(profile, repo)?)
+
    }
+
}
+

+
pub fn oid_from_cli_arg(profile: &Profile, rid: RepoId, commit: &str) -> Result<Oid, UtilError> {
+
    if let Ok(oid) = Oid::from_str(commit) {
+
        Ok(oid)
+
    } else {
+
        lookup_commit(profile, rid, commit)
+
    }
+
}
+

+
pub fn load_profile() -> Result<Profile, UtilError> {
+
    Profile::load().map_err(UtilError::Profile)
+
}
+

+
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
+
        .repository(rid)
+
        .map_err(|e| UtilError::RepoOpen(rid, e))?;
+
    let object = repo
+
        .backend
+
        .revparse_single(gitref)
+
        .map_err(|e| UtilError::RevParse(gitref.into(), e))?;
+

+
    Ok(object.id().into())
+
}
+

+
#[derive(Debug, thiserror::Error)]
+
#[allow(clippy::large_enum_variant)]
+
pub enum UtilError {
+
    #[error("failed to look up node profile")]
+
    Profile(#[source] radicle::profile::Error),
+

+
    #[error("failed to look up open node storage")]
+
    Storage(#[source] radicle::storage::Error),
+

+
    #[error("failed to list repositories in node storage")]
+
    Repositories(#[source] radicle::storage::Error),
+

+
    #[error("failed to look up project info for repository {0}")]
+
    Project(RepoId, #[source] radicle::identity::doc::PayloadError),
+

+
    #[error("node has more than one repository called {0}")]
+
    DuplicateRepositories(String),
+

+
    #[error("node has no repository called: {0}")]
+
    NotFound(String),
+

+
    #[error("failed to open git repository in node storage: {0}")]
+
    RepoOpen(RepoId, #[source] radicle::storage::RepositoryError),
+

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