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
parent bcc8728cc8
commit 6e6ed92026

View file

@ -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<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
/// we support.
#[derive(Debug)]
pub(crate) enum Hook {
/// Push event
Push(Push),
Issue(Issue),
}
impl TryFrom<GitlabHook> for Hook {
@ -87,6 +106,23 @@ impl TryFrom<GitlabHook> 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<String> {
}
}
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,