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