Skip to main content

bitbucket_cli/api/
branch_restrictions.rs

1use anyhow::Result;
2
3use super::BitbucketClient;
4use crate::models::{BranchRestriction, Paginated};
5
6impl BitbucketClient {
7    /// List branch restriction rules on a repository
8    pub async fn list_branch_restrictions(
9        &self,
10        workspace: &str,
11        repo_slug: &str,
12        pagelen: Option<u32>,
13    ) -> Result<Paginated<BranchRestriction>> {
14        let path = format!(
15            "/repositories/{}/{}/branch-restrictions",
16            workspace, repo_slug
17        );
18
19        match pagelen {
20            Some(len) => {
21                self.get_with_query(&path, &[("pagelen", len.to_string().as_str())])
22                    .await
23            }
24            None => self.get(&path).await,
25        }
26    }
27
28    /// Create a branch restriction rule.
29    ///
30    /// `value` is the numeric parameter required by some kinds (e.g. the
31    /// approval count for `require_approvals_to_merge`); it is omitted from
32    /// the request body when `None`.
33    pub async fn create_branch_restriction(
34        &self,
35        workspace: &str,
36        repo_slug: &str,
37        kind: &str,
38        pattern: &str,
39        value: Option<u64>,
40    ) -> Result<BranchRestriction> {
41        let mut body = serde_json::json!({
42            "kind": kind,
43            "pattern": pattern,
44        });
45        if let Some(value) = value {
46            body["value"] = serde_json::json!(value);
47        }
48
49        let path = format!(
50            "/repositories/{}/{}/branch-restrictions",
51            workspace, repo_slug
52        );
53        self.post(&path, &body).await
54    }
55
56    /// Delete a branch restriction rule by id
57    pub async fn delete_branch_restriction(
58        &self,
59        workspace: &str,
60        repo_slug: &str,
61        id: u64,
62    ) -> Result<()> {
63        let path = format!(
64            "/repositories/{}/{}/branch-restrictions/{}",
65            workspace, repo_slug, id
66        );
67        self.delete(&path).await
68    }
69}