hook: Reimplement Hook::Issue for Gitlab

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2024-08-31 15:56:47 +02:00 committed by pep
commit 6e6ed92026

View file

@ -17,7 +17,7 @@ use crate::Error;
pub use forgejo_hooks::Hook as ForgejoHook; pub use forgejo_hooks::Hook as ForgejoHook;
pub use gitlab::webhooks::{ pub use gitlab::webhooks::{
IssueAction, MergeRequestAction, WebHook as GitlabHook, WikiPageAction, IssueAction as GlIssueAction, MergeRequestAction, WebHook as GitlabHook, WikiPageAction,
}; };
use log::debug; use log::debug;
@ -56,12 +56,31 @@ pub(crate) struct Push {
pusher: User, pusher: User,
} }
#[derive(Debug, Clone)]
pub(crate) enum IssueAction {
Update,
Open,
Close,
Reopen,
}
#[derive(Debug, Clone)]
pub(crate) struct Issue {
action: Option<IssueAction>,
repository: Repository,
author: User,
id: u64,
title: String,
url: Option<String>,
}
/// Lowest common denominator struct so that we don't have to duplicate our code for each platform /// Lowest common denominator struct so that we don't have to duplicate our code for each platform
/// we support. /// we support.
#[derive(Debug)] #[derive(Debug)]
pub(crate) enum Hook { pub(crate) enum Hook {
/// Push event /// Push event
Push(Push), Push(Push),
Issue(Issue),
} }
impl TryFrom<GitlabHook> for Hook { impl TryFrom<GitlabHook> for Hook {
@ -87,6 +106,23 @@ impl TryFrom<GitlabHook> for Hook {
name: push.user_name, 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), _ => return Err(Error::UnsupportedHookConversion),
}) })
} }
@ -155,9 +191,9 @@ pub(crate) fn format_hook(hook: &Hook) -> Option<String> {
} }
} }
text text
} /* }
Hook::Issue(issue) => { Hook::Issue(issue) => {
let action = match issue.object_attributes.action { let action = match issue.action {
Some(IssueAction::Update) => return None, Some(IssueAction::Update) => return None,
Some(IssueAction::Open) => "opened", Some(IssueAction::Open) => "opened",
Some(IssueAction::Close) => "closed", Some(IssueAction::Close) => "closed",
@ -166,19 +202,18 @@ pub(crate) fn format_hook(hook: &Hook) -> Option<String> {
}; };
format!( format!(
"[{}] {} {} issue {}: {}{}", "[{}] {} {} issue {}: {}{}",
issue.project.name, issue.repository.name,
issue.user.name, issue.author.name,
action, action,
issue.object_attributes.iid, issue.id,
issue.object_attributes.title, issue.title,
issue issue
.object_attributes
.url .url
.as_ref() .as_ref()
.map(|url| format!(" <{}>", url)) .map(|url| format!(" <{}>", url))
.unwrap_or("".to_owned()) .unwrap_or("".to_owned())
) )
} } /*
Hook::MergeRequest(merge_req) => { Hook::MergeRequest(merge_req) => {
let action = match merge_req.object_attributes.action { let action = match merge_req.object_attributes.action {
Some(MergeRequestAction::Update) => return None, Some(MergeRequestAction::Update) => return None,