gitlab/api/projects/pipelines/
jobs.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 std::collections::BTreeSet;
8
9use derive_builder::Builder;
10
11use crate::api::common::NameOrId;
12use crate::api::endpoint_prelude::*;
13use crate::api::projects::jobs::JobScope;
14
15/// Query for jobs within a pipeline.
16#[derive(Debug, Builder, Clone)]
17#[builder(setter(strip_option))]
18pub struct PipelineJobs<'a> {
19    /// The project to query for the pipeline.
20    #[builder(setter(into))]
21    project: NameOrId<'a>,
22    /// The ID of the pipeline.
23    pipeline: u64,
24
25    /// The scopes to filter jobs by.
26    #[builder(setter(name = "_scopes"), default, private)]
27    scopes: BTreeSet<JobScope>,
28    /// Include retried jobs in the response.
29    #[builder(default)]
30    include_retried: Option<bool>,
31}
32
33impl<'a> PipelineJobs<'a> {
34    /// Create a builder for the endpoint.
35    pub fn builder() -> PipelineJobsBuilder<'a> {
36        PipelineJobsBuilder::default()
37    }
38}
39
40impl PipelineJobsBuilder<'_> {
41    /// Filter jobs by a scope.
42    pub fn scope(&mut self, scope: JobScope) -> &mut Self {
43        self.scopes.get_or_insert_with(BTreeSet::new).insert(scope);
44        self
45    }
46
47    /// Filter jobs by a set of scopes.
48    pub fn scopes<I>(&mut self, scopes: I) -> &mut Self
49    where
50        I: Iterator<Item = JobScope>,
51    {
52        self.scopes.get_or_insert_with(BTreeSet::new).extend(scopes);
53        self
54    }
55}
56
57impl Endpoint for PipelineJobs<'_> {
58    fn method(&self) -> Method {
59        Method::GET
60    }
61
62    fn endpoint(&self) -> Cow<'static, str> {
63        format!("projects/{}/pipelines/{}/jobs", self.project, self.pipeline).into()
64    }
65
66    fn parameters(&self) -> QueryParams {
67        let mut params = QueryParams::default();
68
69        params
70            .extend(self.scopes.iter().map(|&value| ("scope[]", value)))
71            .push_opt("include_retried", self.include_retried);
72
73        params
74    }
75}
76
77impl Pageable for PipelineJobs<'_> {}
78
79#[cfg(test)]
80mod tests {
81    use crate::api::projects::jobs::JobScope;
82    use crate::api::projects::pipelines::{PipelineJobs, PipelineJobsBuilderError};
83    use crate::api::{self, Query};
84    use crate::test::client::{ExpectedUrl, SingleTestClient};
85
86    #[test]
87    fn project_and_pipeline_are_needed() {
88        let err = PipelineJobs::builder().build().unwrap_err();
89        crate::test::assert_missing_field!(err, PipelineJobsBuilderError, "project");
90    }
91
92    #[test]
93    fn project_is_needed() {
94        let err = PipelineJobs::builder().pipeline(1).build().unwrap_err();
95        crate::test::assert_missing_field!(err, PipelineJobsBuilderError, "project");
96    }
97
98    #[test]
99    fn pipeline_is_needed() {
100        let err = PipelineJobs::builder().project(1).build().unwrap_err();
101        crate::test::assert_missing_field!(err, PipelineJobsBuilderError, "pipeline");
102    }
103
104    #[test]
105    fn project_and_pipeline_are_sufficient() {
106        PipelineJobs::builder()
107            .project(1)
108            .pipeline(1)
109            .build()
110            .unwrap();
111    }
112
113    #[test]
114    fn endpoint() {
115        let endpoint = ExpectedUrl::builder()
116            .endpoint("projects/simple%2Fproject/pipelines/1/jobs")
117            .build()
118            .unwrap();
119        let client = SingleTestClient::new_raw(endpoint, "");
120
121        let endpoint = PipelineJobs::builder()
122            .project("simple/project")
123            .pipeline(1)
124            .build()
125            .unwrap();
126        api::ignore(endpoint).query(&client).unwrap();
127    }
128
129    #[test]
130    fn endpoint_scopes() {
131        let endpoint = ExpectedUrl::builder()
132            .endpoint("projects/1/pipelines/1/jobs")
133            .add_query_params(&[("scope[]", "created"), ("scope[]", "success")])
134            .build()
135            .unwrap();
136        let client = SingleTestClient::new_raw(endpoint, "");
137
138        let endpoint = PipelineJobs::builder()
139            .project(1)
140            .pipeline(1)
141            .scope(JobScope::Created)
142            .scopes([JobScope::Created, JobScope::Success].iter().cloned())
143            .build()
144            .unwrap();
145        api::ignore(endpoint).query(&client).unwrap();
146    }
147
148    #[test]
149    fn endpoint_include_retried() {
150        let endpoint = ExpectedUrl::builder()
151            .endpoint("projects/1/pipelines/1/jobs")
152            .add_query_params(&[("include_retried", "true")])
153            .build()
154            .unwrap();
155        let client = SingleTestClient::new_raw(endpoint, "");
156
157        let endpoint = PipelineJobs::builder()
158            .project(1)
159            .pipeline(1)
160            .include_retried(true)
161            .build()
162            .unwrap();
163        api::ignore(endpoint).query(&client).unwrap();
164    }
165}