feat: add gitea webhook template list REST API view

This commit is contained in:
Aravinth Manivannan 2022-12-28 04:37:42 +05:30
parent 0e5db5c7a9
commit 3d11bfdcfc
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88

View file

@ -24,9 +24,12 @@ use super::get_auth_middleware;
use crate::{errors::*, AppCtx};
pub mod routes {
use crate::ctx::Ctx;
pub struct Gitea {
pub add_webhook: &'static str,
pub view_webhook: &'static str,
pub list_webhooks: &'static str,
pub webhook: &'static str,
}
@ -34,6 +37,7 @@ pub mod routes {
pub const fn new() -> Self {
Self {
add_webhook: "/api/v1/gitea/webhook/add",
list_webhooks: "/api/v1/gitea/webhook/add",
view_webhook: "/api/v1/gitea/webhook/view/{auth_token}",
webhook: "/api/v1/gitea/webhook/event/new",
}
@ -42,6 +46,13 @@ pub mod routes {
pub fn get_view(&self, auth_token: &str) -> String {
self.view_webhook.replace("{auth_token}", auth_token)
}
pub fn get_webhook_url(&self, ctx: &Ctx, auth_token: &str) -> String {
format!(
"https://{}{}?auth={auth_token}",
&ctx.settings.server.domain, self.webhook
)
}
}
}
@ -70,6 +81,18 @@ async fn add_webhook(
Ok(HttpResponse::Ok().json(hook))
}
#[actix_web_codegen_const_routes::get(
path = "crate::V1_API_ROUTES.gitea.list_webhooks",
wrap = "get_auth_middleware()"
)]
#[tracing::instrument(name = "Delete webhook" skip(id, ctx))]
async fn list_webhooks(ctx: AppCtx, id: Identity) -> ServiceResult<impl Responder> {
let owner = id.identity().unwrap();
info!("Getting all webhooks created by {}", owner);
let hooks = ctx.db.list_all_webhooks_with_owner(&owner).await?;
Ok(HttpResponse::Ok().json(hooks))
}
#[actix_web_codegen_const_routes::get(
path = "crate::V1_API_ROUTES.gitea.view_webhook",
wrap = "get_auth_middleware()"
@ -107,6 +130,7 @@ async fn webhook(
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(add_webhook);
cfg.service(view_webhook);
cfg.service(list_webhooks);
cfg.service(webhook);
}
@ -122,9 +146,9 @@ mod tests {
#[actix_rt::test]
async fn test_api_gitea_webhook() {
const NAME: &str = "dplyinfwrkuser";
const NAME: &str = "apigiteawebhookuser";
const PASSWORD: &str = "longpasswordasdfa2";
const EMAIL: &str = "dplyinfwrkuser@a.com";
const EMAIL: &str = "apigiteawebhookuser@a.com";
let (_dir, ctx) = tests::get_ctx().await;
let _ = ctx.delete_user(NAME, PASSWORD).await;
@ -143,9 +167,7 @@ mod tests {
.to_request(),
)
.await;
check_status!(add_webhook_resp, StatusCode::OK);
let response: GiteaWebhook = actix_web::test::read_body_json(add_webhook_resp).await;
assert_eq!(response.gitea_url, payload.gitea_url);
@ -154,10 +176,15 @@ mod tests {
&V1_API_ROUTES.gitea.get_view(&response.auth_token),
cookies.clone()
);
check_status!(view_webhook_resp, StatusCode::OK);
let hook: GiteaWebhook = actix_web::test::read_body_json(view_webhook_resp).await;
assert_eq!(hook, response);
let list_all_webhooks_resp =
get_request!(&app, &V1_API_ROUTES.gitea.list_webhooks, cookies.clone());
check_status!(list_all_webhooks_resp, StatusCode::OK);
let hooks: Vec<GiteaWebhook> =
actix_web::test::read_body_json(list_all_webhooks_resp).await;
assert_eq!(vec![hook], hooks);
}
}