1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use super::*;

#[derive(serde::Serialize)]
pub struct CreateCommentBuilder<'octo, 'r> {
    #[serde(skip)]
    handler: &'r super::CommitHandler<'octo>,
    sha: String,
    body: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    path: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    position: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    line: Option<u64>,
}

impl<'octo, 'r> CreateCommentBuilder<'octo, 'r> {
    pub(crate) fn new(handler: &'r super::CommitHandler<'octo>, sha: String, body: String) -> Self {
        Self {
            handler,
            sha,
            body,
            path: None,
            position: None,
            line: None,
        }
    }

    /// Sends the actual request.
    pub async fn send(self) -> crate::Result<models::commits::Comment> {
        let route = format!(
            "/repos/{owner}/{repo}/commits/{commit_sha}/comments",
            owner = self.handler.owner,
            repo = self.handler.repo,
            commit_sha = self.sha,
        );

        self.handler.crab.post(route, Some(&self)).await
    }

    /// Relative path of the file to comment on.
    ///
    /// Required if you provide position.
    ///
    /// For example, if you want to comment on a line in the file
    /// `lib/octocat.rb`, you would provide `lib/octocat.rb`.
    pub fn path<A: Into<String>>(mut self, path: impl Into<Option<A>>) -> Self {
        self.path = path.into().map(A::into);
        self
    }

    pub fn position(mut self, position: impl Into<Option<u64>>) -> Self {
        self.position = position.into();
        self
    }

    pub fn line(mut self, line: impl Into<Option<u64>>) -> Self {
        self.line = line.into();
        self
    }
}

#[cfg(test)]
mod tests {

    #[tokio::test]
    async fn serialize() {
        let octocrab = crate::Octocrab::default();
        let handler = octocrab.commits("owner", "repo");
        let list = handler
            .create_comment("95b3b039e71659a401ef39e86bab691ab6ce5fe5", "boo boo")
            .path("lib/octocat.rb")
            .position(10)
            .line(1);

        assert_eq!(
            serde_json::to_value(list).unwrap(),
            serde_json::json!({
                "sha": "95b3b039e71659a401ef39e86bab691ab6ce5fe5",
                "body": "boo boo",
                "path": "lib/octocat.rb",
                "position": 10,
                "line": 1,
            })
        )
    }
}