gitlab/api/projects/
project.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/// Query for a specific project on an instance.
13#[derive(Debug, Builder, Clone)]
14#[builder(setter(strip_option))]
15pub struct Project<'a> {
16    /// The project to get.
17    #[builder(setter(into))]
18    project: NameOrId<'a>,
19
20    /// Include project statistics in the results.
21    #[builder(default)]
22    statistics: Option<bool>,
23    /// Include project license information in the results.
24    #[builder(default)]
25    license: Option<bool>,
26    /// Search for projects with custom attributes.
27    #[builder(default)]
28    with_custom_attributes: Option<bool>,
29}
30
31impl<'a> Project<'a> {
32    /// Create a builder for the endpoint.
33    pub fn builder() -> ProjectBuilder<'a> {
34        ProjectBuilder::default()
35    }
36}
37
38impl Endpoint for Project<'_> {
39    fn method(&self) -> Method {
40        Method::GET
41    }
42
43    fn endpoint(&self) -> Cow<'static, str> {
44        format!("projects/{}", self.project).into()
45    }
46
47    fn parameters(&self) -> QueryParams {
48        let mut params = QueryParams::default();
49
50        params
51            .push_opt("statistics", self.statistics)
52            .push_opt("license", self.license)
53            .push_opt("with_custom_attributes", self.with_custom_attributes);
54
55        params
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use crate::api::projects::{Project, ProjectBuilderError};
62    use crate::api::{self, Query};
63    use crate::test::client::{ExpectedUrl, SingleTestClient};
64
65    #[test]
66    fn project_is_necessary() {
67        let err = Project::builder().build().unwrap_err();
68        crate::test::assert_missing_field!(err, ProjectBuilderError, "project");
69    }
70
71    #[test]
72    fn project_is_sufficient() {
73        Project::builder().project(1).build().unwrap();
74    }
75
76    #[test]
77    fn endpoint() {
78        let endpoint = ExpectedUrl::builder()
79            .endpoint("projects/simple%2Fproject")
80            .build()
81            .unwrap();
82        let client = SingleTestClient::new_raw(endpoint, "");
83
84        let endpoint = Project::builder()
85            .project("simple/project")
86            .build()
87            .unwrap();
88        api::ignore(endpoint).query(&client).unwrap();
89    }
90
91    #[test]
92    fn endpoint_statistics() {
93        let endpoint = ExpectedUrl::builder()
94            .endpoint("projects/simple%2Fproject")
95            .add_query_params(&[("statistics", "true")])
96            .build()
97            .unwrap();
98        let client = SingleTestClient::new_raw(endpoint, "");
99
100        let endpoint = Project::builder()
101            .project("simple/project")
102            .statistics(true)
103            .build()
104            .unwrap();
105        api::ignore(endpoint).query(&client).unwrap();
106    }
107
108    #[test]
109    fn endpoint_license() {
110        let endpoint = ExpectedUrl::builder()
111            .endpoint("projects/simple%2Fproject")
112            .add_query_params(&[("license", "true")])
113            .build()
114            .unwrap();
115        let client = SingleTestClient::new_raw(endpoint, "");
116
117        let endpoint = Project::builder()
118            .project("simple/project")
119            .license(true)
120            .build()
121            .unwrap();
122        api::ignore(endpoint).query(&client).unwrap();
123    }
124
125    #[test]
126    fn endpoint_with_custom_attributes() {
127        let endpoint = ExpectedUrl::builder()
128            .endpoint("projects/simple%2Fproject")
129            .add_query_params(&[("with_custom_attributes", "true")])
130            .build()
131            .unwrap();
132        let client = SingleTestClient::new_raw(endpoint, "");
133
134        let endpoint = Project::builder()
135            .project("simple/project")
136            .with_custom_attributes(true)
137            .build()
138            .unwrap();
139        api::ignore(endpoint).query(&client).unwrap();
140    }
141}