From ffbbfaf8985b98f2881eecdf61fb659176a68c1f Mon Sep 17 00:00:00 2001 From: Cyborus <87248184+Cyborus04@users.noreply.github.com> Date: Mon, 13 Nov 2023 18:35:44 -0500 Subject: [PATCH] support comment operations --- src/issue.rs | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/src/issue.rs b/src/issue.rs index 66c04ed..ccc1854 100644 --- a/src/issue.rs +++ b/src/issue.rs @@ -17,9 +17,35 @@ impl Forgejo { self.delete(&format!("/repos/{owner}/{repo}/issues/{id}")).await } - pub async fn edit_issue(&self, owner: &str, repo: &str, id: u64, opts: EditIssueOption) -> Result<(), ForgejoError> { + pub async fn edit_issue(&self, owner: &str, repo: &str, id: u64, opts: EditIssueOption) -> Result { self.patch(&format!("/repos/{owner}/{repo}/issues/{id}"), &opts).await } + + + + pub async fn get_repo_comments(&self, owner: &str, repo: &str, query: RepoCommentQuery) -> Result, ForgejoError> { + self.get(&query.to_string(owner, repo)).await + } + + pub async fn get_issue_comments(&self, owner: &str, repo: &str, issue_id: u64, query: IssueCommentQuery) -> Result, ForgejoError> { + self.get(&query.to_string(owner, repo, issue_id)).await + } + + pub async fn create_comment(&self, owner: &str, repo: &str, issue_id: u64, opts: CreateIssueCommentOption) -> Result { + self.post(&format!("/repos/{owner}/{repo}/issues/{issue_id}/comments"), &opts).await + } + + pub async fn get_comment(&self, owner: &str, repo: &str, id: u64) -> Result, ForgejoError> { + self.get_opt(&format!("/repos/{owner}/{repo}/issues/comments/{id}")).await + } + + pub async fn delete_comment(&self, owner: &str, repo: &str, id: u64) -> Result<(), ForgejoError> { + self.delete(&format!("/repos/{owner}/{repo}/issues/comments/{id}")).await + } + + pub async fn edit_comment(&self, owner: &str, repo: &str, id: u64, opts: EditIssueCommentOption) -> Result { + self.patch(&format!("/repos/{owner}/{repo}/issues/comments/{id}"), &opts).await + } } #[derive(serde::Deserialize, Debug, PartialEq)] @@ -93,6 +119,23 @@ impl State { } } +#[derive(serde::Deserialize, Debug, PartialEq)] +pub struct Comment { + pub assets: Vec, + pub body: String, + #[serde(with = "time::serde::rfc3339")] + pub created_at: time::OffsetDateTime, + pub html_url: Url, + pub id: u64, + pub issue_url: Url, + pub original_author: String, + pub original_author_id: u64, + pub pull_request_url: Url, + #[serde(with = "time::serde::rfc3339")] + pub updated_at: time::OffsetDateTime, + pub user: User, +} + #[derive(Default, Debug)] pub struct IssueQuery { pub state: Option, @@ -143,6 +186,40 @@ impl IssueQueryType { } } +#[derive(Default, Debug)] +pub struct IssueCommentQuery { + pub since: Option, + pub before: Option, +} + +impl IssueCommentQuery { + fn to_string(&self, owner: &str, repo: &str, issue_id: u64) -> String { + format!("/repos/{owner}/{repo}/issues/{issue_id}/comments?since={}&before={}", + self.since.map(|t| t.format(&time::format_description::well_known::Rfc3339).unwrap()).unwrap_or_default(), + self.before.map(|t| t.format(&time::format_description::well_known::Rfc3339).unwrap()).unwrap_or_default(), + ) + } +} + +#[derive(Default, Debug)] +pub struct RepoCommentQuery { + pub since: Option, + pub before: Option, + pub page: Option, + pub limit: Option, +} + +impl RepoCommentQuery { + fn to_string(&self, owner: &str, repo: &str) -> String { + format!("/repos/{owner}/{repo}/issues/comments?since={}&before={}&page={}&limit={}", + self.since.map(|t| t.format(&time::format_description::well_known::Rfc3339).unwrap()).unwrap_or_default(), + self.before.map(|t| t.format(&time::format_description::well_known::Rfc3339).unwrap()).unwrap_or_default(), + self.page.map(|page| page.to_string()).unwrap_or_default(), + self.limit.map(|page| page.to_string()).unwrap_or_default(), + ) + } +} + #[derive(serde::Serialize, Debug, PartialEq, Default)] pub struct CreateIssueOption { pub assignees: Vec, @@ -169,3 +246,13 @@ pub struct EditIssueOption { pub title: Option, pub unset_due_date: Option, } + +#[derive(serde::Serialize, Debug, PartialEq, Default)] +pub struct CreateIssueCommentOption { + body: String, +} + +#[derive(serde::Serialize, Debug, PartialEq, Default)] +pub struct EditIssueCommentOption { + body: String, +}