fc-common: add declarative sync for remote builders and channels

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I3dae89f04777f6d941824606aebe34446a6a6964
This commit is contained in:
raf 2026-02-08 21:16:32 +03:00
commit d4d9297d96
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
6 changed files with 455 additions and 27 deletions

View file

@ -2,6 +2,7 @@ use sqlx::PgPool;
use uuid::Uuid;
use crate::{
config::DeclarativeChannel,
error::{CiError, Result},
models::{Channel, CreateChannel},
};
@ -86,6 +87,61 @@ pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
Ok(())
}
/// Upsert a channel (insert or update on conflict).
pub async fn upsert(
pool: &PgPool,
project_id: Uuid,
name: &str,
jobset_id: Uuid,
) -> Result<Channel> {
sqlx::query_as::<_, Channel>(
"INSERT INTO channels (project_id, name, jobset_id) VALUES ($1, $2, $3) \
ON CONFLICT (project_id, name) DO UPDATE SET jobset_id = EXCLUDED.jobset_id \
RETURNING *",
)
.bind(project_id)
.bind(name)
.bind(jobset_id)
.fetch_one(pool)
.await
.map_err(CiError::Database)
}
/// Sync channels from declarative config.
/// Deletes channels not in the declarative list and upserts those that are.
pub async fn sync_for_project(
pool: &PgPool,
project_id: Uuid,
channels: &[DeclarativeChannel],
resolve_jobset: impl Fn(&str) -> Option<Uuid>,
) -> Result<()> {
// Get channel names from declarative config
let names: Vec<&str> = channels.iter().map(|c| c.name.as_str()).collect();
// Delete channels not in declarative config
sqlx::query("DELETE FROM channels WHERE project_id = $1 AND name != ALL($2::text[])")
.bind(project_id)
.bind(&names)
.execute(pool)
.await
.map_err(CiError::Database)?;
// Upsert each channel
for channel in channels {
if let Some(jobset_id) = resolve_jobset(&channel.jobset_name) {
upsert(pool, project_id, &channel.name, jobset_id).await?;
} else {
tracing::warn!(
channel = %channel.name,
jobset_name = %channel.jobset_name,
"Could not resolve jobset for declarative channel"
);
}
}
Ok(())
}
/// Find the channel for a jobset and auto-promote if all builds in the
/// evaluation succeeded.
pub async fn auto_promote_if_complete(