Skip to main content

artifactize_core/viewer/
review.rs

1//! The saved checkout and Artifact targets an active review can show in a Viewer.
2use std::path::{Path, PathBuf};
3
4use crate::{
5    config::Target,
6    inspect::{self, RequestView},
7    repository::Identity,
8    review,
9    types::{ArtifactName, RequestId, ReviewerId, RunId},
10};
11
12/// The Artifact's recorded target, within a particular checkout of that worktree.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct Address {
15    pub worktree: PathBuf,
16    pub checkout: Identity,
17    pub artifact: ArtifactName,
18    pub target: Target,
19}
20
21impl Address {
22    pub fn same_artifact(&self, other: &Self) -> bool {
23        self.worktree == other.worktree
24            && self.checkout == other.checkout
25            && self.artifact == other.artifact
26    }
27}
28
29pub struct ReviewScope {
30    pub owner: RequestId,
31    pub addresses: Vec<Address>,
32}
33
34/// The recorded checkout for a review Viewer, falling back to the exact directory's current
35/// identity only for older Runs that did not save checkout metadata.
36pub async fn review_checkout(state: &Path, run: &RunId) -> Result<(PathBuf, Identity), String> {
37    crate::store::viewer_checkout(state, run).await
38}
39
40/// Resolve the claimed owner and the exact request's Artifact target when it shares another
41/// Run's review. An unclaimed or settled review cannot enable a Viewer to add Points.
42pub async fn review_scope(
43    state: &Path,
44    shown: &RequestId,
45    reviewer: &ReviewerId,
46) -> Result<Option<ReviewScope>, String> {
47    let review::Review { exact, owner } = review::read(state, shown).await?;
48    if review::review_refusal(Some(&owner), reviewer).is_some() {
49        return Ok(None);
50    }
51    let mut addresses = Vec::new();
52    if let Some(address) = address(state, &owner, None).await {
53        addresses.push(address);
54    }
55    if exact.request.id != owner.request.id {
56        let target = inspect::request(state, &exact.request.id)
57            .await
58            .ok()
59            .and_then(|view| {
60                view.definition
61                    .and_then(|definition| definition.artifact().path.value().cloned())
62            });
63        if let Some(address) = address(state, &exact, target).await {
64            addresses.push(address);
65        }
66    }
67    Ok(Some(ReviewScope {
68        owner: owner.request.id.clone(),
69        addresses,
70    }))
71}
72
73async fn address(
74    state: &Path,
75    request: &RequestView,
76    recorded_target: Option<Target>,
77) -> Option<Address> {
78    let artifact = &request.request.target;
79    let target = recorded_target.or_else(|| {
80        request
81            .request
82            .human_definition
83            .as_ref()
84            .and_then(|definition| definition.artifacts.value())
85            .and_then(|artifacts| artifacts.get(artifact))
86            .and_then(|saved| saved.path.value())
87            .cloned()
88    })?;
89    let (worktree, checkout) = review_checkout(state, &request.request.run_id).await.ok()?;
90    Some(Address {
91        worktree,
92        checkout,
93        artifact: artifact.clone(),
94        target,
95    })
96}