From a6195d68de611c618fe79da6a75ad666cdbb7c2b Mon Sep 17 00:00:00 2001 From: pep Date: Wed, 4 Sep 2024 20:26:27 +0200 Subject: [PATCH] forgejo-hooks: Add support for Issue Signed-off-by: pep --- forgejo-hooks/src/lib.rs | 96 ++++++++---- forgejo-hooks/src/tests/issue.json | 235 +++++++++++++++++++++++++++++ forgejo-hooks/src/tests/mod.rs | 8 +- 3 files changed, 307 insertions(+), 32 deletions(-) create mode 100644 forgejo-hooks/src/tests/issue.json diff --git a/forgejo-hooks/src/lib.rs b/forgejo-hooks/src/lib.rs index c7cba46..61bd0e7 100644 --- a/forgejo-hooks/src/lib.rs +++ b/forgejo-hooks/src/lib.rs @@ -26,16 +26,6 @@ pub struct CommitAuthor { pub username: String, } -#[derive(Deserialize, Debug, PartialEq)] -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, PartialEq)] pub struct Commit { pub id: String, @@ -74,31 +64,17 @@ pub struct ExternalWiki { } #[derive(Deserialize, Debug, PartialEq)] -pub struct Repository { +pub struct RepositoryMeta { pub id: u64, - pub owner: User, pub name: String, + pub owner: 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: DateTime, - pub updated_at: DateTime, } #[derive(Deserialize, Debug, PartialEq)] -pub struct RepositoryFull { +pub struct Repository { pub id: u64, - pub owner: RepositoryOwner, + pub owner: User, pub name: String, pub full_name: String, pub description: String, @@ -159,7 +135,7 @@ pub struct RepositoryFull { } #[derive(Deserialize, Debug, PartialEq)] -pub struct RepositoryOwner { +pub struct User { pub id: u64, pub login: String, pub login_name: String, @@ -210,7 +186,7 @@ pub struct AddedBranch { pub sha: String, pub ref_type: String, pub repository: Repository, - pub sender: RepositoryOwner, + pub sender: User, } #[derive(Deserialize, Debug, PartialEq)] @@ -219,7 +195,64 @@ pub struct RemovedBranch { pub ref_type: String, pub pusher_type: String, pub repository: Repository, - pub sender: RepositoryOwner, + pub sender: User, +} + +#[derive(Deserialize, Debug, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum IssueAction { + Opened, +} + +#[derive(Deserialize, Debug, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum IssueState { + Open, +} + +#[derive(Deserialize, Debug, PartialEq)] +pub struct PullRequestMeta { + pub merged: bool, + pub merged_at: DateTime, + pub draft: bool, + pub html_url: String, +} + +#[derive(Deserialize, Debug, PartialEq)] +pub struct IssueAttrs { + pub id: u32, + pub url: String, + pub html_url: String, + pub number: u32, + pub user: User, + pub original_author: String, + pub original_author_id: u32, + pub title: String, + pub body: String, + #[serde(rename(deserialize = "ref"))] + pub ref_: String, + pub assets: Vec, + pub labels: Option>, + pub milestone: Option, + pub assignee: User, + pub assignees: Vec, + pub state: IssueState, + pub is_locked: bool, + pub comments: u32, + pub created_at: DateTime, + pub updated_at: Option>, + pub closed_at: Option>, + pub pull_request: Option, + pub repository: RepositoryMeta, + pub pin_order: u32, +} + +#[derive(Deserialize, Debug, PartialEq)] +pub struct Issue { + pub action: IssueAction, + pub number: u64, + pub issue: IssueAttrs, + pub repository: Repository, } impl From for Hook { @@ -240,4 +273,5 @@ pub enum Hook { Push(Push), AddedBranch(AddedBranch), RemovedBranch(RemovedBranch), + Issue(Issue), } diff --git a/forgejo-hooks/src/tests/issue.json b/forgejo-hooks/src/tests/issue.json new file mode 100644 index 0000000..d20f353 --- /dev/null +++ b/forgejo-hooks/src/tests/issue.json @@ -0,0 +1,235 @@ +{ + "action": "opened", + "number": 5, + "issue": { + "id": 8, + "url": "https://code.bouah.net/api/v1/repos/pep/webhook-test/issues/5", + "html_url": "https://code.bouah.net/pep/webhook-test/issues/5", + "number": 5, + "user": { + "id": 1, + "login": "pep", + "login_name": "", + "source_id": 0, + "full_name": "", + "email": "pep@bouah.net", + "avatar_url": "https://code.bouah.net/avatars/7897535c1d4a7ed71eb7b9bc8773bed6", + "html_url": "https://code.bouah.net/pep", + "language": "en-US", + "is_admin": true, + "last_login": "2024-08-30T16:44:38Z", + "created": "2023-01-02T21:57:48Z", + "restricted": false, + "active": true, + "prohibit_login": false, + "location": "", + "pronouns": "", + "website": "https://bouah.net", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "pep" + }, + "original_author": "", + "original_author_id": 0, + "title": "More.", + "body": "Hey", + "ref": "", + "assets": [], + "labels": [], + "milestone": null, + "assignee": { + "id": 1, + "login": "pep", + "login_name": "", + "source_id": 0, + "full_name": "", + "email": "pep@forgejo-noreply", + "avatar_url": "https://code.bouah.net/avatars/7897535c1d4a7ed71eb7b9bc8773bed6", + "html_url": "https://code.bouah.net/pep", + "language": "", + "is_admin": false, + "last_login": "0001-01-01T00:00:00Z", + "created": "2023-01-02T21:57:48Z", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "pronouns": "", + "website": "https://bouah.net", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "pep" + }, + "assignees": [ + { + "id": 1, + "login": "pep", + "login_name": "", + "source_id": 0, + "full_name": "", + "email": "pep@forgejo-noreply", + "avatar_url": "https://code.bouah.net/avatars/7897535c1d4a7ed71eb7b9bc8773bed6", + "html_url": "https://code.bouah.net/pep", + "language": "", + "is_admin": false, + "last_login": "0001-01-01T00:00:00Z", + "created": "2023-01-02T21:57:48Z", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "pronouns": "", + "website": "https://bouah.net", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "pep" + } + ], + "state": "open", + "is_locked": false, + "comments": 0, + "created_at": "2024-08-30T23:58:57Z", + "updated_at": "2024-08-30T23:58:57Z", + "closed_at": null, + "due_date": null, + "pull_request": null, + "repository": { + "id": 20, + "name": "webhook-test", + "owner": "pep", + "full_name": "pep/webhook-test" + }, + "pin_order": 0 + }, + "repository": { + "id": 20, + "owner": { + "id": 1, + "login": "pep", + "login_name": "", + "source_id": 0, + "full_name": "", + "email": "pep@bouah.net", + "avatar_url": "https://code.bouah.net/avatars/7897535c1d4a7ed71eb7b9bc8773bed6", + "html_url": "https://code.bouah.net/pep", + "language": "", + "is_admin": false, + "last_login": "0001-01-01T00:00:00Z", + "created": "2023-01-02T21:57:48Z", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "pronouns": "", + "website": "https://bouah.net", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "pep" + }, + "name": "webhook-test", + "full_name": "pep/webhook-test", + "description": "", + "empty": false, + "private": false, + "fork": false, + "template": false, + "parent": null, + "mirror": false, + "size": 28, + "language": "", + "languages_url": "https://code.bouah.net/api/v1/repos/pep/webhook-test/languages", + "html_url": "https://code.bouah.net/pep/webhook-test", + "url": "https://code.bouah.net/api/v1/repos/pep/webhook-test", + "link": "", + "ssh_url": "forgejo@code.bouah.net:pep/webhook-test.git", + "clone_url": "https://code.bouah.net/pep/webhook-test.git", + "original_url": "", + "website": "", + "stars_count": 0, + "forks_count": 0, + "watchers_count": 1, + "open_issues_count": 4, + "open_pr_counter": 0, + "release_counter": 0, + "default_branch": "main", + "archived": false, + "created_at": "2024-04-23T11:49:34Z", + "updated_at": "2024-04-23T17:42:53Z", + "archived_at": "1970-01-01T00:00:00Z", + "permissions": { + "admin": true, + "push": true, + "pull": true + }, + "has_issues": true, + "internal_tracker": { + "enable_time_tracker": false, + "allow_only_contributors_to_track_time": true, + "enable_issue_dependencies": true + }, + "has_wiki": true, + "wiki_branch": "master", + "globally_editable_wiki": false, + "has_pull_requests": true, + "has_projects": true, + "has_releases": true, + "has_packages": true, + "has_actions": false, + "ignore_whitespace_conflicts": false, + "allow_merge_commits": true, + "allow_rebase": true, + "allow_rebase_explicit": true, + "allow_squash_merge": true, + "allow_fast_forward_only_merge": false, + "allow_rebase_update": true, + "default_delete_branch_after_merge": false, + "default_merge_style": "rebase", + "default_allow_maintainer_edit": false, + "avatar_url": "", + "internal": false, + "mirror_interval": "", + "object_format_name": "sha1", + "mirror_updated": "0001-01-01T00:00:00Z", + "repo_transfer": null, + "topics": null + }, + "sender": { + "id": 1, + "login": "pep", + "login_name": "", + "source_id": 0, + "full_name": "", + "email": "pep@forgejo-noreply", + "avatar_url": "https://code.bouah.net/avatars/7897535c1d4a7ed71eb7b9bc8773bed6", + "html_url": "https://code.bouah.net/pep", + "language": "", + "is_admin": false, + "last_login": "0001-01-01T00:00:00Z", + "created": "2023-01-02T21:57:48Z", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "pronouns": "", + "website": "https://bouah.net", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "pep" + }, + "commit_id": "" +} diff --git a/forgejo-hooks/src/tests/mod.rs b/forgejo-hooks/src/tests/mod.rs index 00e32cc..cb22c39 100644 --- a/forgejo-hooks/src/tests/mod.rs +++ b/forgejo-hooks/src/tests/mod.rs @@ -13,7 +13,7 @@ // 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 crate::{Hook, Issue, Push}; use serde::Deserialize; /// Deserializes the payload into a struct @@ -52,3 +52,9 @@ fn push_payload() { let payload = std::fs::read_to_string("src/tests/push.json").unwrap(); let _ = parse_roundtrip::(&payload); } + +#[test] +fn issue_opened_payload() { + let payload = std::fs::read_to_string("src/tests/issue.json").unwrap(); + let _ = parse_roundtrip::(&payload); +}