feat: API endpoints to schedule and post results

This commit is contained in:
Aravinth Manivannan 2023-09-29 19:36:28 +05:30
parent 438fe46d58
commit eb870acdb0
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 157 additions and 0 deletions

5
src/api/mod.rs Normal file
View file

@ -0,0 +1,5 @@
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
pub mod v1;

9
src/api/v1/mod.rs Normal file
View file

@ -0,0 +1,9 @@
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
pub mod webhooks;
pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
webhooks::services(cfg);
}

143
src/api/v1/webhooks.rs Normal file
View file

@ -0,0 +1,143 @@
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use crate::complaince::result::Result as CResult;
use crate::errors::*;
use crate::AppCtx;
use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
pub mod routes {
pub struct Tests {
pub init: &'static str,
pub results: &'static str,
}
impl Tests {
pub const fn new() -> Self {
Self {
init: "/api/v1/init",
results: "/api/v1/{auth}/results",
}
}
}
}
const ROUTES: routes::Tests = routes::Tests::new();
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TestEvent {
pub commit: String,
}
#[actix_web_codegen_const_routes::post(path = "ROUTES.results")]
async fn results(
payload: web::Json<CResult>,
auth: web::Path<String>,
ctx: AppCtx,
) -> ServiceResult<impl Responder> {
let auth = auth.into_inner();
{
let r = ctx.results.read().unwrap();
if r.get(&auth).is_none() {
return Err(ServiceError::WrongPassword);
}
}
let tx = {
let mut w = ctx.results.write().unwrap();
w.remove(&auth)
};
if let Some(tx) = tx {
tx.send(payload.into_inner()).await.unwrap();
}
Ok(HttpResponse::Ok())
}
#[actix_web_codegen_const_routes::post(path = "ROUTES.init")]
async fn run_tests(payload: web::Json<TestEvent>, ctx: AppCtx) -> ServiceResult<impl Responder> {
ctx.db.add_job(&payload.commit).await?;
tracing::info!("Creating job for commit {}", payload.commit);
Ok(HttpResponse::Created())
}
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(run_tests);
cfg.service(results);
}
//
//#[cfg(test)]
//mod tests {
// use actix_web::{http::StatusCode, test};
//
// use crate::tests;
// use crate::*;
//
// use super::*;
//
// #[actix_rt::test]
// async fn deploy_update_works() {
// let (_dir, ctx) = tests::get_data().await;
// println!("[log] test configuration {:#?}", ctx.settings);
// let app = get_app!(ctx).await;
// let page = ctx.settings.pages.get(0);
// let page = page.unwrap();
//
// let mut payload = DeployEvent {
// secret: page.secret.clone(),
// branch: page.branch.clone(),
// };
//
// let resp = test::call_service(
// &app,
// post_request!(&payload, V1_API_ROUTES.deploy.update).to_request(),
// )
// .await;
// check_status!(resp, StatusCode::OK);
//
// payload.secret = page.branch.clone();
//
// let resp = test::call_service(
// &app,
// post_request!(&payload, V1_API_ROUTES.deploy.update).to_request(),
// )
// .await;
// check_status!(resp, StatusCode::NOT_FOUND);
// }
//
// #[actix_rt::test]
// async fn deploy_info_works() {
// let (_dir, ctx) = tests::get_data().await;
// println!("[log] test configuration {:#?}", ctx.settings);
// let page = ctx.settings.pages.get(0);
// let page = page.unwrap();
// let app = get_app!(ctx).await;
//
// let mut payload = DeploySecret {
// secret: page.secret.clone(),
// };
//
// let resp = test::call_service(
// &app,
// post_request!(&payload, V1_API_ROUTES.deploy.info).to_request(),
// )
// .await;
//
// check_status!(resp, StatusCode::OK);
//
// let response: DeployInfo = actix_web::test::read_body_json(resp).await;
// assert_eq!(response.head, page.branch);
// assert_eq!(response.remote, page.repo);
//
// payload.secret = page.branch.clone();
//
// let resp = test::call_service(
// &app,
// post_request!(&payload, V1_API_ROUTES.deploy.info).to_request(),
// )
// .await;
// check_status!(resp, StatusCode::NOT_FOUND);
// }
//}
//