Skip to main content

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