gitlab/api/projects/repository/commits/
commit.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::{self, NameOrId};
10use crate::api::endpoint_prelude::*;
11
12/// Query for a specific commit in a project.
13#[derive(Debug, Builder, Clone)]
14#[builder(setter(strip_option))]
15pub struct Commit<'a> {
16    /// The project to get a commit from.
17    #[builder(setter(into))]
18    project: NameOrId<'a>,
19    /// The commit to get.
20    #[builder(setter(into))]
21    commit: Cow<'a, str>,
22
23    /// Include commit stats.
24    #[builder(default)]
25    stats: Option<bool>,
26}
27
28impl<'a> Commit<'a> {
29    /// Create a builder for the endpoint.
30    pub fn builder() -> CommitBuilder<'a> {
31        CommitBuilder::default()
32    }
33}
34
35impl Endpoint for Commit<'_> {
36    fn method(&self) -> Method {
37        Method::GET
38    }
39
40    fn endpoint(&self) -> Cow<'static, str> {
41        format!(
42            "projects/{}/repository/commits/{}",
43            self.project,
44            common::path_escaped(&self.commit),
45        )
46        .into()
47    }
48
49    fn parameters(&self) -> QueryParams {
50        let mut params = QueryParams::default();
51
52        params.push_opt("stats", self.stats);
53
54        params
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use crate::api::projects::repository::commits::{Commit, CommitBuilderError};
61    use crate::api::{self, Query};
62    use crate::test::client::{ExpectedUrl, SingleTestClient};
63
64    #[test]
65    fn project_and_commit_are_necessary() {
66        let err = Commit::builder().build().unwrap_err();
67        crate::test::assert_missing_field!(err, CommitBuilderError, "project");
68    }
69
70    #[test]
71    fn project_is_necessary() {
72        let err = Commit::builder().commit("master").build().unwrap_err();
73        crate::test::assert_missing_field!(err, CommitBuilderError, "project");
74    }
75
76    #[test]
77    fn commit_is_necessary() {
78        let err = Commit::builder().project(1).build().unwrap_err();
79        crate::test::assert_missing_field!(err, CommitBuilderError, "commit");
80    }
81
82    #[test]
83    fn project_and_commit_are_sufficient() {
84        Commit::builder()
85            .project(1)
86            .commit("master")
87            .build()
88            .unwrap();
89    }
90
91    #[test]
92    fn endpoint() {
93        let endpoint = ExpectedUrl::builder().endpoint("projects/simple%2Fproject/repository/commits/0000000000000000000000000000000000000000").build().unwrap();
94        let client = SingleTestClient::new_raw(endpoint, "");
95
96        let endpoint = Commit::builder()
97            .project("simple/project")
98            .commit("0000000000000000000000000000000000000000")
99            .build()
100            .unwrap();
101        api::ignore(endpoint).query(&client).unwrap();
102    }
103
104    #[test]
105    fn endpoint_stats() {
106        let endpoint = ExpectedUrl::builder()
107            .endpoint("projects/simple%2Fproject/repository/commits/0000000000000000000000000000000000000000")
108            .add_query_params(&[("stats", "true")])
109            .build()
110            .unwrap();
111        let client = SingleTestClient::new_raw(endpoint, "");
112
113        let endpoint = Commit::builder()
114            .project("simple/project")
115            .commit("0000000000000000000000000000000000000000")
116            .stats(true)
117            .build()
118            .unwrap();
119        api::ignore(endpoint).query(&client).unwrap();
120    }
121}