use std::path::{Path, PathBuf};
use crate::{
config::Target,
inspect::{self, RequestView},
repository::Identity,
review,
types::{ArtifactName, RequestId, ReviewerId, RunId},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Address {
pub worktree: PathBuf,
pub checkout: Identity,
pub artifact: ArtifactName,
pub target: Target,
}
impl Address {
pub fn same_artifact(&self, other: &Self) -> bool {
self.worktree == other.worktree
&& self.checkout == other.checkout
&& self.artifact == other.artifact
}
}
pub struct ReviewScope {
pub owner: RequestId,
pub addresses: Vec<Address>,
}
pub async fn review_checkout(state: &Path, run: &RunId) -> Result<(PathBuf, Identity), String> {
crate::store::viewer_checkout(state, run).await
}
pub async fn review_scope(
state: &Path,
shown: &RequestId,
reviewer: &ReviewerId,
) -> Result<Option<ReviewScope>, String> {
let review::Review { exact, owner } = review::read(state, shown).await?;
if review::review_refusal(Some(&owner), reviewer).is_some() {
return Ok(None);
}
let mut addresses = Vec::new();
if let Some(address) = address(state, &owner, None).await {
addresses.push(address);
}
if exact.request.id != owner.request.id {
let target = inspect::request(state, &exact.request.id)
.await
.ok()
.and_then(|view| {
view.definition
.and_then(|definition| definition.artifact().path.value().cloned())
});
if let Some(address) = address(state, &exact, target).await {
addresses.push(address);
}
}
Ok(Some(ReviewScope {
owner: owner.request.id.clone(),
addresses,
}))
}
async fn address(
state: &Path,
request: &RequestView,
recorded_target: Option<Target>,
) -> Option<Address> {
let artifact = &request.request.target;
let target = recorded_target.or_else(|| {
request
.request
.human_definition
.as_ref()
.and_then(|definition| definition.artifacts.value())
.and_then(|artifacts| artifacts.get(artifact))
.and_then(|saved| saved.path.value())
.cloned()
})?;
let (worktree, checkout) = review_checkout(state, &request.request.run_id).await.ok()?;
Some(Address {
worktree,
checkout,
artifact: artifact.clone(),
target,
})
}