use serde::{Deserialize, Serialize};
use super::model::{CheckStatus, PrDetail, PrId, Verdict};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct WatchState {
pub(crate) started_at: u64,
pub(crate) baseline: Baseline,
pub(crate) head_oid: Option<String>,
pub(crate) failed: Vec<String>,
pub(crate) gate_passed: bool,
pub(crate) gate: Vec<String>,
pub(crate) owed: Owed,
pub(crate) remarks_through: i64,
pub(crate) remark_ids: Vec<String>,
pub(crate) conflicting: bool,
pub(crate) comment_wakes: u8,
pub(crate) read_failures: u8,
}
impl WatchState {
pub(crate) fn new(started_at: u64) -> Self {
Self {
started_at,
baseline: Baseline::Pending,
head_oid: None,
failed: Vec::new(),
gate_passed: false,
gate: Vec::new(),
owed: Owed::Nothing,
remarks_through: i64::try_from(started_at / 1000).unwrap_or(i64::MAX),
remark_ids: Vec::new(),
conflicting: false,
comment_wakes: 0,
read_failures: 0,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum Baseline {
Pending,
Taken,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum Owed {
Nothing,
Advisory(Vec<String>),
Everything,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum Change {
ChecksFailed { failed: Vec<CheckRef> },
ChecksPassed { count: u32, gate: Gate },
ChecksFinished { passed: u32, failing: Vec<CheckRef> },
Remarks { remarks: Vec<Remark> },
Conflicting,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum Gate {
Required,
All,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct CheckRef {
pub(crate) name: String,
pub(crate) url: Option<String>,
pub(crate) failure: Failure,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum Failure {
Failed,
Cancelled,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct Remark {
pub(crate) author: String,
pub(crate) association: Option<String>,
pub(crate) path: Option<String>,
pub(crate) body: String,
pub(crate) verdict: Option<Reviewed>,
pub(crate) url: String,
pub(crate) at: i64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum Reviewed {
Approved,
ChangesRequested,
Commented,
Dismissed,
}
impl From<Verdict> for Reviewed {
fn from(verdict: Verdict) -> Self {
match verdict {
Verdict::Approved => Self::Approved,
Verdict::ChangesRequested => Self::ChangesRequested,
Verdict::Commented => Self::Commented,
Verdict::Dismissed => Self::Dismissed,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum End {
Merged {
by: Option<String>,
commit: Option<String>,
base: String,
},
Closed,
CommentLoop,
Unreadable(String),
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct News {
#[serde(default)]
pub(crate) seq: u64,
pub(crate) pr: PrId,
pub(crate) url: String,
pub(crate) head: Option<String>,
pub(crate) changes: Vec<Change>,
pub(crate) end: Option<End>,
pub(crate) at: u64,
#[serde(default)]
pub(crate) pushed: bool,
#[serde(default)]
pub(crate) checks: Option<CheckTally>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct CheckTally {
pub(crate) passed: u32,
pub(crate) running: u32,
pub(crate) failed: u32,
pub(crate) unread: u32,
#[serde(default)]
pub(crate) queued_workflows: u32,
}
impl CheckTally {
pub(crate) fn of(detail: &PrDetail) -> Self {
let mut tally = Self {
unread: detail.unread_checks(),
queued_workflows: detail.queued_workflows,
..Self::default()
};
for check in &detail.checks {
let count = match check.status {
CheckStatus::Passed | CheckStatus::Neutral | CheckStatus::Skipped => {
&mut tally.passed
}
CheckStatus::Running | CheckStatus::Pending => &mut tally.running,
CheckStatus::Failed | CheckStatus::Cancelled => &mut tally.failed,
};
*count += 1;
}
tally
}
}
impl News {
pub(crate) fn is_end(&self) -> bool {
self.end.is_some()
}
}