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