diff --git a/src/api/v1/gitea.rs b/src/api/v1/gitea.rs index 16d1dda..bb2789f 100644 --- a/src/api/v1/gitea.rs +++ b/src/api/v1/gitea.rs @@ -251,7 +251,7 @@ mod tests { // no website let mut webhook_payload = WebhookPayload::default(); webhook_payload.reference = format!("refs/origin/{}", page.branch); - webhook_payload.repository.html_url = "foo".into(); + webhook_payload.repository.html_url = "https://no-exist-git.example.org".into(); let body = serde_json::to_string(&webhook_payload).unwrap(); let body = body.as_bytes(); diff --git a/src/ctx/api/v1/gitea.rs b/src/ctx/api/v1/gitea.rs index c9219d6..df1a59d 100644 --- a/src/ctx/api/v1/gitea.rs +++ b/src/ctx/api/v1/gitea.rs @@ -20,6 +20,7 @@ use hmac::{Hmac, Mac}; use serde::{Deserialize, Serialize}; use sha2::Sha256; use tracing::{info, warn}; +use url::Url; use crate::ctx::Ctx; use crate::errors::ServiceError; @@ -174,6 +175,9 @@ impl Ctx { mac.verify_slice(&sig[..])?; let site = self.db.get_site_from_repo_url(url).await?; + self.db + .webhook_link_site(auth_token, &Url::parse(&site.repo_url)?) + .await?; if payload.reference.contains(&site.branch) { info!( "[webhook][forgejo/gitea] received update {:?} from {url} repository on deployed branch", diff --git a/src/db.rs b/src/db.rs index 00cc82e..80b1577 100644 --- a/src/db.rs +++ b/src/db.rs @@ -781,18 +781,34 @@ impl Database { /// register a webhook against a site pub async fn webhook_link_site(&self, auth_token: &str, repo_url: &Url) -> ServiceResult<()> { - sqlx::query!( - "INSERT INTO librepages_gitea_webhook_site_mapping - (site_id, gitea_webhook_id) VALUES ( - (SELECT ID FROM librepages_sites WHERE repo_url = $1), - (SELECT ID FROM librepages_gitea_webhooks WHERE auth_token = $2) - ) ON CONFLICT (site_id, gitea_webhook_id) DO NOTHING;", - repo_url.as_str(), - auth_token + struct Site { + id: i32, + } + let sites = sqlx::query_as!( + Site, + "SELECT ID FROM librepages_sites WHERE repo_url = $1", + repo_url.as_str() ) - .execute(&self.pool) + .fetch_all(&self.pool) .await - .map_err(|e| map_row_not_found_err(e, ServiceError::WebhookNotFound))?; + .map_err(|e| map_row_not_found_err(e, ServiceError::WebsiteNotFound))?; + + for site in sites { + sqlx::query!( + "INSERT INTO librepages_gitea_webhook_site_mapping + (site_id, gitea_webhook_id) VALUES ( + (SELECT ID FROM librepages_sites WHERE repo_url = $1 AND ID = $2), + (SELECT ID FROM librepages_gitea_webhooks WHERE auth_token = $3) + ) ON CONFLICT (site_id, gitea_webhook_id) DO NOTHING;", + repo_url.as_str(), + site.id, + auth_token + ) + .execute(&self.pool) + .await + //.unwrap(); + .map_err(|e| map_row_not_found_err(e, ServiceError::WebhookNotFound))?; + } Ok(()) } }