bitbucket_cli/api/
repos.rs

1use anyhow::Result;
2
3use super::BitbucketClient;
4use crate::models::{CreateRepositoryRequest, Paginated, Repository};
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        let mut query = Vec::new();
15
16        if let Some(p) = page {
17            query.push(("page", p.to_string()));
18        }
19        if let Some(len) = pagelen {
20            query.push(("pagelen", len.to_string()));
21        }
22
23        let query_refs: Vec<(&str, &str)> = query.iter().map(|(k, v)| (*k, v.as_str())).collect();
24
25        let path = format!("/repositories/{}", workspace);
26        self.get_with_query(&path, &query_refs).await
27    }
28
29    /// Get a specific repository
30    pub async fn get_repository(&self, workspace: &str, repo_slug: &str) -> Result<Repository> {
31        let path = format!("/repositories/{}/{}", workspace, repo_slug);
32        self.get(&path).await
33    }
34
35    /// Create a new repository
36    pub async fn create_repository(
37        &self,
38        workspace: &str,
39        repo_slug: &str,
40        request: &CreateRepositoryRequest,
41    ) -> Result<Repository> {
42        let path = format!("/repositories/{}/{}", workspace, repo_slug);
43        self.put(&path, request).await
44    }
45
46    /// Delete a repository
47    pub async fn delete_repository(&self, workspace: &str, repo_slug: &str) -> Result<()> {
48        let path = format!("/repositories/{}/{}", workspace, repo_slug);
49        self.delete(&path).await
50    }
51
52    /// Fork a repository
53    pub async fn fork_repository(
54        &self,
55        workspace: &str,
56        repo_slug: &str,
57        new_workspace: Option<&str>,
58        new_name: Option<&str>,
59    ) -> Result<Repository> {
60        #[derive(serde::Serialize)]
61        struct ForkRequest {
62            #[serde(skip_serializing_if = "Option::is_none")]
63            workspace: Option<WorkspaceRef>,
64            #[serde(skip_serializing_if = "Option::is_none")]
65            name: Option<String>,
66        }
67
68        #[derive(serde::Serialize)]
69        struct WorkspaceRef {
70            slug: String,
71        }
72
73        let request = ForkRequest {
74            workspace: new_workspace.map(|w| WorkspaceRef {
75                slug: w.to_string(),
76            }),
77            name: new_name.map(|n| n.to_string()),
78        };
79
80        let path = format!("/repositories/{}/{}/forks", workspace, repo_slug);
81        self.post(&path, &request).await
82    }
83
84    /// List repository branches
85    pub async fn list_branches(
86        &self,
87        workspace: &str,
88        repo_slug: &str,
89    ) -> Result<Paginated<crate::models::Branch>> {
90        let path = format!("/repositories/{}/{}/refs/branches", workspace, repo_slug);
91        self.get(&path).await
92    }
93
94    /// Get the main branch
95    pub async fn get_main_branch(
96        &self,
97        workspace: &str,
98        repo_slug: &str,
99    ) -> Result<crate::models::Branch> {
100        let path = format!("/repositories/{}/{}/main-branch", workspace, repo_slug);
101        self.get(&path).await
102    }
103}