Skip to main content

gitlab/api/projects/issues/notes/awards/
create.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/// Create a new award on a note on an issue on a project.
13#[derive(Debug, Builder, Clone)]
14pub struct CreateIssueNoteAward<'a> {
15    /// The project the issue belongs to.
16    #[builder(setter(into))]
17    project: NameOrId<'a>,
18    /// The issue of the note to add the award to.
19    issue: u64,
20    /// The ID of the note.
21    note: u64,
22    /// The award to give to the note (without colons).
23    #[builder(setter(into))]
24    name: Cow<'a, str>,
25}
26
27impl<'a> CreateIssueNoteAward<'a> {
28    /// Create a builder for the endpoint.
29    pub fn builder() -> CreateIssueNoteAwardBuilder<'a> {
30        CreateIssueNoteAwardBuilder::default()
31    }
32}
33
34impl Endpoint for CreateIssueNoteAward<'_> {
35    fn method(&self) -> Method {
36        Method::POST
37    }
38
39    fn endpoint(&self) -> Cow<'static, str> {
40        format!(
41            "projects/{}/issues/{}/notes/{}/award_emoji",
42            self.project, self.issue, self.note,
43        )
44        .into()
45    }
46
47    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
48        let mut params = FormParams::default();
49
50        params.push("name", self.name.as_ref());
51
52        params.into_body()
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use http::Method;
59
60    use crate::api::projects::issues::notes::awards::{
61        CreateIssueNoteAward, CreateIssueNoteAwardBuilderError,
62    };
63    use crate::api::{self, Query};
64    use crate::test::client::{ExpectedUrl, SingleTestClient};
65
66    #[test]
67    fn project_issue_note_and_name_are_necessary() {
68        let err = CreateIssueNoteAward::builder().build().unwrap_err();
69        crate::test::assert_missing_field!(err, CreateIssueNoteAwardBuilderError, "project");
70    }
71
72    #[test]
73    fn project_is_necessary() {
74        let err = CreateIssueNoteAward::builder()
75            .issue(1)
76            .note(1)
77            .name("award")
78            .build()
79            .unwrap_err();
80        crate::test::assert_missing_field!(err, CreateIssueNoteAwardBuilderError, "project");
81    }
82
83    #[test]
84    fn issue_is_necessary() {
85        let err = CreateIssueNoteAward::builder()
86            .project(1)
87            .note(1)
88            .name("award")
89            .build()
90            .unwrap_err();
91        crate::test::assert_missing_field!(err, CreateIssueNoteAwardBuilderError, "issue");
92    }
93
94    #[test]
95    fn note_is_necessary() {
96        let err = CreateIssueNoteAward::builder()
97            .project(1)
98            .issue(1)
99            .name("award")
100            .build()
101            .unwrap_err();
102        crate::test::assert_missing_field!(err, CreateIssueNoteAwardBuilderError, "note");
103    }
104
105    #[test]
106    fn name_is_necessary() {
107        let err = CreateIssueNoteAward::builder()
108            .project(1)
109            .issue(1)
110            .note(1)
111            .build()
112            .unwrap_err();
113        crate::test::assert_missing_field!(err, CreateIssueNoteAwardBuilderError, "name");
114    }
115
116    #[test]
117    fn project_issue_note_and_name_are_sufficient() {
118        CreateIssueNoteAward::builder()
119            .project(1)
120            .issue(1)
121            .note(1)
122            .name("award")
123            .build()
124            .unwrap();
125    }
126
127    #[test]
128    fn endpoint() {
129        let endpoint = ExpectedUrl::builder()
130            .method(Method::POST)
131            .endpoint("projects/simple%2Fproject/issues/1/notes/2/award_emoji")
132            .content_type("application/x-www-form-urlencoded")
133            .body_str("name=emoji")
134            .build()
135            .unwrap();
136        let client = SingleTestClient::new_raw(endpoint, "");
137
138        let endpoint = CreateIssueNoteAward::builder()
139            .project("simple/project")
140            .issue(1)
141            .note(2)
142            .name("emoji")
143            .build()
144            .unwrap();
145        api::ignore(endpoint).query(&client).unwrap();
146    }
147}