mirror of
https://seed.radicle.dev/rad:zwTxygwuz5LDGBq255RA2CbNGrz8.git
synced 2026-10-18 19:30:29 +08:00
refactor: use ergo module in trigger command
Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
use radicle::{
|
||||
cob::patch::{cache::Patches, Patch, PatchId},
|
||||
identity::RepoId,
|
||||
profile::Profile,
|
||||
storage::ReadStorage,
|
||||
};
|
||||
use radicle::{cob::patch::PatchId, git::BranchName, storage::git::Repository};
|
||||
|
||||
use radicle_ci_broker::refs::{branch_ref, ref_string};
|
||||
use radicle_ci_broker::{
|
||||
ergo,
|
||||
refs::{branch_ref, ref_string},
|
||||
};
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -53,41 +51,23 @@ pub struct TriggerCmd {
|
||||
|
||||
impl Leaf for TriggerCmd {
|
||||
fn run(&self, args: &Args) -> Result<(), CibToolError> {
|
||||
let profile = util::load_profile()?;
|
||||
let nid = util::lookup_nid(&profile)?;
|
||||
let nid = self.node.unwrap_or(nid);
|
||||
let (rid, _repo_name) = util::lookup_repo(&profile, &self.repo)?;
|
||||
let r = ergo::Radicle::new().map_err(TriggerError::Ergonomic)?;
|
||||
|
||||
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 profile = r.profile();
|
||||
let nid = self.node.unwrap_or(*profile.id());
|
||||
let repo = r
|
||||
.repository_by_name(&self.repo)
|
||||
.map_err(TriggerError::Ergonomic)?;
|
||||
|
||||
let base = util::lookup_commit(&profile, rid, &format!("{oid}^")).unwrap_or(oid);
|
||||
let branch_name = if let Some(name) = &self.name {
|
||||
branch_ref(&ref_string(name).map_err(CibToolError::RefError)?)
|
||||
.map_err(CibToolError::RefError)?
|
||||
} else {
|
||||
let repo = profile
|
||||
.storage
|
||||
.repository(rid)
|
||||
.map_err(|err| CibToolError::RepoOpen(rid, err))?;
|
||||
let project = repo
|
||||
.project()
|
||||
.map_err(|err| CibToolError::GetProject(rid, err))?;
|
||||
project.default_branch().clone()
|
||||
};
|
||||
let event = CiEvent::branch_updated(nid, rid, &branch_name, oid, base)
|
||||
let oid = self.oid(&r, &repo)?;
|
||||
|
||||
let base = r
|
||||
.resolve_commit(&repo.id, &format!("{oid}^"))
|
||||
.unwrap_or(oid);
|
||||
|
||||
let branch_name = self.branch(&r, &repo)?;
|
||||
|
||||
let event = CiEvent::branch_updated(nid, repo.id, &branch_name, oid, base)
|
||||
.map_err(CibToolError::CiEvent)?;
|
||||
|
||||
if self.stdout {
|
||||
@@ -112,42 +92,33 @@ impl Leaf for TriggerCmd {
|
||||
}
|
||||
}
|
||||
|
||||
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)))?;
|
||||
impl TriggerCmd {
|
||||
fn oid(&self, r: &ergo::Radicle, repo: &Repository) -> Result<Oid, TriggerError> {
|
||||
let oid = if let Some(commit) = &self.commit {
|
||||
r.resolve_commit(&repo.id, commit)?
|
||||
} else if let Some(wanted) = &self.patch {
|
||||
*r.patch(&repo.id, wanted)?.head()
|
||||
} else {
|
||||
r.resolve_commit(&repo.id, "HEAD")?
|
||||
};
|
||||
Ok(oid)
|
||||
}
|
||||
|
||||
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));
|
||||
fn branch(&self, r: &ergo::Radicle, repo: &Repository) -> Result<BranchName, TriggerError> {
|
||||
if let Some(name) = &self.name {
|
||||
Ok(branch_ref(&ref_string(name)?)?)
|
||||
} else {
|
||||
let project = r.project(&repo.id).map_err(TriggerError::Ergonomic)?;
|
||||
Ok(project.default_branch().clone())
|
||||
}
|
||||
}
|
||||
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(transparent)]
|
||||
Ergonomic(#[from] radicle_ci_broker::ergo::ErgoError),
|
||||
|
||||
#[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),
|
||||
#[error(transparent)]
|
||||
RefError(#[from] radicle_ci_broker::refs::RefError),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user