feat: db: list all webhooks created by user

This commit is contained in:
Aravinth Manivannan 2022-12-28 03:42:00 +05:30
parent 201032fd07
commit 5d4977f421
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
2 changed files with 120 additions and 64 deletions

View file

@ -98,6 +98,38 @@
},
"query": "SELECT name, password FROM librepages_users WHERE name = ($1)"
},
"278dafae8343ee7b15b7014707a769e6d8f5042478c001d3dbe6cdad919f4546": {
"describe": {
"columns": [
{
"name": "gitea_url",
"ordinal": 0,
"type_info": "Varchar"
},
{
"name": "auth_token",
"ordinal": 1,
"type_info": "Varchar"
},
{
"name": "gitea_webhook_secret",
"ordinal": 2,
"type_info": "Varchar"
}
],
"nullable": [
false,
false,
false
],
"parameters": {
"Left": [
"Text"
]
}
},
"query": "SELECT\n gitea_url, auth_token, gitea_webhook_secret\n FROM\n librepages_gitea_webhooks\n WHERE\n owned_by = (SELECT ID FROM librepages_users WHERE name = $1);\n "
},
"279b5ae27935279b06d2799eef2da6a316324a05d23ba7a729c608c70168c496": {
"describe": {
"columns": [],
@ -315,39 +347,6 @@
},
"query": "DELETE FROM librepages_users WHERE name = ($1)"
},
"5eb0f3aa8f79f8392eda22dbceb088d6951f7bead2b090e2d194c078d057308a": {
"describe": {
"columns": [
{
"name": "gitea_url",
"ordinal": 0,
"type_info": "Varchar"
},
{
"name": "auth_token",
"ordinal": 1,
"type_info": "Varchar"
},
{
"name": "gitea_webhook_secret",
"ordinal": 2,
"type_info": "Varchar"
}
],
"nullable": [
false,
false,
false
],
"parameters": {
"Left": [
"Text",
"Text"
]
}
},
"query": "SELECT\n gitea_url, auth_token, gitea_webhook_secret\n FROM\n librepages_gitea_webhooks\n WHERE\n auth_token = $1\n AND \n owned_by = (SELECT ID FROM librepages_users WHERE name = $2);\n "
},
"65f6181364cd8c6ed4eae3f62b5ae771a27e8da6e698c235ca77d4cec784d04b": {
"describe": {
"columns": [
@ -453,6 +452,39 @@
},
"query": "SELECT\n time,\n pub_id\n FROM\n librepages_site_deploy_events\n WHERE\n site = (SELECT ID FROM librepages_sites WHERE hostname = $1)\n AND\n event_type = (SELECT ID FROM librepages_deploy_event_type WHERE name = $2)\n AND\n time = (\n SELECT MAX(time) \n FROM\n librepages_site_deploy_events\n WHERE\n site = (SELECT ID FROM librepages_sites WHERE hostname = $1)\n )\n "
},
"78d6aacc46441d72e42bc8d74f36f98056b442dd0e624757b1f25db29610cb08": {
"describe": {
"columns": [
{
"name": "gitea_url",
"ordinal": 0,
"type_info": "Varchar"
},
{
"name": "auth_token",
"ordinal": 1,
"type_info": "Varchar"
},
{
"name": "gitea_webhook_secret",
"ordinal": 2,
"type_info": "Varchar"
}
],
"nullable": [
false,
false,
false
],
"parameters": {
"Left": [
"Text",
"Text"
]
}
},
"query": "SELECT\n gitea_url, auth_token, gitea_webhook_secret\n FROM\n librepages_gitea_webhooks\n WHERE\n auth_token = $1\n AND\n owned_by = (SELECT ID FROM librepages_users WHERE name = $2);\n "
},
"7d2b7a4a57b9b031d15db57116807355e9e03b7bf9b0cff0958bfebe4bc1d1be": {
"describe": {
"columns": [

View file

@ -709,19 +709,41 @@ impl Database {
Ok(hook)
}
pub async fn list_all_webhooks_with_owner(
&self,
owner: &str,
) -> ServiceResult<Vec<GiteaWebhook>> {
let mut db_hooks = sqlx::query_as!(
InnerGiteaWebhook,
"SELECT
gitea_url, auth_token, gitea_webhook_secret
FROM
librepages_gitea_webhooks
WHERE
owned_by = (SELECT ID FROM librepages_users WHERE name = $1);
",
owner
)
.fetch_all(&self.pool)
.await
.map_err(|e| map_row_not_found_err(e, ServiceError::WebhookNotFound))?;
let mut hooks = Vec::with_capacity(db_hooks.len());
for hook in db_hooks.drain(0..) {
hooks.push(hook.to_webhook()?)
}
Ok(hooks)
}
pub async fn get_webhook_with_owner(
&self,
auth_token: &str,
owner: &str,
) -> ServiceResult<GiteaWebhook> {
struct H {
gitea_url: String,
auth_token: String,
gitea_webhook_secret: String,
}
let h = sqlx::query_as!(
H,
InnerGiteaWebhook,
"SELECT
gitea_url, auth_token, gitea_webhook_secret
FROM
@ -738,24 +760,12 @@ impl Database {
.await
.map_err(|e| map_row_not_found_err(e, ServiceError::WebhookNotFound))?;
let h = GiteaWebhook {
gitea_url: Url::parse(&h.gitea_url).unwrap(),
auth_token: h.auth_token,
gitea_webhook_secret: h.gitea_webhook_secret,
};
Ok(h)
h.to_webhook()
}
pub async fn get_webhook(&self, auth_token: &str) -> ServiceResult<GiteaWebhook> {
struct H {
gitea_url: String,
auth_token: String,
gitea_webhook_secret: String,
}
let h = sqlx::query_as!(
H,
InnerGiteaWebhook,
"SELECT gitea_url, auth_token, gitea_webhook_secret
FROM librepages_gitea_webhooks
WHERE auth_token = $1
@ -766,17 +776,11 @@ impl Database {
.await
.map_err(|e| map_row_not_found_err(e, ServiceError::WebhookNotFound))?;
let h = GiteaWebhook {
gitea_url: Url::parse(&h.gitea_url).unwrap(),
auth_token: h.auth_token,
gitea_webhook_secret: h.gitea_webhook_secret,
};
Ok(h)
h.to_webhook()
}
/// register a webhook against a site
pub async fn webhoo_link_site(&self, auth_token: &str, repo_url: &Url) -> ServiceResult<()> {
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 (
@ -919,6 +923,22 @@ impl GiteaWebhook {
}
}
struct InnerGiteaWebhook {
gitea_url: String,
auth_token: String,
gitea_webhook_secret: String,
}
impl InnerGiteaWebhook {
fn to_webhook(self) -> ServiceResult<GiteaWebhook> {
Ok(GiteaWebhook {
gitea_url: Url::parse(&self.gitea_url)?,
auth_token: self.auth_token,
gitea_webhook_secret: self.gitea_webhook_secret,
})
}
}
fn now_unix_time_stamp() -> OffsetDateTime {
OffsetDateTime::now_utc()
}
@ -1228,6 +1248,10 @@ mod tests {
let gitea_url = Url::parse("https://example.org").unwrap();
let hook = db.new_webhook(gitea_url, NAME).await.unwrap();
assert_eq!(hook, db.get_webhook(&hook.auth_token).await.unwrap());
assert_eq!(
vec![hook.clone()],
db.list_all_webhooks_with_owner(NAME).await.unwrap()
);
assert_eq!(
hook,
db.get_webhook_with_owner(&hook.auth_token, NAME)
@ -1239,10 +1263,10 @@ mod tests {
Some(ServiceError::WebhookNotFound)
);
db.webhoo_link_site(&hook.auth_token, &Url::parse(&site.repo_url).unwrap())
db.webhook_link_site(&hook.auth_token, &Url::parse(&site.repo_url).unwrap())
.await
.unwrap();
db.webhoo_link_site(&hook.auth_token, &Url::parse(&site.repo_url).unwrap())
db.webhook_link_site(&hook.auth_token, &Url::parse(&site.repo_url).unwrap())
.await
.unwrap();