//! The merge queue: pull requests waiting to be merged into a protected branch that requires the //! queue. Each entry is `queued`, then `testing` (its candidate commit runs CI), then `merging` //! (the target moves to the tested commit). An entry finishes as `failed` or `removed`. use crate::error::{Error, Result}; use crate::pulls::MergeStyle; /// Where an entry is in the queue. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum QueueState { Queued, Testing, Merging, Failed, Removed, } impl QueueState { pub fn as_str(self) -> &'static str { match self { QueueState::Queued => "queued", QueueState::Testing => "testing", QueueState::Merging => "merging", QueueState::Failed => "failed", QueueState::Removed => "removed", } } /// Whether the entry still waits for the queue (queued, testing or merging). pub fn is_active(self) -> bool { matches!( self, QueueState::Queued | QueueState::Testing | QueueState::Merging ) } } impl std::str::FromStr for QueueState { type Err = Error; fn from_str(s: &str) -> Result { Ok(match s { "queued" => QueueState::Queued, "testing" => QueueState::Testing, "merging" => QueueState::Merging, "failed" => QueueState::Failed, "removed" => QueueState::Removed, other => { return Err(Error::Database(format!( "unknown merge queue state {other}" ))); } }) } } /// One pull request in the queue. #[derive(Debug, Clone, PartialEq, Eq)] pub struct QueueEntry { pub id: String, pub repo_id: String, pub branch: String, pub issue_id: String, pub pull_number: i64, pub merge_style: MergeStyle, pub state: QueueState, /// Enqueue order within the branch. Smaller entries go first. pub seq: i64, pub enqueued_by: String, pub enqueued_at: String, pub updated_at: String, pub finished_at: Option, /// Why a finished entry ended: `merged`, `removed`, `closed`, `head_changed`, `conflict` or /// `tests_failed`. pub reason: Option, /// The target tip the candidate was built on. pub base_oid: Option, /// The candidate commit under test. pub tested_oid: Option, /// The temporary ref that names the candidate. pub test_ref: Option, /// The CI run that tests the candidate. pub run_id: Option, } /// The temporary ref that names a candidate of `branch` for pull request `number`. pub fn test_ref_name(branch: &str, number: i64) -> String { format!("refs/forge/merge-queue/{branch}/{number}") }