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:
parent
abd16319f2
commit
d4d9297d96
6 changed files with 455 additions and 27 deletions
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue