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        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    /// Update settings on an existing repository
47    pub async fn update_repository(
48        &self,
49        workspace: &str,
50        repo_slug: &str,
51        request: &UpdateRepositoryRequest,
52    ) -> Result<Repository> {
53        let path = format!("/repositories/{}/{}", workspace, repo_slug);
54        self.put(&path, request).await
55    }
56
57    /// Delete a repository
58    pub async fn delete_repository(&self, workspace: &str, repo_slug: &str) -> Result<()> {
59        let path = format!("/repositories/{}/{}", workspace, repo_slug);
60        self.delete(&path).await
61    }
62
63    /// Fork a repository
64    pub async fn fork_repository(
65        &self,
66        workspace: &str,
67        repo_slug: &str,
68        new_workspace: Option<&str>,
69        new_name: Option<&str>,
70    ) -> Result<Repository> {
71        #[derive(serde::Serialize)]
72        struct ForkRequest {
73            #[serde(skip_serializing_if = "Option::is_none")]
74            workspace: Option<WorkspaceRef>,
75            #[serde(skip_serializing_if = "Option::is_none")]
76            name: Option<String>,
77        }
78
79        #[derive(serde::Serialize)]
80        struct WorkspaceRef {
81            slug: String,
82        }
83
84        let request = ForkRequest {
85            workspace: new_workspace.map(|w| WorkspaceRef {
86                slug: w.to_string(),
87            }),
88            name: new_name.map(|n| n.to_string()),
89        };
90
91        let path = format!("/repositories/{}/{}/forks", workspace, repo_slug);
92        self.post(&path, &request).await
93    }
94
95    /// List repository branches
96    pub async fn list_branches(
97        &self,
98        workspace: &str,
99        repo_slug: &str,
100    ) -> Result<Paginated<crate::models::Branch>> {
101        let path = format!("/repositories/{}/{}/refs/branches", workspace, repo_slug);
102        self.get(&path).await
103    }
104
105    /// Get the main branch
106    pub async fn get_main_branch(
107        &self,
108        workspace: &str,
109        repo_slug: &str,
110    ) -> Result<crate::models::Branch> {
111        let path = format!("/repositories/{}/{}/main-branch", workspace, repo_slug);
112        self.get(&path).await
113    }
114}