use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct FetchOutcome {
pub remote: String,
pub refspecs: Vec<String>,
pub updated_refs: Vec<UpdatedRef>,
#[serde(default)]
pub checks_imported: usize,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub checks_refused: Vec<crate::check::RefusedCheckRow>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct UpdatedRef {
pub ref_name: String,
pub previous_sha: String,
pub new_sha: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PullOutcome {
pub mem: String,
pub source_ref: String,
pub branch_ref: String,
pub previous_sha: String,
pub new_sha: String,
pub updated_refs: Vec<UpdatedRef>,
#[serde(default)]
pub checks_imported: usize,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub checks_refused: Vec<crate::check::RefusedCheckRow>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub notices: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PushOutcome {
pub mem: String,
pub remote: String,
pub branch_ref: String,
pub new_sha: String,
pub forced: bool,
#[serde(default)]
pub checks_published: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub checks_refused: Vec<crate::check::RefusedCheckRow>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct PushAllOutcome {
pub remote: String,
pub pushed: Vec<PushedRef>,
pub in_sync: Vec<String>,
pub refused: Vec<RefusedRef>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub checks_refused: Vec<crate::check::RefusedCheckRow>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PushedRef {
pub ref_name: String,
pub mem: Option<String>,
pub previous_sha: String,
pub new_sha: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RefusedRef {
pub ref_name: String,
pub mem: Option<String>,
pub code: String,
pub message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct PullAllOutcome {
pub remote: String,
pub pulled: Vec<PulledRef>,
pub in_sync: Vec<String>,
pub local_ahead: Vec<String>,
pub refused: Vec<RefusedRef>,
pub returned_to_service: Vec<String>,
pub quarantined: Vec<QuarantinedAfterPull>,
pub notices: Vec<String>,
pub unmounted_on_remote: Vec<UnmountedRemoteMem>,
pub checks_imported: usize,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub checks_refused: Vec<crate::check::RefusedCheckRow>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct UnmountedRemoteMem {
pub branch: String,
pub schema: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PulledRef {
pub ref_name: String,
pub mem: Option<String>,
pub previous_sha: String,
pub new_sha: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct QuarantinedAfterPull {
pub mem: String,
pub code: String,
pub message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RemoteAddOutcome {
pub remote: String,
pub url: String,
pub updated: bool,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RemoteRefState {
InSync,
LocalAhead,
Behind,
Forked,
Unfetched,
MissingLocal,
UnmountedRemote,
NotOnRemote,
}
impl RemoteRefState {
pub fn as_wire(self) -> &'static str {
match self {
Self::InSync => "in_sync",
Self::LocalAhead => "local_ahead",
Self::Behind => "behind",
Self::Forked => "forked",
Self::Unfetched => "unfetched",
Self::MissingLocal => "missing_local",
Self::UnmountedRemote => "unmounted_remote",
Self::NotOnRemote => "not_on_remote",
}
}
pub fn is_stale(self) -> bool {
matches!(
self,
Self::Behind | Self::Forked | Self::Unfetched | Self::MissingLocal
)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RemoteRefStatus {
pub ref_name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub mem: Option<String>,
pub schemas_ref: bool,
pub state: RemoteRefState,
#[serde(skip_serializing_if = "Option::is_none")]
pub remote_sha: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub local_sha: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct RemoteStatusOutcome {
pub remote: String,
pub refs: Vec<RemoteRefStatus>,
pub notices: Vec<String>,
pub counts: std::collections::BTreeMap<String, usize>,
pub stale: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("the local graph lags remote `{remote}`: {}", stale.join(", "))]
pub struct RemoteStale {
pub remote: String,
pub stale: Vec<String>,
pub counts: std::collections::BTreeMap<String, usize>,
}
impl RemoteStale {
pub fn code(&self) -> &'static str {
"REMOTE_STALE"
}
pub fn details(&self) -> serde_json::Value {
serde_json::json!({
"remote": self.remote,
"stale": self.stale,
"counts": self.counts,
})
}
}
impl RemoteStatusOutcome {
pub fn stale_refusal(&self) -> Option<RemoteStale> {
if !self.stale {
return None;
}
Some(RemoteStale {
remote: self.remote.clone(),
stale: self
.refs
.iter()
.filter(|x| x.state.is_stale())
.map(|x| format!("{} ({})", x.ref_name, x.state.as_wire()))
.collect(),
counts: self.counts.clone(),
})
}
}