gitlab/api/projects/protected_branches/
protected_branches.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 protected branches of a project.
13#[derive(Debug, Clone, Builder)]
14#[builder(setter(strip_option))]
15pub struct ProtectedBranches<'a> {
16    /// The project to query for protected branches.
17    #[builder(setter(into))]
18    project: NameOrId<'a>,
19
20    /// Name or part of the name of protected branches to be searched for.
21    ///
22    /// The search query will be escaped automatically.
23    #[builder(setter(into), default)]
24    search: Option<Cow<'a, str>>,
25}
26
27impl<'a> ProtectedBranches<'a> {
28    /// Create a builder for the endpoint.
29    pub fn builder() -> ProtectedBranchesBuilder<'a> {
30        ProtectedBranchesBuilder::default()
31    }
32}
33
34impl Endpoint for ProtectedBranches<'_> {
35    fn method(&self) -> Method {
36        Method::GET
37    }
38
39    fn endpoint(&self) -> Cow<'static, str> {
40        format!("projects/{}/protected_branches", self.project).into()
41    }
42
43    fn parameters(&self) -> QueryParams {
44        let mut params = QueryParams::default();
45
46        params.push_opt("search", self.search.as_ref());
47
48        params
49    }
50}
51
52impl Pageable for ProtectedBranches<'_> {}
53
54#[cfg(test)]
55mod tests {
56    use crate::api::projects::protected_branches::{
57        ProtectedBranches, ProtectedBranchesBuilderError,
58    };
59    use crate::api::{self, Query};
60    use crate::test::client::{ExpectedUrl, SingleTestClient};
61
62    #[test]
63    fn project_is_needed() {
64        let err = ProtectedBranches::builder().build().unwrap_err();
65        crate::test::assert_missing_field!(err, ProtectedBranchesBuilderError, "project");
66    }
67
68    #[test]
69    fn project_is_sufficient() {
70        ProtectedBranches::builder().project(1).build().unwrap();
71    }
72
73    #[test]
74    fn endpoint() {
75        let endpoint = ExpectedUrl::builder()
76            .endpoint("projects/group%2Fproject/protected_branches")
77            .build()
78            .unwrap();
79        let client = SingleTestClient::new_raw(endpoint, "");
80
81        let endpoint = ProtectedBranches::builder()
82            .project("group/project")
83            .build()
84            .unwrap();
85        api::ignore(endpoint).query(&client).unwrap();
86    }
87
88    #[test]
89    fn endpoint_search() {
90        let endpoint = ExpectedUrl::builder()
91            .endpoint("projects/group%2Fproject/protected_branches")
92            .add_query_params(&[("search", "name")])
93            .build()
94            .unwrap();
95        let client = SingleTestClient::new_raw(endpoint, "");
96
97        let endpoint = ProtectedBranches::builder()
98            .project("group/project")
99            .search("name")
100            .build()
101            .unwrap();
102        api::ignore(endpoint).query(&client).unwrap();
103    }
104}