Skip to main content

bitbucket_cli/api/
repos.rs

1use anyhow::Result;
2
3use super::BitbucketClient;
4use crate::models::{CreateRepositoryRequest, Paginated, Repository, UpdateRepositoryRequest};
5
6impl BitbucketClient {
7    /// List repositories for a workspace
8    pub async fn list_repositories(
9        &self,
10        workspace: &str,
11        page: Option<u32>,
12        pagelen: Option<u32>,
13    ) -> Result<Paginated<Repository>> {
14        self.list_repositories_filtered(workspace, None, None, None, page, pagelen)
15            .await
16    }
17
18    /// List repositories for a workspace with optional filters.
19    ///
20    /// `role` restricts results to repositories where the caller has at least
21    /// that role (member, contributor, admin, or owner), `query` is a
22    /// Bitbucket query (BBQL) expression passed as the `q` parameter, and
23    /// `sort` names a field to sort by (prefix with `-` for descending).
24    pub async fn list_repositories_filtered(
25        &self,
26        workspace: &str,
27        role: Option<&str>,
28        query: Option<&str>,
29        sort: Option<&str>,
30        page: Option<u32>,
31        pagelen: Option<u32>,
32    ) -> Result<Paginated<Repository>> {
33        let mut params = Vec::new();
34
35        if let Some(role) = role {
36            params.push(("role", role.to_string()));
37        }
38        if let Some(q) = query {
39            params.push(("q", q.to_string()));
40        }
41        if let Some(sort) = sort {
42            params.push(("sort", sort.to_string()));
43        }
44        if let Some(p) = page {
45            params.push(("page", p.to_string()));
46        }
47        if let Some(len) = pagelen {
48            params.push(("pagelen", len.to_string()));
49        }
50
51        let query_refs: Vec<(&str, &str)> = params.iter().map(|(k, v)| (*k, v.as_str())).collect();
52
53        let path = format!("/repositories/{}", workspace);
54        self.get_with_query(&path, &query_refs).await
55    }
56
57    /// Get a specific repository
58    pub async fn get_repository(&self, workspace: &str, repo_slug: &str) -> Result<Repository> {
59        let path = format!("/repositories/{}/{}", workspace, repo_slug);
60        self.get(&path).await
61    }
62
63    /// Create a new repository
64    pub async fn create_repository(
65        &self,
66        workspace: &str,
67        repo_slug: &str,
68        request: &CreateRepositoryRequest,
69    ) -> Result<Repository> {
70        let path = format!("/repositories/{}/{}", workspace, repo_slug);
71        self.put(&path, request).await
72    }
73
74    /// Update settings on an existing repository
75    pub async fn update_repository(
76        &self,
77        workspace: &str,
78        repo_slug: &str,
79        request: &UpdateRepositoryRequest,
80    ) -> Result<Repository> {
81        let path = format!("/repositories/{}/{}", workspace, repo_slug);
82        self.put(&path, request).await
83    }
84
85    /// Delete a repository
86    pub async fn delete_repository(&self, workspace: &str, repo_slug: &str) -> Result<()> {
87        let path = format!("/repositories/{}/{}", workspace, repo_slug);
88        self.delete(&path).await
89    }
90
91    /// Fork a repository
92    pub async fn fork_repository(
93        &self,
94        workspace: &str,
95        repo_slug: &str,
96        new_workspace: Option<&str>,
97        new_name: Option<&str>,
98    ) -> Result<Repository> {
99        #[derive(serde::Serialize)]
100        struct ForkRequest {
101            #[serde(skip_serializing_if = "Option::is_none")]
102            workspace: Option<WorkspaceRef>,
103            #[serde(skip_serializing_if = "Option::is_none")]
104            name: Option<String>,
105        }
106
107        #[derive(serde::Serialize)]
108        struct WorkspaceRef {
109            slug: String,
110        }
111
112        let request = ForkRequest {
113            workspace: new_workspace.map(|w| WorkspaceRef {
114                slug: w.to_string(),
115            }),
116            name: new_name.map(|n| n.to_string()),
117        };
118
119        let path = format!("/repositories/{}/{}/forks", workspace, repo_slug);
120        self.post(&path, &request).await
121    }
122
123    /// List users watching a repository
124    pub async fn list_watchers(
125        &self,
126        workspace: &str,
127        repo_slug: &str,
128        pagelen: Option<u32>,
129    ) -> Result<Paginated<crate::models::User>> {
130        let path = format!("/repositories/{}/{}/watchers", workspace, repo_slug);
131        match pagelen {
132            Some(len) => {
133                let len = len.to_string();
134                self.get_with_query(&path, &[("pagelen", len.as_str())])
135                    .await
136            }
137            None => self.get(&path).await,
138        }
139    }
140
141    /// List forks of a repository
142    pub async fn list_forks(
143        &self,
144        workspace: &str,
145        repo_slug: &str,
146        pagelen: Option<u32>,
147    ) -> Result<Paginated<Repository>> {
148        let path = format!("/repositories/{}/{}/forks", workspace, repo_slug);
149        match pagelen {
150            Some(len) => {
151                let len = len.to_string();
152                self.get_with_query(&path, &[("pagelen", len.as_str())])
153                    .await
154            }
155            None => self.get(&path).await,
156        }
157    }
158}