Skip to main content

gitlab/api/projects/jobs/
play.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::*;
11
12/// A job variable for a manual job.
13#[derive(Debug, Builder, Clone)]
14pub struct JobVariableAttribute<'a> {
15    /// The name of the variable.
16    #[builder(setter(into))]
17    key: Cow<'a, str>,
18    /// The value of the variable.
19    #[builder(setter(into))]
20    value: Cow<'a, str>,
21}
22
23impl<'a> JobVariableAttribute<'a> {
24    /// Create a builder for a job variable.
25    pub fn builder() -> JobVariableAttributeBuilder<'a> {
26        JobVariableAttributeBuilder::default()
27    }
28
29    fn add_params<'b>(&'b self, params: &mut FormParams<'b>) {
30        params.push("job_variables_attributes[][key]", self.key.as_ref());
31        params.push("job_variables_attributes[][value]", self.value.as_ref());
32    }
33}
34
35/// Play a job.
36#[derive(Debug, Builder, Clone)]
37pub struct PlayJob<'a> {
38    /// The project which owns the job.
39    #[builder(setter(into))]
40    project: NameOrId<'a>,
41    /// The ID of the job.
42    job: u64,
43
44    /// Variables to set for the job.
45    #[builder(setter(name = "_job_variables_attributes"), default, private)]
46    job_variables_attributes: Vec<JobVariableAttribute<'a>>,
47}
48
49impl<'a> PlayJob<'a> {
50    /// Create a builder for the endpoint.
51    pub fn builder() -> PlayJobBuilder<'a> {
52        PlayJobBuilder::default()
53    }
54}
55
56impl<'a> PlayJobBuilder<'a> {
57    /// Add a job variable to the job.
58    pub fn job_variables_attribute(&mut self, attr: JobVariableAttribute<'a>) -> &mut Self {
59        self.job_variables_attributes
60            .get_or_insert_with(Vec::new)
61            .push(attr);
62        self
63    }
64
65    /// Add job variables to the job.
66    pub fn job_variables_attributes<I>(&mut self, iter: I) -> &mut Self
67    where
68        I: Iterator<Item = JobVariableAttribute<'a>>,
69    {
70        self.job_variables_attributes
71            .get_or_insert_with(Vec::new)
72            .extend(iter);
73        self
74    }
75}
76
77impl Endpoint for PlayJob<'_> {
78    fn method(&self) -> Method {
79        Method::POST
80    }
81
82    fn endpoint(&self) -> Cow<'static, str> {
83        format!("projects/{}/jobs/{}/play", self.project, self.job).into()
84    }
85
86    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
87        let mut params = FormParams::default();
88
89        self.job_variables_attributes
90            .iter()
91            .for_each(|value| value.add_params(&mut params));
92
93        params.into_body()
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use http::Method;
100
101    use crate::api::projects::jobs::{
102        JobVariableAttribute, JobVariableAttributeBuilderError, PlayJob, PlayJobBuilderError,
103    };
104    use crate::api::{self, Query};
105    use crate::test::client::{ExpectedUrl, SingleTestClient};
106
107    #[test]
108    fn key_and_value_are_needed() {
109        let err = JobVariableAttribute::builder().build().unwrap_err();
110        crate::test::assert_missing_field!(err, JobVariableAttributeBuilderError, "key");
111    }
112
113    #[test]
114    fn key_is_needed() {
115        let err = JobVariableAttribute::builder()
116            .value("value")
117            .build()
118            .unwrap_err();
119        crate::test::assert_missing_field!(err, JobVariableAttributeBuilderError, "key");
120    }
121
122    #[test]
123    fn value_is_needed() {
124        let err = JobVariableAttribute::builder()
125            .key("key")
126            .build()
127            .unwrap_err();
128        crate::test::assert_missing_field!(err, JobVariableAttributeBuilderError, "value");
129    }
130
131    #[test]
132    fn key_and_value_are_sufficient() {
133        JobVariableAttribute::builder()
134            .key("key")
135            .value("value")
136            .build()
137            .unwrap();
138    }
139
140    #[test]
141    fn project_and_job_are_needed() {
142        let err = PlayJob::builder().build().unwrap_err();
143        crate::test::assert_missing_field!(err, PlayJobBuilderError, "project");
144    }
145
146    #[test]
147    fn project_is_needed() {
148        let err = PlayJob::builder().job(1).build().unwrap_err();
149        crate::test::assert_missing_field!(err, PlayJobBuilderError, "project");
150    }
151
152    #[test]
153    fn job_is_needed() {
154        let err = PlayJob::builder().project(1).build().unwrap_err();
155        crate::test::assert_missing_field!(err, PlayJobBuilderError, "job");
156    }
157
158    #[test]
159    fn project_and_job_are_sufficient() {
160        PlayJob::builder().project(1).job(1).build().unwrap();
161    }
162
163    #[test]
164    fn endpoint() {
165        let endpoint = ExpectedUrl::builder()
166            .method(Method::POST)
167            .endpoint("projects/simple%2Fproject/jobs/1/play")
168            .content_type("application/x-www-form-urlencoded")
169            .body_str("")
170            .build()
171            .unwrap();
172        let client = SingleTestClient::new_raw(endpoint, "");
173
174        let endpoint = PlayJob::builder()
175            .project("simple/project")
176            .job(1)
177            .build()
178            .unwrap();
179        api::ignore(endpoint).query(&client).unwrap();
180    }
181
182    #[test]
183    fn endpoint_job_variable_attributes() {
184        let endpoint = ExpectedUrl::builder()
185            .method(Method::POST)
186            .endpoint("projects/simple%2Fproject/jobs/1/play")
187            .content_type("application/x-www-form-urlencoded")
188            .body_str(concat!(
189                "job_variables_attributes%5B%5D%5Bkey%5D=var1",
190                "&job_variables_attributes%5B%5D%5Bvalue%5D=value1",
191                "&job_variables_attributes%5B%5D%5Bkey%5D=var2",
192                "&job_variables_attributes%5B%5D%5Bvalue%5D=value2",
193                "&job_variables_attributes%5B%5D%5Bkey%5D=var3",
194                "&job_variables_attributes%5B%5D%5Bvalue%5D=value3",
195            ))
196            .build()
197            .unwrap();
198        let client = SingleTestClient::new_raw(endpoint, "");
199
200        let endpoint = PlayJob::builder()
201            .project("simple/project")
202            .job(1)
203            .job_variables_attribute(
204                JobVariableAttribute::builder()
205                    .key("var1")
206                    .value("value1")
207                    .build()
208                    .unwrap(),
209            )
210            .job_variables_attributes(
211                [
212                    JobVariableAttribute::builder()
213                        .key("var2")
214                        .value("value2")
215                        .build()
216                        .unwrap(),
217                    JobVariableAttribute::builder()
218                        .key("var3")
219                        .value("value3")
220                        .build()
221                        .unwrap(),
222                ]
223                .iter()
224                .cloned(),
225            )
226            .build()
227            .unwrap();
228        api::ignore(endpoint).query(&client).unwrap();
229    }
230}