Skip to main content

gitlab/api/projects/pipeline_schedules/variables/
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::variables::ProjectVariableType;
12
13/// Add a variable to a pipeline schedule.
14#[derive(Debug, Builder, Clone)]
15#[builder(setter(strip_option))]
16pub struct CreatePipelineScheduleVariable<'a> {
17    /// The project to add the variable to.
18    #[builder(setter(into))]
19    project: NameOrId<'a>,
20    /// The pipeline schedule to add the variable to.
21    id: u64,
22    /// The key of the variable
23    #[builder(setter(into))]
24    key: Cow<'a, str>,
25    /// The value of a variable
26    #[builder(setter(into))]
27    value: Cow<'a, str>,
28    /// The type of the variable.
29    #[builder(default)]
30    variable_type: Option<ProjectVariableType>,
31}
32
33impl<'a> CreatePipelineScheduleVariable<'a> {
34    /// Create a builder for the endpoint.
35    pub fn builder() -> CreatePipelineScheduleVariableBuilder<'a> {
36        CreatePipelineScheduleVariableBuilder::default()
37    }
38}
39
40impl Endpoint for CreatePipelineScheduleVariable<'_> {
41    fn method(&self) -> Method {
42        Method::POST
43    }
44
45    fn endpoint(&self) -> Cow<'static, str> {
46        format!(
47            "projects/{}/pipeline_schedules/{}/variables",
48            self.project, self.id
49        )
50        .into()
51    }
52
53    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
54        let mut params = FormParams::default();
55
56        params
57            .push("key", &self.key)
58            .push("value", &self.value)
59            .push_opt("variable_type", self.variable_type);
60
61        params.into_body()
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use http::Method;
68
69    use crate::api::projects::pipeline_schedules::variables::create::{
70        CreatePipelineScheduleVariable, CreatePipelineScheduleVariableBuilderError,
71    };
72    use crate::api::projects::variables::ProjectVariableType;
73    use crate::api::{self, Query};
74    use crate::test::client::{ExpectedUrl, SingleTestClient};
75
76    #[test]
77    fn all_parameters_are_needed() {
78        let err = CreatePipelineScheduleVariable::builder()
79            .build()
80            .unwrap_err();
81        crate::test::assert_missing_field!(
82            err,
83            CreatePipelineScheduleVariableBuilderError,
84            "project",
85        );
86    }
87
88    #[test]
89    fn project_is_necessary() {
90        let err = CreatePipelineScheduleVariable::builder()
91            .id(10)
92            .key("testkey")
93            .value("testvalue")
94            .build()
95            .unwrap_err();
96        crate::test::assert_missing_field!(
97            err,
98            CreatePipelineScheduleVariableBuilderError,
99            "project",
100        );
101    }
102
103    #[test]
104    fn id_is_necessary() {
105        let err = CreatePipelineScheduleVariable::builder()
106            .project(1)
107            .key("testkey")
108            .value("testvalue")
109            .build()
110            .unwrap_err();
111        crate::test::assert_missing_field!(err, CreatePipelineScheduleVariableBuilderError, "id");
112    }
113
114    #[test]
115    fn key_is_necessary() {
116        let err = CreatePipelineScheduleVariable::builder()
117            .project(1)
118            .id(10)
119            .value("testvalue")
120            .build()
121            .unwrap_err();
122        crate::test::assert_missing_field!(err, CreatePipelineScheduleVariableBuilderError, "key");
123    }
124
125    #[test]
126    fn value_level_is_necessary() {
127        let err = CreatePipelineScheduleVariable::builder()
128            .project(1)
129            .id(10)
130            .key("testkey")
131            .build()
132            .unwrap_err();
133        crate::test::assert_missing_field!(
134            err,
135            CreatePipelineScheduleVariableBuilderError,
136            "value",
137        );
138    }
139
140    #[test]
141    fn sufficient_parameters() {
142        CreatePipelineScheduleVariable::builder()
143            .project(1)
144            .id(1)
145            .key("testkey")
146            .value("testvalue")
147            .build()
148            .unwrap();
149    }
150
151    #[test]
152    fn endpoint() {
153        let endpoint = ExpectedUrl::builder()
154            .method(Method::POST)
155            .endpoint("projects/simple%2Fproject/pipeline_schedules/10/variables")
156            .content_type("application/x-www-form-urlencoded")
157            .body_str(concat!("key=testkey", "&value=testvalue"))
158            .build()
159            .unwrap();
160        let client = SingleTestClient::new_raw(endpoint, "");
161
162        let endpoint = CreatePipelineScheduleVariable::builder()
163            .project("simple/project")
164            .id(10)
165            .key("testkey")
166            .value("testvalue")
167            .build()
168            .unwrap();
169        api::ignore(endpoint).query(&client).unwrap();
170    }
171
172    #[test]
173    fn endpoint_variable_type() {
174        let endpoint = ExpectedUrl::builder()
175            .method(Method::POST)
176            .endpoint("projects/simple%2Fproject/pipeline_schedules/10/variables")
177            .content_type("application/x-www-form-urlencoded")
178            .body_str(concat!(
179                "key=testkey",
180                "&value=testvalue",
181                "&variable_type=file"
182            ))
183            .build()
184            .unwrap();
185        let client = SingleTestClient::new_raw(endpoint, "");
186
187        let endpoint = CreatePipelineScheduleVariable::builder()
188            .project("simple/project")
189            .id(10)
190            .key("testkey")
191            .value("testvalue")
192            .variable_type(ProjectVariableType::File)
193            .build()
194            .unwrap();
195        api::ignore(endpoint).query(&client).unwrap();
196    }
197}