feat: db: list all webhooks created by user
This commit is contained in:
parent
201032fd07
commit
5d4977f421
2 changed files with 120 additions and 64 deletions
|
@ -98,6 +98,38 @@
|
||||||
},
|
},
|
||||||
"query": "SELECT name, password FROM librepages_users WHERE name = ($1)"
|
"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": {
|
"279b5ae27935279b06d2799eef2da6a316324a05d23ba7a729c608c70168c496": {
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
|
@ -315,39 +347,6 @@
|
||||||
},
|
},
|
||||||
"query": "DELETE FROM librepages_users WHERE name = ($1)"
|
"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": {
|
"65f6181364cd8c6ed4eae3f62b5ae771a27e8da6e698c235ca77d4cec784d04b": {
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"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 "
|
"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": {
|
"7d2b7a4a57b9b031d15db57116807355e9e03b7bf9b0cff0958bfebe4bc1d1be": {
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
|
|
86
src/db.rs
86
src/db.rs
|
@ -709,19 +709,41 @@ impl Database {
|
||||||
Ok(hook)
|
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(
|
pub async fn get_webhook_with_owner(
|
||||||
&self,
|
&self,
|
||||||
auth_token: &str,
|
auth_token: &str,
|
||||||
owner: &str,
|
owner: &str,
|
||||||
) -> ServiceResult<GiteaWebhook> {
|
) -> ServiceResult<GiteaWebhook> {
|
||||||
struct H {
|
|
||||||
gitea_url: String,
|
|
||||||
auth_token: String,
|
|
||||||
gitea_webhook_secret: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
let h = sqlx::query_as!(
|
let h = sqlx::query_as!(
|
||||||
H,
|
InnerGiteaWebhook,
|
||||||
"SELECT
|
"SELECT
|
||||||
gitea_url, auth_token, gitea_webhook_secret
|
gitea_url, auth_token, gitea_webhook_secret
|
||||||
FROM
|
FROM
|
||||||
|
@ -738,24 +760,12 @@ impl Database {
|
||||||
.await
|
.await
|
||||||
.map_err(|e| map_row_not_found_err(e, ServiceError::WebhookNotFound))?;
|
.map_err(|e| map_row_not_found_err(e, ServiceError::WebhookNotFound))?;
|
||||||
|
|
||||||
let h = GiteaWebhook {
|
h.to_webhook()
|
||||||
gitea_url: Url::parse(&h.gitea_url).unwrap(),
|
|
||||||
auth_token: h.auth_token,
|
|
||||||
gitea_webhook_secret: h.gitea_webhook_secret,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(h)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_webhook(&self, auth_token: &str) -> ServiceResult<GiteaWebhook> {
|
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!(
|
let h = sqlx::query_as!(
|
||||||
H,
|
InnerGiteaWebhook,
|
||||||
"SELECT gitea_url, auth_token, gitea_webhook_secret
|
"SELECT gitea_url, auth_token, gitea_webhook_secret
|
||||||
FROM librepages_gitea_webhooks
|
FROM librepages_gitea_webhooks
|
||||||
WHERE auth_token = $1
|
WHERE auth_token = $1
|
||||||
|
@ -766,17 +776,11 @@ impl Database {
|
||||||
.await
|
.await
|
||||||
.map_err(|e| map_row_not_found_err(e, ServiceError::WebhookNotFound))?;
|
.map_err(|e| map_row_not_found_err(e, ServiceError::WebhookNotFound))?;
|
||||||
|
|
||||||
let h = GiteaWebhook {
|
h.to_webhook()
|
||||||
gitea_url: Url::parse(&h.gitea_url).unwrap(),
|
|
||||||
auth_token: h.auth_token,
|
|
||||||
gitea_webhook_secret: h.gitea_webhook_secret,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(h)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// register a webhook against a site
|
/// 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!(
|
sqlx::query!(
|
||||||
"INSERT INTO librepages_gitea_webhook_site_mapping
|
"INSERT INTO librepages_gitea_webhook_site_mapping
|
||||||
(site_id, gitea_webhook_id) VALUES (
|
(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 {
|
fn now_unix_time_stamp() -> OffsetDateTime {
|
||||||
OffsetDateTime::now_utc()
|
OffsetDateTime::now_utc()
|
||||||
}
|
}
|
||||||
|
@ -1228,6 +1248,10 @@ mod tests {
|
||||||
let gitea_url = Url::parse("https://example.org").unwrap();
|
let gitea_url = Url::parse("https://example.org").unwrap();
|
||||||
let hook = db.new_webhook(gitea_url, NAME).await.unwrap();
|
let hook = db.new_webhook(gitea_url, NAME).await.unwrap();
|
||||||
assert_eq!(hook, db.get_webhook(&hook.auth_token).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!(
|
assert_eq!(
|
||||||
hook,
|
hook,
|
||||||
db.get_webhook_with_owner(&hook.auth_token, NAME)
|
db.get_webhook_with_owner(&hook.auth_token, NAME)
|
||||||
|
@ -1239,10 +1263,10 @@ mod tests {
|
||||||
Some(ServiceError::WebhookNotFound)
|
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
|
.await
|
||||||
.unwrap();
|
.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
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue