feat: REST API: add and view webhooks

This commit is contained in:
Aravinth Manivannan 2022-12-28 03:22:23 +05:30
parent bce25be282
commit 201032fd07
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
3 changed files with 168 additions and 0 deletions

163
src/api/v1/gitea.rs Normal file
View File

@ -0,0 +1,163 @@
/*
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use actix_identity::Identity;
use actix_web::{web, HttpRequest, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use tracing::info;
use url::Url;
use super::get_auth_middleware;
use crate::{errors::*, AppCtx};
pub mod routes {
pub struct Gitea {
pub add_webhook: &'static str,
pub view_webhook: &'static str,
pub webhook: &'static str,
}
impl Gitea {
pub const fn new() -> Self {
Self {
add_webhook: "/api/v1/gitea/webhook/add",
view_webhook: "/api/v1/gitea/webhook/view/{auth_token}",
webhook: "/api/v1/gitea/webhook/event/new",
}
}
pub fn get_view(&self, auth_token: &str) -> String {
self.view_webhook.replace("{auth_token}", auth_token)
}
}
}
#[derive(Serialize, Deserialize)]
pub struct AddWebhook {
pub gitea_url: Url,
}
#[actix_web_codegen_const_routes::post(
path = "crate::V1_API_ROUTES.gitea.add_webhook",
wrap = "get_auth_middleware()"
)]
#[tracing::instrument(name = "Add webhook" skip(id, ctx, payload))]
async fn add_webhook(
ctx: AppCtx,
id: Identity,
payload: web::Json<AddWebhook>,
) -> ServiceResult<impl Responder> {
info!(
"Adding webhook for Gitea instance: {}",
payload.gitea_url.as_str()
);
let owner = id.identity().unwrap();
let payload = payload.into_inner();
let hook = ctx.db.new_webhook(payload.gitea_url, &owner).await?;
Ok(HttpResponse::Ok().json(hook))
}
#[actix_web_codegen_const_routes::get(
path = "crate::V1_API_ROUTES.gitea.view_webhook",
wrap = "get_auth_middleware()"
)]
#[tracing::instrument(name = "Delete webhook" skip(id, ctx, path))]
async fn view_webhook(
ctx: AppCtx,
id: Identity,
path: web::Path<String>,
) -> ServiceResult<impl Responder> {
let path = path.into_inner();
let owner = id.identity().unwrap();
info!("Gitting webhook webhook for Gitea instance: {}", path,);
let hook = ctx.db.get_webhook_with_owner(&path, &owner).await?;
Ok(HttpResponse::Ok().json(hook))
}
#[derive(Serialize, Deserialize)]
struct Auth {
auth: String,
}
#[actix_web_codegen_const_routes::post(path = "crate::V1_API_ROUTES.gitea.webhook")]
#[tracing::instrument(name = "Update ", skip(body, ctx, req, q))]
async fn webhook(
ctx: AppCtx,
body: web::Bytes,
req: HttpRequest,
q: web::Query<Auth>,
) -> ServiceResult<impl Responder> {
ctx.process_webhook(&body, &req, &q.auth).await?;
Ok(HttpResponse::Ok())
}
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(add_webhook);
cfg.service(view_webhook);
cfg.service(webhook);
}
#[cfg(test)]
mod tests {
use actix_web::{http::StatusCode, test};
use crate::db::GiteaWebhook;
use crate::tests;
use crate::*;
use super::*;
#[actix_rt::test]
async fn test_api_gitea_webhook() {
const NAME: &str = "dplyinfwrkuser";
const PASSWORD: &str = "longpasswordasdfa2";
const EMAIL: &str = "dplyinfwrkuser@a.com";
let (_dir, ctx) = tests::get_ctx().await;
let _ = ctx.delete_user(NAME, PASSWORD).await;
let (_, signin_resp) = ctx.register_and_signin(NAME, EMAIL, PASSWORD).await;
let cookies = get_cookie!(signin_resp);
let app = get_app!(ctx).await;
let payload = AddWebhook {
gitea_url: Url::parse("https://git.batnsense.net").unwrap(),
};
let add_webhook_resp = test::call_service(
&app,
post_request!(&payload, V1_API_ROUTES.gitea.add_webhook)
.cookie(cookies.clone())
.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);
let view_webhook_resp = get_request!(
&app,
&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);
}
}

View File

@ -20,6 +20,7 @@ use serde::Deserialize;
pub mod account;
pub mod auth;
pub mod gitea;
pub mod meta;
pub mod pages;
pub mod routes;
@ -30,6 +31,7 @@ pub fn services(cfg: &mut ServiceConfig) {
auth::services(cfg);
account::services(cfg);
meta::services(cfg);
gitea::services(cfg);
pages::services(cfg);
}

View File

@ -19,6 +19,7 @@ use actix_auth_middleware::GetLoginRoute;
use crate::serve::routes::Serve;
use super::gitea::routes::Gitea;
use super::meta::routes::Meta;
use super::pages::routes::Deploy;
@ -92,6 +93,7 @@ pub struct Routes {
pub account: Account,
/// Meta routes
pub meta: Meta,
pub gitea: Gitea,
pub deploy: Deploy,
pub serve: Serve,
}
@ -103,6 +105,7 @@ impl Routes {
auth: Auth::new(),
account: Account::new(),
meta: Meta::new(),
gitea: Gitea::new(),
deploy: Deploy::new(),
serve: Serve::new(),
}