diff options
Diffstat (limited to 'src/db/model')
| -rw-r--r-- | src/db/model/build_state.rs | 169 | ||||
| -rw-r--r-- | src/db/model/mod.rs | 217 | ||||
| -rw-r--r-- | src/db/model/state.rs | 208 |
3 files changed, 594 insertions, 0 deletions
diff --git a/src/db/model/build_state.rs b/src/db/model/build_state.rs new file mode 100644 index 0000000..a5998de --- /dev/null +++ b/src/db/model/build_state.rs @@ -0,0 +1,169 @@ +use serde::{Deserialize, Serialize}; + + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub enum BuildState { + Queued, + Leased, + Running, + BlockedMissingDep, + Succeeded, + Failed, + Cancelled, +} + +impl BuildState { + pub const ALL: &'static [BuildState] = &[ + BuildState::Queued, + BuildState::Leased, + BuildState::Running, + BuildState::BlockedMissingDep, + BuildState::Succeeded, + BuildState::Failed, + BuildState::Cancelled, + ]; + + pub fn as_str(&self) -> &'static str { + match self { + BuildState::Queued => "queued", + BuildState::Leased => "leased", + BuildState::Running => "running", + BuildState::BlockedMissingDep => "blocked_missing_dep", + BuildState::Succeeded => "succeeded", + BuildState::Failed => "failed", + BuildState::Cancelled => "cancelled", + } + } + + pub fn parse(s: &str) -> Option<BuildState> { + BuildState::ALL.iter().copied().find(|b| b.as_str() == s) + } + + pub fn is_terminal(&self) -> bool { + matches!( + self, + BuildState::Succeeded | BuildState::Failed | BuildState::Cancelled + ) + } + + pub fn can_transition(&self, to: BuildState) -> bool { + use BuildState::*; + if *self == to { + return true; + } + match self { + Queued => matches!(to, Leased | Running | BlockedMissingDep | Cancelled), + Leased => matches!(to, Running | Queued | BlockedMissingDep | Succeeded | Failed | Cancelled), + Running => matches!(to, Queued | BlockedMissingDep | Succeeded | Failed | Cancelled), + BlockedMissingDep => matches!(to, Queued | Cancelled), + Failed => matches!(to, Queued | Running), + Cancelled => to == Queued, + Succeeded => false, + } + } + + pub fn predecessors(to: BuildState) -> Vec<&'static str> { + BuildState::ALL + .iter() + .filter(|from| from.can_transition(to)) + .map(|from| from.as_str()) + .collect() + } + + pub fn active() -> &'static [BuildState] { + &[ + BuildState::Queued, + BuildState::Running, + BuildState::Leased, + BuildState::BlockedMissingDep, + ] + } +} + +impl std::fmt::Display for BuildState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum StepState { + Pending, + Running, + Succeeded, + Failed, + Interrupted, + Skipped, +} + +impl StepState { + pub const ALL: &'static [StepState] = &[ + StepState::Pending, + StepState::Running, + StepState::Succeeded, + StepState::Failed, + StepState::Interrupted, + StepState::Skipped, + ]; + + pub fn as_str(&self) -> &'static str { + match self { + StepState::Pending => "pending", + StepState::Running => "running", + StepState::Succeeded => "succeeded", + StepState::Failed => "failed", + StepState::Interrupted => "interrupted", + StepState::Skipped => "skipped", + } + } + + pub fn parse(text: &str) -> Option<StepState> { + StepState::ALL.iter().copied().find(|st| st.as_str() == text) + } + + pub fn is_done(&self) -> bool { + matches!(self, StepState::Succeeded | StepState::Skipped) + } + + pub fn can_transition(&self, to: StepState) -> bool { + use StepState::*; + if *self == to { + return true; + } + match self { + Pending => matches!(to, Running | Succeeded | Failed | Interrupted | Skipped), + Running => matches!(to, Pending | Succeeded | Failed | Interrupted | Skipped), + Failed | Interrupted => matches!(to, Pending | Running), + Succeeded | Skipped => false, + } + } + + pub fn predecessors(to: StepState) -> Vec<&'static str> { + StepState::ALL + .iter() + .filter(|from| from.can_transition(to)) + .map(|from| from.as_str()) + .collect() + } +} + +impl std::fmt::Display for StepState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } +} + +pub const BUILD_STEPS: &[&str] = &[ + "materialize", + "plan", + "chroot", + "sources", + "pkgver", + "build", + "collect", + "verify", + "ingest", + "publish", +]; + diff --git a/src/db/model/mod.rs b/src/db/model/mod.rs new file mode 100644 index 0000000..741f1f4 --- /dev/null +++ b/src/db/model/mod.rs @@ -0,0 +1,217 @@ +use serde::{Deserialize, Serialize}; + +use crate::hash; + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct UpstreamPin { + pub index: usize, + pub url: String, + pub ref_: String, + pub branch: Option<String>, + pub commit: String, +} + +#[derive(Debug, Clone)] +pub struct StageBCacheRow { + pub epoch: String, + pub pkgver: String, + pub pkgrel: String, + pub pins: Vec<UpstreamPin>, +} + +impl StageBCacheRow { + pub fn version(&self) -> String { + crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel) + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct SourceDigest { + pub index: usize, + pub url: String, + pub kind: String, + pub digest: String, + pub size: u64, + pub skip_declared: bool, +} + +impl SourceDigest { + pub fn is_vcs(&self) -> bool { + matches!(self.kind.as_str(), "vcs" | "git" | "svn" | "hg" | "bzr") + } +} + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +pub struct WorldMember { + pub pkgbase: String, + pub version: String, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Identity { + pub aur_commit: String, + pub epoch: String, + pub pkgver: String, + pub pkgrel: String, + pub upstream_pins: Vec<UpstreamPin>, + pub source_digests: Vec<SourceDigest>, +} + +impl Identity { + pub fn hash(&self) -> crate::error::Result<String> { + Ok(format!("sha256:{}", hash::hex(hash::canonical_json(self)?.as_bytes()))) + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct SourceEntry { + pub index: usize, + pub url: String, + pub filename: Option<String>, + pub commit: Option<String>, + pub tag: Option<String>, + pub branch: Option<String>, + pub vcs: Option<String>, + pub local: bool, +} + +impl SourceEntry { + pub fn is_vcs(&self) -> bool { + self.vcs.is_some() + } +} + +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct Recipe { + pub pkgbase: String, + pub pkgver: String, + pub pkgrel: String, + pub epoch: String, + pub pkgdesc: String, + pub url: String, + pub arch: Vec<String>, + pub license: Vec<String>, + pub groups: Vec<String>, + pub depends: Vec<String>, + pub makedepends: Vec<String>, + pub checkdepends: Vec<String>, + pub optdepends: Vec<String>, + pub provides: Vec<String>, + pub conflicts: Vec<String>, + pub source: Vec<String>, + pub sha256sums: Vec<String>, + pub b2sums: Vec<String>, + pub validpgpkeys: Vec<String>, + pub options: Vec<String>, + pub backup: Vec<String>, + pub install: Option<String>, + pub pkgnames: Vec<String>, + pub pkg_depends: Vec<(String, Vec<String>)>, + pub pkgbuild: String, + pub srcinfo: Option<String>, + pub skipgpcheck: bool, + #[serde(skip, default)] + pub source_entries: Vec<SourceEntry>, +} + +impl Recipe { + pub fn full_version(&self) -> String { + crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel) + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ReleaseRow { + pub id: i64, + pub pkgbase_id: i64, + pub candidate_id: Option<i64>, + pub approval_id: Option<i64>, + pub aur_commit: String, + pub recipe_hash: String, + pub upstream_pins_json: String, + pub source_digests_json: String, + pub epoch: String, + pub pkgver: String, + pub pkgrel: String, + pub world_snapshot_id: Option<i64>, + pub state: ReleaseState, + pub published_at: Option<String>, +} + +impl ReleaseRow { + pub fn version(&self) -> String { + crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel) + } + pub fn pins(&self) -> Vec<UpstreamPin> { + serde_json::from_str(&self.upstream_pins_json).unwrap_or_default() + } + pub fn digests(&self) -> Vec<SourceDigest> { + serde_json::from_str(&self.source_digests_json).unwrap_or_default() + } +} + +#[derive(Debug, Clone, Serialize)] +pub struct CandidateRow { + pub id: i64, + pub pkgbase_id: i64, + pub base_release_id: Option<i64>, + pub kind: String, + pub snapshot_id: i64, + pub epoch: String, + pub pkgver: String, + pub pkgrel: String, + pub upstream_pins_json: String, + pub source_digests_json: String, + pub identity_hash: String, + pub diff_digest: String, + pub revision: i64, + pub viewed_at: Option<String>, + pub risk_level: String, + pub risk_signals_json: String, + pub missing_deps_json: String, + pub state: CandidateState, + pub detected_at: String, + pub resolved_at: Option<String>, + pub pkgbase: String, +} + +impl CandidateRow { + pub fn version(&self) -> String { + crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel) + } + pub fn pins(&self) -> Vec<UpstreamPin> { + serde_json::from_str(&self.upstream_pins_json).unwrap_or_default() + } + pub fn digests(&self) -> Vec<SourceDigest> { + serde_json::from_str(&self.source_digests_json).unwrap_or_default() + } + pub fn risk_signals(&self) -> Vec<String> { + serde_json::from_str(&self.risk_signals_json).unwrap_or_default() + } + pub fn missing_deps(&self) -> Vec<String> { + serde_json::from_str(&self.missing_deps_json).unwrap_or_default() + } +} + +pub fn json_or<T: serde::de::DeserializeOwned>(s: &str, fallback: T) -> T { + serde_json::from_str(s).unwrap_or(fallback) +} + +// The state machines sit beside the row shapes rather than inside them: a row +// says what is stored, a state machine says which moves are legal. +mod build_state; +mod state; + +pub use build_state::{ + BUILD_STEPS, + BuildState, + StepState, +}; +pub use state::{ + CandidateState, + ReleaseState, + StateEnum, + SubscriptionState, + candidate_kind, + notice_kind, +}; diff --git a/src/db/model/state.rs b/src/db/model/state.rs new file mode 100644 index 0000000..4882428 --- /dev/null +++ b/src/db/model/state.rs @@ -0,0 +1,208 @@ +use serde::{Deserialize, Serialize}; + + +use crate::db::model::build_state::{BuildState, StepState}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum CandidateState { + PendingReview, + Approved, + Rejected, + Superseded, + Blocked, +} + +impl CandidateState { + pub const ALL: &'static [CandidateState] = &[ + CandidateState::PendingReview, + CandidateState::Approved, + CandidateState::Rejected, + CandidateState::Superseded, + CandidateState::Blocked, + ]; + + pub fn as_str(&self) -> &'static str { + match self { + CandidateState::PendingReview => "pending_review", + CandidateState::Approved => "approved", + CandidateState::Rejected => "rejected", + CandidateState::Superseded => "superseded", + CandidateState::Blocked => "blocked", + } + } + + pub fn parse(s: &str) -> Option<CandidateState> { + CandidateState::ALL.iter().copied().find(|c| c.as_str() == s) + } + + pub fn is_open(&self) -> bool { + *self == CandidateState::PendingReview + } + + pub fn can_transition(&self, to: CandidateState) -> bool { + use CandidateState::*; + if *self == to { + return true; + } + match self { + PendingReview => matches!(to, Approved | Rejected | Superseded | Blocked), + Approved | Rejected => to == Superseded, + Blocked => matches!(to, PendingReview | Approved | Rejected), + Superseded => false, + } + } + + pub fn predecessors(to: CandidateState) -> Vec<&'static str> { + CandidateState::ALL + .iter() + .filter(|from| from.can_transition(to)) + .map(|from| from.as_str()) + .collect() + } +} + +impl std::fmt::Display for CandidateState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } +} + +pub mod candidate_kind { + pub const NEW_VERSION: &str = "new_version"; + pub const FIRST_INTRODUCTION: &str = "first_introduction"; +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum ReleaseState { + Published, + Withdrawn, + FailedUnbuilt, +} + +impl ReleaseState { + pub const ALL: &'static [ReleaseState] = &[ + ReleaseState::Published, + ReleaseState::Withdrawn, + ReleaseState::FailedUnbuilt, + ]; + + pub fn as_str(&self) -> &'static str { + match self { + ReleaseState::Published => "published", + ReleaseState::Withdrawn => "withdrawn", + ReleaseState::FailedUnbuilt => "failed_unbuilt", + } + } + + pub fn parse(s: &str) -> Option<ReleaseState> { + ReleaseState::ALL.iter().copied().find(|r| r.as_str() == s) + } + + pub fn can_transition(&self, to: ReleaseState) -> bool { + use ReleaseState::*; + if *self == to { + return true; + } + match self { + Published => matches!(to, Withdrawn | FailedUnbuilt), + Withdrawn | FailedUnbuilt => false, + } + } +} + +impl std::fmt::Display for ReleaseState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } +} + +pub trait StateEnum: Copy + Sized { + const KIND: &'static str; + fn parse(text: &str) -> Option<Self>; + fn as_str(&self) -> &'static str; +} + +macro_rules! impl_state_enum { + ($t:ty, $kind:literal) => { + impl StateEnum for $t { + const KIND: &'static str = $kind; + fn parse(text: &str) -> Option<Self> { + <$t>::parse(text) + } + fn as_str(&self) -> &'static str { + <$t>::as_str(self) + } + } + }; +} + +impl_state_enum!(BuildState, "build"); +impl_state_enum!(CandidateState, "candidate"); +impl_state_enum!(StepState, "build step"); +impl_state_enum!(ReleaseState, "release"); +impl_state_enum!(SubscriptionState, "subscription"); + +pub mod notice_kind { + pub const PIN_DRIFT: &str = "pin_drift"; + pub const RECIPE_DRIFT: &str = "recipe_drift"; + pub const DIGEST_DRIFT: &str = "digest_drift"; + pub const INTEGRITY_ALERT: &str = "integrity_alert"; + pub const UNPINABLE_PKGVER: &str = "unpinable_pkgver"; + pub const ORPHANED: &str = "orphaned"; + pub const DELETED: &str = "deleted"; + pub const RENAMED: &str = "renamed"; + pub const DOWNGRADE: &str = "downgrade"; +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum SubscriptionState { + Active, + Paused, + Orphaned, + Removed, +} + +impl SubscriptionState { + pub const ALL: &'static [SubscriptionState] = &[ + SubscriptionState::Active, + SubscriptionState::Paused, + SubscriptionState::Orphaned, + SubscriptionState::Removed, + ]; + + pub fn as_str(&self) -> &'static str { + match self { + SubscriptionState::Active => "active", + SubscriptionState::Paused => "paused", + SubscriptionState::Orphaned => "orphaned", + SubscriptionState::Removed => "removed", + } + } + + pub fn parse(text: &str) -> Option<SubscriptionState> { + SubscriptionState::ALL.iter().copied().find(|st| st.as_str() == text) + } + + pub fn can_transition(&self, to: SubscriptionState) -> bool { + use SubscriptionState::*; + if *self == to { + return true; + } + match self { + Active => matches!(to, Paused | Orphaned | Removed), + Paused => matches!(to, Active | Orphaned | Removed), + Orphaned => matches!(to, Active | Removed), + Removed => false, + } + } +} + +impl std::fmt::Display for SubscriptionState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } +} + |
