Skip to main content

bitbucket_cli/api/
permissions.rs

1use anyhow::Result;
2
3use super::BitbucketClient;
4use super::webhooks::encode_path_segment;
5use crate::models::{GroupPermission, Paginated, UserPermission};
6
7impl BitbucketClient {
8    /// List users with explicit permissions on a repository
9    pub async fn list_user_permissions(
10        &self,
11        workspace: &str,
12        repo_slug: &str,
13        pagelen: Option<u32>,
14    ) -> Result<Paginated<UserPermission>> {
15        let path = format!(
16            "/repositories/{}/{}/permissions-config/users",
17            workspace, repo_slug
18        );
19
20        match pagelen {
21            Some(len) => {
22                self.get_with_query(&path, &[("pagelen", len.to_string().as_str())])
23                    .await
24            }
25            None => self.get(&path).await,
26        }
27    }
28
29    /// List groups with explicit permissions on a repository
30    pub async fn list_group_permissions(
31        &self,
32        workspace: &str,
33        repo_slug: &str,
34        pagelen: Option<u32>,
35    ) -> Result<Paginated<GroupPermission>> {
36        let path = format!(
37            "/repositories/{}/{}/permissions-config/groups",
38            workspace, repo_slug
39        );
40
41        match pagelen {
42            Some(len) => {
43                self.get_with_query(&path, &[("pagelen", len.to_string().as_str())])
44                    .await
45            }
46            None => self.get(&path).await,
47        }
48    }
49
50    /// Grant or update a user's explicit permission on a repository.
51    ///
52    /// `selected_user` may be an account ID or a brace-wrapped UUID;
53    /// `permission` is `read`, `write`, or `admin`.
54    pub async fn grant_user_permission(
55        &self,
56        workspace: &str,
57        repo_slug: &str,
58        selected_user: &str,
59        permission: &str,
60    ) -> Result<UserPermission> {
61        let path = format!(
62            "/repositories/{}/{}/permissions-config/users/{}",
63            workspace,
64            repo_slug,
65            encode_path_segment(selected_user)
66        );
67        self.put(&path, &serde_json::json!({ "permission": permission }))
68            .await
69    }
70
71    /// Revoke a user's explicit permission on a repository
72    pub async fn revoke_user_permission(
73        &self,
74        workspace: &str,
75        repo_slug: &str,
76        selected_user: &str,
77    ) -> Result<()> {
78        let path = format!(
79            "/repositories/{}/{}/permissions-config/users/{}",
80            workspace,
81            repo_slug,
82            encode_path_segment(selected_user)
83        );
84        self.delete(&path).await
85    }
86
87    /// Grant or update a group's explicit permission on a repository.
88    ///
89    /// `group_slug` is the workspace group's slug; `permission` is `read`,
90    /// `write`, or `admin`.
91    pub async fn grant_group_permission(
92        &self,
93        workspace: &str,
94        repo_slug: &str,
95        group_slug: &str,
96        permission: &str,
97    ) -> Result<GroupPermission> {
98        let path = format!(
99            "/repositories/{}/{}/permissions-config/groups/{}",
100            workspace,
101            repo_slug,
102            encode_path_segment(group_slug)
103        );
104        self.put(&path, &serde_json::json!({ "permission": permission }))
105            .await
106    }
107
108    /// Revoke a group's explicit permission on a repository
109    pub async fn revoke_group_permission(
110        &self,
111        workspace: &str,
112        repo_slug: &str,
113        group_slug: &str,
114    ) -> Result<()> {
115        let path = format!(
116            "/repositories/{}/{}/permissions-config/groups/{}",
117            workspace,
118            repo_slug,
119            encode_path_segment(group_slug)
120        );
121        self.delete(&path).await
122    }
123}