1use anyhow::Result;
2
3use super::BitbucketClient;
4use crate::api::util::urlencode_segment;
5use crate::models::{
6 BranchDetail, CreateBranchRequest, CreateTagRequest, Paginated, RefTarget, TagDetail,
7};
8
9impl BitbucketClient {
10 pub async fn list_branches_filtered(
16 &self,
17 workspace: &str,
18 repo_slug: &str,
19 query: Option<&str>,
20 sort: Option<&str>,
21 page: Option<u32>,
22 pagelen: Option<u32>,
23 ) -> Result<Paginated<BranchDetail>> {
24 let path = format!("/repositories/{}/{}/refs/branches", workspace, repo_slug);
25 self.get_ref_page(&path, query, sort, page, pagelen).await
26 }
27
28 pub async fn create_branch(
30 &self,
31 workspace: &str,
32 repo_slug: &str,
33 name: &str,
34 target_hash: &str,
35 ) -> Result<BranchDetail> {
36 let path = format!("/repositories/{}/{}/refs/branches", workspace, repo_slug);
37 let request = CreateBranchRequest {
38 name: name.to_string(),
39 target: RefTarget {
40 hash: target_hash.to_string(),
41 },
42 };
43 self.post(&path, &request).await
44 }
45
46 pub async fn get_branch(
48 &self,
49 workspace: &str,
50 repo_slug: &str,
51 name: &str,
52 ) -> Result<BranchDetail> {
53 let path = format!(
54 "/repositories/{}/{}/refs/branches/{}",
55 workspace,
56 repo_slug,
57 urlencode_segment(name)
58 );
59 self.get(&path).await
60 }
61
62 pub async fn delete_branch(&self, workspace: &str, repo_slug: &str, name: &str) -> Result<()> {
64 let path = format!(
65 "/repositories/{}/{}/refs/branches/{}",
66 workspace,
67 repo_slug,
68 urlencode_segment(name)
69 );
70 self.delete(&path).await
71 }
72
73 pub async fn list_tags_filtered(
77 &self,
78 workspace: &str,
79 repo_slug: &str,
80 query: Option<&str>,
81 sort: Option<&str>,
82 page: Option<u32>,
83 pagelen: Option<u32>,
84 ) -> Result<Paginated<TagDetail>> {
85 let path = format!("/repositories/{}/{}/refs/tags", workspace, repo_slug);
86 self.get_ref_page(&path, query, sort, page, pagelen).await
87 }
88
89 pub async fn create_tag(
92 &self,
93 workspace: &str,
94 repo_slug: &str,
95 name: &str,
96 target_hash: &str,
97 message: Option<&str>,
98 ) -> Result<TagDetail> {
99 let path = format!("/repositories/{}/{}/refs/tags", workspace, repo_slug);
100 let request = CreateTagRequest {
101 name: name.to_string(),
102 target: RefTarget {
103 hash: target_hash.to_string(),
104 },
105 message: message.map(String::from),
106 };
107 self.post(&path, &request).await
108 }
109
110 pub async fn get_tag(&self, workspace: &str, repo_slug: &str, name: &str) -> Result<TagDetail> {
112 let path = format!(
113 "/repositories/{}/{}/refs/tags/{}",
114 workspace,
115 repo_slug,
116 urlencode_segment(name)
117 );
118 self.get(&path).await
119 }
120
121 pub async fn delete_tag(&self, workspace: &str, repo_slug: &str, name: &str) -> Result<()> {
123 let path = format!(
124 "/repositories/{}/{}/refs/tags/{}",
125 workspace,
126 repo_slug,
127 urlencode_segment(name)
128 );
129 self.delete(&path).await
130 }
131
132 async fn get_ref_page<T: serde::de::DeserializeOwned>(
135 &self,
136 path: &str,
137 query: Option<&str>,
138 sort: Option<&str>,
139 page: Option<u32>,
140 pagelen: Option<u32>,
141 ) -> Result<Paginated<T>> {
142 let mut params = Vec::new();
143
144 if let Some(q) = query {
145 params.push(("q", q.to_string()));
146 }
147 if let Some(s) = sort {
148 params.push(("sort", s.to_string()));
149 }
150 if let Some(p) = page {
151 params.push(("page", p.to_string()));
152 }
153 if let Some(len) = pagelen {
154 params.push(("pagelen", len.to_string()));
155 }
156
157 let param_refs: Vec<(&str, &str)> = params.iter().map(|(k, v)| (*k, v.as_str())).collect();
158 self.get_with_query(path, ¶m_refs).await
159 }
160}
161
162#[cfg(test)]
163mod tests {
164 use super::*;
165
166 #[test]
167 fn urlencode_leaves_safe_chars_untouched() {
168 assert_eq!(urlencode_segment("v1.0.0-rc_2~x"), "v1.0.0-rc_2~x");
169 }
170
171 #[test]
172 fn urlencode_encodes_slash_in_branch_names() {
173 assert_eq!(urlencode_segment("feature/login"), "feature%2Flogin");
174 }
175
176 #[test]
177 fn urlencode_encodes_spaces_and_specials() {
178 assert_eq!(urlencode_segment("wip branch#1"), "wip%20branch%231");
179 }
180}