use std::collections::BTreeMap; use forge_ci::proto::{MAX_CONCURRENCY, MIN_CONCURRENCY}; pub use forge_ci::proto::RestartMode; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum RunState { WaitingApproval, Queued, Running, Success, Failure, Cancelled, Error, } string_enum!(RunState { WaitingApproval = "waiting_approval", Queued = "queued", Running = "running", Success = "success", Failure = "failure", Cancelled = "cancelled", Error = "error", }); impl RunState { pub fn is_terminal(self) -> bool { matches!( self, RunState::Success | RunState::Failure | RunState::Cancelled | RunState::Error ) } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum JobState { Blocked, Queued, Running, Success, Failure, Cancelled, Error, Skipped, } string_enum!(JobState { Blocked = "blocked", Queued = "queued", Running = "running", Success = "success", Failure = "failure", Cancelled = "cancelled", Error = "error", Skipped = "skipped", }); impl JobState { pub fn is_terminal(self) -> bool { matches!( self, JobState::Success | JobState::Failure | JobState::Cancelled | JobState::Error | JobState::Skipped ) } } #[derive(Debug, Clone, PartialEq, Eq)] pub struct CiRunner { pub id: String, pub name: String, pub labels: Vec, pub created_at: String, pub last_seen_at: Option, pub stopped_at: Option, pub disabled: bool, pub concurrency_override: Option, pub reported: RunnerReport, pub running_jobs: u32, pub reported_config: BTreeMap, pub restart: Option, pub config_overrides: BTreeMap, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct RunnerRestart { pub mode: RestartMode, pub requested_at: String, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum FieldKind { Live, Restart, ReadOnly, } #[derive(Debug, Clone, Copy)] pub struct RunnerField { pub key: &'static str, pub kind: FieldKind, } pub const RUNNER_FIELDS: &[RunnerField] = &[ RunnerField { key: "concurrency", kind: FieldKind::Live, }, RunnerField { key: "labels", kind: FieldKind::Live, }, RunnerField { key: "auto_update", kind: FieldKind::Restart, }, RunnerField { key: "executor", kind: FieldKind::Restart, }, RunnerField { key: "work_dir", kind: FieldKind::Restart, }, RunnerField { key: "limits.memory_headroom", kind: FieldKind::Restart, }, RunnerField { key: "limits.cpus", kind: FieldKind::Restart, }, RunnerField { key: "limits.pids", kind: FieldKind::Restart, }, RunnerField { key: "limits.image_max_gb", kind: FieldKind::Restart, }, RunnerField { key: "limits.job_disk_gb", kind: FieldKind::Restart, }, RunnerField { key: "images.keep_days", kind: FieldKind::Restart, }, RunnerField { key: "cache_max_gib", kind: FieldKind::Restart, }, RunnerField { key: "sandbox.userns", kind: FieldKind::Restart, }, RunnerField { key: "name", kind: FieldKind::Live, }, RunnerField { key: "url", kind: FieldKind::ReadOnly, }, RunnerField { key: "runner_id", kind: FieldKind::ReadOnly, }, RunnerField { key: "runner_secret", kind: FieldKind::ReadOnly, }, RunnerField { key: "registry", kind: FieldKind::ReadOnly, }, ]; pub fn runner_field(key: &str) -> Option<&'static RunnerField> { RUNNER_FIELDS.iter().find(|f| f.key == key) } fn int_in( raw: &str, lo: i64, hi: i64, what: &str, ) -> std::result::Result { match raw.trim().parse::() { Ok(n) if (lo..=hi).contains(&n) => Ok(serde_json::Value::from(n)), _ => Err(format!("{what} must be a whole number from {lo} to {hi}")), } } pub fn validate_runner_field( key: &str, raw: &str, ) -> std::result::Result { use serde_json::Value; let raw = raw.trim(); match key { "concurrency" => int_in( raw, i64::from(MIN_CONCURRENCY), i64::from(MAX_CONCURRENCY), "concurrency", ), "name" => { if raw.is_empty() || raw.len() > 100 { Err("a runner name needs 1 to 100 bytes".into()) } else { Ok(Value::from(raw)) } } "labels" => { let labels: Vec = raw .split(',') .map(|l| l.trim().to_owned()) .filter(|l| !l.is_empty()) .collect(); if labels .iter() .any(|l| l.len() > 64 || l.contains(char::is_whitespace)) { return Err("each label is 1 to 64 characters with no spaces".into()); } Ok(Value::from(labels)) } "auto_update" => match raw { "true" => Ok(Value::Bool(true)), "false" => Ok(Value::Bool(false)), _ => Err("auto_update must be true or false".into()), }, "executor" => match raw { "builtin" | "podman" => Ok(Value::from(raw)), _ => Err("executor must be builtin or podman".into()), }, "sandbox.userns" => match raw { "auto" | "single" | "subuid" => Ok(Value::from(raw)), _ => Err("userns must be auto, single or subuid".into()), }, "work_dir" => { if raw.starts_with('/') && raw.len() <= 512 && !raw.contains('\0') { Ok(Value::from(raw)) } else { Err("work_dir must be an absolute path".into()) } } "limits.memory_headroom" => { if size_in_range(raw, 0, 1 << 50) { Ok(Value::from(raw)) } else { Err("memory_headroom looks like 1g or 512m".into()) } } "limits.cpus" => { let ok = raw.parse::().is_ok_and(|n| n > 0.0 && n <= 1024.0) && raw.chars().all(|c| c.is_ascii_digit() || c == '.'); if ok { Ok(Value::from(raw)) } else { Err("cpus is a positive number such as 2 or 1.5".into()) } } "limits.pids" => int_in(raw, 1, 4_194_304, "pids"), "limits.image_max_gb" => int_in(raw, 1, 100_000, "image_max_gb"), "limits.job_disk_gb" => int_in(raw, 0, 1_000_000, "job_disk_gb"), "images.keep_days" => int_in(raw, 0, 3650, "keep_days"), "cache_max_gib" => int_in(raw, 0, 1_000_000, "cache_max_gib"), _ => Err("this field cannot be changed from the forge".into()), } } #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct RunnerReport { pub concurrency: Option, pub version: Option, pub cpus: Option, pub memory_bytes: Option, pub build: Option, pub boot_id: Option, pub capabilities: Vec, } #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct QueueSkips { pub queued: usize, pub lacking_labels: usize, pub missing_labels: Vec, } fn size_in_range(raw: &str, lo: u64, hi: u64) -> bool { forge_ci::size::parse_size(raw).is_some_and(|n| (lo..=hi).contains(&n)) } pub const CAP_RESTART: &str = "restart"; impl CiRunner { pub fn supports_restart(&self) -> bool { self.reported.capabilities.iter().any(|c| c == CAP_RESTART) } } #[derive(Debug, Clone, PartialEq, Eq)] pub struct CiRun { pub id: String, pub repo_id: String, pub number: i64, pub trigger: String, pub git_ref: String, pub commit_oid: String, pub pull_number: Option, pub actor_id: String, pub state: RunState, pub config_error: Option, pub created_at: String, pub finished_at: Option, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct CiJob { pub id: String, pub run_id: String, pub name: String, pub spec_json: String, pub state: JobState, pub runner_id: Option, pub exit_code: Option, pub message: Option, pub queued_at: Option, pub started_at: Option, pub finished_at: Option, pub heartbeat_at: Option, pub cancel_requested: bool, pub log_bytes: i64, pub oom_kills: Option, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct JobSample { pub git_ref: String, pub branch_rank: i64, pub repo_rank: i64, pub job: String, pub secs: i64, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct JobStep { pub job_id: String, pub seq: i64, pub name: String, pub started_at: String, pub finished_at: Option, pub log_offset: i64, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum StepState { Running, Passed, Failed, Cancelled, } impl JobStep { pub fn state(&self, job: JobState) -> StepState { if self.finished_at.is_some() { return StepState::Passed; } match job { JobState::Success => StepState::Passed, JobState::Cancelled => StepState::Cancelled, JobState::Failure | JobState::Error => StepState::Failed, _ => StepState::Running, } } } #[derive(Debug, Clone, PartialEq, Eq)] pub struct StepSample { pub git_ref: String, pub branch_rank: i64, pub repo_rank: i64, pub job: String, pub step: String, pub seq: i64, pub secs: i64, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct RunnerJob { pub job: CiJob, pub run_number: i64, pub owner: String, pub repo: String, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct CiArtifact { pub id: String, pub job_id: String, pub name: String, pub size: i64, pub sha256: String, pub created_at: String, } #[derive(Debug, Clone, Copy)] pub struct NewJob<'a> { pub name: &'a str, pub spec_json: &'a str, pub needs: &'a [String], } pub struct NewRun<'a> { pub repo_id: &'a str, pub trigger: &'a str, pub git_ref: &'a str, pub commit_oid: &'a str, pub pull_number: Option, pub actor_id: &'a str, pub needs_approval: bool, pub config_error: Option<&'a str>, }