gitlab/api/projects/deployments/
create.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7use derive_builder::Builder;
8
9use crate::api::common::NameOrId;
10use crate::api::endpoint_prelude::*;
11use crate::api::projects::deployments::DeploymentStatus;
12
13/// Create a deployment for a project.
14#[derive(Debug, Builder, Clone)]
15pub struct CreateDeployment<'a> {
16    /// The project to edit a deployment from.
17    #[builder(setter(into))]
18    project: NameOrId<'a>,
19    /// The environment to create the deployment for.
20    #[builder(setter(into))]
21    environment: Cow<'a, str>,
22    /// The SHA of the commit being deployed.
23    #[builder(setter(into))]
24    sha: Cow<'a, str>,
25    /// The name of the branch or tag that is being deployed.
26    #[builder(setter(into))]
27    ref_: Cow<'a, str>,
28    /// Whether `ref_` is a tag or branch.
29    tag: bool,
30    /// The status of the deployment.
31    status: DeploymentStatus,
32}
33
34impl<'a> CreateDeployment<'a> {
35    /// Create a builder for the endpoint.
36    pub fn builder() -> CreateDeploymentBuilder<'a> {
37        CreateDeploymentBuilder::default()
38    }
39}
40
41impl Endpoint for CreateDeployment<'_> {
42    fn method(&self) -> Method {
43        Method::POST
44    }
45
46    fn endpoint(&self) -> Cow<'static, str> {
47        format!("projects/{}/deployments", self.project).into()
48    }
49
50    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
51        let mut params = FormParams::default();
52
53        params
54            .push("environment", &self.environment)
55            .push("sha", &self.sha)
56            .push("ref", &self.ref_)
57            .push("tag", self.tag)
58            .push("status", self.status);
59
60        params.into_body()
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use http::Method;
67
68    use crate::api::projects::deployments::{
69        CreateDeployment, CreateDeploymentBuilderError, DeploymentStatus,
70    };
71    use crate::api::{self, Query};
72    use crate::test::client::{ExpectedUrl, SingleTestClient};
73
74    #[test]
75    fn required_parameters() {
76        let err = CreateDeployment::builder().build().unwrap_err();
77        crate::test::assert_missing_field!(err, CreateDeploymentBuilderError, "project");
78    }
79
80    #[test]
81    fn project_is_necessary() {
82        let err = CreateDeployment::builder()
83            .environment("env")
84            .sha("0000000000000000000000000000000000000000")
85            .ref_("main")
86            .tag(false)
87            .status(DeploymentStatus::Success)
88            .build()
89            .unwrap_err();
90        crate::test::assert_missing_field!(err, CreateDeploymentBuilderError, "project");
91    }
92
93    #[test]
94    fn environment_is_necessary() {
95        let err = CreateDeployment::builder()
96            .project("project")
97            .sha("0000000000000000000000000000000000000000")
98            .ref_("main")
99            .tag(false)
100            .status(DeploymentStatus::Success)
101            .build()
102            .unwrap_err();
103        crate::test::assert_missing_field!(err, CreateDeploymentBuilderError, "environment");
104    }
105
106    #[test]
107    fn sha_is_necessary() {
108        let err = CreateDeployment::builder()
109            .project("project")
110            .environment("env")
111            .ref_("main")
112            .tag(false)
113            .status(DeploymentStatus::Success)
114            .build()
115            .unwrap_err();
116        crate::test::assert_missing_field!(err, CreateDeploymentBuilderError, "sha");
117    }
118
119    #[test]
120    fn ref_is_necessary() {
121        let err = CreateDeployment::builder()
122            .project("project")
123            .environment("env")
124            .sha("0000000000000000000000000000000000000000")
125            .tag(false)
126            .status(DeploymentStatus::Success)
127            .build()
128            .unwrap_err();
129        crate::test::assert_missing_field!(err, CreateDeploymentBuilderError, "ref_");
130    }
131
132    #[test]
133    fn tag_is_necessary() {
134        let err = CreateDeployment::builder()
135            .project("project")
136            .environment("env")
137            .sha("0000000000000000000000000000000000000000")
138            .ref_("main")
139            .status(DeploymentStatus::Success)
140            .build()
141            .unwrap_err();
142        crate::test::assert_missing_field!(err, CreateDeploymentBuilderError, "tag");
143    }
144
145    #[test]
146    fn status_is_necessary() {
147        let err = CreateDeployment::builder()
148            .project("project")
149            .environment("env")
150            .sha("0000000000000000000000000000000000000000")
151            .ref_("main")
152            .tag(false)
153            .build()
154            .unwrap_err();
155        crate::test::assert_missing_field!(err, CreateDeploymentBuilderError, "status");
156    }
157
158    #[test]
159    fn sufficient_parameters() {
160        CreateDeployment::builder()
161            .project("project")
162            .environment("env")
163            .sha("0000000000000000000000000000000000000000")
164            .ref_("main")
165            .tag(false)
166            .status(DeploymentStatus::Success)
167            .build()
168            .unwrap();
169    }
170
171    #[test]
172    fn endpoint() {
173        let endpoint = ExpectedUrl::builder()
174            .method(Method::POST)
175            .endpoint("projects/simple%2Fproject/deployments")
176            .content_type("application/x-www-form-urlencoded")
177            .body_str(concat!(
178                "environment=env",
179                "&sha=0000000000000000000000000000000000000000",
180                "&ref=main",
181                "&tag=false",
182                "&status=canceled",
183            ))
184            .build()
185            .unwrap();
186        let client = SingleTestClient::new_raw(endpoint, "");
187
188        let endpoint = CreateDeployment::builder()
189            .project("simple/project")
190            .environment("env")
191            .sha("0000000000000000000000000000000000000000")
192            .ref_("main")
193            .tag(false)
194            .status(DeploymentStatus::Canceled)
195            .build()
196            .unwrap();
197        api::ignore(endpoint).query(&client).unwrap();
198    }
199}