fc-common: add declarative sync for webhooks and notifications
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I0b7c46feba776837158507bfe883cbfa6a6a6964
This commit is contained in:
parent
dead111dfb
commit
7e6fc22ba2
2 changed files with 132 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ use sqlx::PgPool;
|
|||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
config::DeclarativeNotification,
|
||||
error::{CiError, Result},
|
||||
models::{CreateNotificationConfig, NotificationConfig},
|
||||
};
|
||||
|
|
@ -58,3 +59,65 @@ pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
|
|||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Upsert a notification config (insert or update on conflict).
|
||||
pub async fn upsert(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
notification_type: &str,
|
||||
config: &serde_json::Value,
|
||||
enabled: bool,
|
||||
) -> Result<NotificationConfig> {
|
||||
sqlx::query_as::<_, NotificationConfig>(
|
||||
"INSERT INTO notification_configs (project_id, notification_type, config, \
|
||||
enabled) VALUES ($1, $2, $3, $4) ON CONFLICT (project_id, notification_type) \
|
||||
DO UPDATE SET config = EXCLUDED.config, enabled = EXCLUDED.enabled \
|
||||
RETURNING *",
|
||||
)
|
||||
.bind(project_id)
|
||||
.bind(notification_type)
|
||||
.bind(config)
|
||||
.bind(enabled)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(CiError::Database)
|
||||
}
|
||||
|
||||
/// Sync notification configs from declarative config.
|
||||
/// Deletes configs not in the declarative list and upserts those that are.
|
||||
pub async fn sync_for_project(
|
||||
pool: &PgPool,
|
||||
project_id: Uuid,
|
||||
notifications: &[DeclarativeNotification],
|
||||
) -> Result<()> {
|
||||
// Get notification types from declarative config
|
||||
let types: Vec<&str> = notifications
|
||||
.iter()
|
||||
.map(|n| n.notification_type.as_str())
|
||||
.collect();
|
||||
|
||||
// Delete notification configs not in declarative config
|
||||
sqlx::query(
|
||||
"DELETE FROM notification_configs WHERE project_id = $1 AND \
|
||||
notification_type != ALL($2::text[])",
|
||||
)
|
||||
.bind(project_id)
|
||||
.bind(&types)
|
||||
.execute(pool)
|
||||
.await
|
||||
.map_err(CiError::Database)?;
|
||||
|
||||
// Upsert each notification config
|
||||
for notification in notifications {
|
||||
upsert(
|
||||
pool,
|
||||
project_id,
|
||||
¬ification.notification_type,
|
||||
¬ification.config,
|
||||
notification.enabled,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue