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, Paginated,
6};
7
8impl BitbucketClient {
9    // -----------------------------------------------------------------------
10    // Deploy keys
11    // -----------------------------------------------------------------------
12
13    /// List deploy keys for a repository
14    pub async fn list_deploy_keys(
15        &self,
16        workspace: &str,
17        repo_slug: &str,
18    ) -> Result<Paginated<DeployKey>> {
19        let path = format!("/repositories/{}/{}/deploy-keys", workspace, repo_slug);
20        self.get(&path).await
21    }
22
23    /// Add a deploy key to a repository
24    pub async fn add_deploy_key(
25        &self,
26        workspace: &str,
27        repo_slug: &str,
28        request: &AddDeployKeyRequest,
29    ) -> Result<DeployKey> {
30        let path = format!("/repositories/{}/{}/deploy-keys", workspace, repo_slug);
31        self.post(&path, request).await
32    }
33
34    /// Delete a deploy key from a repository
35    pub async fn delete_deploy_key(
36        &self,
37        workspace: &str,
38        repo_slug: &str,
39        key_id: u64,
40    ) -> Result<()> {
41        let path = format!(
42            "/repositories/{}/{}/deploy-keys/{}",
43            workspace, repo_slug, key_id
44        );
45        self.delete(&path).await
46    }
47
48    // -----------------------------------------------------------------------
49    // Deployment environments
50    // -----------------------------------------------------------------------
51
52    /// List deployment environments for a repository
53    pub async fn list_environments(
54        &self,
55        workspace: &str,
56        repo_slug: &str,
57    ) -> Result<Paginated<Environment>> {
58        let path = format!("/repositories/{}/{}/environments/", workspace, repo_slug);
59        self.get(&path).await
60    }
61
62    /// Create a deployment environment
63    pub async fn create_environment(
64        &self,
65        workspace: &str,
66        repo_slug: &str,
67        request: &CreateEnvironmentRequest,
68    ) -> Result<Environment> {
69        let path = format!("/repositories/{}/{}/environments/", workspace, repo_slug);
70        self.post(&path, request).await
71    }
72
73    /// Delete a deployment environment
74    pub async fn delete_environment(
75        &self,
76        workspace: &str,
77        repo_slug: &str,
78        environment_uuid: &str,
79    ) -> Result<()> {
80        let path = format!(
81            "/repositories/{}/{}/environments/{}",
82            workspace, repo_slug, environment_uuid
83        );
84        self.delete(&path).await
85    }
86
87    // -----------------------------------------------------------------------
88    // Deployments
89    // -----------------------------------------------------------------------
90
91    /// List deployments for a repository
92    pub async fn list_deployments(
93        &self,
94        workspace: &str,
95        repo_slug: &str,
96        pagelen: Option<u32>,
97    ) -> Result<Paginated<Deployment>> {
98        let mut query = Vec::new();
99
100        if let Some(len) = pagelen {
101            query.push(("pagelen", len.to_string()));
102        }
103
104        let query_refs: Vec<(&str, &str)> = query.iter().map(|(k, v)| (*k, v.as_str())).collect();
105
106        let path = format!("/repositories/{}/{}/deployments/", workspace, repo_slug);
107        self.get_with_query(&path, &query_refs).await
108    }
109
110    /// Get a specific deployment
111    pub async fn get_deployment(
112        &self,
113        workspace: &str,
114        repo_slug: &str,
115        deployment_uuid: &str,
116    ) -> Result<Deployment> {
117        let path = format!(
118            "/repositories/{}/{}/deployments/{}",
119            workspace, repo_slug, deployment_uuid
120        );
121        self.get(&path).await
122    }
123}