Refactor hook module to also format Forgejo stuff
Temporarily disable some Gitlab features Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
77c193bfb6
commit
bcc8728cc8
5 changed files with 217 additions and 131 deletions
|
@ -13,7 +13,7 @@
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// 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/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::hook::{format_hook, GitlabHook};
|
use crate::hook::{format_hook, Hook};
|
||||||
|
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use xmpp::jid::{BareJid, Jid};
|
use xmpp::jid::{BareJid, Jid};
|
||||||
|
@ -75,7 +75,7 @@ impl XmppClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn hook(&mut self, wh: GitlabHook) {
|
pub async fn hook(&mut self, wh: Hook) {
|
||||||
debug!("Received Hook");
|
debug!("Received Hook");
|
||||||
if let Some(display) = format_hook(&wh) {
|
if let Some(display) = format_hook(&wh) {
|
||||||
debug!("Hook: {}", display);
|
debug!("Hook: {}", display);
|
||||||
|
|
|
@ -29,6 +29,7 @@ pub(crate) enum Error {
|
||||||
InvalidContentType,
|
InvalidContentType,
|
||||||
InvalidSignature,
|
InvalidSignature,
|
||||||
InvalidRequest,
|
InvalidRequest,
|
||||||
|
UnsupportedHookConversion,
|
||||||
Hex(FromHexError),
|
Hex(FromHexError),
|
||||||
Hmac(HmacInvalidLength),
|
Hmac(HmacInvalidLength),
|
||||||
Hyper(hyper::Error),
|
Hyper(hyper::Error),
|
||||||
|
@ -64,6 +65,7 @@ impl std::fmt::Display for Error {
|
||||||
Error::InvalidContentType => write!(fmt, "the content-type is invalid"),
|
Error::InvalidContentType => write!(fmt, "the content-type is invalid"),
|
||||||
Error::InvalidSignature => write!(fmt, "the signature is invalid"),
|
Error::InvalidSignature => write!(fmt, "the signature is invalid"),
|
||||||
Error::InvalidRequest => write!(fmt, "the request is invalid"),
|
Error::InvalidRequest => write!(fmt, "the request is invalid"),
|
||||||
|
Error::UnsupportedHookConversion => write!(fmt, "Unable to convert hook"),
|
||||||
Error::Hex(e) => write!(fmt, "hex error: {}", e),
|
Error::Hex(e) => write!(fmt, "hex error: {}", e),
|
||||||
Error::Hmac(e) => write!(fmt, "hmac error: {}", e),
|
Error::Hmac(e) => write!(fmt, "hmac error: {}", e),
|
||||||
Error::Hyper(e) => write!(fmt, "hyper error: {}", e),
|
Error::Hyper(e) => write!(fmt, "hyper error: {}", e),
|
||||||
|
|
329
src/hook.rs
329
src/hook.rs
|
@ -13,35 +13,123 @@
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// 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/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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, MergeRequestAction, WebHook as GitlabHook, WikiPageAction,
|
||||||
};
|
};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
|
||||||
#[derive(Debug)]
|
/// Defines a generic user that can be used for many purposes.
|
||||||
pub enum Hook {
|
#[derive(Debug, Clone)]
|
||||||
Forgejo(ForgejoHook),
|
pub(crate) struct User {
|
||||||
Gitlab(GitlabHook),
|
/// Name of the user
|
||||||
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<GitlabHook> for Hook {
|
#[derive(Debug, Clone)]
|
||||||
fn from(hook: GitlabHook) -> Hook {
|
pub(crate) struct Commit {
|
||||||
Hook::Gitlab(hook)
|
/// 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,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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),
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
ref_: push.ref_,
|
||||||
|
object_kind: push.object_kind,
|
||||||
|
commits: push
|
||||||
|
.commits
|
||||||
|
.into_iter()
|
||||||
|
.map(|commit| Commit {
|
||||||
|
message: commit.message,
|
||||||
|
url: commit.url,
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
repository: Repository {
|
||||||
|
name: push.project.name,
|
||||||
|
},
|
||||||
|
pusher: User {
|
||||||
|
name: push.user_name,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
_ => return Err(Error::UnsupportedHookConversion),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format_hook(glh: &GitlabHook) -> Option<String> {
|
impl TryFrom<ForgejoHook> for Hook {
|
||||||
Some(match glh {
|
type Error = Error;
|
||||||
GitlabHook::Push(push) if push.object_kind == "tag_push" => {
|
|
||||||
|
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 {
|
||||||
|
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> {
|
||||||
|
Some(match hook {
|
||||||
|
Hook::Push(push) if push.object_kind == "tag_push" => {
|
||||||
|
let ref_ = push.ref_.strip_prefix("refs/tags/").unwrap_or("?!");
|
||||||
format!(
|
format!(
|
||||||
"[{}] {} pushed tag {}.",
|
"[{}] {} pushed tag {}.",
|
||||||
push.project.name,
|
push.repository.name, push.pusher.name, ref_
|
||||||
push.user_name,
|
|
||||||
push.ref_.strip_prefix("refs/tags/").unwrap_or("?!"),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
GitlabHook::Push(push) => {
|
Hook::Push(push) => {
|
||||||
if push.ref_ != "refs/heads/main" {
|
if push.ref_ != "refs/heads/main" {
|
||||||
// Ignore: Action not on 'main' branch
|
// Ignore: Action not on 'main' branch
|
||||||
return None;
|
return None;
|
||||||
|
@ -53,8 +141,8 @@ pub fn format_hook(glh: &GitlabHook) -> Option<String> {
|
||||||
}
|
}
|
||||||
let mut text = format!(
|
let mut text = format!(
|
||||||
"[{}] {} pushed {} commits to main",
|
"[{}] {} pushed {} commits to main",
|
||||||
push.project.name,
|
push.repository.name,
|
||||||
push.user_name,
|
push.pusher.name,
|
||||||
push.commits.len(),
|
push.commits.len(),
|
||||||
);
|
);
|
||||||
// Display max 3 commits
|
// Display max 3 commits
|
||||||
|
@ -67,109 +155,110 @@ pub fn format_hook(glh: &GitlabHook) -> Option<String> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
text
|
text
|
||||||
}
|
} /*
|
||||||
GitlabHook::Issue(issue) => {
|
Hook::Issue(issue) => {
|
||||||
let action = match issue.object_attributes.action {
|
let action = match issue.object_attributes.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",
|
||||||
Some(IssueAction::Reopen) => "reopened",
|
Some(IssueAction::Reopen) => "reopened",
|
||||||
None => return None,
|
None => return None,
|
||||||
};
|
};
|
||||||
format!(
|
format!(
|
||||||
"[{}] {} {} issue {}: {}{}",
|
"[{}] {} {} issue {}: {}{}",
|
||||||
issue.project.name,
|
issue.project.name,
|
||||||
issue.user.name,
|
issue.user.name,
|
||||||
action,
|
action,
|
||||||
issue.object_attributes.iid,
|
issue.object_attributes.iid,
|
||||||
issue.object_attributes.title,
|
issue.object_attributes.title,
|
||||||
issue
|
issue
|
||||||
.object_attributes
|
.object_attributes
|
||||||
.url
|
.url
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|url| format!(" <{}>", url))
|
.map(|url| format!(" <{}>", url))
|
||||||
.unwrap_or("".to_owned())
|
.unwrap_or("".to_owned())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
GitlabHook::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,
|
||||||
Some(MergeRequestAction::Open) => "opened",
|
Some(MergeRequestAction::Open) => "opened",
|
||||||
Some(MergeRequestAction::Close) => "closed",
|
Some(MergeRequestAction::Close) => "closed",
|
||||||
Some(MergeRequestAction::Reopen) => "reopened",
|
Some(MergeRequestAction::Reopen) => "reopened",
|
||||||
Some(MergeRequestAction::Merge) => "merged",
|
Some(MergeRequestAction::Merge) => "merged",
|
||||||
None => return None,
|
None => return None,
|
||||||
_ => {
|
_ => {
|
||||||
log::warn!(
|
log::warn!(
|
||||||
"Unsupported merge request action: {:?}",
|
"Unsupported merge request action: {:?}",
|
||||||
merge_req.object_attributes.action
|
merge_req.object_attributes.action
|
||||||
);
|
);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
format!(
|
format!(
|
||||||
"[{}] {} {} merge request {}: {}{}",
|
"[{}] {} {} merge request {}: {}{}",
|
||||||
merge_req.project.name,
|
merge_req.project.name,
|
||||||
merge_req.user.name,
|
merge_req.user.name,
|
||||||
action,
|
action,
|
||||||
merge_req.object_attributes.iid,
|
merge_req.object_attributes.iid,
|
||||||
merge_req.object_attributes.title,
|
merge_req.object_attributes.title,
|
||||||
merge_req
|
merge_req
|
||||||
.object_attributes
|
.object_attributes
|
||||||
.url
|
.url
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|url| format!(" <{}>", url))
|
.map(|url| format!(" <{}>", url))
|
||||||
.unwrap_or("".to_owned())
|
.unwrap_or("".to_owned())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
GitlabHook::Note(note) => {
|
Hook::Note(note) => {
|
||||||
if let Some(_) = note.snippet {
|
if let Some(_) = note.snippet {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
if let Some(commit) = ¬e.commit {
|
if let Some(commit) = ¬e.commit {
|
||||||
format!(
|
format!(
|
||||||
"[{}] {} commented on commit {:?} <{}>",
|
"[{}] {} commented on commit {:?} <{}>",
|
||||||
note.project.name, note.user.name, commit.id, commit.url,
|
note.project.name, note.user.name, commit.id, commit.url,
|
||||||
)
|
)
|
||||||
} else if let Some(issue) = ¬e.issue {
|
} else if let Some(issue) = ¬e.issue {
|
||||||
format!(
|
format!(
|
||||||
"[{}] {} commented on issue {}: {} <{}>",
|
"[{}] {} commented on issue {}: {} <{}>",
|
||||||
note.project.name,
|
note.project.name,
|
||||||
note.user.name,
|
note.user.name,
|
||||||
issue.iid,
|
issue.iid,
|
||||||
issue.title,
|
issue.title,
|
||||||
note.object_attributes.url,
|
note.object_attributes.url,
|
||||||
)
|
)
|
||||||
} else if let Some(mr) = ¬e.merge_request {
|
} else if let Some(mr) = ¬e.merge_request {
|
||||||
format!(
|
format!(
|
||||||
"[{}] {} commented on merge request {}: {} <{}>",
|
"[{}] {} commented on merge request {}: {} <{}>",
|
||||||
note.project.name, note.user.name, mr.iid, mr.title, note.object_attributes.url,
|
note.project.name, note.user.name, mr.iid, mr.title, note.object_attributes.url,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
GitlabHook::Build(build) => {
|
Hook::Build(build) => {
|
||||||
println!("Build: {:?}", build);
|
println!("Build: {:?}", build);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
GitlabHook::WikiPage(page) => {
|
Hook::WikiPage(page) => {
|
||||||
let action = match page.object_attributes.action {
|
let action = match page.object_attributes.action {
|
||||||
WikiPageAction::Update => "updated",
|
WikiPageAction::Update => "updated",
|
||||||
WikiPageAction::Create => "created",
|
WikiPageAction::Create => "created",
|
||||||
};
|
};
|
||||||
format!(
|
format!(
|
||||||
"[{}] {} {} wiki page {} <{}>",
|
"[{}] {} {} wiki page {} <{}>",
|
||||||
page.project.name,
|
page.project.name,
|
||||||
page.user.name,
|
page.user.name,
|
||||||
action,
|
action,
|
||||||
page.object_attributes.title,
|
page.object_attributes.title,
|
||||||
page.object_attributes.url,
|
page.object_attributes.url,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
_glh => {
|
_ => {
|
||||||
debug!("Hook not supported");
|
debug!("Hook not supported");
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,7 +91,7 @@ async fn main() -> Result<!, Error> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wh = value_rx.recv() => {
|
wh = value_rx.recv() => {
|
||||||
if let Some(Hook::Gitlab(hook)) = wh {
|
if let Some(hook) = wh {
|
||||||
client.hook(hook).await
|
client.hook(hook).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
11
src/web.rs
11
src/web.rs
|
@ -68,7 +68,7 @@ async fn hooks_inner(req: Request<Incoming>, secret: &str) -> Result<Hook, Error
|
||||||
|
|
||||||
let hook: GitlabHook = serde_json::from_slice(&payload[..])?;
|
let hook: GitlabHook = serde_json::from_slice(&payload[..])?;
|
||||||
debug!("Found Gitlab payload");
|
debug!("Found Gitlab payload");
|
||||||
return Ok(Hook::Gitlab(hook));
|
return Ok(Hook::try_from(hook)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate hmac signature
|
// Generate hmac signature
|
||||||
|
@ -87,7 +87,7 @@ async fn hooks_inner(req: Request<Incoming>, secret: &str) -> Result<Hook, Error
|
||||||
|
|
||||||
let hook: ForgejoHook = serde_json::from_slice(&payload[..])?;
|
let hook: ForgejoHook = serde_json::from_slice(&payload[..])?;
|
||||||
debug!("Found Forgejo payload.");
|
debug!("Found Forgejo payload.");
|
||||||
return Ok(Hook::Forgejo(hook));
|
return Ok(Hook::try_from(hook)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
// No match found for the payload
|
// No match found for the payload
|
||||||
|
@ -103,12 +103,7 @@ pub async fn hooks(
|
||||||
match hooks_inner(req, secret).await {
|
match hooks_inner(req, secret).await {
|
||||||
Ok(wh) => {
|
Ok(wh) => {
|
||||||
debug!("Passed: {:?}", wh);
|
debug!("Passed: {:?}", wh);
|
||||||
|
value_tx.lock().unwrap().send(wh).unwrap();
|
||||||
match wh {
|
|
||||||
hook @ Hook::Gitlab(_) => value_tx.lock().unwrap().send(hook).unwrap(),
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Response::new(Full::new(Bytes::from("Hello, World!"))))
|
Ok(Response::new(Full::new(Bytes::from("Hello, World!"))))
|
||||||
}
|
}
|
||||||
Err(err) => error_res(err),
|
Err(err) => error_res(err),
|
||||||
|
|
Loading…
Reference in a new issue