Compare commits

...

4 commits

Author SHA1 Message Date
b8ae4a3b6a
forgejo-hooks: Add support for branch pushes
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2024-04-23 17:21:27 +02:00
b9b0303e88
Sub-crate: forgejo-hooks
Supports commit push for the moment

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2024-04-23 17:19:47 +02:00
991474e4ad
Implement gitea hook support instead
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2024-04-21 17:41:47 +02:00
fd85de951c
WIP: Add support for forgejo webhooks
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2024-04-20 19:06:19 +02:00
7 changed files with 261 additions and 31 deletions

View file

@ -9,6 +9,7 @@ license = "AGPL-3.0+"
[dependencies]
clap = { version = "4.3", features = [ "cargo" ] }
gitlab = "0.1610"
gitea-rs = "1.19"
hyper = { version = "0.14", features = [ "full" ] }
jid = { version = "*", features = [ "serde" ] }
log = "0.4"

7
forgejo-hooks/Cargo.toml Normal file
View file

@ -0,0 +1,7 @@
[package]
name = "forgejo-hooks"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0", features = ["derive"] }

172
forgejo-hooks/src/lib.rs Normal file
View file

@ -0,0 +1,172 @@
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub struct CommitAuthor {
pub name: String,
pub email: String,
pub username: String,
}
#[derive(Deserialize, Debug)]
pub struct User {
pub id: u32,
pub login: String,
pub full_name: String,
pub email: String,
pub avatar_url: String,
pub username: String,
}
#[derive(Deserialize, Debug)]
pub struct Commit {
pub id: String,
pub message: String,
pub url: String,
pub author: CommitAuthor,
pub committer: CommitAuthor,
pub timestamp: String,
}
#[derive(Deserialize, Debug)]
pub struct Perms {
admin: bool,
push: bool,
pull: bool,
}
#[derive(Deserialize, Debug)]
pub struct InternalTracker {
enable_time_tracker: bool,
allow_only_contributors_to_track_time: bool,
enable_issue_dependencies: bool
}
#[derive(Deserialize, Debug)]
pub struct Repository {
pub id: u32,
pub owner: User,
pub name: String,
pub full_name: String,
pub description: String,
pub private: bool,
pub fork: bool,
pub html_url: String,
pub ssh_url: String,
pub clone_url: String,
pub website: String,
pub stars_count: u32,
pub forks_count: u32,
pub watchers_count: u32,
pub open_issues_count: u32,
pub default_branch: String,
pub created_at: String,
pub updated_at: String,
}
#[derive(Deserialize, Debug)]
pub struct RepositoryFull {
pub id: u32,
pub owner: RepositoryOwner,
pub name: String,
pub full_name: String,
pub description: String,
pub empty: bool,
pub private: bool,
pub fork: bool,
pub template: bool,
pub parent: Option<Box<RepositoryFull>>,
pub mirror: bool,
pub size: u32,
pub language: String,
pub languages_url: String,
pub html_url: String,
pub url: String,
pub link: String,
pub ssh_url: String,
pub clone_url: String,
pub website: String,
pub stars_count: u32,
pub forks_count: u32,
pub watchers_count: u32,
pub open_issues_count: u32,
pub open_pr_counter: u32,
pub default_branch: String,
pub created_at: String,
pub updated_at: String,
pub archived_at: String,
pub permissions: Perms,
pub has_issues: bool,
pub internal_tracker: InternalTracker,
pub has_wiki: bool,
pub has_pull_requests: bool,
pub has_projects: bool,
pub has_releases: bool,
pub has_actions: bool,
pub ignore_whitespace_conflicts: bool,
pub allow_merge_commits: bool,
pub allow_rebase: bool,
pub allow_rebase_explicit: bool,
pub allow_squash_merge: bool,
pub allow_rebase_update: bool,
pub default_delete_branch_after_merge: bool,
pub default_merge_style: String,
pub default_allow_maintainer_edit: bool,
pub avatar_url: String,
pub internal: bool,
pub mirror_internal: String,
pub mirror_updated: String,
pub repo_transfer: Option<String>,
}
#[derive(Deserialize, Debug)]
pub struct RepositoryOwner {
pub id: u32,
pub login: String,
pub login_name: String,
pub full_name: String,
pub email: String,
pub avatar_url: String,
pub language: String,
pub is_admin: bool,
pub last_login: String,
pub created: String,
pub restricted: bool,
pub active: bool,
pub prohibit_login: bool,
pub location: String,
pub website: String,
pub description: String,
pub visibility: String,
pub followers_count: u32,
pub following_count: u32,
pub starred_repos_count: u32,
pub username: String,
}
#[derive(Deserialize, Debug)]
pub struct Push {
#[serde(rename(deserialize = "ref"))]
pub ref_: String,
pub before: String,
pub compare_url: String,
pub commits: Vec<Commit>,
pub repository: Repository,
pub pusher: User,
pub sender: User,
}
#[derive(Deserialize, Debug)]
pub struct Branch {
pub sha: String,
pub ref_type: String,
pub repository: Repository,
pub sender: RepositoryOwner,
}
#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum Hook {
Push(Push),
Branch(Branch),
}

View file

@ -13,7 +13,7 @@
// 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::webhook::{format_webhook, WebHook};
use crate::webhook::{format_webhook, GitlabHook};
use log::debug;
use xmpp::parsers::message::MessageType;
@ -74,7 +74,7 @@ impl XmppClient {
}
}
pub async fn webhook(&mut self, wh: WebHook) {
pub async fn webhook(&mut self, wh: GitlabHook) {
debug!("Received Webhook");
if let Some(display) = format_webhook(&wh) {
debug!("Webhook: {}", display);

View file

@ -24,7 +24,7 @@ mod webhook;
use crate::bot::XmppClient;
use crate::error::Error;
use crate::web::webhooks;
use crate::webhook::WebHook;
use crate::webhook::GitlabHook;
use std::convert::Infallible;
use std::fs::File;
@ -133,7 +133,7 @@ async fn main() -> Result<!, Error> {
}
};
let (value_tx, mut value_rx) = mpsc::unbounded_channel::<WebHook>();
let (value_tx, mut value_rx) = mpsc::unbounded_channel::<GitlabHook>();
if let Some(token) = config.webhook_token {
let value_tx = Arc::new(Mutex::new(value_tx));

View file

@ -14,7 +14,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::error::Error;
use crate::webhook::WebHook;
use crate::webhook::{Hook, GiteaHook, GitlabHook};
use std::convert::Infallible;
use std::str::from_utf8;
@ -35,42 +35,73 @@ fn error_res<E: std::fmt::Debug>(e: E) -> Result<Response<Body>, Infallible> {
Ok(res)
}
async fn webhooks_inner(req: Request<Body>, token: &str) -> Result<WebHook, Error> {
async fn webhooks_inner(req: Request<Body>, token: &str) -> Result<Hook, Error> {
match req.method() {
&Method::POST => (),
_ => return Err(Error::MethodMismatch),
}
debug!("Headers: {:?}", req.headers());
let headers = req.headers();
if let Some(content_type) = headers.get(header::CONTENT_TYPE)
&& let Some(header_token) = headers.get("X-Gitlab-Token")
{
if content_type != "application/json" {
return Err(Error::InvalidContentType);
}
if header_token != token {
return Err(Error::InvalidToken);
}
}
/*
let tmp = body::to_bytes(req.into_body()).await?;
let text: &str = from_utf8(&tmp)?;
Ok(serde_json::from_str(text)?)
debug!("FOO0 text: {:?}", text);
let json = serde_json::from_str(text)?;
debug!("FOO0 json: {:?}", json);
return Err(Error::InvalidToken); // XXX: Test.
*/
if let Some(content_type) = headers.get(header::CONTENT_TYPE)
&& content_type != "application/json"
{
return Err(Error::InvalidContentType);
}
if let Some(header_token) = headers.get("X-Gilab-Token") {
debug!("FOO1: X-Gitlab-Token");
if header_token != token {
Err(Error::InvalidToken)
} else {
let tmp = body::to_bytes(req.into_body()).await?;
let text: &str = from_utf8(&tmp)?;
let hook: GitlabHook = serde_json::from_str(text)?;
debug!("FOO1: {:?}", hook);
Ok(Hook::Gitlab(hook))
}
} else if let Some(header_token) = headers.get("Authorization") {
debug!("FOO3: Authorization");
if header_token != token {
Err(Error::InvalidToken)
} else {
let tmp = body::to_bytes(req.into_body()).await?;
let text: &str = from_utf8(&tmp)?;
debug!("FOO3 text: {:?}", text);
let hook: GiteaHook = serde_json::from_str(text)?;
debug!("FOO3 json: {:?}", hook);
Ok(Hook::Gitea(hook))
}
} else {
debug!("FOO3: else");
Err(Error::InvalidToken)
}
}
pub async fn webhooks(
req: Request<Body>,
token: String,
value_tx: Arc<Mutex<UnboundedSender<WebHook>>>,
value_tx: Arc<Mutex<UnboundedSender<GitlabHook>>>,
) -> Result<Response<Body>, Infallible> {
match webhooks_inner(req, token.as_ref()).await {
Ok(wh) => {
debug!("Passed: {:?}", wh);
value_tx.lock().unwrap().send(wh).unwrap();
match wh {
Hook::Gitlab(glh) => value_tx.lock().unwrap().send(glh).unwrap(),
_ => (),
}
Ok(Response::new("Hello world".into()))
}

View file

@ -13,12 +13,31 @@
// 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/>.
pub use gitlab::webhooks::{IssueAction, MergeRequestAction, WebHook, WikiPageAction};
pub use gitlab::webhooks::{IssueAction, MergeRequestAction, WebHook as GitlabHook, WikiPageAction};
pub use gitea_rs::models::hook::Hook as GiteaHook;
use log::debug;
pub fn format_webhook(wh: &WebHook) -> Option<String> {
Some(match wh {
WebHook::Push(push) => {
#[derive(Debug)]
pub enum Hook {
Gitea(GiteaHook),
Gitlab(GitlabHook),
}
impl From<GiteaHook> for Hook {
fn from(hook: GiteaHook) -> Hook {
Hook::Gitea(hook)
}
}
impl From<GitlabHook> for Hook {
fn from(hook: GitlabHook) -> Hook {
Hook::Gitlab(hook)
}
}
pub fn format_webhook(glh: &GitlabHook) -> Option<String> {
Some(match glh {
GitlabHook::Push(push) => {
if push.ref_ != "refs/heads/main" {
// Ignore: Action not on 'main' branch
return None;
@ -45,7 +64,7 @@ pub fn format_webhook(wh: &WebHook) -> Option<String> {
}
text
}
WebHook::Issue(issue) => {
GitlabHook::Issue(issue) => {
let action = match issue.object_attributes.action {
Some(IssueAction::Update) => return None,
Some(IssueAction::Open) => "opened",
@ -68,7 +87,7 @@ pub fn format_webhook(wh: &WebHook) -> Option<String> {
.unwrap_or("".to_owned())
)
}
WebHook::MergeRequest(merge_req) => {
GitlabHook::MergeRequest(merge_req) => {
let action = match merge_req.object_attributes.action {
Some(MergeRequestAction::Update) => return None,
Some(MergeRequestAction::Open) => "opened",
@ -93,7 +112,7 @@ pub fn format_webhook(wh: &WebHook) -> Option<String> {
.unwrap_or("".to_owned())
)
}
WebHook::Note(note) => {
GitlabHook::Note(note) => {
if let Some(_) = note.snippet {
return None;
}
@ -120,11 +139,11 @@ pub fn format_webhook(wh: &WebHook) -> Option<String> {
unreachable!()
}
}
WebHook::Build(build) => {
GitlabHook::Build(build) => {
println!("Build: {:?}", build);
return None;
}
WebHook::WikiPage(page) => {
GitlabHook::WikiPage(page) => {
let action = match page.object_attributes.action {
WikiPageAction::Update => "updated",
WikiPageAction::Create => "created",
@ -138,7 +157,7 @@ pub fn format_webhook(wh: &WebHook) -> Option<String> {
page.object_attributes.url,
)
}
_wh => {
_glh => {
debug!("Webhook not supported");
return None;
}