From 6e6ed92026d903c879f1279b1fec7e3a97b25d29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 31 Aug 2024 15:56:47 +0200 Subject: [PATCH] hook: Reimplement Hook::Issue for Gitlab MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/hook.rs | 83 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 24 deletions(-) diff --git a/src/hook.rs b/src/hook.rs index 32fc1df..c149310 100644 --- a/src/hook.rs +++ b/src/hook.rs @@ -17,7 +17,7 @@ use crate::Error; pub use forgejo_hooks::Hook as ForgejoHook; pub use gitlab::webhooks::{ - IssueAction, MergeRequestAction, WebHook as GitlabHook, WikiPageAction, + IssueAction as GlIssueAction, MergeRequestAction, WebHook as GitlabHook, WikiPageAction, }; use log::debug; @@ -56,12 +56,31 @@ pub(crate) struct Push { pusher: User, } +#[derive(Debug, Clone)] +pub(crate) enum IssueAction { + Update, + Open, + Close, + Reopen, +} + +#[derive(Debug, Clone)] +pub(crate) struct Issue { + action: Option, + repository: Repository, + author: User, + id: u64, + title: String, + url: Option, +} + /// Lowest common denominator struct so that we don't have to duplicate our code for each platform /// we support. #[derive(Debug)] pub(crate) enum Hook { /// Push event Push(Push), + Issue(Issue), } impl TryFrom for Hook { @@ -87,6 +106,23 @@ impl TryFrom for Hook { name: push.user_name, }, }), + GitlabHook::Issue(issue) => Hook::Issue(Issue { + action: issue.object_attributes.action.map(|action| match action { + GlIssueAction::Update => IssueAction::Update, + GlIssueAction::Open => IssueAction::Open, + GlIssueAction::Close => IssueAction::Close, + GlIssueAction::Reopen => IssueAction::Reopen, + }), + repository: Repository { + name: issue.project.name, + }, + author: User { + name: issue.user.name, + }, + id: issue.object_attributes.iid, + title: issue.object_attributes.title, + url: issue.object_attributes.url, + }), _ => return Err(Error::UnsupportedHookConversion), }) } @@ -155,30 +191,29 @@ pub(crate) fn format_hook(hook: &Hook) -> Option { } } text + } + Hook::Issue(issue) => { + let action = match issue.action { + Some(IssueAction::Update) => return None, + Some(IssueAction::Open) => "opened", + Some(IssueAction::Close) => "closed", + Some(IssueAction::Reopen) => "reopened", + None => return None, + }; + format!( + "[{}] {} {} issue {}: {}{}", + issue.repository.name, + issue.author.name, + action, + issue.id, + issue.title, + issue + .url + .as_ref() + .map(|url| format!(" <{}>", url)) + .unwrap_or("".to_owned()) + ) } /* - Hook::Issue(issue) => { - let action = match issue.object_attributes.action { - Some(IssueAction::Update) => return None, - Some(IssueAction::Open) => "opened", - Some(IssueAction::Close) => "closed", - Some(IssueAction::Reopen) => "reopened", - None => return None, - }; - format!( - "[{}] {} {} issue {}: {}{}", - issue.project.name, - issue.user.name, - action, - issue.object_attributes.iid, - issue.object_attributes.title, - issue - .object_attributes - .url - .as_ref() - .map(|url| format!(" <{}>", url)) - .unwrap_or("".to_owned()) - ) - } Hook::MergeRequest(merge_req) => { let action = match merge_req.object_attributes.action { Some(MergeRequestAction::Update) => return None,