Skip to main content

bitbucket_cli/api/
deploy.rs

1use anyhow::Result;
2
3use super::BitbucketClient;
4use crate::models::{
5    AddDeployKeyRequest, CreateEnvironmentRequest, DeployKey, Deployment, Environment,
6    EnvironmentVariable, EnvironmentVariableInput, Paginated,
7};
8
9impl BitbucketClient {
10    // -----------------------------------------------------------------------
11    // Deploy keys
12    // -----------------------------------------------------------------------
13
14    /// List deploy keys for a repository
15    pub async fn list_deploy_keys(
16        &self,
17        workspace: &str,
18        repo_slug: &str,
19    ) -> Result<Paginated<DeployKey>> {
20        let path = format!("/repositories/{}/{}/deploy-keys", workspace, repo_slug);
21        self.get(&path).await
22    }
23
24    /// Add a deploy key to a repository
25    pub async fn add_deploy_key(
26        &self,
27        workspace: &str,
28        repo_slug: &str,
29        request: &AddDeployKeyRequest,
30    ) -> Result<DeployKey> {
31        let path = format!("/repositories/{}/{}/deploy-keys", workspace, repo_slug);
32        self.post(&path, request).await
33    }
34
35    /// Delete a deploy key from a repository
36    pub async fn delete_deploy_key(
37        &self,
38        workspace: &str,
39        repo_slug: &str,
40        key_id: u64,
41    ) -> Result<()> {
42        let path = format!(
43            "/repositories/{}/{}/deploy-keys/{}",
44            workspace, repo_slug, key_id
45        );
46        self.delete(&path).await
47    }
48
49    // -----------------------------------------------------------------------
50    // Deployment environments
51    // -----------------------------------------------------------------------
52
53    /// List deployment environments for a repository
54    pub async fn list_environments(
55        &self,
56        workspace: &str,
57        repo_slug: &str,
58    ) -> Result<Paginated<Environment>> {
59        let path = format!("/repositories/{}/{}/environments/", workspace, repo_slug);
60        self.get(&path).await
61    }
62
63    /// Create a deployment environment
64    pub async fn create_environment(
65        &self,
66        workspace: &str,
67        repo_slug: &str,
68        request: &CreateEnvironmentRequest,
69    ) -> Result<Environment> {
70        let path = format!("/repositories/{}/{}/environments/", workspace, repo_slug);
71        self.post(&path, request).await
72    }
73
74    /// Delete a deployment environment
75    pub async fn delete_environment(
76        &self,
77        workspace: &str,
78        repo_slug: &str,
79        environment_uuid: &str,
80    ) -> Result<()> {
81        let path = format!(
82            "/repositories/{}/{}/environments/{}",
83            workspace, repo_slug, environment_uuid
84        );
85        self.delete(&path).await
86    }
87
88    // -----------------------------------------------------------------------
89    // Deployment-environment variables
90    // -----------------------------------------------------------------------
91
92    /// List the variables configured on a deployment environment.
93    pub async fn list_environment_variables(
94        &self,
95        workspace: &str,
96        repo_slug: &str,
97        environment_uuid: &str,
98    ) -> Result<Vec<EnvironmentVariable>> {
99        let path = format!(
100            "/repositories/{}/{}/deployments_config/environments/{}/variables/",
101            workspace, repo_slug, environment_uuid
102        );
103        self.get_all_pages(&path).await
104    }
105
106    /// Create a variable on a deployment environment.
107    pub async fn create_environment_variable(
108        &self,
109        workspace: &str,
110        repo_slug: &str,
111        environment_uuid: &str,
112        variable: &EnvironmentVariableInput,
113    ) -> Result<EnvironmentVariable> {
114        let path = format!(
115            "/repositories/{}/{}/deployments_config/environments/{}/variables/",
116            workspace, repo_slug, environment_uuid
117        );
118        self.post(&path, variable).await
119    }
120
121    /// Update an existing variable on a deployment environment.
122    pub async fn update_environment_variable(
123        &self,
124        workspace: &str,
125        repo_slug: &str,
126        environment_uuid: &str,
127        variable_uuid: &str,
128        variable: &EnvironmentVariableInput,
129    ) -> Result<EnvironmentVariable> {
130        let path = format!(
131            "/repositories/{}/{}/deployments_config/environments/{}/variables/{}",
132            workspace, repo_slug, environment_uuid, variable_uuid
133        );
134        self.put(&path, variable).await
135    }
136
137    /// Delete a variable from a deployment environment.
138    pub async fn delete_environment_variable(
139        &self,
140        workspace: &str,
141        repo_slug: &str,
142        environment_uuid: &str,
143        variable_uuid: &str,
144    ) -> Result<()> {
145        let path = format!(
146            "/repositories/{}/{}/deployments_config/environments/{}/variables/{}",
147            workspace, repo_slug, environment_uuid, variable_uuid
148        );
149        self.delete(&path).await
150    }
151
152    // -----------------------------------------------------------------------
153    // Deployments
154    // -----------------------------------------------------------------------
155
156    /// List deployments for a repository
157    pub async fn list_deployments(
158        &self,
159        workspace: &str,
160        repo_slug: &str,
161        pagelen: Option<u32>,
162    ) -> Result<Paginated<Deployment>> {
163        let mut query = Vec::new();
164
165        if let Some(len) = pagelen {
166            query.push(("pagelen", len.to_string()));
167        }
168
169        let query_refs: Vec<(&str, &str)> = query.iter().map(|(k, v)| (*k, v.as_str())).collect();
170
171        let path = format!("/repositories/{}/{}/deployments/", workspace, repo_slug);
172        self.get_with_query(&path, &query_refs).await
173    }
174
175    /// Get a specific deployment
176    pub async fn get_deployment(
177        &self,
178        workspace: &str,
179        repo_slug: &str,
180        deployment_uuid: &str,
181    ) -> Result<Deployment> {
182        let path = format!(
183            "/repositories/{}/{}/deployments/{}",
184            workspace, repo_slug, deployment_uuid
185        );
186        self.get(&path).await
187    }
188}