gitlab/api/projects/repository/commits/
refs.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::*, ParamValue};
11
12/// Commit reference types
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14#[non_exhaustive]
15pub enum CommitRefsType {
16    /// Scope branch
17    Branch,
18    /// Scope tag
19    Tag,
20    /// Scope all
21    All,
22}
23
24impl CommitRefsType {
25    fn as_str(self) -> &'static str {
26        match self {
27            CommitRefsType::Branch => "branch",
28            CommitRefsType::Tag => "tag",
29            CommitRefsType::All => "all",
30        }
31    }
32}
33
34impl ParamValue<'static> for CommitRefsType {
35    fn as_value(&self) -> Cow<'static, str> {
36        self.as_str().into()
37    }
38}
39
40/// Post a comment on a specific commit in a project.
41#[derive(Debug, Builder, Clone)]
42#[builder(setter(strip_option))]
43pub struct CommitReferences<'a> {
44    /// The project to get a commit from.
45    #[builder(setter(into))]
46    project: NameOrId<'a>,
47    /// The commit sha
48    #[builder(setter(into))]
49    sha: Cow<'a, str>,
50    /// The ref types
51    #[builder(default)]
52    type_: Option<CommitRefsType>,
53}
54
55impl<'a> CommitReferences<'a> {
56    /// Create a builder for the endpoint.
57    pub fn builder() -> CommitReferencesBuilder<'a> {
58        CommitReferencesBuilder::default()
59    }
60}
61
62impl Endpoint for CommitReferences<'_> {
63    fn method(&self) -> Method {
64        Method::GET
65    }
66
67    fn endpoint(&self) -> Cow<'static, str> {
68        format!(
69            "projects/{}/repository/commits/{}/refs",
70            self.project, self.sha,
71        )
72        .into()
73    }
74
75    fn parameters(&self) -> QueryParams {
76        let mut params = QueryParams::default();
77
78        params.push_opt("type", self.type_);
79
80        params
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use http::Method;
87
88    use crate::api::projects::repository::commits::refs::{
89        CommitReferences, CommitReferencesBuilderError,
90    };
91    use crate::api::{self, Query};
92    use crate::test::client::{ExpectedUrl, SingleTestClient};
93
94    use super::CommitRefsType;
95
96    #[test]
97    fn commit_refs_type_as_str() {
98        let items = &[
99            (CommitRefsType::Branch, "branch"),
100            (CommitRefsType::Tag, "tag"),
101            (CommitRefsType::All, "all"),
102        ];
103
104        for (i, s) in items {
105            assert_eq!(i.as_str(), *s);
106        }
107    }
108
109    #[test]
110    fn project_is_necessary() {
111        let err = CommitReferences::builder()
112            .sha("0000000000000000000000000000000000000000")
113            .build()
114            .unwrap_err();
115        crate::test::assert_missing_field!(err, CommitReferencesBuilderError, "project");
116    }
117
118    #[test]
119    fn sha_is_necessary() {
120        let err = CommitReferences::builder().project(1).build().unwrap_err();
121        crate::test::assert_missing_field!(err, CommitReferencesBuilderError, "sha");
122    }
123
124    #[test]
125    fn project_and_sha_are_sufficient() {
126        CommitReferences::builder()
127            .project(1)
128            .sha("0000000000000000000000000000000000000000")
129            .build()
130            .unwrap();
131    }
132
133    #[test]
134    fn endpoint() {
135        let endpoint = ExpectedUrl::builder()
136            .method(Method::GET)
137            .endpoint("projects/simple%2Fproject/repository/commits/0000000000000000000000000000000000000000/refs")
138            .build()
139            .unwrap();
140        let client = SingleTestClient::new_raw(endpoint, "");
141
142        let endpoint = CommitReferences::builder()
143            .project("simple/project")
144            .sha("0000000000000000000000000000000000000000")
145            .build()
146            .unwrap();
147        api::ignore(endpoint).query(&client).unwrap();
148    }
149
150    #[test]
151    fn endpoint_type() {
152        let endpoint = ExpectedUrl::builder()
153            .method(Method::GET)
154            .endpoint("projects/simple%2Fproject/repository/commits/0000000000000000000000000000000000000000/refs")
155            .add_query_params(&[("type", "all")])
156            .build()
157            .unwrap();
158        let client = SingleTestClient::new_raw(endpoint, "");
159
160        let endpoint = CommitReferences::builder()
161            .project("simple/project")
162            .sha("0000000000000000000000000000000000000000")
163            .type_(CommitRefsType::All)
164            .build()
165            .unwrap();
166        api::ignore(endpoint).query(&client).unwrap();
167    }
168}