treewide: address all clippy lints
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I5cf55cc4cb558c3f9f764c71224e87176a6a6964
This commit is contained in:
parent
967d51e867
commit
0ca92f2710
63 changed files with 1788 additions and 1087 deletions
|
|
@ -30,11 +30,13 @@ impl std::fmt::Debug for AlertManager {
|
|||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("AlertManager")
|
||||
.field("config", &self.config)
|
||||
.finish()
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl AlertManager {
|
||||
/// Create an alert manager from config.
|
||||
#[must_use]
|
||||
pub fn new(config: AlertConfig) -> Self {
|
||||
Self {
|
||||
config,
|
||||
|
|
@ -42,10 +44,14 @@ impl AlertManager {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn is_enabled(&self) -> bool {
|
||||
/// Check if alerts are enabled in the config.
|
||||
#[must_use]
|
||||
pub const fn is_enabled(&self) -> bool {
|
||||
self.config.enabled
|
||||
}
|
||||
|
||||
/// Calculate failure rate and dispatch alerts if threshold exceeded.
|
||||
/// Returns the computed failure rate if alerts are enabled.
|
||||
pub async fn check_and_alert(
|
||||
&self,
|
||||
pool: &PgPool,
|
||||
|
|
@ -56,16 +62,15 @@ impl AlertManager {
|
|||
return None;
|
||||
}
|
||||
|
||||
let failure_rate = match build_metrics::calculate_failure_rate(
|
||||
let Ok(failure_rate) = build_metrics::calculate_failure_rate(
|
||||
pool,
|
||||
project_id,
|
||||
jobset_id,
|
||||
self.config.time_window_minutes,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(rate) => rate,
|
||||
Err(_) => return None,
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
|
||||
if failure_rate > self.config.error_threshold {
|
||||
|
|
@ -74,6 +79,7 @@ impl AlertManager {
|
|||
|
||||
if time_since_last >= self.config.time_window_minutes {
|
||||
state.last_alert_at = Utc::now();
|
||||
drop(state);
|
||||
info!(
|
||||
"Alert: failure rate {:.1}% exceeds threshold {:.1}%",
|
||||
failure_rate, self.config.error_threshold
|
||||
|
|
|
|||
|
|
@ -21,11 +21,10 @@ use crate::{
|
|||
/// Supports ${VAR}, $VAR, and ~ for home directory.
|
||||
fn expand_path(path: &str) -> String {
|
||||
let expanded = if path.starts_with('~') {
|
||||
if let Some(home) = std::env::var_os("HOME") {
|
||||
path.replacen('~', &home.to_string_lossy(), 1)
|
||||
} else {
|
||||
path.to_string()
|
||||
}
|
||||
std::env::var_os("HOME").map_or_else(
|
||||
|| path.to_string(),
|
||||
|home| path.replacen('~', &home.to_string_lossy(), 1),
|
||||
)
|
||||
} else {
|
||||
path.to_string()
|
||||
};
|
||||
|
|
@ -51,24 +50,25 @@ fn expand_path(path: &str) -> String {
|
|||
|
||||
/// Resolve secret for a webhook from inline value or file.
|
||||
fn resolve_webhook_secret(webhook: &DeclarativeWebhook) -> Option<String> {
|
||||
if let Some(ref secret) = webhook.secret {
|
||||
Some(secret.clone())
|
||||
} else if let Some(ref file) = webhook.secret_file {
|
||||
let expanded = expand_path(file);
|
||||
match std::fs::read_to_string(&expanded) {
|
||||
Ok(s) => Some(s.trim().to_string()),
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
forge_type = %webhook.forge_type,
|
||||
file = %expanded,
|
||||
"Failed to read webhook secret file: {e}"
|
||||
);
|
||||
None
|
||||
},
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
webhook.secret.as_ref().map_or_else(
|
||||
|| {
|
||||
webhook.secret_file.as_ref().and_then(|file| {
|
||||
let expanded = expand_path(file);
|
||||
match std::fs::read_to_string(&expanded) {
|
||||
Ok(s) => Some(s.trim().to_string()),
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
forge_type = %webhook.forge_type,
|
||||
file = %expanded,
|
||||
"Failed to read webhook secret file: {e}"
|
||||
);
|
||||
None
|
||||
},
|
||||
}
|
||||
})
|
||||
},
|
||||
|secret| Some(secret.clone()),
|
||||
)
|
||||
}
|
||||
|
||||
/// Bootstrap declarative configuration into the database.
|
||||
|
|
@ -76,6 +76,10 @@ fn resolve_webhook_secret(webhook: &DeclarativeWebhook) -> Option<String> {
|
|||
/// This function is idempotent: running it multiple times with the same config
|
||||
/// produces the same database state. It upserts (insert or update) all
|
||||
/// configured projects, jobsets, API keys, and users.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operations fail.
|
||||
pub async fn run(pool: &PgPool, config: &DeclarativeConfig) -> Result<()> {
|
||||
if config.projects.is_empty()
|
||||
&& config.api_keys.is_empty()
|
||||
|
|
@ -120,10 +124,10 @@ pub async fn run(pool: &PgPool, config: &DeclarativeConfig) -> Result<()> {
|
|||
let state = decl_jobset.state.as_ref().map(|s| {
|
||||
match s.as_str() {
|
||||
"disabled" => JobsetState::Disabled,
|
||||
"enabled" => JobsetState::Enabled,
|
||||
"one_shot" => JobsetState::OneShot,
|
||||
"one_at_a_time" => JobsetState::OneAtATime,
|
||||
_ => JobsetState::Enabled, // Default to enabled for unknown values
|
||||
_ => JobsetState::Enabled, /* Default to enabled for "enabled" or
|
||||
* unknown values */
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -239,24 +243,25 @@ pub async fn run(pool: &PgPool, config: &DeclarativeConfig) -> Result<()> {
|
|||
// Upsert users
|
||||
for decl_user in &config.users {
|
||||
// Resolve password from inline or file
|
||||
let password = if let Some(ref p) = decl_user.password {
|
||||
Some(p.clone())
|
||||
} else if let Some(ref file) = decl_user.password_file {
|
||||
let expanded = expand_path(file);
|
||||
match std::fs::read_to_string(&expanded) {
|
||||
Ok(p) => Some(p.trim().to_string()),
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
username = %decl_user.username,
|
||||
file = %expanded,
|
||||
"Failed to read password file: {e}"
|
||||
);
|
||||
None
|
||||
},
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let password = decl_user.password.as_ref().map_or_else(
|
||||
|| {
|
||||
decl_user.password_file.as_ref().and_then(|file| {
|
||||
let expanded = expand_path(file);
|
||||
match std::fs::read_to_string(&expanded) {
|
||||
Ok(p) => Some(p.trim().to_string()),
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
username = %decl_user.username,
|
||||
file = %expanded,
|
||||
"Failed to read password file: {e}"
|
||||
);
|
||||
None
|
||||
},
|
||||
}
|
||||
})
|
||||
},
|
||||
|p| Some(p.clone()),
|
||||
);
|
||||
|
||||
// Check if user exists
|
||||
let existing =
|
||||
|
|
|
|||
|
|
@ -202,16 +202,18 @@ pub struct SigningConfig {
|
|||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
#[derive(Default)]
|
||||
pub struct CacheUploadConfig {
|
||||
pub enabled: bool,
|
||||
pub store_uri: Option<String>,
|
||||
/// S3-specific configuration (used when store_uri starts with s3://)
|
||||
/// S3-specific configuration (used when `store_uri` starts with s3://)
|
||||
pub s3: Option<S3CacheConfig>,
|
||||
}
|
||||
|
||||
/// S3-specific cache configuration.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
#[derive(Default)]
|
||||
pub struct S3CacheConfig {
|
||||
/// AWS region (e.g., "us-east-1")
|
||||
pub region: Option<String>,
|
||||
|
|
@ -223,36 +225,12 @@ pub struct S3CacheConfig {
|
|||
pub secret_access_key: Option<String>,
|
||||
/// Session token for temporary credentials (optional)
|
||||
pub session_token: Option<String>,
|
||||
/// Endpoint URL for S3-compatible services (e.g., MinIO)
|
||||
/// Endpoint URL for S3-compatible services (e.g., `MinIO`)
|
||||
pub endpoint_url: Option<String>,
|
||||
/// Whether to use path-style addressing (for MinIO compatibility)
|
||||
/// Whether to use path-style addressing (for `MinIO` compatibility)
|
||||
pub use_path_style: bool,
|
||||
}
|
||||
|
||||
impl Default for S3CacheConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
region: None,
|
||||
prefix: None,
|
||||
access_key_id: None,
|
||||
secret_access_key: None,
|
||||
session_token: None,
|
||||
endpoint_url: None,
|
||||
use_path_style: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CacheUploadConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
enabled: false,
|
||||
store_uri: None,
|
||||
s3: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Declarative project/jobset/api-key/user definitions.
|
||||
/// These are upserted on server startup, enabling fully declarative operation.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
|
|
@ -493,6 +471,11 @@ impl Default for DatabaseConfig {
|
|||
}
|
||||
|
||||
impl DatabaseConfig {
|
||||
/// Validate database configuration.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if configuration is invalid.
|
||||
pub fn validate(&self) -> anyhow::Result<()> {
|
||||
if self.url.is_empty() {
|
||||
return Err(anyhow::anyhow!("Database URL cannot be empty"));
|
||||
|
|
@ -606,6 +589,11 @@ impl Default for CacheConfig {
|
|||
}
|
||||
|
||||
impl Config {
|
||||
/// Load configuration from file and environment variables.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if configuration loading or validation fails.
|
||||
pub fn load() -> anyhow::Result<Self> {
|
||||
let mut settings = config_crate::Config::builder();
|
||||
|
||||
|
|
@ -639,6 +627,11 @@ impl Config {
|
|||
Ok(config)
|
||||
}
|
||||
|
||||
/// Validate all configuration sections.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if any configuration section is invalid.
|
||||
pub fn validate(&self) -> anyhow::Result<()> {
|
||||
// Validate database URL
|
||||
if self.database.url.is_empty() {
|
||||
|
|
|
|||
|
|
@ -12,6 +12,11 @@ pub struct Database {
|
|||
}
|
||||
|
||||
impl Database {
|
||||
/// Create a new database connection pool from config.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if connection fails or health check fails.
|
||||
pub async fn new(config: DatabaseConfig) -> anyhow::Result<Self> {
|
||||
info!("Initializing database connection pool");
|
||||
|
||||
|
|
@ -32,11 +37,17 @@ impl Database {
|
|||
Ok(Self { pool })
|
||||
}
|
||||
|
||||
/// Get a reference to the underlying connection pool.
|
||||
#[must_use]
|
||||
pub const fn pool(&self) -> &PgPool {
|
||||
&self.pool
|
||||
}
|
||||
|
||||
/// Run a simple query to verify the database is reachable.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if query fails or returns unexpected result.
|
||||
pub async fn health_check(pool: &PgPool) -> anyhow::Result<()> {
|
||||
debug!("Performing database health check");
|
||||
|
||||
|
|
@ -52,11 +63,17 @@ impl Database {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Close the connection pool gracefully.
|
||||
pub async fn close(&self) {
|
||||
info!("Closing database connection pool");
|
||||
self.pool.close().await;
|
||||
}
|
||||
|
||||
/// Query database metadata (version, user, address).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if query fails.
|
||||
pub async fn get_connection_info(&self) -> anyhow::Result<ConnectionInfo> {
|
||||
let row = sqlx::query(
|
||||
r"
|
||||
|
|
@ -80,7 +97,9 @@ impl Database {
|
|||
})
|
||||
}
|
||||
|
||||
pub async fn get_pool_stats(&self) -> PoolStats {
|
||||
/// Get current connection pool statistics (size, idle, active).
|
||||
#[must_use]
|
||||
pub fn get_pool_stats(&self) -> PoolStats {
|
||||
let pool = &self.pool;
|
||||
|
||||
PoolStats {
|
||||
|
|
|
|||
|
|
@ -51,6 +51,7 @@ pub enum CiError {
|
|||
}
|
||||
|
||||
impl CiError {
|
||||
/// Check if this error indicates a disk-full condition.
|
||||
#[must_use]
|
||||
pub fn is_disk_full(&self) -> bool {
|
||||
let msg = self.to_string().to_lowercase();
|
||||
|
|
@ -65,6 +66,10 @@ impl CiError {
|
|||
pub type Result<T> = std::result::Result<T, CiError>;
|
||||
|
||||
/// Check disk space on the given path
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if statfs call fails or path is invalid.
|
||||
pub fn check_disk_space(path: &std::path::Path) -> Result<DiskSpaceInfo> {
|
||||
fn to_gb(bytes: u64) -> f64 {
|
||||
bytes as f64 / 1024.0 / 1024.0 / 1024.0
|
||||
|
|
@ -83,9 +88,9 @@ pub fn check_disk_space(path: &std::path::Path) -> Result<DiskSpaceInfo> {
|
|||
return Err(CiError::Io(std::io::Error::last_os_error()));
|
||||
}
|
||||
|
||||
let bavail = statfs.f_bavail * (statfs.f_bsize as u64);
|
||||
let bfree = statfs.f_bfree * (statfs.f_bsize as u64);
|
||||
let btotal = statfs.f_blocks * (statfs.f_bsize as u64);
|
||||
let bavail = statfs.f_bavail * statfs.f_bsize.cast_unsigned();
|
||||
let bfree = statfs.f_bfree * statfs.f_bsize.cast_unsigned();
|
||||
let btotal = statfs.f_blocks * statfs.f_bsize.cast_unsigned();
|
||||
|
||||
Ok(DiskSpaceInfo {
|
||||
total_gb: to_gb(btotal),
|
||||
|
|
|
|||
|
|
@ -13,6 +13,10 @@ use uuid::Uuid;
|
|||
/// Remove GC root symlinks with mtime older than `max_age`. Returns count
|
||||
/// removed. Symlinks whose filename matches a UUID in `pinned_build_ids` are
|
||||
/// skipped regardless of age.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if directory read fails.
|
||||
pub fn cleanup_old_roots(
|
||||
roots_dir: &Path,
|
||||
max_age: Duration,
|
||||
|
|
@ -29,23 +33,20 @@ pub fn cleanup_old_roots(
|
|||
let entry = entry?;
|
||||
|
||||
// Check if this root is pinned (filename is a build UUID with keep=true)
|
||||
if let Some(name) = entry.file_name().to_str() {
|
||||
if let Ok(build_id) = name.parse::<Uuid>() {
|
||||
if pinned_build_ids.contains(&build_id) {
|
||||
debug!(build_id = %build_id, "Skipping pinned GC root");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if let Some(name) = entry.file_name().to_str()
|
||||
&& let Ok(build_id) = name.parse::<Uuid>()
|
||||
&& pinned_build_ids.contains(&build_id)
|
||||
{
|
||||
debug!(build_id = %build_id, "Skipping pinned GC root");
|
||||
continue;
|
||||
}
|
||||
|
||||
let metadata = match entry.metadata() {
|
||||
Ok(m) => m,
|
||||
Err(_) => continue,
|
||||
let Ok(metadata) = entry.metadata() else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let modified = match metadata.modified() {
|
||||
Ok(t) => t,
|
||||
Err(_) => continue,
|
||||
let Ok(modified) = metadata.modified() else {
|
||||
continue;
|
||||
};
|
||||
|
||||
if let Ok(age) = now.duration_since(modified)
|
||||
|
|
@ -71,6 +72,11 @@ pub struct GcRoots {
|
|||
}
|
||||
|
||||
impl GcRoots {
|
||||
/// Create a GC roots manager. Creates the directory if enabled.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if directory creation or permission setting fails.
|
||||
pub fn new(roots_dir: PathBuf, enabled: bool) -> std::io::Result<Self> {
|
||||
if enabled {
|
||||
std::fs::create_dir_all(&roots_dir)?;
|
||||
|
|
@ -87,6 +93,10 @@ impl GcRoots {
|
|||
}
|
||||
|
||||
/// Register a GC root for a build output. Returns the symlink path.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if path is invalid or symlink creation fails.
|
||||
pub fn register(
|
||||
&self,
|
||||
build_id: &uuid::Uuid,
|
||||
|
|
|
|||
|
|
@ -9,6 +9,11 @@ pub struct LogStorage {
|
|||
}
|
||||
|
||||
impl LogStorage {
|
||||
/// Create a log storage instance. Creates the directory if needed.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if directory creation fails.
|
||||
pub fn new(log_dir: PathBuf) -> std::io::Result<Self> {
|
||||
std::fs::create_dir_all(&log_dir)?;
|
||||
Ok(Self { log_dir })
|
||||
|
|
@ -27,6 +32,10 @@ impl LogStorage {
|
|||
}
|
||||
|
||||
/// Write build log content to file
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if file write fails.
|
||||
pub fn write_log(
|
||||
&self,
|
||||
build_id: &Uuid,
|
||||
|
|
@ -50,6 +59,10 @@ impl LogStorage {
|
|||
}
|
||||
|
||||
/// Read a build log from disk. Returns None if the file doesn't exist.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if file read fails.
|
||||
pub fn read_log(&self, build_id: &Uuid) -> std::io::Result<Option<String>> {
|
||||
let path = self.log_path(build_id);
|
||||
if !path.exists() {
|
||||
|
|
@ -60,6 +73,10 @@ impl LogStorage {
|
|||
}
|
||||
|
||||
/// Delete a build log
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if file deletion fails.
|
||||
pub fn delete_log(&self, build_id: &Uuid) -> std::io::Result<()> {
|
||||
let path = self.log_path(build_id);
|
||||
if path.exists() {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,10 @@ use sqlx::{PgPool, Postgres, migrate::MigrateDatabase};
|
|||
use tracing::{error, info, warn};
|
||||
|
||||
/// Runs database migrations and ensures the database exists
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operations or migrations fail.
|
||||
pub async fn run_migrations(database_url: &str) -> anyhow::Result<()> {
|
||||
info!("Starting database migrations");
|
||||
|
||||
|
|
@ -39,6 +43,10 @@ async fn create_connection_pool(database_url: &str) -> anyhow::Result<PgPool> {
|
|||
}
|
||||
|
||||
/// Validates that all required tables exist and have the expected structure
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if schema validation fails or required tables are missing.
|
||||
pub async fn validate_schema(pool: &PgPool) -> anyhow::Result<()> {
|
||||
info!("Validating database schema");
|
||||
|
||||
|
|
|
|||
|
|
@ -32,6 +32,11 @@ pub enum Commands {
|
|||
},
|
||||
}
|
||||
|
||||
/// Execute the CLI command.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if command execution fails.
|
||||
pub async fn run() -> anyhow::Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
|
|
|
|||
|
|
@ -147,20 +147,23 @@ pub enum BuildStatus {
|
|||
|
||||
impl BuildStatus {
|
||||
/// Returns true if the build has completed (not pending or running).
|
||||
pub fn is_finished(&self) -> bool {
|
||||
#[must_use]
|
||||
pub const fn is_finished(&self) -> bool {
|
||||
!matches!(self, Self::Pending | Self::Running)
|
||||
}
|
||||
|
||||
/// Returns true if the build succeeded.
|
||||
/// Note: Does NOT include CachedFailure - a cached failure is still a
|
||||
/// Note: Does NOT include `CachedFailure` - a cached failure is still a
|
||||
/// failure.
|
||||
pub fn is_success(&self) -> bool {
|
||||
#[must_use]
|
||||
pub const fn is_success(&self) -> bool {
|
||||
matches!(self, Self::Succeeded)
|
||||
}
|
||||
|
||||
/// Returns true if the build completed without needing a retry.
|
||||
/// This includes both successful builds and cached failures.
|
||||
pub fn is_terminal(&self) -> bool {
|
||||
#[must_use]
|
||||
pub const fn is_terminal(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::Succeeded
|
||||
|
|
@ -180,7 +183,8 @@ impl BuildStatus {
|
|||
|
||||
/// Returns the database integer representation of this status.
|
||||
/// Note: This uses an internal numbering scheme (0-13), not Hydra exit codes.
|
||||
pub fn as_i32(&self) -> i32 {
|
||||
#[must_use]
|
||||
pub const fn as_i32(&self) -> i32 {
|
||||
match self {
|
||||
Self::Pending => 0,
|
||||
Self::Running => 1,
|
||||
|
|
@ -199,9 +203,10 @@ impl BuildStatus {
|
|||
}
|
||||
}
|
||||
|
||||
/// Converts a database integer to BuildStatus.
|
||||
/// This is the inverse of as_i32() for reading from the database.
|
||||
pub fn from_i32(code: i32) -> Option<Self> {
|
||||
/// Converts a database integer to `BuildStatus`.
|
||||
/// This is the inverse of `as_i32()` for reading from the database.
|
||||
#[must_use]
|
||||
pub const fn from_i32(code: i32) -> Option<Self> {
|
||||
match code {
|
||||
0 => Some(Self::Pending),
|
||||
1 => Some(Self::Running),
|
||||
|
|
@ -221,17 +226,17 @@ impl BuildStatus {
|
|||
}
|
||||
}
|
||||
|
||||
/// Converts a Hydra-compatible exit code to a BuildStatus.
|
||||
/// Converts a Hydra-compatible exit code to a `BuildStatus`.
|
||||
/// Note: These codes follow Hydra's conventions and differ from
|
||||
/// as_i32/from_i32.
|
||||
pub fn from_exit_code(exit_code: i32) -> Self {
|
||||
/// `as_i32/from_i32`.
|
||||
#[must_use]
|
||||
pub const fn from_exit_code(exit_code: i32) -> Self {
|
||||
match exit_code {
|
||||
0 => Self::Succeeded,
|
||||
1 => Self::Failed,
|
||||
2 => Self::DependencyFailed,
|
||||
3 => Self::Aborted,
|
||||
3 | 5 => Self::Aborted, // 5 is obsolete in Hydra, treat as aborted
|
||||
4 => Self::Cancelled,
|
||||
5 => Self::Aborted, // Obsolete in Hydra, treat as aborted
|
||||
6 => Self::FailedWithOutput,
|
||||
7 => Self::Timeout,
|
||||
8 => Self::CachedFailure,
|
||||
|
|
@ -262,7 +267,7 @@ impl std::fmt::Display for BuildStatus {
|
|||
Self::NarSizeLimitExceeded => "nar size limit exceeded",
|
||||
Self::NonDeterministic => "non-deterministic",
|
||||
};
|
||||
write!(f, "{}", s)
|
||||
write!(f, "{s}")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -320,7 +325,7 @@ pub mod metric_units {
|
|||
pub const BYTES: &str = "bytes";
|
||||
}
|
||||
|
||||
/// Active jobset view — enabled jobsets joined with project info.
|
||||
/// Active jobsets joined with project info.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
|
||||
pub struct ActiveJobset {
|
||||
pub id: Uuid,
|
||||
|
|
@ -398,7 +403,7 @@ pub struct JobsetInput {
|
|||
pub created_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
/// Release channel — tracks the latest "good" evaluation for a jobset.
|
||||
/// Tracks the latest "good" evaluation for a jobset.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
|
||||
pub struct Channel {
|
||||
pub id: Uuid,
|
||||
|
|
@ -430,6 +435,21 @@ pub struct RemoteBuilder {
|
|||
pub last_failure: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
/// Parameters for creating or updating a remote builder.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RemoteBuilderParams<'a> {
|
||||
pub name: &'a str,
|
||||
pub ssh_uri: &'a str,
|
||||
pub systems: &'a [String],
|
||||
pub max_jobs: i32,
|
||||
pub speed_factor: i32,
|
||||
pub supported_features: &'a [String],
|
||||
pub mandatory_features: &'a [String],
|
||||
pub enabled: bool,
|
||||
pub public_host_key: Option<&'a str>,
|
||||
pub ssh_key_file: Option<&'a str>,
|
||||
}
|
||||
|
||||
/// User account for authentication and personalization
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
|
||||
pub struct User {
|
||||
|
|
|
|||
|
|
@ -84,6 +84,10 @@ fn to_flake_ref(url: &str) -> String {
|
|||
}
|
||||
|
||||
/// Probe a flake repository to discover its outputs and suggest jobsets.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if nix flake show command fails or times out.
|
||||
pub async fn probe_flake(
|
||||
repo_url: &str,
|
||||
revision: Option<&str>,
|
||||
|
|
@ -157,13 +161,10 @@ pub async fn probe_flake(
|
|||
CiError::NixEval(format!("Failed to parse flake show output: {e}"))
|
||||
})?;
|
||||
|
||||
let top = match raw.as_object() {
|
||||
Some(obj) => obj,
|
||||
None => {
|
||||
return Err(CiError::NixEval(
|
||||
"Unexpected flake show output format".to_string(),
|
||||
));
|
||||
},
|
||||
let Some(top) = raw.as_object() else {
|
||||
return Err(CiError::NixEval(
|
||||
"Unexpected flake show output format".to_string(),
|
||||
));
|
||||
};
|
||||
|
||||
let mut outputs = Vec::new();
|
||||
|
|
@ -220,7 +221,7 @@ pub async fn probe_flake(
|
|||
}
|
||||
|
||||
// Sort jobsets by priority (highest first)
|
||||
suggested_jobsets.sort_by(|a, b| b.priority.cmp(&a.priority));
|
||||
suggested_jobsets.sort_by_key(|j| std::cmp::Reverse(j.priority));
|
||||
|
||||
// Extract metadata from the flake
|
||||
let metadata = FlakeMetadata {
|
||||
|
|
@ -441,7 +442,7 @@ mod tests {
|
|||
},
|
||||
];
|
||||
|
||||
jobsets.sort_by(|a, b| b.priority.cmp(&a.priority));
|
||||
jobsets.sort_by_key(|j| std::cmp::Reverse(j.priority));
|
||||
assert_eq!(jobsets[0].name, "hydraJobs");
|
||||
assert_eq!(jobsets[1].name, "checks");
|
||||
assert_eq!(jobsets[2].name, "packages");
|
||||
|
|
|
|||
|
|
@ -267,9 +267,7 @@ async fn set_github_status(
|
|||
build: &Build,
|
||||
) {
|
||||
// Parse owner/repo from URL
|
||||
let (owner, repo) = if let Some(v) = parse_github_repo(repo_url) {
|
||||
v
|
||||
} else {
|
||||
let Some((owner, repo)) = parse_github_repo(repo_url) else {
|
||||
warn!("Cannot parse GitHub owner/repo from {repo_url}");
|
||||
return;
|
||||
};
|
||||
|
|
@ -330,9 +328,7 @@ async fn set_gitea_status(
|
|||
build: &Build,
|
||||
) {
|
||||
// Parse owner/repo from URL (try to extract from the gitea URL)
|
||||
let (owner, repo) = if let Some(v) = parse_gitea_repo(repo_url, base_url) {
|
||||
v
|
||||
} else {
|
||||
let Some((owner, repo)) = parse_gitea_repo(repo_url, base_url) else {
|
||||
warn!("Cannot parse Gitea owner/repo from {repo_url}");
|
||||
return;
|
||||
};
|
||||
|
|
@ -390,9 +386,7 @@ async fn set_gitlab_status(
|
|||
build: &Build,
|
||||
) {
|
||||
// Parse project path from URL
|
||||
let project_path = if let Some(p) = parse_gitlab_project(repo_url, base_url) {
|
||||
p
|
||||
} else {
|
||||
let Some(project_path) = parse_gitlab_project(repo_url, base_url) else {
|
||||
warn!("Cannot parse GitLab project from {repo_url}");
|
||||
return;
|
||||
};
|
||||
|
|
@ -606,6 +600,10 @@ async fn send_email_notification(
|
|||
}
|
||||
|
||||
/// Process a notification task from the retry queue
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if notification delivery fails.
|
||||
pub async fn process_notification_task(
|
||||
task: &crate::models::NotificationTask,
|
||||
) -> Result<(), String> {
|
||||
|
|
@ -618,7 +616,7 @@ pub async fn process_notification_task(
|
|||
.as_str()
|
||||
.ok_or("Missing url in webhook payload")?;
|
||||
let status_str = match payload["build_status"].as_str() {
|
||||
Some("succeeded") | Some("cached_failure") => "success",
|
||||
Some("succeeded" | "cached_failure") => "success",
|
||||
Some("failed") => "failure",
|
||||
Some("cancelled") => "cancelled",
|
||||
Some("aborted") => "aborted",
|
||||
|
|
@ -667,9 +665,7 @@ pub async fn process_notification_task(
|
|||
.ok_or_else(|| format!("Cannot parse GitHub repo from {repo_url}"))?;
|
||||
|
||||
let (state, description) = match payload["build_status"].as_str() {
|
||||
Some("succeeded") | Some("cached_failure") => {
|
||||
("success", "Build succeeded")
|
||||
},
|
||||
Some("succeeded" | "cached_failure") => ("success", "Build succeeded"),
|
||||
Some("failed") => ("failure", "Build failed"),
|
||||
Some("running") => ("pending", "Build in progress"),
|
||||
Some("cancelled") => ("error", "Build cancelled"),
|
||||
|
|
@ -721,9 +717,7 @@ pub async fn process_notification_task(
|
|||
.ok_or_else(|| format!("Cannot parse Gitea repo from {repo_url}"))?;
|
||||
|
||||
let (state, description) = match payload["build_status"].as_str() {
|
||||
Some("succeeded") | Some("cached_failure") => {
|
||||
("success", "Build succeeded")
|
||||
},
|
||||
Some("succeeded" | "cached_failure") => ("success", "Build succeeded"),
|
||||
Some("failed") => ("failure", "Build failed"),
|
||||
Some("running") => ("pending", "Build in progress"),
|
||||
Some("cancelled") => ("error", "Build cancelled"),
|
||||
|
|
@ -774,9 +768,7 @@ pub async fn process_notification_task(
|
|||
})?;
|
||||
|
||||
let (state, description) = match payload["build_status"].as_str() {
|
||||
Some("succeeded") | Some("cached_failure") => {
|
||||
("success", "Build succeeded")
|
||||
},
|
||||
Some("succeeded" | "cached_failure") => ("success", "Build succeeded"),
|
||||
Some("failed") => ("failed", "Build failed"),
|
||||
Some("running") => ("running", "Build in progress"),
|
||||
Some("cancelled") => ("canceled", "Build cancelled"),
|
||||
|
|
@ -814,6 +806,14 @@ pub async fn process_notification_task(
|
|||
Ok(())
|
||||
},
|
||||
"email" => {
|
||||
use lettre::{
|
||||
AsyncSmtpTransport,
|
||||
AsyncTransport,
|
||||
Message,
|
||||
Tokio1Executor,
|
||||
transport::smtp::authentication::Credentials,
|
||||
};
|
||||
|
||||
// Email sending is complex, so we'll reuse the existing function
|
||||
// by deserializing the config from payload
|
||||
let email_config: EmailConfig =
|
||||
|
|
@ -841,7 +841,6 @@ pub async fn process_notification_task(
|
|||
.ok_or("Missing build_status")?;
|
||||
let status = match status_str {
|
||||
"succeeded" => BuildStatus::Succeeded,
|
||||
"failed" => BuildStatus::Failed,
|
||||
_ => BuildStatus::Failed,
|
||||
};
|
||||
|
||||
|
|
@ -849,23 +848,13 @@ pub async fn process_notification_task(
|
|||
.as_str()
|
||||
.ok_or("Missing project_name")?;
|
||||
|
||||
// Simplified email send (direct implementation to avoid complex struct
|
||||
// creation)
|
||||
use lettre::{
|
||||
AsyncSmtpTransport,
|
||||
AsyncTransport,
|
||||
Message,
|
||||
Tokio1Executor,
|
||||
transport::smtp::authentication::Credentials,
|
||||
};
|
||||
|
||||
let status_display = match status {
|
||||
BuildStatus::Succeeded => "SUCCESS",
|
||||
_ => "FAILURE",
|
||||
};
|
||||
|
||||
let subject =
|
||||
format!("[FC] {} - {} ({})", status_display, job_name, project_name);
|
||||
format!("[FC] {status_display} - {job_name} ({project_name})");
|
||||
let body = format!(
|
||||
"Build notification from FC CI\n\nProject: {}\nJob: {}\nStatus: \
|
||||
{}\nDerivation: {}\nOutput: {}\nBuild ID: {}\n",
|
||||
|
|
|
|||
|
|
@ -6,6 +6,11 @@ use crate::{
|
|||
models::ApiKey,
|
||||
};
|
||||
|
||||
/// Create a new API key.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails or key already exists.
|
||||
pub async fn create(
|
||||
pool: &PgPool,
|
||||
name: &str,
|
||||
|
|
@ -31,6 +36,11 @@ pub async fn create(
|
|||
})
|
||||
}
|
||||
|
||||
/// Insert or update an API key by hash.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operation fails.
|
||||
pub async fn upsert(
|
||||
pool: &PgPool,
|
||||
name: &str,
|
||||
|
|
@ -50,6 +60,11 @@ pub async fn upsert(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Find an API key by its hash.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_by_hash(
|
||||
pool: &PgPool,
|
||||
key_hash: &str,
|
||||
|
|
@ -61,6 +76,11 @@ pub async fn get_by_hash(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// List all API keys.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list(pool: &PgPool) -> Result<Vec<ApiKey>> {
|
||||
sqlx::query_as::<_, ApiKey>("SELECT * FROM api_keys ORDER BY created_at DESC")
|
||||
.fetch_all(pool)
|
||||
|
|
@ -68,6 +88,11 @@ pub async fn list(pool: &PgPool) -> Result<Vec<ApiKey>> {
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Delete an API key by ID.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails or key not found.
|
||||
pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
||||
let result = sqlx::query("DELETE FROM api_keys WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -79,6 +104,11 @@ pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Update the `last_used_at` timestamp for an API key.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails.
|
||||
pub async fn touch_last_used(pool: &PgPool, id: Uuid) -> Result<()> {
|
||||
sqlx::query("UPDATE api_keys SET last_used_at = NOW() WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,11 @@ use crate::{
|
|||
models::BuildDependency,
|
||||
};
|
||||
|
||||
/// Create a build dependency relationship.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails or dependency already exists.
|
||||
pub async fn create(
|
||||
pool: &PgPool,
|
||||
build_id: Uuid,
|
||||
|
|
@ -31,6 +36,11 @@ pub async fn create(
|
|||
})
|
||||
}
|
||||
|
||||
/// List all dependencies for a build.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_for_build(
|
||||
pool: &PgPool,
|
||||
build_id: Uuid,
|
||||
|
|
@ -46,6 +56,10 @@ pub async fn list_for_build(
|
|||
|
||||
/// Batch check if all dependency builds are completed for multiple builds at
|
||||
/// once. Returns a map from `build_id` to whether all deps are completed.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn check_deps_for_builds(
|
||||
pool: &PgPool,
|
||||
build_ids: &[Uuid],
|
||||
|
|
@ -77,6 +91,10 @@ pub async fn check_deps_for_builds(
|
|||
}
|
||||
|
||||
/// Check if all dependency builds for a given build are completed.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn all_deps_completed(pool: &PgPool, build_id: Uuid) -> Result<bool> {
|
||||
let row: (i64,) = sqlx::query_as(
|
||||
"SELECT COUNT(*) FROM build_dependencies bd JOIN builds b ON \
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ use crate::{
|
|||
models::BuildMetric,
|
||||
};
|
||||
|
||||
type PercentileRow = (DateTime<Utc>, Option<f64>, Option<f64>, Option<f64>);
|
||||
|
||||
/// Time-series data point for metrics visualization.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TimeseriesPoint {
|
||||
|
|
@ -32,6 +34,11 @@ pub struct DurationPercentiles {
|
|||
pub p99: Option<f64>,
|
||||
}
|
||||
|
||||
/// Insert or update a build metric.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operation fails.
|
||||
pub async fn upsert(
|
||||
pool: &PgPool,
|
||||
build_id: Uuid,
|
||||
|
|
@ -54,6 +61,11 @@ pub async fn upsert(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Calculate build failure rate over a time window.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn calculate_failure_rate(
|
||||
pool: &PgPool,
|
||||
project_id: Option<Uuid>,
|
||||
|
|
@ -87,6 +99,10 @@ pub async fn calculate_failure_rate(
|
|||
|
||||
/// Get build success/failure counts over time.
|
||||
/// Buckets builds by time interval for charting.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_build_stats_timeseries(
|
||||
pool: &PgPool,
|
||||
project_id: Option<Uuid>,
|
||||
|
|
@ -136,6 +152,10 @@ pub async fn get_build_stats_timeseries(
|
|||
}
|
||||
|
||||
/// Get build duration percentiles over time.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_duration_percentiles_timeseries(
|
||||
pool: &PgPool,
|
||||
project_id: Option<Uuid>,
|
||||
|
|
@ -143,18 +163,17 @@ pub async fn get_duration_percentiles_timeseries(
|
|||
hours: i32,
|
||||
bucket_minutes: i32,
|
||||
) -> Result<Vec<DurationPercentiles>> {
|
||||
let rows: Vec<(DateTime<Utc>, Option<f64>, Option<f64>, Option<f64>)> =
|
||||
sqlx::query_as(
|
||||
"SELECT
|
||||
let rows: Vec<PercentileRow> = sqlx::query_as(
|
||||
"SELECT
|
||||
date_trunc('minute', b.completed_at) +
|
||||
(EXTRACT(MINUTE FROM b.completed_at)::int / $4) * INTERVAL '1 minute' \
|
||||
* $4 AS bucket_time,
|
||||
* $4 AS bucket_time,
|
||||
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY EXTRACT(EPOCH FROM \
|
||||
(b.completed_at - b.started_at))) AS p50,
|
||||
(b.completed_at - b.started_at))) AS p50,
|
||||
PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY EXTRACT(EPOCH FROM \
|
||||
(b.completed_at - b.started_at))) AS p95,
|
||||
(b.completed_at - b.started_at))) AS p95,
|
||||
PERCENTILE_CONT(0.99) WITHIN GROUP (ORDER BY EXTRACT(EPOCH FROM \
|
||||
(b.completed_at - b.started_at))) AS p99
|
||||
(b.completed_at - b.started_at))) AS p99
|
||||
FROM builds b
|
||||
JOIN evaluations e ON b.evaluation_id = e.id
|
||||
JOIN jobsets j ON e.jobset_id = j.id
|
||||
|
|
@ -165,14 +184,14 @@ pub async fn get_duration_percentiles_timeseries(
|
|||
AND ($3::uuid IS NULL OR j.id = $3)
|
||||
GROUP BY bucket_time
|
||||
ORDER BY bucket_time ASC",
|
||||
)
|
||||
.bind(hours)
|
||||
.bind(project_id)
|
||||
.bind(jobset_id)
|
||||
.bind(bucket_minutes)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(CiError::Database)?;
|
||||
)
|
||||
.bind(hours)
|
||||
.bind(project_id)
|
||||
.bind(jobset_id)
|
||||
.bind(bucket_minutes)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(CiError::Database)?;
|
||||
|
||||
Ok(
|
||||
rows
|
||||
|
|
@ -190,6 +209,10 @@ pub async fn get_duration_percentiles_timeseries(
|
|||
}
|
||||
|
||||
/// Get queue depth over time.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_queue_depth_timeseries(
|
||||
pool: &PgPool,
|
||||
hours: i32,
|
||||
|
|
@ -228,6 +251,10 @@ pub async fn get_queue_depth_timeseries(
|
|||
}
|
||||
|
||||
/// Get per-system build distribution.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_system_distribution(
|
||||
pool: &PgPool,
|
||||
project_id: Option<Uuid>,
|
||||
|
|
|
|||
|
|
@ -6,6 +6,11 @@ use crate::{
|
|||
models::{BuildProduct, CreateBuildProduct},
|
||||
};
|
||||
|
||||
/// Create a build product record.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails.
|
||||
pub async fn create(
|
||||
pool: &PgPool,
|
||||
input: CreateBuildProduct,
|
||||
|
|
@ -27,6 +32,11 @@ pub async fn create(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Get a build product by ID.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails or product not found.
|
||||
pub async fn get(pool: &PgPool, id: Uuid) -> Result<BuildProduct> {
|
||||
sqlx::query_as::<_, BuildProduct>(
|
||||
"SELECT * FROM build_products WHERE id = $1",
|
||||
|
|
@ -37,6 +47,11 @@ pub async fn get(pool: &PgPool, id: Uuid) -> Result<BuildProduct> {
|
|||
.ok_or_else(|| CiError::NotFound(format!("Build product {id} not found")))
|
||||
}
|
||||
|
||||
/// List all build products for a build.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_for_build(
|
||||
pool: &PgPool,
|
||||
build_id: Uuid,
|
||||
|
|
|
|||
|
|
@ -6,6 +6,11 @@ use crate::{
|
|||
models::{BuildStep, CreateBuildStep},
|
||||
};
|
||||
|
||||
/// Create a build step record.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails or step already exists.
|
||||
pub async fn create(
|
||||
pool: &PgPool,
|
||||
input: CreateBuildStep,
|
||||
|
|
@ -32,6 +37,11 @@ pub async fn create(
|
|||
})
|
||||
}
|
||||
|
||||
/// Mark a build step as completed.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails or step not found.
|
||||
pub async fn complete(
|
||||
pool: &PgPool,
|
||||
id: Uuid,
|
||||
|
|
@ -52,6 +62,11 @@ pub async fn complete(
|
|||
.ok_or_else(|| CiError::NotFound(format!("Build step {id} not found")))
|
||||
}
|
||||
|
||||
/// List all build steps for a build.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_for_build(
|
||||
pool: &PgPool,
|
||||
build_id: Uuid,
|
||||
|
|
|
|||
|
|
@ -6,6 +6,11 @@ use crate::{
|
|||
models::{Build, BuildStats, BuildStatus, CreateBuild},
|
||||
};
|
||||
|
||||
/// Create a new build record in pending state.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails or job already exists.
|
||||
pub async fn create(pool: &PgPool, input: CreateBuild) -> Result<Build> {
|
||||
let is_aggregate = input.is_aggregate.unwrap_or(false);
|
||||
sqlx::query_as::<_, Build>(
|
||||
|
|
@ -35,6 +40,11 @@ pub async fn create(pool: &PgPool, input: CreateBuild) -> Result<Build> {
|
|||
})
|
||||
}
|
||||
|
||||
/// Find a succeeded build by derivation path (for build result caching).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_completed_by_drv_path(
|
||||
pool: &PgPool,
|
||||
drv_path: &str,
|
||||
|
|
@ -48,6 +58,11 @@ pub async fn get_completed_by_drv_path(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Get a build by ID.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails or build not found.
|
||||
pub async fn get(pool: &PgPool, id: Uuid) -> Result<Build> {
|
||||
sqlx::query_as::<_, Build>("SELECT * FROM builds WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -56,6 +71,11 @@ pub async fn get(pool: &PgPool, id: Uuid) -> Result<Build> {
|
|||
.ok_or_else(|| CiError::NotFound(format!("Build {id} not found")))
|
||||
}
|
||||
|
||||
/// List all builds for a given evaluation.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_for_evaluation(
|
||||
pool: &PgPool,
|
||||
evaluation_id: Uuid,
|
||||
|
|
@ -69,6 +89,12 @@ pub async fn list_for_evaluation(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// List pending builds, prioritizing non-aggregate jobs.
|
||||
/// Returns up to `limit * worker_count` builds.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_pending(
|
||||
pool: &PgPool,
|
||||
limit: i64,
|
||||
|
|
@ -99,6 +125,10 @@ pub async fn list_pending(
|
|||
|
||||
/// Atomically claim a pending build by setting it to running.
|
||||
/// Returns `None` if the build was already claimed by another worker.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails.
|
||||
pub async fn start(pool: &PgPool, id: Uuid) -> Result<Option<Build>> {
|
||||
sqlx::query_as::<_, Build>(
|
||||
"UPDATE builds SET status = 'running', started_at = NOW() WHERE id = $1 \
|
||||
|
|
@ -110,6 +140,11 @@ pub async fn start(pool: &PgPool, id: Uuid) -> Result<Option<Build>> {
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Mark a build as completed with final status and outputs.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails or build not found.
|
||||
pub async fn complete(
|
||||
pool: &PgPool,
|
||||
id: Uuid,
|
||||
|
|
@ -132,6 +167,11 @@ pub async fn complete(
|
|||
.ok_or_else(|| CiError::NotFound(format!("Build {id} not found")))
|
||||
}
|
||||
|
||||
/// List recent builds ordered by creation time.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_recent(pool: &PgPool, limit: i64) -> Result<Vec<Build>> {
|
||||
sqlx::query_as::<_, Build>(
|
||||
"SELECT * FROM builds ORDER BY created_at DESC LIMIT $1",
|
||||
|
|
@ -142,6 +182,11 @@ pub async fn list_recent(pool: &PgPool, limit: i64) -> Result<Vec<Build>> {
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// List all builds for a project.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_for_project(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -157,6 +202,11 @@ pub async fn list_for_project(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Get aggregate build statistics.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_stats(pool: &PgPool) -> Result<BuildStats> {
|
||||
match sqlx::query_as::<_, BuildStats>("SELECT * FROM build_stats")
|
||||
.fetch_optional(pool)
|
||||
|
|
@ -178,6 +228,10 @@ pub async fn get_stats(pool: &PgPool) -> Result<BuildStats> {
|
|||
|
||||
/// Reset builds that were left in 'running' state (orphaned by a crashed
|
||||
/// runner). Limited to 50 builds per call to prevent thundering herd.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails.
|
||||
pub async fn reset_orphaned(
|
||||
pool: &PgPool,
|
||||
older_than_secs: i64,
|
||||
|
|
@ -197,6 +251,10 @@ pub async fn reset_orphaned(
|
|||
|
||||
/// List builds with optional `evaluation_id`, status, system, and `job_name`
|
||||
/// filters, with pagination.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_filtered(
|
||||
pool: &PgPool,
|
||||
evaluation_id: Option<Uuid>,
|
||||
|
|
@ -223,6 +281,11 @@ pub async fn list_filtered(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Count builds matching filter criteria.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn count_filtered(
|
||||
pool: &PgPool,
|
||||
evaluation_id: Option<Uuid>,
|
||||
|
|
@ -247,6 +310,10 @@ pub async fn count_filtered(
|
|||
|
||||
/// Return the subset of the given build IDs whose status is 'cancelled'.
|
||||
/// Used by the cancel-checker loop to detect builds cancelled while running.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_cancelled_among(
|
||||
pool: &PgPool,
|
||||
build_ids: &[Uuid],
|
||||
|
|
@ -265,6 +332,11 @@ pub async fn get_cancelled_among(
|
|||
Ok(rows.into_iter().map(|(id,)| id).collect())
|
||||
}
|
||||
|
||||
/// Cancel a build.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails or build not in cancellable state.
|
||||
pub async fn cancel(pool: &PgPool, id: Uuid) -> Result<Build> {
|
||||
sqlx::query_as::<_, Build>(
|
||||
"UPDATE builds SET status = 'cancelled', completed_at = NOW() WHERE id = \
|
||||
|
|
@ -281,6 +353,10 @@ pub async fn cancel(pool: &PgPool, id: Uuid) -> Result<Build> {
|
|||
}
|
||||
|
||||
/// Cancel a build and all its transitive dependents.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn cancel_cascade(pool: &PgPool, id: Uuid) -> Result<Vec<Build>> {
|
||||
let mut cancelled = Vec::new();
|
||||
|
||||
|
|
@ -312,7 +388,11 @@ pub async fn cancel_cascade(pool: &PgPool, id: Uuid) -> Result<Vec<Build>> {
|
|||
}
|
||||
|
||||
/// Restart a build by resetting it to pending state.
|
||||
/// Only works for failed, succeeded, cancelled, or cached_failure builds.
|
||||
/// Only works for failed, succeeded, cancelled, or `cached_failure` builds.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails or build not in restartable state.
|
||||
pub async fn restart(pool: &PgPool, id: Uuid) -> Result<Build> {
|
||||
let build = sqlx::query_as::<_, Build>(
|
||||
"UPDATE builds SET status = 'pending', started_at = NULL, completed_at = \
|
||||
|
|
@ -339,6 +419,10 @@ pub async fn restart(pool: &PgPool, id: Uuid) -> Result<Build> {
|
|||
}
|
||||
|
||||
/// Mark a build's outputs as signed.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails.
|
||||
pub async fn mark_signed(pool: &PgPool, id: Uuid) -> Result<()> {
|
||||
sqlx::query("UPDATE builds SET signed = true WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -350,6 +434,10 @@ pub async fn mark_signed(pool: &PgPool, id: Uuid) -> Result<()> {
|
|||
|
||||
/// Batch-fetch completed builds by derivation paths.
|
||||
/// Returns a map from `drv_path` to Build for deduplication.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_completed_by_drv_paths(
|
||||
pool: &PgPool,
|
||||
drv_paths: &[String],
|
||||
|
|
@ -375,6 +463,10 @@ pub async fn get_completed_by_drv_paths(
|
|||
}
|
||||
|
||||
/// Return the set of build IDs that have `keep = true` (GC-pinned).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_pinned_ids(
|
||||
pool: &PgPool,
|
||||
) -> Result<std::collections::HashSet<Uuid>> {
|
||||
|
|
@ -387,6 +479,10 @@ pub async fn list_pinned_ids(
|
|||
}
|
||||
|
||||
/// Set the `keep` (GC pin) flag on a build.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails or build not found.
|
||||
pub async fn set_keep(pool: &PgPool, id: Uuid, keep: bool) -> Result<Build> {
|
||||
sqlx::query_as::<_, Build>(
|
||||
"UPDATE builds SET keep = $1 WHERE id = $2 RETURNING *",
|
||||
|
|
@ -399,6 +495,10 @@ pub async fn set_keep(pool: &PgPool, id: Uuid, keep: bool) -> Result<Build> {
|
|||
}
|
||||
|
||||
/// Set the `builder_id` for a build.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails.
|
||||
pub async fn set_builder(
|
||||
pool: &PgPool,
|
||||
id: Uuid,
|
||||
|
|
|
|||
|
|
@ -7,6 +7,11 @@ use crate::{
|
|||
models::{Channel, CreateChannel},
|
||||
};
|
||||
|
||||
/// Create a release channel.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails or channel already exists.
|
||||
pub async fn create(pool: &PgPool, input: CreateChannel) -> Result<Channel> {
|
||||
sqlx::query_as::<_, Channel>(
|
||||
"INSERT INTO channels (project_id, name, jobset_id) VALUES ($1, $2, $3) \
|
||||
|
|
@ -30,6 +35,11 @@ pub async fn create(pool: &PgPool, input: CreateChannel) -> Result<Channel> {
|
|||
})
|
||||
}
|
||||
|
||||
/// Get a channel by ID.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails or channel not found.
|
||||
pub async fn get(pool: &PgPool, id: Uuid) -> Result<Channel> {
|
||||
sqlx::query_as::<_, Channel>("SELECT * FROM channels WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -38,6 +48,11 @@ pub async fn get(pool: &PgPool, id: Uuid) -> Result<Channel> {
|
|||
.ok_or_else(|| CiError::NotFound(format!("Channel {id} not found")))
|
||||
}
|
||||
|
||||
/// List all channels for a project.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_for_project(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -51,6 +66,11 @@ pub async fn list_for_project(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// List all channels.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_all(pool: &PgPool) -> Result<Vec<Channel>> {
|
||||
sqlx::query_as::<_, Channel>("SELECT * FROM channels ORDER BY name")
|
||||
.fetch_all(pool)
|
||||
|
|
@ -59,6 +79,10 @@ pub async fn list_all(pool: &PgPool) -> Result<Vec<Channel>> {
|
|||
}
|
||||
|
||||
/// Promote an evaluation to a channel (set it as the current evaluation).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails or channel not found.
|
||||
pub async fn promote(
|
||||
pool: &PgPool,
|
||||
channel_id: Uuid,
|
||||
|
|
@ -75,6 +99,11 @@ pub async fn promote(
|
|||
.ok_or_else(|| CiError::NotFound(format!("Channel {channel_id} not found")))
|
||||
}
|
||||
|
||||
/// Delete a channel.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails or channel not found.
|
||||
pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
||||
let result = sqlx::query("DELETE FROM channels WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -88,6 +117,10 @@ pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
|||
}
|
||||
|
||||
/// Upsert a channel (insert or update on conflict).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operation fails.
|
||||
pub async fn upsert(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -109,6 +142,10 @@ pub async fn upsert(
|
|||
|
||||
/// Sync channels from declarative config.
|
||||
/// Deletes channels not in the declarative list and upserts those that are.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operations fail.
|
||||
pub async fn sync_for_project(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -146,6 +183,10 @@ pub async fn sync_for_project(
|
|||
|
||||
/// Find the channel for a jobset and auto-promote if all builds in the
|
||||
/// evaluation succeeded.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operations fail.
|
||||
pub async fn auto_promote_if_complete(
|
||||
pool: &PgPool,
|
||||
jobset_id: Uuid,
|
||||
|
|
@ -166,7 +207,7 @@ pub async fn auto_promote_if_complete(
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
// All builds completed — promote to any channels tracking this jobset
|
||||
// All builds completed, promote to any channels tracking this jobset
|
||||
let channels =
|
||||
sqlx::query_as::<_, Channel>("SELECT * FROM channels WHERE jobset_id = $1")
|
||||
.bind(jobset_id)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,11 @@ use crate::{
|
|||
models::{CreateEvaluation, Evaluation, EvaluationStatus},
|
||||
};
|
||||
|
||||
/// Create a new evaluation in pending state.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails or evaluation already exists.
|
||||
pub async fn create(
|
||||
pool: &PgPool,
|
||||
input: CreateEvaluation,
|
||||
|
|
@ -36,6 +41,11 @@ pub async fn create(
|
|||
})
|
||||
}
|
||||
|
||||
/// Get an evaluation by ID.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails or evaluation not found.
|
||||
pub async fn get(pool: &PgPool, id: Uuid) -> Result<Evaluation> {
|
||||
sqlx::query_as::<_, Evaluation>("SELECT * FROM evaluations WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -44,6 +54,11 @@ pub async fn get(pool: &PgPool, id: Uuid) -> Result<Evaluation> {
|
|||
.ok_or_else(|| CiError::NotFound(format!("Evaluation {id} not found")))
|
||||
}
|
||||
|
||||
/// List all evaluations for a jobset.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_for_jobset(
|
||||
pool: &PgPool,
|
||||
jobset_id: Uuid,
|
||||
|
|
@ -60,6 +75,10 @@ pub async fn list_for_jobset(
|
|||
|
||||
/// List evaluations with optional `jobset_id` and status filters, with
|
||||
/// pagination.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_filtered(
|
||||
pool: &PgPool,
|
||||
jobset_id: Option<Uuid>,
|
||||
|
|
@ -81,6 +100,11 @@ pub async fn list_filtered(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Count evaluations matching filter criteria.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn count_filtered(
|
||||
pool: &PgPool,
|
||||
jobset_id: Option<Uuid>,
|
||||
|
|
@ -98,6 +122,11 @@ pub async fn count_filtered(
|
|||
Ok(row.0)
|
||||
}
|
||||
|
||||
/// Update evaluation status and optional error message.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails or evaluation not found.
|
||||
pub async fn update_status(
|
||||
pool: &PgPool,
|
||||
id: Uuid,
|
||||
|
|
@ -116,6 +145,11 @@ pub async fn update_status(
|
|||
.ok_or_else(|| CiError::NotFound(format!("Evaluation {id} not found")))
|
||||
}
|
||||
|
||||
/// Get the latest evaluation for a jobset.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_latest(
|
||||
pool: &PgPool,
|
||||
jobset_id: Uuid,
|
||||
|
|
@ -131,6 +165,10 @@ pub async fn get_latest(
|
|||
}
|
||||
|
||||
/// Set the inputs hash for an evaluation (used for eval caching).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails.
|
||||
pub async fn set_inputs_hash(
|
||||
pool: &PgPool,
|
||||
id: Uuid,
|
||||
|
|
@ -147,6 +185,10 @@ pub async fn set_inputs_hash(
|
|||
|
||||
/// Check if an evaluation with the same `inputs_hash` already exists for this
|
||||
/// jobset.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_by_inputs_hash(
|
||||
pool: &PgPool,
|
||||
jobset_id: Uuid,
|
||||
|
|
@ -163,6 +205,11 @@ pub async fn get_by_inputs_hash(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Count total evaluations.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn count(pool: &PgPool) -> Result<i64> {
|
||||
let row: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM evaluations")
|
||||
.fetch_one(pool)
|
||||
|
|
@ -171,7 +218,11 @@ pub async fn count(pool: &PgPool) -> Result<i64> {
|
|||
Ok(row.0)
|
||||
}
|
||||
|
||||
/// Get an evaluation by jobset_id and commit_hash.
|
||||
/// Get an evaluation by `jobset_id` and `commit_hash`.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_by_jobset_and_commit(
|
||||
pool: &PgPool,
|
||||
jobset_id: Uuid,
|
||||
|
|
|
|||
|
|
@ -6,6 +6,11 @@ use crate::{
|
|||
models::BuildStatus,
|
||||
};
|
||||
|
||||
/// Check if a derivation path is in the failed paths cache.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn is_cached_failure(pool: &PgPool, drv_path: &str) -> Result<bool> {
|
||||
let row: Option<(bool,)> =
|
||||
sqlx::query_as("SELECT true FROM failed_paths_cache WHERE drv_path = $1")
|
||||
|
|
@ -17,6 +22,11 @@ pub async fn is_cached_failure(pool: &PgPool, drv_path: &str) -> Result<bool> {
|
|||
Ok(row.is_some())
|
||||
}
|
||||
|
||||
/// Insert a failed derivation path into the cache.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails.
|
||||
pub async fn insert(
|
||||
pool: &PgPool,
|
||||
drv_path: &str,
|
||||
|
|
@ -40,6 +50,11 @@ pub async fn insert(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Remove a derivation path from the failed paths cache.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails.
|
||||
pub async fn invalidate(pool: &PgPool, drv_path: &str) -> Result<()> {
|
||||
sqlx::query("DELETE FROM failed_paths_cache WHERE drv_path = $1")
|
||||
.bind(drv_path)
|
||||
|
|
@ -50,6 +65,11 @@ pub async fn invalidate(pool: &PgPool, drv_path: &str) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Remove expired entries from the failed paths cache.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails.
|
||||
pub async fn cleanup_expired(pool: &PgPool, ttl_seconds: u64) -> Result<u64> {
|
||||
let result = sqlx::query(
|
||||
"DELETE FROM failed_paths_cache WHERE failed_at < NOW() - \
|
||||
|
|
|
|||
|
|
@ -7,6 +7,11 @@ use crate::{
|
|||
models::JobsetInput,
|
||||
};
|
||||
|
||||
/// Create a new jobset input.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails or input already exists.
|
||||
pub async fn create(
|
||||
pool: &PgPool,
|
||||
jobset_id: Uuid,
|
||||
|
|
@ -38,6 +43,11 @@ pub async fn create(
|
|||
})
|
||||
}
|
||||
|
||||
/// List all inputs for a jobset.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_for_jobset(
|
||||
pool: &PgPool,
|
||||
jobset_id: Uuid,
|
||||
|
|
@ -51,6 +61,11 @@ pub async fn list_for_jobset(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Delete a jobset input.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails or input not found.
|
||||
pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
||||
let result = sqlx::query("DELETE FROM jobset_inputs WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -63,6 +78,10 @@ pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
|||
}
|
||||
|
||||
/// Upsert a jobset input (insert or update on conflict).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operation fails.
|
||||
pub async fn upsert(
|
||||
pool: &PgPool,
|
||||
jobset_id: Uuid,
|
||||
|
|
@ -89,6 +108,10 @@ pub async fn upsert(
|
|||
|
||||
/// Sync jobset inputs from declarative config.
|
||||
/// Deletes inputs not in the config and upserts those that are.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operations fail.
|
||||
pub async fn sync_for_jobset(
|
||||
pool: &PgPool,
|
||||
jobset_id: Uuid,
|
||||
|
|
|
|||
|
|
@ -6,6 +6,11 @@ use crate::{
|
|||
models::{ActiveJobset, CreateJobset, Jobset, JobsetState, UpdateJobset},
|
||||
};
|
||||
|
||||
/// Create a new jobset with defaults applied.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails or jobset already exists.
|
||||
pub async fn create(pool: &PgPool, input: CreateJobset) -> Result<Jobset> {
|
||||
let state = input.state.unwrap_or(JobsetState::Enabled);
|
||||
// Sync enabled with state if state was explicitly set, otherwise use
|
||||
|
|
@ -50,6 +55,11 @@ pub async fn create(pool: &PgPool, input: CreateJobset) -> Result<Jobset> {
|
|||
})
|
||||
}
|
||||
|
||||
/// Get a jobset by ID.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails or jobset not found.
|
||||
pub async fn get(pool: &PgPool, id: Uuid) -> Result<Jobset> {
|
||||
sqlx::query_as::<_, Jobset>("SELECT * FROM jobsets WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -58,6 +68,11 @@ pub async fn get(pool: &PgPool, id: Uuid) -> Result<Jobset> {
|
|||
.ok_or_else(|| CiError::NotFound(format!("Jobset {id} not found")))
|
||||
}
|
||||
|
||||
/// List all jobsets for a project.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_for_project(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -76,6 +91,11 @@ pub async fn list_for_project(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Count jobsets for a project.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn count_for_project(pool: &PgPool, project_id: Uuid) -> Result<i64> {
|
||||
let row: (i64,) =
|
||||
sqlx::query_as("SELECT COUNT(*) FROM jobsets WHERE project_id = $1")
|
||||
|
|
@ -86,6 +106,11 @@ pub async fn count_for_project(pool: &PgPool, project_id: Uuid) -> Result<i64> {
|
|||
Ok(row.0)
|
||||
}
|
||||
|
||||
/// Update a jobset with partial fields.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails or jobset not found.
|
||||
pub async fn update(
|
||||
pool: &PgPool,
|
||||
id: Uuid,
|
||||
|
|
@ -139,6 +164,11 @@ pub async fn update(
|
|||
})
|
||||
}
|
||||
|
||||
/// Delete a jobset.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails or jobset not found.
|
||||
pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
||||
let result = sqlx::query("DELETE FROM jobsets WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -152,6 +182,11 @@ pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Insert or update a jobset by name.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operation fails.
|
||||
pub async fn upsert(pool: &PgPool, input: CreateJobset) -> Result<Jobset> {
|
||||
let state = input.state.unwrap_or(JobsetState::Enabled);
|
||||
// Sync enabled with state if state was explicitly set, otherwise use
|
||||
|
|
@ -191,6 +226,11 @@ pub async fn upsert(pool: &PgPool, input: CreateJobset) -> Result<Jobset> {
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// List all active jobsets with project info.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_active(pool: &PgPool) -> Result<Vec<ActiveJobset>> {
|
||||
sqlx::query_as::<_, ActiveJobset>("SELECT * FROM active_jobsets")
|
||||
.fetch_all(pool)
|
||||
|
|
@ -199,6 +239,10 @@ pub async fn list_active(pool: &PgPool) -> Result<Vec<ActiveJobset>> {
|
|||
}
|
||||
|
||||
/// Mark a one-shot jobset as complete (set state to disabled).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails.
|
||||
pub async fn mark_one_shot_complete(pool: &PgPool, id: Uuid) -> Result<()> {
|
||||
sqlx::query(
|
||||
"UPDATE jobsets SET state = 'disabled', enabled = false WHERE id = $1 AND \
|
||||
|
|
@ -212,6 +256,10 @@ pub async fn mark_one_shot_complete(pool: &PgPool, id: Uuid) -> Result<()> {
|
|||
}
|
||||
|
||||
/// Update the `last_checked_at` timestamp for a jobset.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails.
|
||||
pub async fn update_last_checked(pool: &PgPool, id: Uuid) -> Result<()> {
|
||||
sqlx::query("UPDATE jobsets SET last_checked_at = NOW() WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -222,6 +270,10 @@ pub async fn update_last_checked(pool: &PgPool, id: Uuid) -> Result<()> {
|
|||
}
|
||||
|
||||
/// Check if a jobset has any running builds.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn has_running_builds(
|
||||
pool: &PgPool,
|
||||
jobset_id: Uuid,
|
||||
|
|
@ -240,6 +292,10 @@ pub async fn has_running_builds(
|
|||
/// List jobsets that are due for evaluation based on their `check_interval`.
|
||||
/// Returns jobsets where `last_checked_at` is NULL or older than
|
||||
/// `check_interval` seconds.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_due_for_eval(
|
||||
pool: &PgPool,
|
||||
limit: i64,
|
||||
|
|
|
|||
|
|
@ -7,6 +7,11 @@ use crate::{
|
|||
models::{CreateNotificationConfig, NotificationConfig},
|
||||
};
|
||||
|
||||
/// Create a new notification config.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails or config already exists.
|
||||
pub async fn create(
|
||||
pool: &PgPool,
|
||||
input: CreateNotificationConfig,
|
||||
|
|
@ -33,6 +38,11 @@ pub async fn create(
|
|||
})
|
||||
}
|
||||
|
||||
/// List all enabled notification configs for a project.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_for_project(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -47,6 +57,11 @@ pub async fn list_for_project(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Delete a notification config.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails or config not found.
|
||||
pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
||||
let result = sqlx::query("DELETE FROM notification_configs WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -61,6 +76,10 @@ pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
|||
}
|
||||
|
||||
/// Upsert a notification config (insert or update on conflict).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operation fails.
|
||||
pub async fn upsert(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -85,6 +104,10 @@ pub async fn upsert(
|
|||
|
||||
/// Sync notification configs from declarative config.
|
||||
/// Deletes configs not in the declarative list and upserts those that are.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operations fail.
|
||||
pub async fn sync_for_project(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
|
|||
|
|
@ -6,6 +6,10 @@ use uuid::Uuid;
|
|||
use crate::{error::Result, models::NotificationTask};
|
||||
|
||||
/// Create a new notification task for later delivery
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails.
|
||||
pub async fn create(
|
||||
pool: &PgPool,
|
||||
notification_type: &str,
|
||||
|
|
@ -13,11 +17,11 @@ pub async fn create(
|
|||
max_attempts: i32,
|
||||
) -> Result<NotificationTask> {
|
||||
let task = sqlx::query_as::<_, NotificationTask>(
|
||||
r#"
|
||||
r"
|
||||
INSERT INTO notification_tasks (notification_type, payload, max_attempts)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING *
|
||||
"#,
|
||||
",
|
||||
)
|
||||
.bind(notification_type)
|
||||
.bind(payload)
|
||||
|
|
@ -29,19 +33,23 @@ pub async fn create(
|
|||
}
|
||||
|
||||
/// Fetch pending tasks that are ready for retry
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_pending(
|
||||
pool: &PgPool,
|
||||
limit: i32,
|
||||
) -> Result<Vec<NotificationTask>> {
|
||||
let tasks = sqlx::query_as::<_, NotificationTask>(
|
||||
r#"
|
||||
r"
|
||||
SELECT *
|
||||
FROM notification_tasks
|
||||
WHERE status = 'pending'
|
||||
AND next_retry_at <= NOW()
|
||||
ORDER BY next_retry_at ASC
|
||||
LIMIT $1
|
||||
"#,
|
||||
",
|
||||
)
|
||||
.bind(limit)
|
||||
.fetch_all(pool)
|
||||
|
|
@ -51,14 +59,18 @@ pub async fn list_pending(
|
|||
}
|
||||
|
||||
/// Mark a task as running (claimed by worker)
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails.
|
||||
pub async fn mark_running(pool: &PgPool, task_id: Uuid) -> Result<()> {
|
||||
sqlx::query(
|
||||
r#"
|
||||
r"
|
||||
UPDATE notification_tasks
|
||||
SET status = 'running',
|
||||
attempts = attempts + 1
|
||||
WHERE id = $1
|
||||
"#,
|
||||
",
|
||||
)
|
||||
.bind(task_id)
|
||||
.execute(pool)
|
||||
|
|
@ -68,14 +80,18 @@ pub async fn mark_running(pool: &PgPool, task_id: Uuid) -> Result<()> {
|
|||
}
|
||||
|
||||
/// Mark a task as completed successfully
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails.
|
||||
pub async fn mark_completed(pool: &PgPool, task_id: Uuid) -> Result<()> {
|
||||
sqlx::query(
|
||||
r#"
|
||||
r"
|
||||
UPDATE notification_tasks
|
||||
SET status = 'completed',
|
||||
completed_at = NOW()
|
||||
WHERE id = $1
|
||||
"#,
|
||||
",
|
||||
)
|
||||
.bind(task_id)
|
||||
.execute(pool)
|
||||
|
|
@ -86,13 +102,17 @@ pub async fn mark_completed(pool: &PgPool, task_id: Uuid) -> Result<()> {
|
|||
|
||||
/// Mark a task as failed and schedule retry with exponential backoff
|
||||
/// Backoff formula: 1s, 2s, 4s, 8s, 16s...
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails.
|
||||
pub async fn mark_failed_and_retry(
|
||||
pool: &PgPool,
|
||||
task_id: Uuid,
|
||||
error: &str,
|
||||
) -> Result<()> {
|
||||
sqlx::query(
|
||||
r#"
|
||||
r"
|
||||
UPDATE notification_tasks
|
||||
SET status = CASE
|
||||
WHEN attempts >= max_attempts THEN 'failed'::varchar
|
||||
|
|
@ -101,14 +121,14 @@ pub async fn mark_failed_and_retry(
|
|||
last_error = $2,
|
||||
next_retry_at = CASE
|
||||
WHEN attempts >= max_attempts THEN NOW()
|
||||
ELSE NOW() + (POWER(2, attempts) || ' seconds')::interval
|
||||
ELSE NOW() + (POWER(2, attempts - 1) || ' seconds')::interval
|
||||
END,
|
||||
completed_at = CASE
|
||||
WHEN attempts >= max_attempts THEN NOW()
|
||||
ELSE NULL
|
||||
END
|
||||
WHERE id = $1
|
||||
"#,
|
||||
",
|
||||
)
|
||||
.bind(task_id)
|
||||
.bind(error)
|
||||
|
|
@ -119,11 +139,15 @@ pub async fn mark_failed_and_retry(
|
|||
}
|
||||
|
||||
/// Get task by ID
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get(pool: &PgPool, task_id: Uuid) -> Result<NotificationTask> {
|
||||
let task = sqlx::query_as::<_, NotificationTask>(
|
||||
r#"
|
||||
r"
|
||||
SELECT * FROM notification_tasks WHERE id = $1
|
||||
"#,
|
||||
",
|
||||
)
|
||||
.bind(task_id)
|
||||
.fetch_one(pool)
|
||||
|
|
@ -133,17 +157,21 @@ pub async fn get(pool: &PgPool, task_id: Uuid) -> Result<NotificationTask> {
|
|||
}
|
||||
|
||||
/// Clean up old completed/failed tasks (older than retention days)
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails.
|
||||
pub async fn cleanup_old_tasks(
|
||||
pool: &PgPool,
|
||||
retention_days: i64,
|
||||
) -> Result<u64> {
|
||||
let result = sqlx::query(
|
||||
r#"
|
||||
r"
|
||||
DELETE FROM notification_tasks
|
||||
WHERE status IN ('completed', 'failed')
|
||||
AND (completed_at < NOW() - ($1 || ' days')::interval
|
||||
OR created_at < NOW() - ($1 || ' days')::interval)
|
||||
"#,
|
||||
",
|
||||
)
|
||||
.bind(retention_days)
|
||||
.execute(pool)
|
||||
|
|
@ -153,11 +181,15 @@ pub async fn cleanup_old_tasks(
|
|||
}
|
||||
|
||||
/// Count pending tasks (for monitoring)
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn count_pending(pool: &PgPool) -> Result<i64> {
|
||||
let count: (i64,) = sqlx::query_as(
|
||||
r#"
|
||||
r"
|
||||
SELECT COUNT(*) FROM notification_tasks WHERE status = 'pending'
|
||||
"#,
|
||||
",
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
|
@ -166,11 +198,15 @@ pub async fn count_pending(pool: &PgPool) -> Result<i64> {
|
|||
}
|
||||
|
||||
/// Count failed tasks (for monitoring)
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn count_failed(pool: &PgPool) -> Result<i64> {
|
||||
let count: (i64,) = sqlx::query_as(
|
||||
r#"
|
||||
r"
|
||||
SELECT COUNT(*) FROM notification_tasks WHERE status = 'failed'
|
||||
"#,
|
||||
",
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
|
|
|||
|
|
@ -12,6 +12,10 @@ use crate::{
|
|||
};
|
||||
|
||||
/// Add a member to a project with role validation
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if validation fails or database insert fails.
|
||||
pub async fn create(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -43,6 +47,10 @@ pub async fn create(
|
|||
}
|
||||
|
||||
/// Get a project member by ID
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails or member not found.
|
||||
pub async fn get(pool: &PgPool, id: Uuid) -> Result<ProjectMember> {
|
||||
sqlx::query_as::<_, ProjectMember>(
|
||||
"SELECT * FROM project_members WHERE id = $1",
|
||||
|
|
@ -61,6 +69,10 @@ pub async fn get(pool: &PgPool, id: Uuid) -> Result<ProjectMember> {
|
|||
}
|
||||
|
||||
/// Get a project member by project and user
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_by_project_and_user(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -77,6 +89,10 @@ pub async fn get_by_project_and_user(
|
|||
}
|
||||
|
||||
/// List all members of a project
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_for_project(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -91,6 +107,10 @@ pub async fn list_for_project(
|
|||
}
|
||||
|
||||
/// List all projects a user is a member of
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_for_user(
|
||||
pool: &PgPool,
|
||||
user_id: Uuid,
|
||||
|
|
@ -105,6 +125,10 @@ pub async fn list_for_user(
|
|||
}
|
||||
|
||||
/// Update a project member's role with validation
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if validation fails or database update fails.
|
||||
pub async fn update(
|
||||
pool: &PgPool,
|
||||
id: Uuid,
|
||||
|
|
@ -135,6 +159,10 @@ pub async fn update(
|
|||
}
|
||||
|
||||
/// Remove a member from a project
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails or member not found.
|
||||
pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
||||
let result = sqlx::query("DELETE FROM project_members WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -147,6 +175,10 @@ pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
|||
}
|
||||
|
||||
/// Remove a specific user from a project
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails or user not found.
|
||||
pub async fn delete_by_project_and_user(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -168,6 +200,10 @@ pub async fn delete_by_project_and_user(
|
|||
}
|
||||
|
||||
/// Check if a user has a specific role or higher in a project
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn check_permission(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -186,6 +222,10 @@ pub async fn check_permission(
|
|||
}
|
||||
|
||||
/// Upsert a project member (insert or update on conflict).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if validation fails or database operation fails.
|
||||
pub async fn upsert(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -211,6 +251,10 @@ pub async fn upsert(
|
|||
|
||||
/// Sync project members from declarative config.
|
||||
/// Deletes members not in the declarative list and upserts those that are.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operations fail.
|
||||
pub async fn sync_for_project(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
|
|||
|
|
@ -6,6 +6,11 @@ use crate::{
|
|||
models::{CreateProject, Project, UpdateProject},
|
||||
};
|
||||
|
||||
/// Create a new project.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails or project name already exists.
|
||||
pub async fn create(pool: &PgPool, input: CreateProject) -> Result<Project> {
|
||||
sqlx::query_as::<_, Project>(
|
||||
"INSERT INTO projects (name, description, repository_url) VALUES ($1, $2, \
|
||||
|
|
@ -26,6 +31,11 @@ pub async fn create(pool: &PgPool, input: CreateProject) -> Result<Project> {
|
|||
})
|
||||
}
|
||||
|
||||
/// Get a project by ID.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails or project not found.
|
||||
pub async fn get(pool: &PgPool, id: Uuid) -> Result<Project> {
|
||||
sqlx::query_as::<_, Project>("SELECT * FROM projects WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -34,6 +44,11 @@ pub async fn get(pool: &PgPool, id: Uuid) -> Result<Project> {
|
|||
.ok_or_else(|| CiError::NotFound(format!("Project {id} not found")))
|
||||
}
|
||||
|
||||
/// Get a project by name.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails or project not found.
|
||||
pub async fn get_by_name(pool: &PgPool, name: &str) -> Result<Project> {
|
||||
sqlx::query_as::<_, Project>("SELECT * FROM projects WHERE name = $1")
|
||||
.bind(name)
|
||||
|
|
@ -42,6 +57,11 @@ pub async fn get_by_name(pool: &PgPool, name: &str) -> Result<Project> {
|
|||
.ok_or_else(|| CiError::NotFound(format!("Project '{name}' not found")))
|
||||
}
|
||||
|
||||
/// List projects with pagination.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list(
|
||||
pool: &PgPool,
|
||||
limit: i64,
|
||||
|
|
@ -57,6 +77,11 @@ pub async fn list(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Count total number of projects.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn count(pool: &PgPool) -> Result<i64> {
|
||||
let row: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM projects")
|
||||
.fetch_one(pool)
|
||||
|
|
@ -65,12 +90,17 @@ pub async fn count(pool: &PgPool) -> Result<i64> {
|
|||
Ok(row.0)
|
||||
}
|
||||
|
||||
/// Update a project with partial fields.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails or project not found.
|
||||
pub async fn update(
|
||||
pool: &PgPool,
|
||||
id: Uuid,
|
||||
input: UpdateProject,
|
||||
) -> Result<Project> {
|
||||
// Build dynamic update — only set provided fields
|
||||
// Dynamic update - only set provided fields
|
||||
let existing = get(pool, id).await?;
|
||||
|
||||
let name = input.name.unwrap_or(existing.name);
|
||||
|
|
@ -97,6 +127,11 @@ pub async fn update(
|
|||
})
|
||||
}
|
||||
|
||||
/// Insert or update a project by name.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operation fails.
|
||||
pub async fn upsert(pool: &PgPool, input: CreateProject) -> Result<Project> {
|
||||
sqlx::query_as::<_, Project>(
|
||||
"INSERT INTO projects (name, description, repository_url) VALUES ($1, $2, \
|
||||
|
|
@ -111,6 +146,11 @@ pub async fn upsert(pool: &PgPool, input: CreateProject) -> Result<Project> {
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Delete a project by ID.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails or project not found.
|
||||
pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
||||
let result = sqlx::query("DELETE FROM projects WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
|
|||
|
|
@ -7,6 +7,11 @@ use crate::{
|
|||
models::{CreateRemoteBuilder, RemoteBuilder},
|
||||
};
|
||||
|
||||
/// Create a new remote builder.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails or builder already exists.
|
||||
pub async fn create(
|
||||
pool: &PgPool,
|
||||
input: CreateRemoteBuilder,
|
||||
|
|
@ -40,6 +45,11 @@ pub async fn create(
|
|||
})
|
||||
}
|
||||
|
||||
/// Get a remote builder by ID.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails or builder not found.
|
||||
pub async fn get(pool: &PgPool, id: Uuid) -> Result<RemoteBuilder> {
|
||||
sqlx::query_as::<_, RemoteBuilder>(
|
||||
"SELECT * FROM remote_builders WHERE id = $1",
|
||||
|
|
@ -50,6 +60,11 @@ pub async fn get(pool: &PgPool, id: Uuid) -> Result<RemoteBuilder> {
|
|||
.ok_or_else(|| CiError::NotFound(format!("Remote builder {id} not found")))
|
||||
}
|
||||
|
||||
/// List all remote builders.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list(pool: &PgPool) -> Result<Vec<RemoteBuilder>> {
|
||||
sqlx::query_as::<_, RemoteBuilder>(
|
||||
"SELECT * FROM remote_builders ORDER BY speed_factor DESC, name",
|
||||
|
|
@ -59,6 +74,11 @@ pub async fn list(pool: &PgPool) -> Result<Vec<RemoteBuilder>> {
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// List all enabled remote builders.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_enabled(pool: &PgPool) -> Result<Vec<RemoteBuilder>> {
|
||||
sqlx::query_as::<_, RemoteBuilder>(
|
||||
"SELECT * FROM remote_builders WHERE enabled = true ORDER BY speed_factor \
|
||||
|
|
@ -71,6 +91,10 @@ pub async fn list_enabled(pool: &PgPool) -> Result<Vec<RemoteBuilder>> {
|
|||
|
||||
/// Find a suitable builder for the given system.
|
||||
/// Excludes builders that are temporarily disabled due to consecutive failures.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn find_for_system(
|
||||
pool: &PgPool,
|
||||
system: &str,
|
||||
|
|
@ -87,9 +111,14 @@ pub async fn find_for_system(
|
|||
}
|
||||
|
||||
/// Record a build failure for a remote builder.
|
||||
/// Increments consecutive_failures (capped at 4), sets last_failure,
|
||||
/// and computes disabled_until with exponential backoff.
|
||||
///
|
||||
/// Increments `consecutive_failures` (capped at 4), sets `last_failure`,
|
||||
/// and computes `disabled_until` with exponential backoff.
|
||||
/// Backoff formula (from Hydra): delta = 60 * 3^(min(failures, 4) - 1) seconds.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails or builder not found.
|
||||
pub async fn record_failure(pool: &PgPool, id: Uuid) -> Result<RemoteBuilder> {
|
||||
sqlx::query_as::<_, RemoteBuilder>(
|
||||
"UPDATE remote_builders SET consecutive_failures = \
|
||||
|
|
@ -105,7 +134,11 @@ pub async fn record_failure(pool: &PgPool, id: Uuid) -> Result<RemoteBuilder> {
|
|||
}
|
||||
|
||||
/// Record a build success for a remote builder.
|
||||
/// Resets consecutive_failures and clears disabled_until.
|
||||
/// Resets `consecutive_failures` and clears `disabled_until`.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails or builder not found.
|
||||
pub async fn record_success(pool: &PgPool, id: Uuid) -> Result<RemoteBuilder> {
|
||||
sqlx::query_as::<_, RemoteBuilder>(
|
||||
"UPDATE remote_builders SET consecutive_failures = 0, disabled_until = \
|
||||
|
|
@ -117,12 +150,17 @@ pub async fn record_success(pool: &PgPool, id: Uuid) -> Result<RemoteBuilder> {
|
|||
.ok_or_else(|| CiError::NotFound(format!("Remote builder {id} not found")))
|
||||
}
|
||||
|
||||
/// Update a remote builder with partial fields.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails or builder not found.
|
||||
pub async fn update(
|
||||
pool: &PgPool,
|
||||
id: Uuid,
|
||||
input: crate::models::UpdateRemoteBuilder,
|
||||
) -> Result<RemoteBuilder> {
|
||||
// Build dynamic update — use COALESCE pattern
|
||||
// Dynamic update using COALESCE pattern
|
||||
sqlx::query_as::<_, RemoteBuilder>(
|
||||
"UPDATE remote_builders SET name = COALESCE($1, name), ssh_uri = \
|
||||
COALESCE($2, ssh_uri), systems = COALESCE($3, systems), max_jobs = \
|
||||
|
|
@ -148,6 +186,11 @@ pub async fn update(
|
|||
.ok_or_else(|| CiError::NotFound(format!("Remote builder {id} not found")))
|
||||
}
|
||||
|
||||
/// Delete a remote builder.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails or builder not found.
|
||||
pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
||||
let result = sqlx::query("DELETE FROM remote_builders WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -160,6 +203,11 @@ pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Count total remote builders.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn count(pool: &PgPool) -> Result<i64> {
|
||||
let row: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM remote_builders")
|
||||
.fetch_one(pool)
|
||||
|
|
@ -169,18 +217,13 @@ pub async fn count(pool: &PgPool) -> Result<i64> {
|
|||
}
|
||||
|
||||
/// Upsert a remote builder (insert or update on conflict by name).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operation fails.
|
||||
pub async fn upsert(
|
||||
pool: &PgPool,
|
||||
name: &str,
|
||||
ssh_uri: &str,
|
||||
systems: &[String],
|
||||
max_jobs: i32,
|
||||
speed_factor: i32,
|
||||
supported_features: &[String],
|
||||
mandatory_features: &[String],
|
||||
enabled: bool,
|
||||
public_host_key: Option<&str>,
|
||||
ssh_key_file: Option<&str>,
|
||||
params: &crate::models::RemoteBuilderParams<'_>,
|
||||
) -> Result<RemoteBuilder> {
|
||||
sqlx::query_as::<_, RemoteBuilder>(
|
||||
"INSERT INTO remote_builders (name, ssh_uri, systems, max_jobs, \
|
||||
|
|
@ -194,16 +237,16 @@ pub async fn upsert(
|
|||
remote_builders.public_host_key), ssh_key_file = \
|
||||
COALESCE(EXCLUDED.ssh_key_file, remote_builders.ssh_key_file) RETURNING *",
|
||||
)
|
||||
.bind(name)
|
||||
.bind(ssh_uri)
|
||||
.bind(systems)
|
||||
.bind(max_jobs)
|
||||
.bind(speed_factor)
|
||||
.bind(supported_features)
|
||||
.bind(mandatory_features)
|
||||
.bind(enabled)
|
||||
.bind(public_host_key)
|
||||
.bind(ssh_key_file)
|
||||
.bind(params.name)
|
||||
.bind(params.ssh_uri)
|
||||
.bind(params.systems)
|
||||
.bind(params.max_jobs)
|
||||
.bind(params.speed_factor)
|
||||
.bind(params.supported_features)
|
||||
.bind(params.mandatory_features)
|
||||
.bind(params.enabled)
|
||||
.bind(params.public_host_key)
|
||||
.bind(params.ssh_key_file)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(CiError::Database)
|
||||
|
|
@ -211,6 +254,10 @@ pub async fn upsert(
|
|||
|
||||
/// Sync remote builders from declarative config.
|
||||
/// Deletes builders not in the declarative list and upserts those that are.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operations fail.
|
||||
pub async fn sync_all(
|
||||
pool: &PgPool,
|
||||
builders: &[DeclarativeRemoteBuilder],
|
||||
|
|
@ -227,20 +274,19 @@ pub async fn sync_all(
|
|||
|
||||
// Upsert each builder
|
||||
for builder in builders {
|
||||
upsert(
|
||||
pool,
|
||||
&builder.name,
|
||||
&builder.ssh_uri,
|
||||
&builder.systems,
|
||||
builder.max_jobs,
|
||||
builder.speed_factor,
|
||||
&builder.supported_features,
|
||||
&builder.mandatory_features,
|
||||
builder.enabled,
|
||||
builder.public_host_key.as_deref(),
|
||||
builder.ssh_key_file.as_deref(),
|
||||
)
|
||||
.await?;
|
||||
let params = crate::models::RemoteBuilderParams {
|
||||
name: &builder.name,
|
||||
ssh_uri: &builder.ssh_uri,
|
||||
systems: &builder.systems,
|
||||
max_jobs: builder.max_jobs,
|
||||
speed_factor: builder.speed_factor,
|
||||
supported_features: &builder.supported_features,
|
||||
mandatory_features: &builder.mandatory_features,
|
||||
enabled: builder.enabled,
|
||||
public_host_key: builder.public_host_key.as_deref(),
|
||||
ssh_key_file: builder.ssh_key_file.as_deref(),
|
||||
};
|
||||
upsert(pool, ¶ms).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -146,6 +146,10 @@ pub struct SearchResults {
|
|||
}
|
||||
|
||||
/// Execute a comprehensive search across all entities
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn search(
|
||||
pool: &PgPool,
|
||||
params: &SearchParams,
|
||||
|
|
@ -511,6 +515,10 @@ async fn search_builds(
|
|||
}
|
||||
|
||||
/// Quick search - simple text search across entities
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn quick_search(
|
||||
pool: &PgPool,
|
||||
query: &str,
|
||||
|
|
|
|||
|
|
@ -9,6 +9,10 @@ use crate::{
|
|||
};
|
||||
|
||||
/// Create a new starred job
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails or job already starred.
|
||||
pub async fn create(
|
||||
pool: &PgPool,
|
||||
user_id: Uuid,
|
||||
|
|
@ -35,6 +39,10 @@ pub async fn create(
|
|||
}
|
||||
|
||||
/// Get a starred job by ID
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails or starred job not found.
|
||||
pub async fn get(pool: &PgPool, id: Uuid) -> Result<StarredJob> {
|
||||
sqlx::query_as::<_, StarredJob>("SELECT * FROM starred_jobs WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -51,6 +59,10 @@ pub async fn get(pool: &PgPool, id: Uuid) -> Result<StarredJob> {
|
|||
}
|
||||
|
||||
/// List starred jobs for a user with pagination
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_for_user(
|
||||
pool: &PgPool,
|
||||
user_id: Uuid,
|
||||
|
|
@ -70,6 +82,10 @@ pub async fn list_for_user(
|
|||
}
|
||||
|
||||
/// Count starred jobs for a user
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn count_for_user(pool: &PgPool, user_id: Uuid) -> Result<i64> {
|
||||
let (count,): (i64,) =
|
||||
sqlx::query_as("SELECT COUNT(*) FROM starred_jobs WHERE user_id = $1")
|
||||
|
|
@ -80,6 +96,10 @@ pub async fn count_for_user(pool: &PgPool, user_id: Uuid) -> Result<i64> {
|
|||
}
|
||||
|
||||
/// Check if a user has starred a specific job
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn is_starred(
|
||||
pool: &PgPool,
|
||||
user_id: Uuid,
|
||||
|
|
@ -101,6 +121,10 @@ pub async fn is_starred(
|
|||
}
|
||||
|
||||
/// Delete a starred job
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails or starred job not found.
|
||||
pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
||||
let result = sqlx::query("DELETE FROM starred_jobs WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -113,6 +137,10 @@ pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
|||
}
|
||||
|
||||
/// Delete a starred job by user and job details
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails or starred job not found.
|
||||
pub async fn delete_by_job(
|
||||
pool: &PgPool,
|
||||
user_id: Uuid,
|
||||
|
|
@ -137,6 +165,10 @@ pub async fn delete_by_job(
|
|||
}
|
||||
|
||||
/// Delete all starred jobs for a user (when user is deleted)
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails.
|
||||
pub async fn delete_all_for_user(pool: &PgPool, user_id: Uuid) -> Result<()> {
|
||||
sqlx::query("DELETE FROM starred_jobs WHERE user_id = $1")
|
||||
.bind(user_id)
|
||||
|
|
|
|||
|
|
@ -17,6 +17,10 @@ use crate::{
|
|||
};
|
||||
|
||||
/// Hash a password using argon2id
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if password hashing fails.
|
||||
pub fn hash_password(password: &str) -> Result<String> {
|
||||
use argon2::{
|
||||
Argon2,
|
||||
|
|
@ -33,6 +37,10 @@ pub fn hash_password(password: &str) -> Result<String> {
|
|||
}
|
||||
|
||||
/// Verify a password against a hash
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if password hash parsing fails.
|
||||
pub fn verify_password(password: &str, hash: &str) -> Result<bool> {
|
||||
use argon2::{Argon2, PasswordHash, PasswordVerifier};
|
||||
|
||||
|
|
@ -47,6 +55,10 @@ pub fn verify_password(password: &str, hash: &str) -> Result<bool> {
|
|||
}
|
||||
|
||||
/// Create a new user with validation
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if validation fails or database insert fails.
|
||||
pub async fn create(pool: &PgPool, data: &CreateUser) -> Result<User> {
|
||||
// Validate username
|
||||
validate_username(&data.username)
|
||||
|
|
@ -94,6 +106,10 @@ pub async fn create(pool: &PgPool, data: &CreateUser) -> Result<User> {
|
|||
}
|
||||
|
||||
/// Authenticate a user with username and password
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if credentials are invalid or database query fails.
|
||||
pub async fn authenticate(
|
||||
pool: &PgPool,
|
||||
creds: &LoginCredentials,
|
||||
|
|
@ -129,6 +145,10 @@ pub async fn authenticate(
|
|||
}
|
||||
|
||||
/// Get a user by ID
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails or user not found.
|
||||
pub async fn get(pool: &PgPool, id: Uuid) -> Result<User> {
|
||||
sqlx::query_as::<_, User>("SELECT * FROM users WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -145,6 +165,10 @@ pub async fn get(pool: &PgPool, id: Uuid) -> Result<User> {
|
|||
}
|
||||
|
||||
/// Get a user by username
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_by_username(
|
||||
pool: &PgPool,
|
||||
username: &str,
|
||||
|
|
@ -157,6 +181,10 @@ pub async fn get_by_username(
|
|||
}
|
||||
|
||||
/// Get a user by email
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_by_email(pool: &PgPool, email: &str) -> Result<Option<User>> {
|
||||
sqlx::query_as::<_, User>("SELECT * FROM users WHERE email = $1")
|
||||
.bind(email)
|
||||
|
|
@ -166,6 +194,10 @@ pub async fn get_by_email(pool: &PgPool, email: &str) -> Result<Option<User>> {
|
|||
}
|
||||
|
||||
/// List all users with pagination
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list(pool: &PgPool, limit: i64, offset: i64) -> Result<Vec<User>> {
|
||||
sqlx::query_as::<_, User>(
|
||||
"SELECT * FROM users ORDER BY created_at DESC LIMIT $1 OFFSET $2",
|
||||
|
|
@ -178,6 +210,10 @@ pub async fn list(pool: &PgPool, limit: i64, offset: i64) -> Result<Vec<User>> {
|
|||
}
|
||||
|
||||
/// Count total users
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn count(pool: &PgPool) -> Result<i64> {
|
||||
let (count,): (i64,) = sqlx::query_as("SELECT COUNT(*) FROM users")
|
||||
.fetch_one(pool)
|
||||
|
|
@ -186,6 +222,10 @@ pub async fn count(pool: &PgPool) -> Result<i64> {
|
|||
}
|
||||
|
||||
/// Update a user with the provided data
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if validation fails or database update fails.
|
||||
pub async fn update(
|
||||
pool: &PgPool,
|
||||
id: Uuid,
|
||||
|
|
@ -220,6 +260,10 @@ pub async fn update(
|
|||
}
|
||||
|
||||
/// Update user email with validation
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if validation fails or database update fails.
|
||||
pub async fn update_email(
|
||||
pool: &PgPool,
|
||||
id: Uuid,
|
||||
|
|
@ -245,6 +289,10 @@ pub async fn update_email(
|
|||
}
|
||||
|
||||
/// Update user full name with validation
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if validation fails or database update fails.
|
||||
pub async fn update_full_name(
|
||||
pool: &PgPool,
|
||||
id: Uuid,
|
||||
|
|
@ -263,6 +311,10 @@ pub async fn update_full_name(
|
|||
}
|
||||
|
||||
/// Update user password with validation
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if validation fails or database update fails.
|
||||
pub async fn update_password(
|
||||
pool: &PgPool,
|
||||
id: Uuid,
|
||||
|
|
@ -281,6 +333,10 @@ pub async fn update_password(
|
|||
}
|
||||
|
||||
/// Update user role with validation
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if validation fails or database update fails.
|
||||
pub async fn update_role(pool: &PgPool, id: Uuid, role: &str) -> Result<()> {
|
||||
validate_role(role, VALID_ROLES)
|
||||
.map_err(|e| CiError::Validation(e.to_string()))?;
|
||||
|
|
@ -294,6 +350,10 @@ pub async fn update_role(pool: &PgPool, id: Uuid, role: &str) -> Result<()> {
|
|||
}
|
||||
|
||||
/// Enable/disable user
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails.
|
||||
pub async fn set_enabled(pool: &PgPool, id: Uuid, enabled: bool) -> Result<()> {
|
||||
sqlx::query("UPDATE users SET enabled = $1 WHERE id = $2")
|
||||
.bind(enabled)
|
||||
|
|
@ -304,6 +364,10 @@ pub async fn set_enabled(pool: &PgPool, id: Uuid, enabled: bool) -> Result<()> {
|
|||
}
|
||||
|
||||
/// Set public dashboard preference
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database update fails.
|
||||
pub async fn set_public_dashboard(
|
||||
pool: &PgPool,
|
||||
id: Uuid,
|
||||
|
|
@ -318,6 +382,10 @@ pub async fn set_public_dashboard(
|
|||
}
|
||||
|
||||
/// Delete a user
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails or user not found.
|
||||
pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
||||
let result = sqlx::query("DELETE FROM users WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -330,6 +398,10 @@ pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
|||
}
|
||||
|
||||
/// Create or update OAuth user
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if validation fails or database operation fails.
|
||||
pub async fn upsert_oauth_user(
|
||||
pool: &PgPool,
|
||||
username: &str,
|
||||
|
|
@ -399,6 +471,10 @@ pub async fn upsert_oauth_user(
|
|||
}
|
||||
|
||||
/// Create a new session for a user. Returns (`session_token`, `session_id`).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails.
|
||||
pub async fn create_session(
|
||||
pool: &PgPool,
|
||||
user_id: Uuid,
|
||||
|
|
@ -427,6 +503,10 @@ pub async fn create_session(
|
|||
}
|
||||
|
||||
/// Validate a session token and return the associated user if valid.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn validate_session(
|
||||
pool: &PgPool,
|
||||
token: &str,
|
||||
|
|
@ -444,17 +524,16 @@ pub async fn validate_session(
|
|||
.await?;
|
||||
|
||||
// Update last_used_at
|
||||
if result.is_some() {
|
||||
if let Err(e) = sqlx::query(
|
||||
if result.is_some()
|
||||
&& let Err(e) = sqlx::query(
|
||||
"UPDATE user_sessions SET last_used_at = NOW() WHERE session_token_hash \
|
||||
= $1",
|
||||
)
|
||||
.bind(&token_hash)
|
||||
.execute(pool)
|
||||
.await
|
||||
{
|
||||
tracing::warn!(token_hash = %token_hash, "Failed to update session last_used_at: {e}");
|
||||
}
|
||||
{
|
||||
tracing::warn!(token_hash = %token_hash, "Failed to update session last_used_at: {e}");
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
|
|
|
|||
|
|
@ -7,6 +7,11 @@ use crate::{
|
|||
models::{CreateWebhookConfig, WebhookConfig},
|
||||
};
|
||||
|
||||
/// Create a new webhook config.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database insert fails or config already exists.
|
||||
pub async fn create(
|
||||
pool: &PgPool,
|
||||
input: CreateWebhookConfig,
|
||||
|
|
@ -34,6 +39,11 @@ pub async fn create(
|
|||
})
|
||||
}
|
||||
|
||||
/// Get a webhook config by ID.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails or config not found.
|
||||
pub async fn get(pool: &PgPool, id: Uuid) -> Result<WebhookConfig> {
|
||||
sqlx::query_as::<_, WebhookConfig>(
|
||||
"SELECT * FROM webhook_configs WHERE id = $1",
|
||||
|
|
@ -44,6 +54,11 @@ pub async fn get(pool: &PgPool, id: Uuid) -> Result<WebhookConfig> {
|
|||
.ok_or_else(|| CiError::NotFound(format!("Webhook config {id} not found")))
|
||||
}
|
||||
|
||||
/// List all webhook configs for a project.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn list_for_project(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -58,6 +73,11 @@ pub async fn list_for_project(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Get a webhook config by project and forge type.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database query fails.
|
||||
pub async fn get_by_project_and_forge(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -74,6 +94,11 @@ pub async fn get_by_project_and_forge(
|
|||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Delete a webhook config.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database delete fails or config not found.
|
||||
pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
||||
let result = sqlx::query("DELETE FROM webhook_configs WHERE id = $1")
|
||||
.bind(id)
|
||||
|
|
@ -86,6 +111,10 @@ pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
|||
}
|
||||
|
||||
/// Upsert a webhook config (insert or update on conflict).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operation fails.
|
||||
pub async fn upsert(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
@ -110,6 +139,10 @@ pub async fn upsert(
|
|||
|
||||
/// Sync webhook configs from declarative config.
|
||||
/// Deletes configs not in the declarative list and upserts those that are.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if database operations fail.
|
||||
pub async fn sync_for_project(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
|
|
|
|||
|
|
@ -82,14 +82,12 @@ fn is_internal_host(host: &str) -> bool {
|
|||
return true;
|
||||
}
|
||||
// Block 172.16-31.x.x
|
||||
if host.starts_with("172.") {
|
||||
if let Some(second_octet) = host.split('.').nth(1) {
|
||||
if let Ok(n) = second_octet.parse::<u8>() {
|
||||
if (16..=31).contains(&n) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if host.starts_with("172.")
|
||||
&& let Some(second_octet) = host.split('.').nth(1)
|
||||
&& let Ok(n) = second_octet.parse::<u8>()
|
||||
&& (16..=31).contains(&n)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
// Block 192.168.x.x
|
||||
if host.starts_with("192.168.") {
|
||||
|
|
@ -100,6 +98,11 @@ fn is_internal_host(host: &str) -> bool {
|
|||
|
||||
/// Trait for validating request DTOs before persisting.
|
||||
pub trait Validate {
|
||||
/// Validate the DTO.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if validation fails.
|
||||
fn validate(&self) -> Result<(), String>;
|
||||
}
|
||||
|
||||
|
|
@ -129,19 +132,23 @@ fn validate_repository_url(url: &str) -> Result<(), String> {
|
|||
);
|
||||
}
|
||||
// Reject URLs targeting common internal/metadata endpoints
|
||||
if let Some(host) = extract_host_from_url(url) {
|
||||
if is_internal_host(&host) {
|
||||
return Err(
|
||||
"repository_url must not target internal or metadata addresses"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
if let Some(host) = extract_host_from_url(url)
|
||||
&& is_internal_host(&host)
|
||||
{
|
||||
return Err(
|
||||
"repository_url must not target internal or metadata addresses"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Validate that a URL uses one of the allowed schemes.
|
||||
/// Logs a warning when insecure schemes (`file`, `http`) are used.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if URL scheme is not in the allowed list.
|
||||
pub fn validate_url_scheme(
|
||||
url: &str,
|
||||
allowed_schemes: &[String],
|
||||
|
|
@ -187,6 +194,11 @@ fn validate_description(desc: &str) -> Result<(), String> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Validate nix expression format.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if expression contains invalid characters or path traversal.
|
||||
pub fn validate_nix_expression(expr: &str) -> Result<(), String> {
|
||||
if expr.is_empty() {
|
||||
return Err("nix_expression cannot be empty".to_string());
|
||||
|
|
@ -465,7 +477,7 @@ mod tests {
|
|||
#[test]
|
||||
fn store_path_rejects_just_prefix() {
|
||||
// "/nix/store/" alone has no hash, but structurally starts_with and has no
|
||||
// .., so it passes. This is fine — the DB lookup won't find anything
|
||||
// .., so it passes. This is fine - the DB lookup won't find anything
|
||||
// for it.
|
||||
assert!(is_valid_store_path("/nix/store/"));
|
||||
}
|
||||
|
|
@ -554,7 +566,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_create_project_invalid_name() {
|
||||
let p = CreateProject {
|
||||
name: "".to_string(),
|
||||
name: String::new(),
|
||||
description: None,
|
||||
repository_url: "https://github.com/test/repo".to_string(),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -34,6 +34,10 @@ impl std::error::Error for ValidationError {}
|
|||
/// Requirements:
|
||||
/// - 3-32 characters
|
||||
/// - Alphanumeric, underscore, hyphen only
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if username format is invalid.
|
||||
pub fn validate_username(username: &str) -> Result<(), ValidationError> {
|
||||
if username.is_empty() {
|
||||
return Err(ValidationError {
|
||||
|
|
@ -55,6 +59,10 @@ pub fn validate_username(username: &str) -> Result<(), ValidationError> {
|
|||
}
|
||||
|
||||
/// Validate email format
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if email format is invalid.
|
||||
pub fn validate_email(email: &str) -> Result<(), ValidationError> {
|
||||
if email.is_empty() {
|
||||
return Err(ValidationError {
|
||||
|
|
@ -80,6 +88,10 @@ pub fn validate_email(email: &str) -> Result<(), ValidationError> {
|
|||
/// - At least one lowercase letter
|
||||
/// - At least one number
|
||||
/// - At least one special character
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if password does not meet requirements.
|
||||
pub fn validate_password(password: &str) -> Result<(), ValidationError> {
|
||||
if password.len() < 12 {
|
||||
return Err(ValidationError {
|
||||
|
|
@ -128,6 +140,10 @@ pub fn validate_password(password: &str) -> Result<(), ValidationError> {
|
|||
}
|
||||
|
||||
/// Validate role against allowed roles
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if role is not in the allowed list.
|
||||
pub fn validate_role(
|
||||
role: &str,
|
||||
allowed: &[&str],
|
||||
|
|
@ -152,6 +168,10 @@ pub fn validate_role(
|
|||
/// Validate full name (optional field)
|
||||
/// - Max 255 characters
|
||||
/// - Must not contain control characters
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if full name contains invalid characters or is too long.
|
||||
pub fn validate_full_name(name: &str) -> Result<(), ValidationError> {
|
||||
if name.len() > 255 {
|
||||
return Err(ValidationError {
|
||||
|
|
@ -174,6 +194,10 @@ pub fn validate_full_name(name: &str) -> Result<(), ValidationError> {
|
|||
/// Requirements:
|
||||
/// - 1-255 characters
|
||||
/// - Alphanumeric + common path characters
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error if job name format is invalid.
|
||||
pub fn validate_job_name(name: &str) -> Result<(), ValidationError> {
|
||||
if name.is_empty() {
|
||||
return Err(ValidationError {
|
||||
|
|
|
|||
|
|
@ -21,8 +21,7 @@ async fn test_database_connection() -> anyhow::Result<()> {
|
|||
Err(e) => {
|
||||
println!(
|
||||
"Skipping test_database_connection: no PostgreSQL instance available \
|
||||
- {}",
|
||||
e
|
||||
- {e}"
|
||||
);
|
||||
return Ok(());
|
||||
},
|
||||
|
|
@ -38,7 +37,7 @@ async fn test_database_connection() -> anyhow::Result<()> {
|
|||
assert!(!info.version.is_empty());
|
||||
|
||||
// Test pool stats
|
||||
let stats = db.get_pool_stats().await;
|
||||
let stats = db.get_pool_stats();
|
||||
assert!(stats.size >= 1);
|
||||
|
||||
db.close().await;
|
||||
|
|
@ -58,8 +57,7 @@ async fn test_database_health_check() -> anyhow::Result<()> {
|
|||
Err(e) => {
|
||||
println!(
|
||||
"Skipping test_database_health_check: no PostgreSQL instance \
|
||||
available - {}",
|
||||
e
|
||||
available - {e}"
|
||||
);
|
||||
return Ok(());
|
||||
},
|
||||
|
|
@ -83,8 +81,7 @@ async fn test_connection_info() -> anyhow::Result<()> {
|
|||
Ok(pool) => pool,
|
||||
Err(e) => {
|
||||
println!(
|
||||
"Skipping test_connection_info: no PostgreSQL instance available - {}",
|
||||
e
|
||||
"Skipping test_connection_info: no PostgreSQL instance available - {e}"
|
||||
);
|
||||
return Ok(());
|
||||
},
|
||||
|
|
@ -104,8 +101,7 @@ async fn test_connection_info() -> anyhow::Result<()> {
|
|||
Ok(db) => db,
|
||||
Err(e) => {
|
||||
println!(
|
||||
"Skipping test_connection_info: database connection failed - {}",
|
||||
e
|
||||
"Skipping test_connection_info: database connection failed - {e}"
|
||||
);
|
||||
pool.close().await;
|
||||
return Ok(());
|
||||
|
|
@ -141,14 +137,13 @@ async fn test_pool_stats() -> anyhow::Result<()> {
|
|||
Ok(db) => db,
|
||||
Err(e) => {
|
||||
println!(
|
||||
"Skipping test_pool_stats: no PostgreSQL instance available - {}",
|
||||
e
|
||||
"Skipping test_pool_stats: no PostgreSQL instance available - {e}"
|
||||
);
|
||||
return Ok(());
|
||||
},
|
||||
};
|
||||
|
||||
let stats = db.get_pool_stats().await;
|
||||
let stats = db.get_pool_stats();
|
||||
|
||||
assert!(stats.size >= 1);
|
||||
assert!(stats.idle >= 1);
|
||||
|
|
@ -173,12 +168,12 @@ async fn test_database_config_validation() -> anyhow::Result<()> {
|
|||
assert!(config.validate().is_ok());
|
||||
|
||||
// Invalid URL
|
||||
let mut config = config.clone();
|
||||
let mut config = config;
|
||||
config.url = "invalid://url".to_string();
|
||||
assert!(config.validate().is_err());
|
||||
|
||||
// Empty URL
|
||||
config.url = "".to_string();
|
||||
config.url = String::new();
|
||||
assert!(config.validate().is_err());
|
||||
|
||||
// Zero max connections
|
||||
|
|
|
|||
|
|
@ -20,12 +20,9 @@ async fn test_database_connection_full() -> anyhow::Result<()> {
|
|||
};
|
||||
|
||||
// Try to connect, skip test if database is not available
|
||||
let db = match Database::new(config).await {
|
||||
Ok(db) => db,
|
||||
Err(_) => {
|
||||
println!("Skipping database test: no PostgreSQL instance available");
|
||||
return Ok(());
|
||||
},
|
||||
let Ok(db) = Database::new(config).await else {
|
||||
println!("Skipping database test: no PostgreSQL instance available");
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
// Test health check
|
||||
|
|
@ -38,7 +35,7 @@ async fn test_database_connection_full() -> anyhow::Result<()> {
|
|||
assert!(!info.version.is_empty());
|
||||
|
||||
// Test pool stats
|
||||
let stats = db.get_pool_stats().await;
|
||||
let stats = db.get_pool_stats();
|
||||
assert!(stats.size >= 1);
|
||||
assert!(stats.idle >= 1);
|
||||
assert_eq!(stats.size, stats.idle + stats.active);
|
||||
|
|
@ -67,21 +64,21 @@ fn test_config_loading() -> anyhow::Result<()> {
|
|||
#[test]
|
||||
fn test_config_validation() -> anyhow::Result<()> {
|
||||
// Test valid config
|
||||
let config = Config::default();
|
||||
assert!(config.validate().is_ok());
|
||||
let base_config = Config::default();
|
||||
assert!(base_config.validate().is_ok());
|
||||
|
||||
// Test invalid database URL
|
||||
let mut config = config.clone();
|
||||
let mut config = base_config.clone();
|
||||
config.database.url = "invalid://url".to_string();
|
||||
assert!(config.validate().is_err());
|
||||
|
||||
// Test invalid port
|
||||
let mut config = config.clone();
|
||||
let mut config = base_config.clone();
|
||||
config.server.port = 0;
|
||||
assert!(config.validate().is_err());
|
||||
|
||||
// Test invalid connections
|
||||
let mut config = config.clone();
|
||||
let mut config = base_config.clone();
|
||||
config.database.max_connections = 0;
|
||||
assert!(config.validate().is_err());
|
||||
|
||||
|
|
@ -90,12 +87,12 @@ fn test_config_validation() -> anyhow::Result<()> {
|
|||
assert!(config.validate().is_err());
|
||||
|
||||
// Test invalid evaluator settings
|
||||
let mut config = config.clone();
|
||||
let mut config = base_config.clone();
|
||||
config.evaluator.poll_interval = 0;
|
||||
assert!(config.validate().is_err());
|
||||
|
||||
// Test invalid queue runner settings
|
||||
let mut config = config.clone();
|
||||
let mut config = base_config;
|
||||
config.queue_runner.workers = 0;
|
||||
assert!(config.validate().is_err());
|
||||
|
||||
|
|
@ -109,12 +106,12 @@ fn test_database_config_validation() -> anyhow::Result<()> {
|
|||
assert!(config.validate().is_ok());
|
||||
|
||||
// Test invalid URL
|
||||
let mut config = config.clone();
|
||||
let mut config = config;
|
||||
config.url = "invalid://url".to_string();
|
||||
assert!(config.validate().is_err());
|
||||
|
||||
// Test empty URL
|
||||
config.url = "".to_string();
|
||||
config.url = String::new();
|
||||
assert!(config.validate().is_err());
|
||||
|
||||
// Test zero max connections
|
||||
|
|
|
|||
|
|
@ -1,15 +1,12 @@
|
|||
//! Integration tests for repository CRUD operations.
|
||||
//! Requires TEST_DATABASE_URL to be set to a PostgreSQL connection string.
|
||||
//! Requires `TEST_DATABASE_URL` to be set to a `PostgreSQL` connection string.
|
||||
|
||||
use fc_common::{models::*, repo};
|
||||
|
||||
async fn get_pool() -> Option<sqlx::PgPool> {
|
||||
let url = match std::env::var("TEST_DATABASE_URL") {
|
||||
Ok(url) => url,
|
||||
Err(_) => {
|
||||
println!("Skipping repo test: TEST_DATABASE_URL not set");
|
||||
return None;
|
||||
},
|
||||
let Ok(url) = std::env::var("TEST_DATABASE_URL") else {
|
||||
println!("Skipping repo test: TEST_DATABASE_URL not set");
|
||||
return None;
|
||||
};
|
||||
|
||||
let pool = sqlx::postgres::PgPoolOptions::new()
|
||||
|
|
@ -85,7 +82,7 @@ async fn create_test_build(
|
|||
evaluation_id: eval_id,
|
||||
job_name: job_name.to_string(),
|
||||
drv_path: drv_path.to_string(),
|
||||
system: system.map(|s| s.to_string()),
|
||||
system: system.map(std::string::ToString::to_string),
|
||||
outputs: None,
|
||||
is_aggregate: None,
|
||||
constituents: None,
|
||||
|
|
@ -98,9 +95,8 @@ async fn create_test_build(
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_project_crud() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Create
|
||||
|
|
@ -148,9 +144,8 @@ async fn test_project_crud() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_project_unique_constraint() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let name = format!("unique-test-{}", uuid::Uuid::new_v4());
|
||||
|
|
@ -176,9 +171,8 @@ async fn test_project_unique_constraint() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_jobset_crud() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let project = create_test_project(&pool, "jobset").await;
|
||||
|
|
@ -242,9 +236,8 @@ async fn test_jobset_crud() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_evaluation_and_build_lifecycle() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Set up project and jobset
|
||||
|
|
@ -391,9 +384,8 @@ async fn test_evaluation_and_build_lifecycle() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_not_found_errors() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let fake_id = uuid::Uuid::new_v4();
|
||||
|
|
@ -423,9 +415,8 @@ async fn test_not_found_errors() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_batch_get_completed_by_drv_paths() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let project = create_test_project(&pool, "batch-drv").await;
|
||||
|
|
@ -493,9 +484,8 @@ async fn test_batch_get_completed_by_drv_paths() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_batch_check_deps_for_builds() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let project = create_test_project(&pool, "batch-deps").await;
|
||||
|
|
@ -568,9 +558,8 @@ async fn test_batch_check_deps_for_builds() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_list_filtered_with_system_filter() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let project = create_test_project(&pool, "filter-sys").await;
|
||||
|
|
@ -641,9 +630,8 @@ async fn test_list_filtered_with_system_filter() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_list_filtered_with_job_name_filter() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let project = create_test_project(&pool, "filter-job").await;
|
||||
|
|
@ -705,9 +693,8 @@ async fn test_list_filtered_with_job_name_filter() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_reset_orphaned_batch_limit() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let project = create_test_project(&pool, "orphan").await;
|
||||
|
|
@ -747,9 +734,8 @@ async fn test_reset_orphaned_batch_limit() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_build_cancel_cascade() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let project = create_test_project(&pool, "cancel-cascade").await;
|
||||
|
|
@ -786,9 +772,8 @@ async fn test_build_cancel_cascade() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_dedup_by_drv_path() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let project = create_test_project(&pool, "dedup").await;
|
||||
|
|
|
|||
|
|
@ -1,16 +1,13 @@
|
|||
//! Integration tests for advanced search functionality
|
||||
//! Requires TEST_DATABASE_URL to be set to a PostgreSQL connection string.
|
||||
//! Requires `TEST_DATABASE_URL` to be set to a `PostgreSQL` connection string.
|
||||
|
||||
use fc_common::{BuildStatus, models::*, repo, repo::search::*};
|
||||
use uuid::Uuid;
|
||||
|
||||
async fn get_pool() -> Option<sqlx::PgPool> {
|
||||
let url = match std::env::var("TEST_DATABASE_URL") {
|
||||
Ok(url) => url,
|
||||
Err(_) => {
|
||||
println!("Skipping search test: TEST_DATABASE_URL not set");
|
||||
return None;
|
||||
},
|
||||
let Ok(url) = std::env::var("TEST_DATABASE_URL") else {
|
||||
println!("Skipping search test: TEST_DATABASE_URL not set");
|
||||
return None;
|
||||
};
|
||||
|
||||
let pool = sqlx::postgres::PgPoolOptions::new()
|
||||
|
|
@ -27,9 +24,8 @@ async fn get_pool() -> Option<sqlx::PgPool> {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_project_search() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Create test projects
|
||||
|
|
@ -93,9 +89,8 @@ async fn test_project_search() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_build_search_with_filters() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Setup: project -> jobset -> evaluation -> builds
|
||||
|
|
@ -209,7 +204,7 @@ async fn test_build_search_with_filters() {
|
|||
|
||||
// Search with status filter (succeeded)
|
||||
let params = SearchParams {
|
||||
query: "".to_string(),
|
||||
query: String::new(),
|
||||
entities: vec![SearchEntity::Builds],
|
||||
limit: 10,
|
||||
offset: 0,
|
||||
|
|
@ -240,9 +235,8 @@ async fn test_build_search_with_filters() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_multi_entity_search() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Create project with jobset, evaluation, and build
|
||||
|
|
@ -324,9 +318,8 @@ async fn test_multi_entity_search() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_search_pagination() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Create multiple projects
|
||||
|
|
@ -334,7 +327,7 @@ async fn test_search_pagination() {
|
|||
for i in 0..5 {
|
||||
let project = repo::projects::create(&pool, CreateProject {
|
||||
name: format!("page-test-{}-{}", i, Uuid::new_v4().simple()),
|
||||
description: Some(format!("Page test project {}", i)),
|
||||
description: Some(format!("Page test project {i}")),
|
||||
repository_url: "https://github.com/test/page".to_string(),
|
||||
})
|
||||
.await
|
||||
|
|
@ -385,9 +378,8 @@ async fn test_search_pagination() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_search_sorting() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Create projects in reverse alphabetical order
|
||||
|
|
@ -433,14 +425,13 @@ async fn test_search_sorting() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_empty_search() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Empty query should return all entities (up to limit)
|
||||
let params = SearchParams {
|
||||
query: "".to_string(),
|
||||
query: String::new(),
|
||||
entities: vec![SearchEntity::Projects],
|
||||
limit: 10,
|
||||
offset: 0,
|
||||
|
|
@ -459,9 +450,8 @@ async fn test_empty_search() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_quick_search() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Create test data: project -> jobset -> evaluation -> build
|
||||
|
|
|
|||
|
|
@ -1,17 +1,14 @@
|
|||
//! Integration tests for user management - CRUD, authentication, and
|
||||
//! relationships. Requires TEST_DATABASE_URL to be set to a PostgreSQL
|
||||
//! relationships. Requires `TEST_DATABASE_URL` to be set to a `PostgreSQL`
|
||||
//! connection string.
|
||||
|
||||
use fc_common::{models::*, repo};
|
||||
use uuid::Uuid;
|
||||
|
||||
async fn get_pool() -> Option<sqlx::PgPool> {
|
||||
let url = match std::env::var("TEST_DATABASE_URL") {
|
||||
Ok(url) => url,
|
||||
Err(_) => {
|
||||
println!("Skipping repo test: TEST_DATABASE_URL not set");
|
||||
return None;
|
||||
},
|
||||
let Ok(url) = std::env::var("TEST_DATABASE_URL") else {
|
||||
println!("Skipping repo test: TEST_DATABASE_URL not set");
|
||||
return None;
|
||||
};
|
||||
|
||||
let pool = sqlx::postgres::PgPoolOptions::new()
|
||||
|
|
@ -28,13 +25,12 @@ async fn get_pool() -> Option<sqlx::PgPool> {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_user_crud() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let username = format!("test-user-{}", Uuid::new_v4().simple());
|
||||
let email = format!("{}@example.com", username);
|
||||
let email = format!("{username}@example.com");
|
||||
|
||||
// Create user
|
||||
let user = repo::users::create(&pool, &CreateUser {
|
||||
|
|
@ -82,7 +78,7 @@ async fn test_user_crud() {
|
|||
assert!(count > 0);
|
||||
|
||||
// Update email
|
||||
let new_email = format!("updated-{}", email);
|
||||
let new_email = format!("updated-{email}");
|
||||
let updated = repo::users::update_email(&pool, user.id, &new_email)
|
||||
.await
|
||||
.expect("update email");
|
||||
|
|
@ -135,9 +131,8 @@ async fn test_user_crud() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_user_authentication() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let username = format!("auth-test-{}", Uuid::new_v4().simple());
|
||||
|
|
@ -146,7 +141,7 @@ async fn test_user_authentication() {
|
|||
// Create user
|
||||
let user = repo::users::create(&pool, &CreateUser {
|
||||
username: username.clone(),
|
||||
email: format!("{}@example.com", username),
|
||||
email: format!("{username}@example.com"),
|
||||
full_name: None,
|
||||
password: password.to_string(),
|
||||
role: None,
|
||||
|
|
@ -234,13 +229,12 @@ async fn test_password_hashing() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_user_unique_constraints() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let username = format!("unique-{}", Uuid::new_v4().simple());
|
||||
let email = format!("{}@example.com", username);
|
||||
let email = format!("{username}@example.com");
|
||||
|
||||
// Create first user
|
||||
let _ = repo::users::create(&pool, &CreateUser {
|
||||
|
|
@ -256,7 +250,7 @@ async fn test_user_unique_constraints() {
|
|||
// Try to create with same username
|
||||
let result = repo::users::create(&pool, &CreateUser {
|
||||
username: username.clone(),
|
||||
email: format!("other-{}", email),
|
||||
email: format!("other-{email}"),
|
||||
full_name: None,
|
||||
password: "password".to_string(),
|
||||
role: None,
|
||||
|
|
@ -266,7 +260,7 @@ async fn test_user_unique_constraints() {
|
|||
|
||||
// Try to create with same email
|
||||
let result = repo::users::create(&pool, &CreateUser {
|
||||
username: format!("other-{}", username),
|
||||
username: format!("other-{username}"),
|
||||
email: email.clone(),
|
||||
full_name: None,
|
||||
password: "password".to_string(),
|
||||
|
|
@ -285,13 +279,12 @@ async fn test_user_unique_constraints() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_oauth_user_creation() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let username = format!("oauth-user-{}", Uuid::new_v4().simple());
|
||||
let email = format!("{}@github.com", username);
|
||||
let email = format!("{username}@github.com");
|
||||
let oauth_provider_id = format!("github_{}", Uuid::new_v4().simple());
|
||||
|
||||
// Create OAuth user
|
||||
|
|
@ -330,9 +323,8 @@ async fn test_oauth_user_creation() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_starred_jobs_crud() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Create prerequisite data
|
||||
|
|
@ -442,9 +434,8 @@ async fn test_starred_jobs_crud() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_starred_jobs_delete_by_job() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Setup
|
||||
|
|
@ -516,9 +507,8 @@ async fn test_starred_jobs_delete_by_job() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_project_members_crud() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Setup
|
||||
|
|
@ -615,9 +605,8 @@ async fn test_project_members_crud() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_project_members_permissions() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Setup
|
||||
|
|
@ -809,9 +798,8 @@ async fn test_project_members_permissions() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn test_user_not_found_errors() {
|
||||
let pool = match get_pool().await {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
let Some(pool) = get_pool().await else {
|
||||
return;
|
||||
};
|
||||
|
||||
let fake_id = Uuid::new_v4();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue