gitlab/api/projects/repository/branches/
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 for a specific branch in a project.
13#[derive(Debug, Builder, Clone)]
14#[builder(setter(strip_option))]
15pub struct Branches<'a> {
16    /// The project to get a branch from.
17    #[builder(setter(into))]
18    project: NameOrId<'a>,
19    /// Filter branches by a search query.
20    ///
21    /// The `^` and `$` anchors are supported to search for "starts with" and "ends with"
22    /// operators.
23    #[builder(setter(into), default)]
24    search: Option<Cow<'a, str>>,
25    /// Filter branches matching an [`re2`][re2] regular expression.
26    ///
27    /// [re2]: https://github.com/google/re2/wiki/Syntax
28    #[builder(setter(into), default)]
29    regex: Option<Cow<'a, str>>,
30}
31
32impl<'a> Branches<'a> {
33    /// Create a builder for the endpoint.
34    pub fn builder() -> BranchesBuilder<'a> {
35        BranchesBuilder::default()
36    }
37}
38
39impl Endpoint for Branches<'_> {
40    fn method(&self) -> Method {
41        Method::GET
42    }
43
44    fn endpoint(&self) -> Cow<'static, str> {
45        format!("projects/{}/repository/branches", self.project).into()
46    }
47
48    fn parameters(&self) -> QueryParams {
49        let mut params = QueryParams::default();
50
51        params
52            .push_opt("search", self.search.as_ref())
53            .push_opt("regex", self.regex.as_ref());
54
55        params
56    }
57}
58
59impl Pageable for Branches<'_> {}
60
61#[cfg(test)]
62mod tests {
63    use crate::api::projects::repository::branches::{Branches, BranchesBuilderError};
64    use crate::api::{self, Query};
65    use crate::test::client::{ExpectedUrl, SingleTestClient};
66
67    #[test]
68    fn project_is_necessary() {
69        let err = Branches::builder().build().unwrap_err();
70        crate::test::assert_missing_field!(err, BranchesBuilderError, "project");
71    }
72
73    #[test]
74    fn project_is_sufficient() {
75        Branches::builder().project(1).build().unwrap();
76    }
77
78    #[test]
79    fn endpoint() {
80        let endpoint = ExpectedUrl::builder()
81            .endpoint("projects/simple%2Fproject/repository/branches")
82            .build()
83            .unwrap();
84        let client = SingleTestClient::new_raw(endpoint, "");
85
86        let endpoint = Branches::builder()
87            .project("simple/project")
88            .build()
89            .unwrap();
90        api::ignore(endpoint).query(&client).unwrap();
91    }
92
93    #[test]
94    fn endpoint_search() {
95        let endpoint = ExpectedUrl::builder()
96            .endpoint("projects/simple%2Fproject/repository/branches")
97            .add_query_params(&[("search", "query")])
98            .build()
99            .unwrap();
100        let client = SingleTestClient::new_raw(endpoint, "");
101
102        let endpoint = Branches::builder()
103            .project("simple/project")
104            .search("query")
105            .build()
106            .unwrap();
107        api::ignore(endpoint).query(&client).unwrap();
108    }
109
110    #[test]
111    fn endpoint_regex() {
112        let endpoint = ExpectedUrl::builder()
113            .endpoint("projects/simple%2Fproject/repository/branches")
114            .add_query_params(&[("regex", "query")])
115            .build()
116            .unwrap();
117        let client = SingleTestClient::new_raw(endpoint, "");
118
119        let endpoint = Branches::builder()
120            .project("simple/project")
121            .regex("query")
122            .build()
123            .unwrap();
124        api::ignore(endpoint).query(&client).unwrap();
125    }
126}