Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/issue desc #59

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/webhooks/github/webhook_handlers/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct PushEvent {
forced: bool,
commits: Vec<Commit>,
#[serde(rename = "ref")]
pub ref_field: String,
ref_field: String,
before: String,
after: String,
}
Expand Down
4 changes: 3 additions & 1 deletion src/webhooks/gitlab/http_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
46 changes: 36 additions & 10 deletions src/webhooks/gitlab/webhook_handlers/issue.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -13,6 +23,7 @@ struct IssueDetails {
title: String,
url: String,
action: String,
description: String,
}

#[derive(Debug, Deserialize)]
Expand All @@ -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!("<b>{user_name}</b> opened a new issue <a href=\"{url}\">{title}</a>\n",)
} else if action == "update" {
format!("<b>{user_name}</b> updated issue <a href=\"{url}\">{title}</a>\n",)
} else if action == "close" {
format!("<b>{user_name}</b> closed issue <a href=\"{url}\">{title}</a>\n",)
} else if action == "reopen" {
format!("<b>{user_name}</b> reopened issue <a href=\"{url}\">{title}</a>\n",)
} else {
format!("<b>{user_name}</b> {action} issue <a href=\"{url}\">{title}</a>\n",)
match action {
IssueAction::Open => format!(
"<b>{user_name}</b> opened a new issue <a href=\"{url}\">{title}</a>\n{description}\n",
),
IssueAction::Update => format!(
"<b>{user_name}</b> updated issue <a href=\"{url}\">{title}</a>\n{description}\n",
),
IssueAction::Close => {
format!("<b>{user_name}</b> closed issue <a href=\"{url}\">{title}</a>\n",)
}
IssueAction::Reopen => {
format!("<b>{user_name}</b> reopened issue <a href=\"{url}\">{title}</a>\n",)
}
IssueAction::Other(action) => {
format!("<b>{user_name}</b> {action} issue <a href=\"{url}\">{title}</a>\n",)
}
}
}
65 changes: 54 additions & 11 deletions src/webhooks/gitlab/webhook_handlers/note.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -17,25 +27,58 @@ struct User {
struct NoteDetails {
url: String,
noteable_type: String,
note: String,
}

trait ProcessNote {
fn process(&self, full_message: bool) -> String;
}

pub fn handle_note_event(body: &web::Bytes) -> 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 = &note_event.user.name;
let note_details = &note_event.object_attributes;
let url = &note_details.url;
let noteable_type = &note_details.noteable_type;
let note = note_event.process(full_message);

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!("<b>{user_name}</b> commented on an <a href=\"{url}\">issue</a>\n")
} else if noteable_type == "MergeRequest" {
format!("<b>{user_name}</b> commented on a <a href=\"{url}\">merge request </a>\n")
} else if noteable_type == "Commit" {
format!("<b>{user_name}</b> commented on a <a href=\"{url}\">commit</a>\n")
} else if noteable_type == "Snippet" {
format!("<b>{user_name}</b> commented on a <a href=\"{url}\">snippet</a>\n")
} else {
format!("<b>{user_name}</b> commented on a <a href=\"{url}\">{noteable_type}</a>\n")
match noteable_type {
NoteableType::Issue => {
format!("<b>{user_name}</b> commented on an <a href=\"{url}\">issue</a>\n{note}\n")
}
NoteableType::MergeRequest => {
format!(
"<b>{user_name}</b> commented on a <a href=\"{url}\">merge request</a>\n{note}\n"
)
}
NoteableType::Commit => {
format!("<b>{user_name}</b> commented on a <a href=\"{url}\">commit</a>\n{note}\n")
}
NoteableType::Snippet => {
format!("<b>{user_name}</b> commented on a <a href=\"{url}\">snippet</a>\n{note}\n")
}
NoteableType::Other(type_name) => {
format!("<b>{user_name}</b> commented on a <a href=\"{url}\">{type_name}</a>\n{note}\n")
}
}
}
4 changes: 1 addition & 3 deletions src/webhooks/gitlab/webhook_handlers/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct PushEvent {
before: String,
after: String,
#[serde(rename = "ref")]
pub ref_field: String,
ref_field: String,
project: Project,
commits: Vec<Commit>,
user_name: String,
Expand Down Expand Up @@ -107,8 +107,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() {
Expand Down
2 changes: 1 addition & 1 deletion src/webhooks/gitlab/webhook_handlers/tag_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
Loading