gitlab/api/projects/variables/
update.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::{self, NameOrId};
10use crate::api::endpoint_prelude::*;
11use crate::api::projects::variables::{ProjectVariableFilter, ProjectVariableType};
12
13/// Edit a variable of a project.
14#[derive(Debug, Builder, Clone)]
15#[builder(setter(strip_option))]
16pub struct UpdateProjectVariable<'a> {
17    /// The project to edit the variable on.
18    #[builder(setter(into))]
19    project: NameOrId<'a>,
20    /// The name of the variable.
21    #[builder(setter(into))]
22    key: Cow<'a, str>,
23    /// The value of the variable.
24    #[builder(setter(into))]
25    value: Cow<'a, str>,
26    /// The type of the variable.
27    #[builder(default)]
28    variable_type: Option<ProjectVariableType>,
29    /// Whether the variable is protected.
30    #[builder(default)]
31    protected: Option<bool>,
32    /// Whether the variable is masked.
33    #[builder(default)]
34    masked: Option<bool>,
35    /// Whether the variable is masked and hidden.
36    #[builder(default)]
37    masked_and_hidden: Option<bool>,
38    /// Whether the variable is treated as a raw string.
39    #[builder(default)]
40    raw: Option<bool>,
41    /// The environment scope of the variable.
42    #[builder(setter(into), default)]
43    environment_scope: Option<Cow<'a, str>>,
44    /// Filters to apply to the returned variables.
45    #[builder(default)]
46    filter: Option<ProjectVariableFilter<'a>>,
47    /// The description of the variable.
48    #[builder(setter(into), default)]
49    description: Option<Cow<'a, str>>,
50}
51
52impl<'a> UpdateProjectVariable<'a> {
53    /// Create a builder for the endpoint.
54    pub fn builder() -> UpdateProjectVariableBuilder<'a> {
55        UpdateProjectVariableBuilder::default()
56    }
57}
58
59impl Endpoint for UpdateProjectVariable<'_> {
60    fn method(&self) -> Method {
61        Method::PUT
62    }
63
64    fn endpoint(&self) -> Cow<'static, str> {
65        format!(
66            "projects/{}/variables/{}",
67            self.project,
68            common::path_escaped(&self.key),
69        )
70        .into()
71    }
72
73    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
74        let mut params = FormParams::default();
75
76        params
77            .push("value", &self.value)
78            .push_opt("variable_type", self.variable_type)
79            .push_opt("protected", self.protected)
80            .push_opt("masked", self.masked)
81            .push_opt("masked_and_hidden", self.masked_and_hidden)
82            .push_opt("raw", self.raw)
83            .push_opt("environment_scope", self.environment_scope.as_ref())
84            .push_opt("description", self.description.as_ref());
85
86        if let Some(filter) = self.filter.as_ref() {
87            filter.add_query(&mut params);
88        }
89
90        params.into_body()
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use http::Method;
97
98    use crate::api::projects::variables::update::{
99        ProjectVariableFilter, ProjectVariableType, UpdateProjectVariable,
100        UpdateProjectVariableBuilderError,
101    };
102    use crate::api::{self, Query};
103    use crate::test::client::{ExpectedUrl, SingleTestClient};
104
105    #[test]
106    fn all_parameters_are_needed() {
107        let err = UpdateProjectVariable::builder().build().unwrap_err();
108        crate::test::assert_missing_field!(err, UpdateProjectVariableBuilderError, "project");
109    }
110
111    #[test]
112    fn project_is_necessary() {
113        let err = UpdateProjectVariable::builder()
114            .key("testkey")
115            .value("testvalue")
116            .build()
117            .unwrap_err();
118        crate::test::assert_missing_field!(err, UpdateProjectVariableBuilderError, "project");
119    }
120
121    #[test]
122    fn key_is_necessary() {
123        let err = UpdateProjectVariable::builder()
124            .project(1)
125            .value("testvalue")
126            .build()
127            .unwrap_err();
128        crate::test::assert_missing_field!(err, UpdateProjectVariableBuilderError, "key");
129    }
130
131    #[test]
132    fn value_level_is_necessary() {
133        let err = UpdateProjectVariable::builder()
134            .project(1)
135            .key("testkey")
136            .build()
137            .unwrap_err();
138        crate::test::assert_missing_field!(err, UpdateProjectVariableBuilderError, "value");
139    }
140
141    #[test]
142    fn sufficient_parameters() {
143        UpdateProjectVariable::builder()
144            .project(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::PUT)
155            .endpoint("projects/simple%2Fproject/variables/testkey")
156            .content_type("application/x-www-form-urlencoded")
157            .body_str("value=testvalue")
158            .build()
159            .unwrap();
160        let client = SingleTestClient::new_raw(endpoint, "");
161
162        let endpoint = UpdateProjectVariable::builder()
163            .project("simple/project")
164            .key("testkey")
165            .value("testvalue")
166            .build()
167            .unwrap();
168        api::ignore(endpoint).query(&client).unwrap();
169    }
170
171    #[test]
172    fn endpoint_variable_type() {
173        let endpoint = ExpectedUrl::builder()
174            .method(Method::PUT)
175            .endpoint("projects/simple%2Fproject/variables/testkey")
176            .content_type("application/x-www-form-urlencoded")
177            .body_str(concat!("value=testvalue", "&variable_type=file"))
178            .build()
179            .unwrap();
180        let client = SingleTestClient::new_raw(endpoint, "");
181
182        let endpoint = UpdateProjectVariable::builder()
183            .project("simple/project")
184            .key("testkey")
185            .value("testvalue")
186            .variable_type(ProjectVariableType::File)
187            .build()
188            .unwrap();
189        api::ignore(endpoint).query(&client).unwrap();
190    }
191
192    #[test]
193    fn endpoint_protected() {
194        let endpoint = ExpectedUrl::builder()
195            .method(Method::PUT)
196            .endpoint("projects/simple%2Fproject/variables/testkey")
197            .content_type("application/x-www-form-urlencoded")
198            .body_str(concat!("value=testvalue", "&protected=true"))
199            .build()
200            .unwrap();
201        let client = SingleTestClient::new_raw(endpoint, "");
202
203        let endpoint = UpdateProjectVariable::builder()
204            .project("simple/project")
205            .key("testkey")
206            .value("testvalue")
207            .protected(true)
208            .build()
209            .unwrap();
210        api::ignore(endpoint).query(&client).unwrap();
211    }
212
213    #[test]
214    fn endpoint_masked() {
215        let endpoint = ExpectedUrl::builder()
216            .method(Method::PUT)
217            .endpoint("projects/simple%2Fproject/variables/testkey")
218            .content_type("application/x-www-form-urlencoded")
219            .body_str(concat!("value=testvalue", "&masked=true"))
220            .build()
221            .unwrap();
222        let client = SingleTestClient::new_raw(endpoint, "");
223
224        let endpoint = UpdateProjectVariable::builder()
225            .project("simple/project")
226            .key("testkey")
227            .value("testvalue")
228            .masked(true)
229            .build()
230            .unwrap();
231        api::ignore(endpoint).query(&client).unwrap();
232    }
233
234    #[test]
235    fn endpoint_masked_and_hidden() {
236        let endpoint = ExpectedUrl::builder()
237            .method(Method::PUT)
238            .endpoint("projects/simple%2Fproject/variables/testkey")
239            .content_type("application/x-www-form-urlencoded")
240            .body_str(concat!("value=testvalue", "&masked_and_hidden=true"))
241            .build()
242            .unwrap();
243        let client = SingleTestClient::new_raw(endpoint, "");
244
245        let endpoint = UpdateProjectVariable::builder()
246            .project("simple/project")
247            .key("testkey")
248            .value("testvalue")
249            .masked_and_hidden(true)
250            .build()
251            .unwrap();
252        api::ignore(endpoint).query(&client).unwrap();
253    }
254
255    #[test]
256    fn endpoint_raw() {
257        let endpoint = ExpectedUrl::builder()
258            .method(Method::PUT)
259            .endpoint("projects/simple%2Fproject/variables/testkey")
260            .content_type("application/x-www-form-urlencoded")
261            .body_str(concat!("value=testvalue", "&raw=true"))
262            .build()
263            .unwrap();
264        let client = SingleTestClient::new_raw(endpoint, "");
265
266        let endpoint = UpdateProjectVariable::builder()
267            .project("simple/project")
268            .key("testkey")
269            .value("testvalue")
270            .raw(true)
271            .build()
272            .unwrap();
273        api::ignore(endpoint).query(&client).unwrap();
274    }
275
276    #[test]
277    fn endpoint_environment_scope() {
278        let endpoint = ExpectedUrl::builder()
279            .method(Method::PUT)
280            .endpoint("projects/simple%2Fproject/variables/testkey")
281            .content_type("application/x-www-form-urlencoded")
282            .body_str(concat!("value=testvalue", "&environment_scope=*"))
283            .build()
284            .unwrap();
285        let client = SingleTestClient::new_raw(endpoint, "");
286
287        let endpoint = UpdateProjectVariable::builder()
288            .project("simple/project")
289            .key("testkey")
290            .value("testvalue")
291            .environment_scope("*")
292            .build()
293            .unwrap();
294        api::ignore(endpoint).query(&client).unwrap();
295    }
296
297    #[test]
298    fn endpoint_filter() {
299        let endpoint = ExpectedUrl::builder()
300            .method(Method::PUT)
301            .endpoint("projects/simple%2Fproject/variables/testkey")
302            .content_type("application/x-www-form-urlencoded")
303            .body_str(concat!(
304                "value=testvalue",
305                "&filter%5Benvironment_scope%5D=production",
306            ))
307            .build()
308            .unwrap();
309        let client = SingleTestClient::new_raw(endpoint, "");
310
311        let endpoint = UpdateProjectVariable::builder()
312            .project("simple/project")
313            .key("testkey")
314            .value("testvalue")
315            .filter(
316                ProjectVariableFilter::builder()
317                    .environment_scope("production")
318                    .build()
319                    .unwrap(),
320            )
321            .build()
322            .unwrap();
323        api::ignore(endpoint).query(&client).unwrap();
324    }
325
326    #[test]
327    fn endpoint_description() {
328        let endpoint = ExpectedUrl::builder()
329            .method(Method::PUT)
330            .endpoint("projects/simple%2Fproject/variables/testkey")
331            .content_type("application/x-www-form-urlencoded")
332            .body_str(concat!("value=testvalue", "&description=desc"))
333            .build()
334            .unwrap();
335        let client = SingleTestClient::new_raw(endpoint, "");
336
337        let endpoint = UpdateProjectVariable::builder()
338            .project("simple/project")
339            .key("testkey")
340            .value("testvalue")
341            .description("desc")
342            .build()
343            .unwrap();
344        api::ignore(endpoint).query(&client).unwrap();
345    }
346}