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// Deployments
55// ---------------------------------------------------------------------------
56
57/// A deployment of a release to an environment.
58///
59/// The Bitbucket deployments API is only loosely documented, so the nested
60/// shapes here are kept permissive (`Option` everywhere, raw JSON for the
61/// deep-nested parts).
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub struct Deployment {
64    pub uuid: Option<String>,
65    pub state: Option<DeploymentState>,
66    pub environment: Option<Environment>,
67    pub release: Option<serde_json::Value>,
68}
69
70/// The state of a deployment (e.g. UNDEPLOYED, IN_PROGRESS, COMPLETED).
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct DeploymentState {
73    pub name: Option<String>,
74    /// Nested status object; shape varies by state, so it is kept raw.
75    pub status: Option<serde_json::Value>,
76}
77
78// ---------------------------------------------------------------------------
79// Branching model
80// ---------------------------------------------------------------------------
81
82/// A repository's effective branching model.
83#[derive(Debug, Clone, Serialize, Deserialize)]
84pub struct BranchingModel {
85    pub development: Option<ModelBranch>,
86    pub production: Option<ModelBranch>,
87    pub branch_types: Option<Vec<BranchType>>,
88}
89
90/// The development or production branch in a branching model.
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct ModelBranch {
93    pub name: Option<String>,
94    /// The concrete branch this resolves to (only present in the effective
95    /// model, not in the settings representation).
96    pub branch: Option<crate::models::Branch>,
97    pub use_mainbranch: Option<bool>,
98}
99
100/// A branch type prefix mapping (e.g. kind `feature` → prefix `feature/`).
101#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct BranchType {
103    pub kind: Option<String>,
104    pub prefix: Option<String>,
105}
106
107/// Partial update for a repository's branching model settings. Unset fields
108/// are omitted from the request body so the corresponding settings are left
109/// untouched.
110#[derive(Debug, Clone, Default, Serialize)]
111pub struct UpdateBranchingModelRequest {
112    #[serde(skip_serializing_if = "Option::is_none")]
113    pub development: Option<BranchingModelBranchSettings>,
114    #[serde(skip_serializing_if = "Option::is_none")]
115    pub production: Option<BranchingModelBranchSettings>,
116}
117
118impl UpdateBranchingModelRequest {
119    pub fn is_empty(&self) -> bool {
120        self.development.is_none() && self.production.is_none()
121    }
122}
123
124/// Settings for one branch (development or production) in a branching model
125/// settings update. `enabled` only applies to the production branch.
126#[derive(Debug, Clone, Default, Serialize)]
127pub struct BranchingModelBranchSettings {
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub name: Option<String>,
130    #[serde(skip_serializing_if = "Option::is_none")]
131    pub use_mainbranch: Option<bool>,
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub enabled: Option<bool>,
134}
135
136#[cfg(test)]
137mod tests {
138    use super::{
139        BranchingModelBranchSettings, CreateEnvironmentRequest, EnvironmentType,
140        UpdateBranchingModelRequest,
141    };
142
143    #[test]
144    fn add_deploy_key_request_serializes_key_and_label() {
145        let request = super::AddDeployKeyRequest {
146            key: "ssh-ed25519 AAAA host".to_string(),
147            label: "deploy".to_string(),
148        };
149        let json = serde_json::to_value(&request).unwrap();
150        assert_eq!(
151            json,
152            serde_json::json!({ "key": "ssh-ed25519 AAAA host", "label": "deploy" })
153        );
154    }
155
156    #[test]
157    fn create_environment_request_nests_environment_type() {
158        let request = CreateEnvironmentRequest {
159            name: "prod".to_string(),
160            environment_type: EnvironmentType {
161                name: Some("Production".to_string()),
162            },
163        };
164        let json = serde_json::to_value(&request).unwrap();
165        assert_eq!(
166            json,
167            serde_json::json!({
168                "name": "prod",
169                "environment_type": { "name": "Production" }
170            })
171        );
172    }
173
174    #[test]
175    fn update_branching_model_request_omits_unset_fields() {
176        let request = UpdateBranchingModelRequest {
177            development: Some(BranchingModelBranchSettings {
178                name: Some("develop".to_string()),
179                use_mainbranch: Some(false),
180                enabled: None,
181            }),
182            production: None,
183        };
184        let json = serde_json::to_value(&request).unwrap();
185        assert_eq!(
186            json,
187            serde_json::json!({
188                "development": { "name": "develop", "use_mainbranch": false }
189            })
190        );
191    }
192
193    #[test]
194    fn update_branching_model_request_is_empty() {
195        assert!(UpdateBranchingModelRequest::default().is_empty());
196
197        let with_production = UpdateBranchingModelRequest {
198            production: Some(BranchingModelBranchSettings::default()),
199            ..Default::default()
200        };
201        assert!(!with_production.is_empty());
202    }
203}