Split hook modules

Some structs changed to public on the way to facilitate writes.

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2024-08-31 22:41:26 +02:00 committed by pep
parent 2f101c617c
commit 4bbb3f8994
5 changed files with 347 additions and 289 deletions

View file

@ -23,7 +23,7 @@ use hmac::digest::InvalidLength as HmacInvalidLength;
use hyper::StatusCode;
#[derive(Debug)]
pub(crate) enum Error {
pub enum Error {
MethodMismatch(hyper::Method),
InvalidSecret,
InvalidContentType,

48
src/hooks/forgejo.rs Normal file
View file

@ -0,0 +1,48 @@
// Copyright (C) 2024-2099 The crate authors.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
// for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::hooks::types::{Commit, Hook, Push, Repository, User};
use crate::Error;
pub use ::forgejo_hooks::Hook as ForgejoHook;
impl TryFrom<ForgejoHook> for Hook {
type Error = Error;
fn try_from(hook: ForgejoHook) -> Result<Hook, Error> {
Ok(match hook {
ForgejoHook::Push(push) => Hook::Push(Push {
ref_: push.ref_,
object_kind: String::from("push"),
commits: push
.commits
.into_iter()
.map(|commit| Commit {
ref_: commit.id,
message: commit.message,
url: commit.url,
})
.collect(),
repository: Repository {
name: push.repository.name,
},
pusher: User {
name: push.pusher.login,
},
}),
_ => return Err(Error::UnsupportedHookConversion),
})
}
}

164
src/hooks/gitlab.rs Normal file
View file

@ -0,0 +1,164 @@
// Copyright (C) 2024-2099 The crate authors.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
// for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::hooks::types::{
Commit, Hook, Issue, IssueAction, IssueAttrs, MergeRequest, MergeRequestAction,
MergeRequestAttrs, Note, Push, Repository, User,
};
use crate::Error;
pub use ::gitlab::webhooks::{
CommitHookAttrs, IssueAction as GlIssueAction, IssueHook as GlIssueHook,
IssueHookAttrs as GlIssueHookAttrs, MergeRequestAction as GlMergeRequestAction,
MergeRequestHook as GlMergeRequestHook, MergeRequestHookAttrs as GlMergeRequestHookAttrs,
NoteHook as GlNoteHook, PushHook as GlPushHook, WebHook as GitlabHook,
};
impl From<CommitHookAttrs> for Commit {
fn from(other: CommitHookAttrs) -> Commit {
Commit {
ref_: other.id,
message: other.message,
url: other.url,
}
}
}
impl From<GlPushHook> for Push {
fn from(other: GlPushHook) -> Push {
Push {
ref_: other.ref_,
object_kind: other.object_kind,
commits: other.commits.into_iter().map(Into::into).collect(),
repository: Repository {
name: other.project.name,
},
pusher: User {
name: other.user_name,
},
}
}
}
impl From<GlIssueAction> for IssueAction {
fn from(other: GlIssueAction) -> IssueAction {
match other {
GlIssueAction::Update => IssueAction::Update,
GlIssueAction::Open => IssueAction::Open,
GlIssueAction::Close => IssueAction::Close,
GlIssueAction::Reopen => IssueAction::Reopen,
}
}
}
impl From<GlIssueHook> for Issue {
fn from(other: GlIssueHook) -> Issue {
Issue {
action: other.object_attributes.action.map(Into::into),
repository: Repository {
name: other.project.name,
},
author: User {
name: other.user.name,
},
id: other.object_attributes.iid,
title: other.object_attributes.title,
url: other.object_attributes.url,
}
}
}
impl From<GlIssueHookAttrs> for IssueAttrs {
fn from(other: GlIssueHookAttrs) -> IssueAttrs {
IssueAttrs {
id: other.iid,
title: other.title,
}
}
}
impl From<GlMergeRequestAction> for MergeRequestAction {
fn from(other: GlMergeRequestAction) -> MergeRequestAction {
match other {
GlMergeRequestAction::Update => MergeRequestAction::Update,
GlMergeRequestAction::Open => MergeRequestAction::Open,
GlMergeRequestAction::Close => MergeRequestAction::Close,
GlMergeRequestAction::Reopen => MergeRequestAction::Reopen,
GlMergeRequestAction::Merge => MergeRequestAction::Merge,
GlMergeRequestAction::Approved => MergeRequestAction::Approved,
GlMergeRequestAction::Unapproved => MergeRequestAction::Unapproved,
GlMergeRequestAction::Approval => MergeRequestAction::Approval,
GlMergeRequestAction::Unapproval => MergeRequestAction::Unapproval,
}
}
}
impl From<GlMergeRequestHook> for MergeRequest {
fn from(other: GlMergeRequestHook) -> MergeRequest {
MergeRequest {
action: other.object_attributes.action.map(Into::into),
repository: Repository {
name: other.project.name,
},
author: User {
name: other.user.name,
},
id: other.object_attributes.iid,
title: other.object_attributes.title,
url: other.object_attributes.url,
}
}
}
impl From<GlMergeRequestHookAttrs> for MergeRequestAttrs {
fn from(other: GlMergeRequestHookAttrs) -> MergeRequestAttrs {
MergeRequestAttrs {
id: other.id,
title: other.title,
}
}
}
impl From<GlNoteHook> for Note {
fn from(other: GlNoteHook) -> Note {
Note {
snippet: other.snippet.is_some(),
commit: other.commit.map(Into::into),
issue: other.issue.map(Into::into),
merge_request: other.merge_request.map(Into::into),
repository: Repository {
name: other.project.name,
},
author: User {
name: other.user.name,
},
url: other.object_attributes.url,
}
}
}
impl TryFrom<GitlabHook> for Hook {
type Error = Error;
fn try_from(hook: GitlabHook) -> Result<Hook, Error> {
Ok(match hook {
GitlabHook::Push(push) => Hook::Push((*push).into()),
GitlabHook::Issue(issue) => Hook::Issue((*issue).into()),
GitlabHook::MergeRequest(mr) => Hook::MergeRequest((*mr).into()),
GitlabHook::Note(note) => Hook::Note((*note).into()),
_ => return Err(Error::UnsupportedHookConversion),
})
}
}

View file

@ -13,296 +13,16 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::Error;
mod forgejo;
mod gitlab;
mod types;
pub use forgejo_hooks::Hook as ForgejoHook;
pub use gitlab::webhooks::{
CommitHookAttrs, IssueAction as GlIssueAction, IssueHook as GlIssueHook,
IssueHookAttrs as GlIssueHookAttrs, MergeRequestAction as GlMergeRequestAction,
MergeRequestHook as GlMergeRequestHook, MergeRequestHookAttrs as GlMergeRequestHookAttrs,
NoteHook as GlNoteHook, PushHook as GlPushHook, WebHook as GitlabHook, WikiPageAction,
};
use log::debug;
pub use crate::hooks::forgejo::ForgejoHook;
pub use crate::hooks::gitlab::GitlabHook;
pub use crate::hooks::types::Hook;
use crate::hooks::types::{IssueAction, MergeRequestAction};
/// Defines a generic user that can be used for many purposes.
#[derive(Debug, Clone)]
pub(crate) struct User {
/// Name of the user
name: String,
}
#[derive(Debug, Clone)]
pub(crate) struct Commit {
/// Reference of the commit
ref_: String,
/// Commit message
message: String,
/// URL where the commit can be read at
url: String,
}
#[derive(Debug, Clone)]
pub(crate) struct Repository {
/// Name of the project.
name: String,
}
#[derive(Debug, Clone)]
pub(crate) struct Push {
/// Reference where commits have been pushed to.
ref_: String,
/// The event which occured.
object_kind: String,
/// Commit list.
commits: Vec<Commit>,
/// Project repository.
repository: Repository,
/// Person who pushed the commits. It isn't necessarily the same as commit authors.
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>,
}
#[derive(Debug, Clone)]
pub(crate) struct IssueAttrs {
id: u64,
title: String,
}
#[derive(Debug, Clone)]
pub(crate) enum MergeRequestAction {
Update,
Open,
Close,
Reopen,
Merge,
Approved,
Unapproved,
Approval,
Unapproval,
}
#[derive(Debug, Clone)]
pub(crate) struct MergeRequest {
action: Option<MergeRequestAction>,
repository: Repository,
author: User,
id: u64,
title: String,
url: Option<String>,
}
#[derive(Debug, Clone)]
pub(crate) struct MergeRequestAttrs {
id: u64,
title: String,
}
#[derive(Debug, Clone)]
pub(crate) struct Note {
snippet: bool,
commit: Option<Commit>,
issue: Option<IssueAttrs>,
merge_request: Option<MergeRequestAttrs>,
repository: Repository,
author: User,
url: 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),
MergeRequest(MergeRequest),
Note(Note),
}
impl From<CommitHookAttrs> for Commit {
fn from(other: CommitHookAttrs) -> Commit {
Commit {
ref_: other.id,
message: other.message,
url: other.url,
}
}
}
impl From<GlPushHook> for Push {
fn from(other: GlPushHook) -> Push {
Push {
ref_: other.ref_,
object_kind: other.object_kind,
commits: other.commits.into_iter().map(Into::into).collect(),
repository: Repository {
name: other.project.name,
},
pusher: User {
name: other.user_name,
},
}
}
}
impl From<GlIssueAction> for IssueAction {
fn from(other: GlIssueAction) -> IssueAction {
match other {
GlIssueAction::Update => IssueAction::Update,
GlIssueAction::Open => IssueAction::Open,
GlIssueAction::Close => IssueAction::Close,
GlIssueAction::Reopen => IssueAction::Reopen,
}
}
}
impl From<GlIssueHook> for Issue {
fn from(other: GlIssueHook) -> Issue {
Issue {
action: other.object_attributes.action.map(Into::into),
repository: Repository {
name: other.project.name,
},
author: User {
name: other.user.name,
},
id: other.object_attributes.iid,
title: other.object_attributes.title,
url: other.object_attributes.url,
}
}
}
impl From<GlIssueHookAttrs> for IssueAttrs {
fn from(other: GlIssueHookAttrs) -> IssueAttrs {
IssueAttrs {
id: other.iid,
title: other.title,
}
}
}
impl From<GlMergeRequestAction> for MergeRequestAction {
fn from(other: GlMergeRequestAction) -> MergeRequestAction {
match other {
GlMergeRequestAction::Update => MergeRequestAction::Update,
GlMergeRequestAction::Open => MergeRequestAction::Open,
GlMergeRequestAction::Close => MergeRequestAction::Close,
GlMergeRequestAction::Reopen => MergeRequestAction::Reopen,
GlMergeRequestAction::Merge => MergeRequestAction::Merge,
GlMergeRequestAction::Approved => MergeRequestAction::Approved,
GlMergeRequestAction::Unapproved => MergeRequestAction::Unapproved,
GlMergeRequestAction::Approval => MergeRequestAction::Approval,
GlMergeRequestAction::Unapproval => MergeRequestAction::Unapproval,
}
}
}
impl From<GlMergeRequestHook> for MergeRequest {
fn from(other: GlMergeRequestHook) -> MergeRequest {
MergeRequest {
action: other.object_attributes.action.map(Into::into),
repository: Repository {
name: other.project.name,
},
author: User {
name: other.user.name,
},
id: other.object_attributes.iid,
title: other.object_attributes.title,
url: other.object_attributes.url,
}
}
}
impl From<GlMergeRequestHookAttrs> for MergeRequestAttrs {
fn from(other: GlMergeRequestHookAttrs) -> MergeRequestAttrs {
MergeRequestAttrs {
id: other.id,
title: other.title,
}
}
}
impl From<GlNoteHook> for Note {
fn from(other: GlNoteHook) -> Note {
Note {
snippet: other.snippet.is_some(),
commit: other.commit.map(Into::into),
issue: other.issue.map(Into::into),
merge_request: other.merge_request.map(Into::into),
repository: Repository {
name: other.project.name,
},
author: User {
name: other.user.name,
},
url: other.object_attributes.url,
}
}
}
impl TryFrom<GitlabHook> for Hook {
type Error = Error;
fn try_from(hook: GitlabHook) -> Result<Hook, Error> {
Ok(match hook {
GitlabHook::Push(push) => Hook::Push((*push).into()),
GitlabHook::Issue(issue) => Hook::Issue((*issue).into()),
GitlabHook::MergeRequest(mr) => Hook::MergeRequest((*mr).into()),
GitlabHook::Note(note) => Hook::Note((*note).into()),
_ => return Err(Error::UnsupportedHookConversion),
})
}
}
impl TryFrom<ForgejoHook> for Hook {
type Error = Error;
fn try_from(hook: ForgejoHook) -> Result<Hook, Error> {
Ok(match hook {
ForgejoHook::Push(push) => Hook::Push(Push {
ref_: push.ref_,
object_kind: String::from("push"),
commits: push
.commits
.into_iter()
.map(|commit| Commit {
ref_: commit.id,
message: commit.message,
url: commit.url,
})
.collect(),
repository: Repository {
name: push.repository.name,
},
pusher: User {
name: push.pusher.login,
},
}),
_ => return Err(Error::UnsupportedHookConversion),
})
}
}
pub(crate) fn format_hook(hook: &Hook) -> Option<String> {
pub fn format_hook(hook: &Hook) -> Option<String> {
Some(match hook {
Hook::Push(push) if push.object_kind == "tag_push" => {
let ref_ = push.ref_.strip_prefix("refs/tags/").unwrap_or("?!");

126
src/hooks/types.rs Normal file
View file

@ -0,0 +1,126 @@
// Copyright (C) 2024-2099 The crate authors.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
// for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/// Defines a generic user that can be used for many purposes.
#[derive(Debug, Clone)]
pub struct User {
/// Name of the user
pub name: String,
}
#[derive(Debug, Clone)]
pub struct Commit {
/// Reference of the commit
pub ref_: String,
/// Commit message
pub message: String,
/// URL where the commit can be read at
pub url: String,
}
#[derive(Debug, Clone)]
pub struct Repository {
/// Name of the project.
pub name: String,
}
#[derive(Debug, Clone)]
pub struct Push {
/// Reference where commits have been pushed to.
pub ref_: String,
/// The event which occured.
pub object_kind: String,
/// Commit list.
pub commits: Vec<Commit>,
/// Project repository.
pub repository: Repository,
/// Person who pushed the commits. It isn't necessarily the same as commit authors.
pub pusher: User,
}
#[derive(Debug, Clone)]
pub enum IssueAction {
Update,
Open,
Close,
Reopen,
}
#[derive(Debug, Clone)]
pub struct Issue {
pub action: Option<IssueAction>,
pub repository: Repository,
pub author: User,
pub id: u64,
pub title: String,
pub url: Option<String>,
}
#[derive(Debug, Clone)]
pub struct IssueAttrs {
pub id: u64,
pub title: String,
}
#[derive(Debug, Clone)]
pub enum MergeRequestAction {
Update,
Open,
Close,
Reopen,
Merge,
Approved,
Unapproved,
Approval,
Unapproval,
}
#[derive(Debug, Clone)]
pub struct MergeRequest {
pub action: Option<MergeRequestAction>,
pub repository: Repository,
pub author: User,
pub id: u64,
pub title: String,
pub url: Option<String>,
}
#[derive(Debug, Clone)]
pub struct MergeRequestAttrs {
pub id: u64,
pub title: String,
}
#[derive(Debug, Clone)]
pub struct Note {
pub snippet: bool,
pub commit: Option<Commit>,
pub issue: Option<IssueAttrs>,
pub merge_request: Option<MergeRequestAttrs>,
pub repository: Repository,
pub author: User,
pub url: String,
}
/// Lowest common denominator struct so that we don't have to duplicate our code for each platform
/// we support.
#[derive(Debug)]
pub enum Hook {
/// Push event
Push(Push),
Issue(Issue),
MergeRequest(MergeRequest),
Note(Note),
}