Skip to main content

bitbucket_cli/models/
deploy.rs

1use serde::{Deserialize, Serialize};
2
3// ---------------------------------------------------------------------------
4// Deploy keys
5// ---------------------------------------------------------------------------
6
7/// An SSH deploy key granting read access to a repository.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct DeployKey {
10    pub id: Option<u64>,
11    /// The SSH public key material (e.g. `ssh-ed25519 AAAA... user@host`).
12    pub key: Option<String>,
13    pub label: Option<String>,
14    pub comment: Option<String>,
15}
16
17/// Request body for adding a deploy key to a repository.
18#[derive(Debug, Clone, Serialize)]
19#[non_exhaustive]
20pub struct AddDeployKeyRequest {
21    pub key: String,
22    pub label: String,
23}
24
25// ---------------------------------------------------------------------------
26// Deployment environments
27// ---------------------------------------------------------------------------
28
29/// A deployment environment (e.g. Test, Staging, Production).
30#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct Environment {
32    pub uuid: Option<String>,
33    pub name: Option<String>,
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub environment_type: Option<EnvironmentType>,
36}
37
38/// The category of a deployment environment: Test, Staging, or Production.
39#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct EnvironmentType {
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub name: Option<String>,
43}
44
45/// Request body for creating a deployment environment.
46#[derive(Debug, Clone, Serialize)]
47#[non_exhaustive]
48pub struct CreateEnvironmentRequest {
49    pub name: String,
50    pub environment_type: EnvironmentType,
51}
52
53// ---------------------------------------------------------------------------
54// Deployment-environment variables
55// ---------------------------------------------------------------------------
56
57/// A variable configured on a single deployment environment.
58///
59/// These are distinct from repository and workspace pipeline variables: their
60/// value is scoped to one environment (`deployment: <env>` steps), so the same
61/// key can hold different values per environment. Secured variables come back
62/// from the API without a `value`.
63#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct EnvironmentVariable {
65    pub uuid: Option<String>,
66    pub key: Option<String>,
67    pub value: Option<String>,
68    pub secured: Option<bool>,
69}
70
71/// Request body for creating or updating a deployment-environment variable.
72#[derive(Debug, Clone, Serialize)]
73pub struct EnvironmentVariableInput {
74    pub key: String,
75    pub value: String,
76    pub secured: bool,
77}
78
79// ---------------------------------------------------------------------------
80// Deployments
81// ---------------------------------------------------------------------------
82
83/// A deployment of a release to an environment.
84///
85/// The Bitbucket deployments API is only loosely documented, so the nested
86/// shapes here are kept permissive (`Option` everywhere, raw JSON for the
87/// deep-nested parts).
88#[derive(Debug, Clone, Serialize, Deserialize)]
89pub struct Deployment {
90    pub uuid: Option<String>,
91    pub state: Option<DeploymentState>,
92    pub environment: Option<Environment>,
93    pub release: Option<serde_json::Value>,
94}
95
96/// The state of a deployment (e.g. UNDEPLOYED, IN_PROGRESS, COMPLETED).
97#[derive(Debug, Clone, Serialize, Deserialize)]
98pub struct DeploymentState {
99    pub name: Option<String>,
100    /// Nested status object; shape varies by state, so it is kept raw.
101    pub status: Option<serde_json::Value>,
102}
103
104// ---------------------------------------------------------------------------
105// Branching model
106// ---------------------------------------------------------------------------
107
108/// A repository's effective branching model.
109#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct BranchingModel {
111    pub development: Option<ModelBranch>,
112    pub production: Option<ModelBranch>,
113    pub branch_types: Option<Vec<BranchType>>,
114}
115
116/// The development or production branch in a branching model.
117#[derive(Debug, Clone, Serialize, Deserialize)]
118pub struct ModelBranch {
119    pub name: Option<String>,
120    /// The concrete branch this resolves to (only present in the effective
121    /// model, not in the settings representation).
122    pub branch: Option<crate::models::Branch>,
123    pub use_mainbranch: Option<bool>,
124}
125
126/// A branch type prefix mapping (e.g. kind `feature` → prefix `feature/`).
127#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct BranchType {
129    pub kind: Option<String>,
130    pub prefix: Option<String>,
131}
132
133/// Partial update for a repository's branching model settings. Unset fields
134/// are omitted from the request body so the corresponding settings are left
135/// untouched.
136#[derive(Debug, Clone, Default, Serialize)]
137pub struct UpdateBranchingModelRequest {
138    #[serde(skip_serializing_if = "Option::is_none")]
139    pub development: Option<BranchingModelBranchSettings>,
140    #[serde(skip_serializing_if = "Option::is_none")]
141    pub production: Option<BranchingModelBranchSettings>,
142}
143
144impl UpdateBranchingModelRequest {
145    pub fn is_empty(&self) -> bool {
146        self.development.is_none() && self.production.is_none()
147    }
148}
149
150/// Settings for one branch (development or production) in a branching model
151/// settings update. `enabled` only applies to the production branch.
152#[derive(Debug, Clone, Default, Serialize)]
153pub struct BranchingModelBranchSettings {
154    #[serde(skip_serializing_if = "Option::is_none")]
155    pub name: Option<String>,
156    #[serde(skip_serializing_if = "Option::is_none")]
157    pub use_mainbranch: Option<bool>,
158    #[serde(skip_serializing_if = "Option::is_none")]
159    pub enabled: Option<bool>,
160}
161
162#[cfg(test)]
163mod tests {
164    use super::{
165        BranchingModelBranchSettings, CreateEnvironmentRequest, EnvironmentType,
166        UpdateBranchingModelRequest,
167    };
168
169    #[test]
170    fn add_deploy_key_request_serializes_key_and_label() {
171        let request = super::AddDeployKeyRequest {
172            key: "ssh-ed25519 AAAA host".to_string(),
173            label: "deploy".to_string(),
174        };
175        let json = serde_json::to_value(&request).unwrap();
176        assert_eq!(
177            json,
178            serde_json::json!({ "key": "ssh-ed25519 AAAA host", "label": "deploy" })
179        );
180    }
181
182    #[test]
183    fn create_environment_request_nests_environment_type() {
184        let request = CreateEnvironmentRequest {
185            name: "prod".to_string(),
186            environment_type: EnvironmentType {
187                name: Some("Production".to_string()),
188            },
189        };
190        let json = serde_json::to_value(&request).unwrap();
191        assert_eq!(
192            json,
193            serde_json::json!({
194                "name": "prod",
195                "environment_type": { "name": "Production" }
196            })
197        );
198    }
199
200    #[test]
201    fn update_branching_model_request_omits_unset_fields() {
202        let request = UpdateBranchingModelRequest {
203            development: Some(BranchingModelBranchSettings {
204                name: Some("develop".to_string()),
205                use_mainbranch: Some(false),
206                enabled: None,
207            }),
208            production: None,
209        };
210        let json = serde_json::to_value(&request).unwrap();
211        assert_eq!(
212            json,
213            serde_json::json!({
214                "development": { "name": "develop", "use_mainbranch": false }
215            })
216        );
217    }
218
219    #[test]
220    fn update_branching_model_request_is_empty() {
221        assert!(UpdateBranchingModelRequest::default().is_empty());
222
223        let with_production = UpdateBranchingModelRequest {
224            production: Some(BranchingModelBranchSettings::default()),
225            ..Default::default()
226        };
227        assert!(!with_production.is_empty());
228    }
229}