From 3660e981fd6c3fb913e5f562f1c723701273d41d Mon Sep 17 00:00:00 2001 From: MrExplode Date: Sat, 21 Oct 2023 21:46:42 +0200 Subject: [PATCH] feat: Add Dockerfile --- Dockerfile | 13 +++++++++++++ src/docker.rs | 16 ++++++++++++++-- src/jira.rs | 16 ++++++++++++++-- src/main.rs | 33 +++++++++++++++++++++++++++++++-- 4 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c06d9ca --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM rust:1.73-alpine as builder + +WORKDIR /app +COPY . . + +RUN cargo install --path . + +FROM alpine:latest as runner + +WORKDIR /app +COPY --from=builder /hooks . + +CMD [ "/app/hooks" ] \ No newline at end of file diff --git a/src/docker.rs b/src/docker.rs index cd85f9f..6c59c53 100644 --- a/src/docker.rs +++ b/src/docker.rs @@ -1,13 +1,16 @@ use std::env; +use std::sync::Arc; -use actix_web::{post, HttpResponse, Responder}; +use actix_web::{post, web::Data, HttpResponse, Responder}; use actix_web_httpauth::extractors::bearer::BearerAuth; use serde_json::json; +use crate::Clients; + const ENV_KEY: &str = "DOCKER_BEARER"; #[post("/docker")] -async fn handle(auth: BearerAuth) -> impl Responder { +async fn handle(auth: BearerAuth, req_body: String, clients: Data>) -> impl Responder { let env_token = match env::var(ENV_KEY) { Ok(v) => v, Err(_) => return HttpResponse::InternalServerError().json(json!({"error": "Missing environment value"})), @@ -17,5 +20,14 @@ async fn handle(auth: BearerAuth) -> impl Responder { return HttpResponse::Unauthorized().finish(); } + let _ = clients + .docker_client + .send(|message| { + message + .username("Docker") + .content(format!("```{}```", req_body).as_str()) + }) + .await; + HttpResponse::Accepted().finish() } diff --git a/src/jira.rs b/src/jira.rs index 37a6cbf..976880a 100644 --- a/src/jira.rs +++ b/src/jira.rs @@ -1,9 +1,16 @@ use std::env; +use std::sync::Arc; -use actix_web::{post, web, HttpResponse, Responder}; +use actix_web::{ + post, + web::{self, Data}, + HttpResponse, Responder, +}; use serde::Deserialize; use serde_json::json; +use crate::Clients; + const ENV_KEY: &str = "JIRA_TOKEN"; #[derive(Deserialize)] @@ -12,7 +19,7 @@ struct Info { } #[post("/jira")] -async fn handle(info: web::Query) -> impl Responder { +async fn handle(info: web::Query, req_body: String, clients: Data>) -> impl Responder { let env_token = match env::var(ENV_KEY) { Ok(v) => v, Err(_) => return HttpResponse::InternalServerError().json(json!({"error": "Missing environment value"})), @@ -22,5 +29,10 @@ async fn handle(info: web::Query) -> impl Responder { return HttpResponse::Unauthorized().finish(); } + let _ = clients + .jira_client + .send(|message| message.username("Jira").content(format!("```{}```", req_body).as_str())) + .await; + HttpResponse::Accepted().finish() } diff --git a/src/main.rs b/src/main.rs index 933740c..1eca2d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,48 @@ mod docker; mod jira; -use std::env; +use std::sync::Arc; +use std::{env, process}; +use actix_web::web::Data; use actix_web::{App, HttpServer}; use actix_web_httpauth::extractors::bearer; +use webhook::client::WebhookClient; + +struct Clients { + jira_client: WebhookClient, + docker_client: WebhookClient, +} #[actix_web::main] async fn main() -> std::io::Result<()> { env::set_var("RUST_LOG", "debug"); env_logger::init(); - HttpServer::new(|| { + let jira_url = match env::var("JIRA_URL") { + Ok(v) => v, + Err(_) => "".to_string(), + }; + + let docker_url = match env::var("DOCKER_URL") { + Ok(v) => v, + Err(_) => "".to_string(), + }; + + if jira_url.is_empty() || docker_url.is_empty() { + println!("Missing webhook url"); + process::exit(-1); + } + + let data = Arc::new(Clients { + jira_client: WebhookClient::new(&jira_url), + docker_client: WebhookClient::new(&docker_url), + }); + + HttpServer::new(move || { App::new() .app_data(bearer::Config::default()) + .app_data(Data::new(data.clone())) .service(jira::handle) .service(docker::handle) })