bitbucket_cli/api/
projects.rs1use anyhow::Result;
2
3use super::BitbucketClient;
4use crate::models::{CreateProjectRequest, Paginated, ProjectDetail, UpdateProjectRequest};
5
6impl BitbucketClient {
7 pub async fn list_projects(
13 &self,
14 workspace: &str,
15 query: Option<&str>,
16 sort: Option<&str>,
17 page: Option<u32>,
18 pagelen: Option<u32>,
19 ) -> Result<Paginated<ProjectDetail>> {
20 let mut params = Vec::new();
21
22 if let Some(q) = query {
23 params.push(("q", q.to_string()));
24 }
25 if let Some(sort) = sort {
26 params.push(("sort", sort.to_string()));
27 }
28 if let Some(p) = page {
29 params.push(("page", p.to_string()));
30 }
31 if let Some(len) = pagelen {
32 params.push(("pagelen", len.to_string()));
33 }
34
35 let query_refs: Vec<(&str, &str)> = params.iter().map(|(k, v)| (*k, v.as_str())).collect();
36
37 let path = format!("/workspaces/{}/projects", workspace);
38 self.get_with_query(&path, &query_refs).await
39 }
40
41 pub async fn get_project(&self, workspace: &str, key: &str) -> Result<ProjectDetail> {
43 let path = format!("/workspaces/{}/projects/{}", workspace, key);
44 self.get(&path).await
45 }
46
47 pub async fn create_project(
49 &self,
50 workspace: &str,
51 request: &CreateProjectRequest,
52 ) -> Result<ProjectDetail> {
53 let path = format!("/workspaces/{}/projects", workspace);
54 self.post(&path, request).await
55 }
56
57 pub async fn update_project(
59 &self,
60 workspace: &str,
61 key: &str,
62 request: &UpdateProjectRequest,
63 ) -> Result<ProjectDetail> {
64 let path = format!("/workspaces/{}/projects/{}", workspace, key);
65 self.put(&path, request).await
66 }
67
68 pub async fn delete_project(&self, workspace: &str, key: &str) -> Result<()> {
70 let path = format!("/workspaces/{}/projects/{}", workspace, key);
71 self.delete(&path).await
72 }
73}
74
75#[cfg(test)]
76mod tests {
77 use crate::models::{Paginated, ProjectDetail};
78
79 #[test]
80 fn project_list_payload_deserializes() {
81 let payload = r#"{
82 "pagelen": 10,
83 "page": 1,
84 "size": 2,
85 "values": [
86 {
87 "uuid": "{a1b2c3d4-0000-1111-2222-333344445555}",
88 "key": "PROJ",
89 "name": "My Project",
90 "description": "The main project",
91 "is_private": true,
92 "type": "project",
93 "created_on": "2020-04-07T21:38:29.542346+00:00",
94 "updated_on": "2021-06-01T08:00:00.000000+00:00",
95 "links": {
96 "html": { "href": "https://bitbucket.org/acme/workspace/projects/PROJ" }
97 }
98 },
99 {
100 "key": "OTHER",
101 "name": "Other",
102 "type": "project"
103 }
104 ]
105 }"#;
106
107 let page: Paginated<ProjectDetail> = serde_json::from_str(payload).unwrap();
108 assert_eq!(page.values.len(), 2);
109 assert_eq!(page.values[0].key, "PROJ");
110 assert_eq!(page.values[0].is_private, Some(true));
111 assert!(page.values[0].created_on.is_some());
112 assert!(page.values[0].updated_on.is_some());
113 assert_eq!(page.values[1].key, "OTHER");
114 assert!(page.values[1].uuid.is_none());
115 assert!(page.next.is_none());
116 }
117}