From 918d178093897bf1cfe0ddc530f9e92005e86ca6 Mon Sep 17 00:00:00 2001 From: sekomer Date: Sat, 9 Nov 2024 12:37:32 +0300 Subject: [PATCH 1/4] feat: add issue description functionality for gitlab --- src/webhooks/gitlab/webhook_handlers/issue.rs | 46 +++++++++++++++---- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/webhooks/gitlab/webhook_handlers/issue.rs b/src/webhooks/gitlab/webhook_handlers/issue.rs index 4d6622d..4537c8c 100644 --- a/src/webhooks/gitlab/webhook_handlers/issue.rs +++ b/src/webhooks/gitlab/webhook_handlers/issue.rs @@ -1,7 +1,17 @@ use actix_web::web; +use html_escape::encode_text; use serde::Deserialize; use ureq::serde_json; +#[derive(Debug)] +enum IssueAction { + Open, + Update, + Close, + Reopen, + Other(String), +} + #[derive(Debug, Deserialize)] struct IssueEvent { user: User, @@ -13,6 +23,7 @@ struct IssueDetails { title: String, url: String, action: String, + description: String, } #[derive(Debug, Deserialize)] @@ -28,16 +39,31 @@ pub fn handle_issue_event(body: &web::Bytes) -> String { let title = &issue_details.title; let action = &issue_details.action; let user_name = &issue_event.user.name; + let description = encode_text(&issue_details.description); + + let action = match action.as_str() { + "open" => IssueAction::Open, + "update" => IssueAction::Update, + "close" => IssueAction::Close, + "reopen" => IssueAction::Reopen, + other => IssueAction::Other(other.to_string()), + }; - if action == "open" { - format!("{user_name} opened a new issue {title}\n",) - } else if action == "update" { - format!("{user_name} updated issue {title}\n",) - } else if action == "close" { - format!("{user_name} closed issue {title}\n",) - } else if action == "reopen" { - format!("{user_name} reopened issue {title}\n",) - } else { - format!("{user_name} {action} issue {title}\n",) + match action { + IssueAction::Open => format!( + "{user_name} opened a new issue {title}\n{description}\n", + ), + IssueAction::Update => format!( + "{user_name} updated issue {title}\n{description}\n", + ), + IssueAction::Close => { + format!("{user_name} closed issue {title}\n",) + } + IssueAction::Reopen => { + format!("{user_name} reopened issue {title}\n",) + } + IssueAction::Other(action) => { + format!("{user_name} {action} issue {title}\n",) + } } } From 3ac6d1ace9baa8e20e343a25a85c3cbdba3894a4 Mon Sep 17 00:00:00 2001 From: sekomer Date: Sat, 9 Nov 2024 12:44:05 +0300 Subject: [PATCH 2/4] feat: add comments to note event for gitlab --- src/webhooks/gitlab/webhook_handlers/note.rs | 48 ++++++++++++++++---- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/src/webhooks/gitlab/webhook_handlers/note.rs b/src/webhooks/gitlab/webhook_handlers/note.rs index a99e71e..0abddcc 100644 --- a/src/webhooks/gitlab/webhook_handlers/note.rs +++ b/src/webhooks/gitlab/webhook_handlers/note.rs @@ -1,7 +1,17 @@ use actix_web::web; +use html_escape::encode_text; use serde::Deserialize; use ureq::serde_json; +#[derive(Debug)] +enum NoteableType { + Issue, + MergeRequest, + Commit, + Snippet, + Other(String), +} + #[derive(Debug, serde::Deserialize)] struct NoteEvent { user: User, @@ -17,6 +27,7 @@ struct User { struct NoteDetails { url: String, noteable_type: String, + note: String, } pub fn handle_note_event(body: &web::Bytes) -> String { @@ -26,16 +37,33 @@ pub fn handle_note_event(body: &web::Bytes) -> String { let note_details = ¬e_event.object_attributes; let url = ¬e_details.url; let noteable_type = ¬e_details.noteable_type; + let note = encode_text(¬e_details.note); + + let noteable_type = match noteable_type.as_str() { + "Issue" => NoteableType::Issue, + "MergeRequest" => NoteableType::MergeRequest, + "Commit" => NoteableType::Commit, + "Snippet" => NoteableType::Snippet, + other => NoteableType::Other(other.to_string()), + }; - if noteable_type == "Issue" { - format!("{user_name} commented on an issue\n") - } else if noteable_type == "MergeRequest" { - format!("{user_name} commented on a merge request \n") - } else if noteable_type == "Commit" { - format!("{user_name} commented on a commit\n") - } else if noteable_type == "Snippet" { - format!("{user_name} commented on a snippet\n") - } else { - format!("{user_name} commented on a {noteable_type}\n") + match noteable_type { + NoteableType::Issue => { + format!("{user_name} commented on an issue\n{note}\n") + } + NoteableType::MergeRequest => { + format!( + "{user_name} commented on a merge request\n{note}\n" + ) + } + NoteableType::Commit => { + format!("{user_name} commented on a commit\n{note}\n") + } + NoteableType::Snippet => { + format!("{user_name} commented on a snippet\n{note}\n") + } + NoteableType::Other(type_name) => { + format!("{user_name} commented on a {type_name}\n{note}\n") + } } } From e09f46e19d90e77054da07fcb31f08544ffa5519 Mon Sep 17 00:00:00 2001 From: sekomer Date: Sat, 9 Nov 2024 13:53:52 +0300 Subject: [PATCH 3/4] refactor: remove unused pub identifiers --- src/webhooks/github/webhook_handlers/push.rs | 2 +- src/webhooks/gitlab/webhook_handlers/push.rs | 4 +--- src/webhooks/gitlab/webhook_handlers/tag_push.rs | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/webhooks/github/webhook_handlers/push.rs b/src/webhooks/github/webhook_handlers/push.rs index f926ef2..2329ee3 100644 --- a/src/webhooks/github/webhook_handlers/push.rs +++ b/src/webhooks/github/webhook_handlers/push.rs @@ -9,7 +9,7 @@ pub struct PushEvent { forced: bool, commits: Vec, #[serde(rename = "ref")] - pub ref_field: String, + ref_field: String, before: String, after: String, } diff --git a/src/webhooks/gitlab/webhook_handlers/push.rs b/src/webhooks/gitlab/webhook_handlers/push.rs index f452b5c..be390de 100644 --- a/src/webhooks/gitlab/webhook_handlers/push.rs +++ b/src/webhooks/gitlab/webhook_handlers/push.rs @@ -7,7 +7,7 @@ struct PushEvent { before: String, after: String, #[serde(rename = "ref")] - pub ref_field: String, + ref_field: String, project: Project, commits: Vec, user_name: String, @@ -106,8 +106,6 @@ fn create_first_row(push_event: &PushEvent) -> CreateFirstRow { #[cfg(test)] mod tests { use super::*; - use actix_web::web; - use serde_json::json; #[test] fn test_create_first_row_create_branch() { diff --git a/src/webhooks/gitlab/webhook_handlers/tag_push.rs b/src/webhooks/gitlab/webhook_handlers/tag_push.rs index c8ad3ac..a6400d2 100644 --- a/src/webhooks/gitlab/webhook_handlers/tag_push.rs +++ b/src/webhooks/gitlab/webhook_handlers/tag_push.rs @@ -5,7 +5,7 @@ use ureq::serde_json; #[derive(Debug, Deserialize)] struct TagPushEvent { #[serde(rename = "ref")] - pub ref_field: String, + ref_field: String, project: Project, user_name: String, } From 3201484c39d3d0ccec4d29efdc5e40fa9fe5a53a Mon Sep 17 00:00:00 2001 From: sekomer Date: Thu, 28 Nov 2024 09:09:04 +0300 Subject: [PATCH 4/4] feat: add query parameter handling for full message option in gitLab webhook --- src/webhooks/gitlab/http_server.rs | 4 +++- src/webhooks/gitlab/webhook_handlers/note.rs | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/webhooks/gitlab/http_server.rs b/src/webhooks/gitlab/http_server.rs index 031aecd..0325adc 100644 --- a/src/webhooks/gitlab/http_server.rs +++ b/src/webhooks/gitlab/http_server.rs @@ -102,12 +102,14 @@ pub async fn handle_gitlab_webhook( body: web::Bytes, ) -> impl Responder { if let Some(event_name) = req.headers().get("x-gitlab-event") { + let full_message = req.query_string().contains("full_message=true"); + log::info!("Event: {:?}", event_name); let message = match event_name.to_str() { Ok("Push Hook") => handle_push_event(&body), Ok("Tag Push Hook") => handle_tag_push_event(&body), Ok("Issue Hook") => handle_issue_event(&body), - Ok("Note Hook") => handle_note_event(&body), + Ok("Note Hook") => handle_note_event(&body, full_message), // Ok("Pipeline Hook") => handle_pipeline_event(&body), Ok("Merge Request Hook") => handle_merge_request_event(&body), Ok("Job Hook") => handle_job_event(&body), diff --git a/src/webhooks/gitlab/webhook_handlers/note.rs b/src/webhooks/gitlab/webhook_handlers/note.rs index 0abddcc..924d7b2 100644 --- a/src/webhooks/gitlab/webhook_handlers/note.rs +++ b/src/webhooks/gitlab/webhook_handlers/note.rs @@ -30,14 +30,29 @@ struct NoteDetails { note: String, } -pub fn handle_note_event(body: &web::Bytes) -> String { +trait ProcessNote { + fn process(&self, full_message: bool) -> String; +} + +impl ProcessNote for NoteEvent { + fn process(&self, full_message: bool) -> String { + let note = encode_text(&self.object_attributes.note); + if full_message { + note.into_owned() + } else { + note.chars().take(100).collect() + } + } +} + +pub fn handle_note_event(body: &web::Bytes, full_message: bool) -> String { let note_event: NoteEvent = serde_json::from_slice(body).unwrap(); let user_name = ¬e_event.user.name; let note_details = ¬e_event.object_attributes; let url = ¬e_details.url; let noteable_type = ¬e_details.noteable_type; - let note = encode_text(¬e_details.note); + let note = note_event.process(full_message); let noteable_type = match noteable_type.as_str() { "Issue" => NoteableType::Issue,