fix: many-to-one repository mapping.
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
DESCRIPTION A repository can have multiple deployments, this caused problems when linking webhooks to repositories. This patch links a webhook to all available deployments from the repository.
This commit is contained in:
parent
d919bad570
commit
e4a8a6f5e4
3 changed files with 31 additions and 11 deletions
|
@ -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();
|
||||
|
|
|
@ -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",
|
||||
|
|
36
src/db.rs
36
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(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue