rust_cnb/
git_settings.rs

1//! GitSettings API 客户端
2
3use crate::error::{ApiError, Result};
4use reqwest::Client;
5use serde_json::Value;
6use url::Url;
7
8/// GitSettings API 客户端
9pub struct GitSettingsClient {
10    base_url: String,
11    client: Client,
12}
13
14impl GitSettingsClient {
15    /// 创建新的 GitSettings API 客户端
16    pub fn new(base_url: String, client: Client) -> Self {
17        Self { base_url, client }
18    }
19
20    /// 设置认证信息
21    pub fn with_auth(self, token: &str) -> Self {
22        // 这里可以扩展认证逻辑
23        self
24    }
25
26    /// 查询仓库合并请求设置。List pull request settings.
27    pub async fn get_repo_settings_pull_request(
28        &self,
29        repo: String,
30    ) -> Result<Value> {
31        let path = format!("/{}/-/settings/pull-request", repo);
32        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
33        
34
35                let request = self.client.request(
36            reqwest::Method::GET,
37            url
38        );
39        
40
41
42
43        let response = request.send().await?;
44        
45        if response.status().is_success() {
46            let json: Value = response.json().await?;
47            Ok(json)
48        } else {
49            Err(ApiError::HttpError(response.status().as_u16()))
50        }
51    }
52
53    /// 设置仓库推送设置。Set pull request settings.
54    pub async fn put_repo_settings_pull_request(
55        &self,
56        repo: String,
57        pull_request_form: serde_json::Value,
58    ) -> Result<Value> {
59        let path = format!("/{}/-/settings/pull-request", repo);
60        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
61        
62
63        
64        let mut request = self.client.request(
65            reqwest::Method::PUT,
66            url
67        );
68
69
70
71        request = request.json(&pull_request_form);
72
73        let response = request.send().await?;
74        
75        if response.status().is_success() {
76            let json: Value = response.json().await?;
77            Ok(json)
78        } else {
79            Err(ApiError::HttpError(response.status().as_u16()))
80        }
81    }
82
83    /// 查询仓库保护分支规则列表。List branch protection rules.
84    pub async fn get_repo_settings_branch_protections(
85        &self,
86        repo: String,
87    ) -> Result<Value> {
88        let path = format!("/{}/-/settings/branch-protections", repo);
89        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
90        
91
92                let request = self.client.request(
93            reqwest::Method::GET,
94            url
95        );
96        
97
98
99
100        let response = request.send().await?;
101        
102        if response.status().is_success() {
103            let json: Value = response.json().await?;
104            Ok(json)
105        } else {
106            Err(ApiError::HttpError(response.status().as_u16()))
107        }
108    }
109
110    /// 新增仓库保护分支规则。Create branch protection rule.
111    pub async fn post_repo_settings_branch_protections(
112        &self,
113        repo: String,
114        branch_protection_form: serde_json::Value,
115    ) -> Result<Value> {
116        let path = format!("/{}/-/settings/branch-protections", repo);
117        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
118        
119
120        
121        let mut request = self.client.request(
122            reqwest::Method::POST,
123            url
124        );
125
126
127
128        request = request.json(&branch_protection_form);
129
130        let response = request.send().await?;
131        
132        if response.status().is_success() {
133            let json: Value = response.json().await?;
134            Ok(json)
135        } else {
136            Err(ApiError::HttpError(response.status().as_u16()))
137        }
138    }
139
140    /// 查询仓库推送设置。List push limit settings.
141    pub async fn get_repo_settings_push_limit(
142        &self,
143        repo: String,
144    ) -> Result<Value> {
145        let path = format!("/{}/-/settings/push-limit", repo);
146        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
147        
148
149                let request = self.client.request(
150            reqwest::Method::GET,
151            url
152        );
153        
154
155
156
157        let response = request.send().await?;
158        
159        if response.status().is_success() {
160            let json: Value = response.json().await?;
161            Ok(json)
162        } else {
163            Err(ApiError::HttpError(response.status().as_u16()))
164        }
165    }
166
167    /// 设置仓库推送设置。Set push limit settings.
168    pub async fn put_repo_settings_push_limit(
169        &self,
170        repo: String,
171        push_limit_form: serde_json::Value,
172    ) -> Result<Value> {
173        let path = format!("/{}/-/settings/push-limit", repo);
174        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
175        
176
177        
178        let mut request = self.client.request(
179            reqwest::Method::PUT,
180            url
181        );
182
183
184
185        request = request.json(&push_limit_form);
186
187        let response = request.send().await?;
188        
189        if response.status().is_success() {
190            let json: Value = response.json().await?;
191            Ok(json)
192        } else {
193            Err(ApiError::HttpError(response.status().as_u16()))
194        }
195    }
196
197    /// 查询仓库保护分支规则。Get branch protection rule.
198    pub async fn get_repo_settings_branch_protections_id(
199        &self,
200        repo: String,
201        id: String,
202    ) -> Result<Value> {
203        let path = format!("/{}/-/settings/branch-protections/{}", repo, id);
204        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
205        
206
207                let request = self.client.request(
208            reqwest::Method::GET,
209            url
210        );
211        
212
213
214
215        let response = request.send().await?;
216        
217        if response.status().is_success() {
218            let json: Value = response.json().await?;
219            Ok(json)
220        } else {
221            Err(ApiError::HttpError(response.status().as_u16()))
222        }
223    }
224
225    /// 删除仓库保护分支规则。 Delete branch protection rule.
226    pub async fn delete_repo_settings_branch_protections_id(
227        &self,
228        repo: String,
229        id: String,
230    ) -> Result<Value> {
231        let path = format!("/{}/-/settings/branch-protections/{}", repo, id);
232        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
233        
234
235                let request = self.client.request(
236            reqwest::Method::DELETE,
237            url
238        );
239        
240
241
242
243        let response = request.send().await?;
244        
245        if response.status().is_success() {
246            let json: Value = response.json().await?;
247            Ok(json)
248        } else {
249            Err(ApiError::HttpError(response.status().as_u16()))
250        }
251    }
252
253    /// 更新仓库保护分支规则。Update branch protection rule.
254    pub async fn patch_repo_settings_branch_protections_id(
255        &self,
256        repo: String,
257        id: String,
258        branch_protection_form: serde_json::Value,
259    ) -> Result<Value> {
260        let path = format!("/{}/-/settings/branch-protections/{}", repo, id);
261        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
262        
263
264        
265        let mut request = self.client.request(
266            reqwest::Method::PATCH,
267            url
268        );
269
270
271
272        request = request.json(&branch_protection_form);
273
274        let response = request.send().await?;
275        
276        if response.status().is_success() {
277            let json: Value = response.json().await?;
278            Ok(json)
279        } else {
280            Err(ApiError::HttpError(response.status().as_u16()))
281        }
282    }
283
284    /// 查询仓库云原生构建设置。List pipeline settings.
285    pub async fn get_repo_settings_cloud_native_build(
286        &self,
287        repo: String,
288    ) -> Result<Value> {
289        let path = format!("/{}/-/settings/cloud-native-build", repo);
290        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
291        
292
293                let request = self.client.request(
294            reqwest::Method::GET,
295            url
296        );
297        
298
299
300
301        let response = request.send().await?;
302        
303        if response.status().is_success() {
304            let json: Value = response.json().await?;
305            Ok(json)
306        } else {
307            Err(ApiError::HttpError(response.status().as_u16()))
308        }
309    }
310
311    /// 更新仓库云原生构建设置。Update pipeline settings.
312    pub async fn put_repo_settings_cloud_native_build(
313        &self,
314        repo: String,
315        pipeline_form: serde_json::Value,
316    ) -> Result<Value> {
317        let path = format!("/{}/-/settings/cloud-native-build", repo);
318        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
319        
320
321        
322        let mut request = self.client.request(
323            reqwest::Method::PUT,
324            url
325        );
326
327
328
329        request = request.json(&pipeline_form);
330
331        let response = request.send().await?;
332        
333        if response.status().is_success() {
334            let json: Value = response.json().await?;
335            Ok(json)
336        } else {
337            Err(ApiError::HttpError(response.status().as_u16()))
338        }
339    }
340
341}