From 88f891e19bc81ef39fb0de71042a0a3e30376c9d Mon Sep 17 00:00:00 2001 From: MrExplode Date: Tue, 31 Oct 2023 23:45:16 +0100 Subject: [PATCH] feat: Properly implement request logging I swear it works this time!!! --- Cargo.lock | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/jira.rs | 43 ++++++++-------- 3 files changed, 161 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 50cf13f..581fa7a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -247,6 +247,21 @@ dependencies = [ "alloc-no-stdlib", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -316,6 +331,12 @@ dependencies = [ "alloc-stdlib", ] +[[package]] +name = "bumpalo" +version = "3.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" + [[package]] name = "bytes" version = "1.5.0" @@ -347,6 +368,20 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-targets", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -628,6 +663,7 @@ version = "0.1.0" dependencies = [ "actix-web", "actix-web-httpauth", + "chrono", "dotenv", "env_logger", "serde", @@ -713,6 +749,29 @@ dependencies = [ "tokio-native-tls", ] +[[package]] +name = "iana-time-zone" +version = "0.1.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8326b86b6cff230b97d0d312a6c40a60726df3332e721f72a1b035f451663b20" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "idna" version = "0.4.0" @@ -759,6 +818,15 @@ dependencies = [ "libc", ] +[[package]] +name = "js-sys" +version = "0.3.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5f195fe497f702db0f318b07fdd68edb16955aed830df8363d837542f8f935a" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "language-tags" version = "0.3.2" @@ -867,6 +935,15 @@ dependencies = [ "tempfile", ] +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + [[package]] name = "object" version = "0.32.1" @@ -1529,6 +1606,60 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn 2.0.38", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.38", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" + [[package]] name = "webhook" version = "2.1.2" @@ -1572,6 +1703,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-core" +version = "0.51.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index a155de5..6f26dca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ serde_json = "1.0" serde_qs = "0.12.0" env_logger = "0.10.0" dotenv = "0.15.0" +chrono = "0.4.31" [[bin]] name = "hooks" diff --git a/src/jira.rs b/src/jira.rs index f75b84c..68c85af 100644 --- a/src/jira.rs +++ b/src/jira.rs @@ -8,6 +8,7 @@ use actix_web::{ web::{self, Data}, HttpResponse, Responder, }; +use chrono::Utc; use serde::Deserialize; use serde_json::json; use webhook::models::Message; @@ -27,12 +28,7 @@ struct Info { } #[post("/jira")] -async fn handle( - info: web::Query, - body: web::Json, - raw_body: String, - clients: Data>, -) -> impl Responder { +async fn handle(info: web::Query, 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"})), @@ -42,22 +38,34 @@ async fn handle( return HttpResponse::Unauthorized().finish(); } - if env::var("DEBUG_REQUESTS").unwrap_or("".to_string()) == "" { - let _ = log_request(raw_body); + if env::var("LOG_REQUESTS").unwrap_or("".to_string()) != "" { + let _ = log_request(&body); } - let _ = clients.jira_client.send_message(&message(&body)).await; + let j: JiraData = serde_json::from_str(&body).unwrap(); + let _ = clients.jira_client.send_message(&create_message(j)).await; HttpResponse::Accepted().finish() } -fn log_request(data: String) -> std::io::Result<()> { - let mut file = File::open("last_request.json")?; +fn log_request(data: &String) -> std::io::Result<()> { + let mut file = File::create(format!("request_{}.json", Utc::now().format("%m-%d_%H-%M-%S")))?; file.write_all(data.as_bytes())?; Ok(()) } -fn message(data: &web::Json) -> Message { +fn root_url(url: &str) -> String { + let idx = url.replace("https://", "").find('/').unwrap(); + url.chars().take(8 + idx).collect() +} + +fn extract_event_name(event: &str) -> String { + let mut chars: Vec = event.replace('_', " ").replace("jira:", "").chars().collect(); + chars[0] = chars[0].to_uppercase().next().unwrap(); + chars.into_iter().collect() +} + +fn create_message(data: JiraData) -> Message { let root_url = root_url(&data.user.self_url); let event_name = extract_event_name(&data.webhook_event); let mut msg: Message = Message::new(); @@ -98,14 +106,3 @@ fn message(data: &web::Json) -> Message { }); msg } - -fn root_url(url: &str) -> String { - let idx = url.replace("https://", "").find('/').unwrap(); - url.chars().take(8 + idx).collect() -} - -fn extract_event_name(event: &str) -> String { - let mut chars: Vec = event.replace('_', " ").replace("jira:", "").chars().collect(); - chars[0] = chars[0].to_uppercase().next().unwrap(); - chars.into_iter().collect() -}