Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions apps/origin-src/src/annotations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod retention;
use std::collections::HashSet;

use origin_platform::{Native, Platform};
use origin_shared::api::Annotation;
use origin_shared::{
ChangeId, EncodedObject, Kind, Principal, Record, RecordId, RevisionId, Statement, StatementId,
};
Expand All @@ -33,17 +34,6 @@ pub struct Annotations<'a, P = Native> {
index: Index,
}

/// A statement, and whether its issuer superseded it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Annotation {
/// The statement's id.
pub id: StatementId,
/// The statement.
pub statement: Statement,
/// The first statement from the same issuer that supersedes it.
pub superseded_by: Option<StatementId>,
}

impl<'a, P: Platform> Annotations<'a, P> {
/// The statements of `store`, as `index` knows them.
pub fn new(store: &'a Store<P>, index: Index) -> Self {
Expand Down Expand Up @@ -190,12 +180,31 @@ impl<'a, P: Platform> Annotations<'a, P> {
Ok(None)
}

/// The statements about `subject`, in arrival order.
/// The statements about `subject`, in arrival order, each saying
/// whether its claim holds, as [`Annotations::claims`] finds: so readers
/// such as CI runners count results as the landing queue does.
///
/// # Errors
///
/// Fails if a statement cannot be read.
/// Fails if a statement, or a record or tree claims read, cannot be
/// read.
pub fn about(&self, subject: RecordId) -> Result<Vec<Annotation>, StoreError> {
let mut listed = self.listed(subject)?;
let holding: HashSet<StatementId> =
self.claims_of(&listed)?.iter().map(Claim::id).collect();
for annotation in &mut listed {
annotation.holds = holding.contains(&annotation.id);
}
Ok(listed)
}

/// The statements about `subject`, in arrival order, without finding
/// which hold.
///
/// # Errors
///
/// Fails if a statement cannot be read.
fn listed(&self, subject: RecordId) -> Result<Vec<Annotation>, StoreError> {
self.index
.statements(subject)
.iter()
Expand All @@ -205,6 +214,7 @@ impl<'a, P: Platform> Annotations<'a, P> {
id,
superseded_by: self.superseded_by(id, &statement)?,
statement,
holds: false,
})
})
.collect()
Expand All @@ -223,7 +233,7 @@ impl<'a, P: Platform> Annotations<'a, P> {
issuer: &Principal,
) -> Result<Option<StatementId>, StoreError> {
Ok(self
.about(subject)?
.listed(subject)?
.into_iter()
.rev()
.find(|annotation| {
Expand Down
23 changes: 21 additions & 2 deletions apps/origin-src/src/annotations/promotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use std::collections::HashSet;

use origin_platform::Platform;
use origin_shared::api::Annotation;
use origin_shared::{Principal, Record, RecordId, Statement, StatementId, TreeId};

use super::Annotations;
Expand Down Expand Up @@ -77,12 +78,25 @@ impl<P: Platform> Annotations<'_, P> {
///
/// Fails if a record or a tree cannot be read.
pub fn claims(&self, subject: RecordId) -> Result<Vec<Claim>, StoreError> {
self.claims_of(&self.listed(subject)?)
}

/// The claims that hold of `listed`, the statements about one record,
/// as [`Annotations::claims`] finds them.
///
/// # Errors
///
/// Fails if a record or a tree cannot be read.
pub(super) fn claims_of(&self, listed: &[Annotation]) -> Result<Vec<Claim>, StoreError> {
let mut claims = Vec::new();
for annotation in self.about(subject)? {
for annotation in listed {
if annotation.superseded_by.is_some() {
continue;
}
let claim = match self.store.claim(annotation.id, annotation.statement) {
let claim = match self
.store
.claim(annotation.id, annotation.statement.clone())
{
Ok(Ok(claim)) => claim,
// A clone may lack the trees a promotion is checked with.
Ok(Err(_)) | Err(StoreError::NotFound(_)) => continue,
Expand Down Expand Up @@ -592,6 +606,11 @@ mod tests {
);
let annotations = Annotations::open(&store).unwrap();
assert_eq!(annotations.claims(RecordId::Revision(second)).unwrap(), []);
let about = annotations.about(RecordId::Revision(second)).unwrap();
assert!(
about.iter().all(|annotation| !annotation.holds),
"{about:?}"
);
assert_eq!(promote(&store, second), [("lint".to_owned(), Some(rerun))]);
}
}