1use anyhow::Result;
2
3use super::BitbucketClient;
4use crate::models::{
5 CreateIssueCommentRequest, CreateIssueRequest, Issue, IssueComment, IssueState, Paginated,
6};
7
8impl BitbucketClient {
9 pub async fn list_issues(
11 &self,
12 workspace: &str,
13 repo_slug: &str,
14 state: Option<IssueState>,
15 page: Option<u32>,
16 pagelen: Option<u32>,
17 ) -> Result<Paginated<Issue>> {
18 let query = issue_list_query(state, page, pagelen);
19 let query_refs: Vec<(&str, &str)> = query.iter().map(|(k, v)| (*k, v.as_str())).collect();
20
21 let path = format!("/repositories/{}/{}/issues", workspace, repo_slug);
22 self.get_with_query(&path, &query_refs).await
23 }
24
25 pub async fn get_issue(
27 &self,
28 workspace: &str,
29 repo_slug: &str,
30 issue_id: u64,
31 ) -> Result<Issue> {
32 let path = format!(
33 "/repositories/{}/{}/issues/{}",
34 workspace, repo_slug, issue_id
35 );
36 self.get(&path).await
37 }
38
39 pub async fn create_issue(
41 &self,
42 workspace: &str,
43 repo_slug: &str,
44 request: &CreateIssueRequest,
45 ) -> Result<Issue> {
46 let path = format!("/repositories/{}/{}/issues", workspace, repo_slug);
47 self.post(&path, request).await
48 }
49
50 pub async fn update_issue(
52 &self,
53 workspace: &str,
54 repo_slug: &str,
55 issue_id: u64,
56 title: Option<&str>,
57 content: Option<&str>,
58 state: Option<IssueState>,
59 ) -> Result<Issue> {
60 #[derive(serde::Serialize)]
61 struct UpdateRequest {
62 #[serde(skip_serializing_if = "Option::is_none")]
63 title: Option<String>,
64 #[serde(skip_serializing_if = "Option::is_none")]
65 content: Option<ContentRequest>,
66 #[serde(skip_serializing_if = "Option::is_none")]
67 state: Option<IssueState>,
68 }
69
70 #[derive(serde::Serialize)]
71 struct ContentRequest {
72 raw: String,
73 }
74
75 let request = UpdateRequest {
76 title: title.map(|t| t.to_string()),
77 content: content.map(|c| ContentRequest { raw: c.to_string() }),
78 state,
79 };
80
81 let path = format!(
82 "/repositories/{}/{}/issues/{}",
83 workspace, repo_slug, issue_id
84 );
85 self.put(&path, &request).await
86 }
87
88 pub async fn delete_issue(
90 &self,
91 workspace: &str,
92 repo_slug: &str,
93 issue_id: u64,
94 ) -> Result<()> {
95 let path = format!(
96 "/repositories/{}/{}/issues/{}",
97 workspace, repo_slug, issue_id
98 );
99 self.delete(&path).await
100 }
101
102 pub async fn list_issue_comments(
104 &self,
105 workspace: &str,
106 repo_slug: &str,
107 issue_id: u64,
108 ) -> Result<Paginated<IssueComment>> {
109 let path = format!(
110 "/repositories/{}/{}/issues/{}/comments",
111 workspace, repo_slug, issue_id
112 );
113 self.get(&path).await
114 }
115
116 pub async fn add_issue_comment(
118 &self,
119 workspace: &str,
120 repo_slug: &str,
121 issue_id: u64,
122 content: &str,
123 ) -> Result<IssueComment> {
124 let request = CreateIssueCommentRequest {
125 content: crate::models::IssueContentRequest {
126 raw: content.to_string(),
127 },
128 };
129
130 let path = format!(
131 "/repositories/{}/{}/issues/{}/comments",
132 workspace, repo_slug, issue_id
133 );
134 self.post(&path, &request).await
135 }
136
137 pub async fn vote_issue(&self, workspace: &str, repo_slug: &str, issue_id: u64) -> Result<()> {
139 let path = format!(
140 "/repositories/{}/{}/issues/{}/vote",
141 workspace, repo_slug, issue_id
142 );
143 self.put::<serde_json::Value, _>(&path, &serde_json::json!({}))
144 .await?;
145 Ok(())
146 }
147
148 pub async fn unvote_issue(
150 &self,
151 workspace: &str,
152 repo_slug: &str,
153 issue_id: u64,
154 ) -> Result<()> {
155 let path = format!(
156 "/repositories/{}/{}/issues/{}/vote",
157 workspace, repo_slug, issue_id
158 );
159 self.delete(&path).await
160 }
161
162 pub async fn watch_issue(&self, workspace: &str, repo_slug: &str, issue_id: u64) -> Result<()> {
164 let path = format!(
165 "/repositories/{}/{}/issues/{}/watch",
166 workspace, repo_slug, issue_id
167 );
168 self.put::<serde_json::Value, _>(&path, &serde_json::json!({}))
169 .await?;
170 Ok(())
171 }
172
173 pub async fn unwatch_issue(
175 &self,
176 workspace: &str,
177 repo_slug: &str,
178 issue_id: u64,
179 ) -> Result<()> {
180 let path = format!(
181 "/repositories/{}/{}/issues/{}/watch",
182 workspace, repo_slug, issue_id
183 );
184 self.delete(&path).await
185 }
186}
187
188fn issue_list_query(
193 state: Option<IssueState>,
194 page: Option<u32>,
195 pagelen: Option<u32>,
196) -> Vec<(&'static str, String)> {
197 let mut query = Vec::new();
198
199 if let Some(s) = state {
200 query.push(("q", format!("state=\"{}\"", s)));
201 }
202 if let Some(p) = page {
203 query.push(("page", p.to_string()));
204 }
205 if let Some(len) = pagelen {
206 query.push(("pagelen", len.to_string()));
207 }
208
209 query
210}
211
212#[cfg(test)]
213mod tests {
214 use super::issue_list_query;
215 use crate::models::IssueState;
216
217 #[test]
218 fn open_state_builds_quoted_bbql_filter() {
219 assert_eq!(
220 issue_list_query(Some(IssueState::Open), None, None),
221 vec![("q", "state=\"open\"".to_string())]
222 );
223 }
224
225 #[test]
226 fn on_hold_state_keeps_two_word_value_quoted() {
227 assert_eq!(
228 issue_list_query(Some(IssueState::OnHold), None, None),
229 vec![("q", "state=\"on hold\"".to_string())]
230 );
231 }
232
233 #[test]
234 fn no_state_omits_q_pair() {
235 let query = issue_list_query(None, None, None);
236 assert!(query.is_empty());
237 }
238
239 #[test]
240 fn page_and_pagelen_pass_through_as_strings() {
241 assert_eq!(
242 issue_list_query(None, Some(2), Some(50)),
243 vec![("page", "2".to_string()), ("pagelen", "50".to_string())]
244 );
245 }
246
247 #[test]
248 fn state_page_and_pagelen_combine_in_order() {
249 assert_eq!(
250 issue_list_query(Some(IssueState::Open), Some(3), Some(25)),
251 vec![
252 ("q", "state=\"open\"".to_string()),
253 ("page", "3".to_string()),
254 ("pagelen", "25".to_string()),
255 ]
256 );
257 }
258}