diff --git a/forgejo-hooks/Cargo.toml b/forgejo-hooks/Cargo.toml
index 0cf8bce..8bfee68 100644
--- a/forgejo-hooks/Cargo.toml
+++ b/forgejo-hooks/Cargo.toml
@@ -7,3 +7,7 @@ license = "AGPL-3.0+"
[dependencies]
chrono = { version = "0.4", default-features = false, features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
+
+[dev-dependencies]
+serde_json = "1.0"
+serde_path_to_error = "0.1.16"
diff --git a/forgejo-hooks/src/lib.rs b/forgejo-hooks/src/lib.rs
index 227ef85..0016ff5 100644
--- a/forgejo-hooks/src/lib.rs
+++ b/forgejo-hooks/src/lib.rs
@@ -13,17 +13,20 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
+#[cfg(test)]
+mod tests;
+
use chrono::{DateTime, Utc};
use serde::Deserialize;
-#[derive(Deserialize, Debug)]
+#[derive(Deserialize, Debug, PartialEq)]
pub struct CommitAuthor {
pub name: String,
pub email: String,
pub username: String,
}
-#[derive(Deserialize, Debug)]
+#[derive(Deserialize, Debug, PartialEq)]
pub struct User {
pub id: u32,
pub login: String,
@@ -33,7 +36,7 @@ pub struct User {
pub username: String,
}
-#[derive(Deserialize, Debug)]
+#[derive(Deserialize, Debug, PartialEq)]
pub struct Commit {
pub id: String,
pub message: String,
@@ -43,21 +46,21 @@ pub struct Commit {
pub timestamp: DateTime,
}
-#[derive(Deserialize, Debug)]
-pub struct Perms {
+#[derive(Deserialize, Debug, PartialEq)]
+pub struct Permissions {
admin: bool,
push: bool,
pull: bool,
}
-#[derive(Deserialize, Debug)]
+#[derive(Deserialize, Debug, PartialEq)]
pub struct InternalTracker {
enable_time_tracker: bool,
allow_only_contributors_to_track_time: bool,
enable_issue_dependencies: bool,
}
-#[derive(Deserialize, Debug)]
+#[derive(Deserialize, Debug, PartialEq)]
pub struct Repository {
pub id: u32,
pub owner: User,
@@ -79,7 +82,7 @@ pub struct Repository {
pub updated_at: DateTime,
}
-#[derive(Deserialize, Debug)]
+#[derive(Deserialize, Debug, PartialEq)]
pub struct RepositoryFull {
pub id: u32,
pub owner: RepositoryOwner,
@@ -134,7 +137,7 @@ pub struct RepositoryFull {
pub repo_transfer: Option,
}
-#[derive(Deserialize, Debug)]
+#[derive(Deserialize, Debug, PartialEq)]
pub struct RepositoryOwner {
pub id: u32,
pub login: String,
@@ -159,7 +162,7 @@ pub struct RepositoryOwner {
pub username: String,
}
-#[derive(Deserialize, Debug)]
+#[derive(Deserialize, Debug, PartialEq)]
pub struct Push {
#[serde(rename(deserialize = "ref"))]
pub ref_: String,
@@ -171,12 +174,12 @@ pub struct Push {
pub sender: User,
}
-#[derive(Deserialize, Debug)]
+#[derive(Deserialize, Debug, PartialEq)]
pub enum RefType {
Branch,
}
-#[derive(Deserialize, Debug)]
+#[derive(Deserialize, Debug, PartialEq)]
pub struct AddedBranch {
pub sha: String,
pub ref_type: String,
@@ -184,7 +187,7 @@ pub struct AddedBranch {
pub sender: RepositoryOwner,
}
-#[derive(Deserialize, Debug)]
+#[derive(Deserialize, Debug, PartialEq)]
pub struct RemovedBranch {
pub sha: String,
pub ref_type: String,
@@ -193,7 +196,19 @@ pub struct RemovedBranch {
pub sender: RepositoryOwner,
}
-#[derive(Deserialize, Debug)]
+impl From for Hook {
+ fn from(push: Push) -> Self {
+ Self::Push(push)
+ }
+}
+
+impl From for Hook {
+ fn from(issue: Issue) -> Self {
+ Self::Issue(issue)
+ }
+}
+
+#[derive(Deserialize, Debug, PartialEq)]
#[serde(untagged)]
pub enum Hook {
Push(Push),
diff --git a/forgejo-hooks/src/tests/mod.rs b/forgejo-hooks/src/tests/mod.rs
new file mode 100644
index 0000000..00e32cc
--- /dev/null
+++ b/forgejo-hooks/src/tests/mod.rs
@@ -0,0 +1,54 @@
+// 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 .
+
+use crate::{Hook, Push};
+use serde::Deserialize;
+
+/// Deserializes the payload into a struct
+fn parse