feat: db: get webhook with owner's username

This commit is contained in:
Aravinth Manivannan 2022-12-28 03:21:42 +05:30
parent 51e3924d71
commit bce25be282
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
2 changed files with 81 additions and 4 deletions

View file

@ -315,6 +315,39 @@
},
"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": [

View file

@ -709,7 +709,45 @@ impl Database {
Ok(hook)
}
pub async fn get_wenhook(&self, auth_token: &str) -> ServiceResult<GiteaWebhook> {
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,
"SELECT
gitea_url, auth_token, gitea_webhook_secret
FROM
librepages_gitea_webhooks
WHERE
auth_token = $1
AND
owned_by = (SELECT ID FROM librepages_users WHERE name = $2);
",
auth_token,
owner
)
.fetch_one(&self.pool)
.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)
}
pub async fn get_webhook(&self, auth_token: &str) -> ServiceResult<GiteaWebhook> {
struct H {
gitea_url: String,
auth_token: String,
@ -864,7 +902,7 @@ pub struct LibrePagesEvent {
pub id: Uuid,
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct GiteaWebhook {
pub gitea_url: Url,
pub gitea_webhook_secret: String,
@ -1189,9 +1227,15 @@ mod tests {
// add webhook
let gitea_url = Url::parse("https://example.org").unwrap();
let hook = db.new_webhook(gitea_url, NAME).await.unwrap();
assert_eq!(hook, db.get_wenhook(&hook.auth_token).await.unwrap());
assert_eq!(hook, db.get_webhook(&hook.auth_token).await.unwrap());
assert_eq!(
db.get_wenhook(&hook.gitea_webhook_secret).await.err(),
hook,
db.get_webhook_with_owner(&hook.auth_token, NAME)
.await
.unwrap()
);
assert_eq!(
db.get_webhook(&hook.gitea_webhook_secret).await.err(),
Some(ServiceError::WebhookNotFound)
);