gitlab/api/projects/repository/commits/
comment.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::*;
11use crate::api::ParamValue;
12
13/// Line types within a diff.
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15#[non_exhaustive]
16pub enum LineType {
17    /// A line added in the diff.
18    New,
19    /// A line removed in the diff.
20    Old,
21}
22
23impl LineType {
24    fn as_str(self) -> &'static str {
25        match self {
26            LineType::New => "new",
27            LineType::Old => "old",
28        }
29    }
30}
31
32impl ParamValue<'static> for LineType {
33    fn as_value(&self) -> Cow<'static, str> {
34        self.as_str().into()
35    }
36}
37
38/// Post a comment on a specific commit in a project.
39#[derive(Debug, Builder, Clone)]
40#[builder(setter(strip_option))]
41pub struct CommentOnCommit<'a> {
42    /// The project to get a commit from.
43    #[builder(setter(into))]
44    project: NameOrId<'a>,
45    /// The commit to comment on.
46    #[builder(setter(into))]
47    commit: Cow<'a, str>,
48    /// The text of the comment.
49    #[builder(setter(into))]
50    note: Cow<'a, str>,
51
52    /// The path to comment on.
53    #[builder(setter(into), default)]
54    path: Option<Cow<'a, str>>,
55    /// The line within the path to comment on.
56    #[builder(default)]
57    line: Option<u64>,
58    /// Set the line type to comment on.
59    #[builder(default)]
60    line_type: Option<LineType>,
61}
62
63impl<'a> CommentOnCommit<'a> {
64    /// Create a builder for the endpoint.
65    pub fn builder() -> CommentOnCommitBuilder<'a> {
66        CommentOnCommitBuilder::default()
67    }
68}
69
70impl Endpoint for CommentOnCommit<'_> {
71    fn method(&self) -> Method {
72        Method::POST
73    }
74
75    fn endpoint(&self) -> Cow<'static, str> {
76        format!(
77            "projects/{}/repository/commits/{}/comments",
78            self.project,
79            common::path_escaped(&self.commit),
80        )
81        .into()
82    }
83
84    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
85        let mut params = FormParams::default();
86
87        params
88            .push("note", &self.note)
89            .push_opt("path", self.path.as_ref())
90            .push_opt("line", self.line)
91            .push_opt("line_type", self.line_type);
92
93        params.into_body()
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use http::Method;
100
101    use crate::api::projects::repository::commits::{
102        CommentOnCommit, CommentOnCommitBuilderError, LineType,
103    };
104    use crate::api::{self, Query};
105    use crate::test::client::{ExpectedUrl, SingleTestClient};
106
107    #[test]
108    fn line_type_as_str() {
109        let items = &[(LineType::New, "new"), (LineType::Old, "old")];
110
111        for (i, s) in items {
112            assert_eq!(i.as_str(), *s);
113        }
114    }
115
116    #[test]
117    fn project_commit_and_note_are_necessary() {
118        let err = CommentOnCommit::builder().build().unwrap_err();
119        crate::test::assert_missing_field!(err, CommentOnCommitBuilderError, "project");
120    }
121
122    #[test]
123    fn project_is_necessary() {
124        let err = CommentOnCommit::builder()
125            .commit("master")
126            .note("note")
127            .build()
128            .unwrap_err();
129        crate::test::assert_missing_field!(err, CommentOnCommitBuilderError, "project");
130    }
131
132    #[test]
133    fn commit_is_necessary() {
134        let err = CommentOnCommit::builder()
135            .project(1)
136            .note("note")
137            .build()
138            .unwrap_err();
139        crate::test::assert_missing_field!(err, CommentOnCommitBuilderError, "commit");
140    }
141
142    #[test]
143    fn note_is_necessary() {
144        let err = CommentOnCommit::builder()
145            .project(1)
146            .commit("master")
147            .build()
148            .unwrap_err();
149        crate::test::assert_missing_field!(err, CommentOnCommitBuilderError, "note");
150    }
151
152    #[test]
153    fn project_commit_and_note_are_sufficient() {
154        CommentOnCommit::builder()
155            .project(1)
156            .commit("master")
157            .note("note")
158            .build()
159            .unwrap();
160    }
161
162    #[test]
163    fn endpoint() {
164        let endpoint = ExpectedUrl::builder()
165            .method(Method::POST)
166            .endpoint("projects/simple%2Fproject/repository/commits/0000000000000000000000000000000000000000/comments")
167            .content_type("application/x-www-form-urlencoded")
168            .body_str("note=comment+content")
169            .build()
170            .unwrap();
171        let client = SingleTestClient::new_raw(endpoint, "");
172
173        let endpoint = CommentOnCommit::builder()
174            .project("simple/project")
175            .commit("0000000000000000000000000000000000000000")
176            .note("comment content")
177            .build()
178            .unwrap();
179        api::ignore(endpoint).query(&client).unwrap();
180    }
181
182    #[test]
183    fn endpoint_path() {
184        let endpoint = ExpectedUrl::builder()
185            .method(Method::POST)
186            .endpoint("projects/simple%2Fproject/repository/commits/0000000000000000000000000000000000000000/comments")
187            .content_type("application/x-www-form-urlencoded")
188            .body_str(concat!(
189                "note=comment+content",
190                "&path=path%2Fto%2Ffile",
191            ))
192            .build()
193            .unwrap();
194        let client = SingleTestClient::new_raw(endpoint, "");
195
196        let endpoint = CommentOnCommit::builder()
197            .project("simple/project")
198            .commit("0000000000000000000000000000000000000000")
199            .note("comment content")
200            .path("path/to/file")
201            .build()
202            .unwrap();
203        api::ignore(endpoint).query(&client).unwrap();
204    }
205
206    #[test]
207    fn endpoint_line() {
208        let endpoint = ExpectedUrl::builder()
209            .method(Method::POST)
210            .endpoint("projects/simple%2Fproject/repository/commits/0000000000000000000000000000000000000000/comments")
211            .content_type("application/x-www-form-urlencoded")
212            .body_str(concat!(
213                "note=comment+content",
214                "&line=1",
215            ))
216            .build()
217            .unwrap();
218        let client = SingleTestClient::new_raw(endpoint, "");
219
220        let endpoint = CommentOnCommit::builder()
221            .project("simple/project")
222            .commit("0000000000000000000000000000000000000000")
223            .note("comment content")
224            .line(1)
225            .build()
226            .unwrap();
227        api::ignore(endpoint).query(&client).unwrap();
228    }
229
230    #[test]
231    fn endpoint_line_type() {
232        let endpoint = ExpectedUrl::builder()
233            .method(Method::POST)
234            .endpoint("projects/simple%2Fproject/repository/commits/0000000000000000000000000000000000000000/comments")
235            .content_type("application/x-www-form-urlencoded")
236            .body_str(concat!(
237                "note=comment+content",
238                "&line_type=new",
239            ))
240            .build()
241            .unwrap();
242        let client = SingleTestClient::new_raw(endpoint, "");
243
244        let endpoint = CommentOnCommit::builder()
245            .project("simple/project")
246            .commit("0000000000000000000000000000000000000000")
247            .note("comment content")
248            .line_type(LineType::New)
249            .build()
250            .unwrap();
251        api::ignore(endpoint).query(&client).unwrap();
252    }
253}