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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/// A builder pattern struct for constructing an Octocrab request to update a
/// pull request.
#[derive(serde::Serialize)]
pub struct UpdatePullRequestBuilder<'octo, 'b> {
    #[serde(skip)]
    handler: &'b super::PullRequestHandler<'octo>,
    pull_number: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    title: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    base: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    state: Option<crate::params::pulls::State>,
    #[serde(skip_serializing_if = "Option::is_none")]
    body: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    maintainer_can_modify: Option<bool>,
}

impl<'octo, 'b> UpdatePullRequestBuilder<'octo, 'b> {
    pub(crate) fn new(
        handler: &'b super::PullRequestHandler<'octo>,
        pull_number: impl Into<u64>,
    ) -> Self {
        Self {
            handler,
            pull_number: pull_number.into(),
            title: None,
            base: None,
            state: None,
            body: None,
            maintainer_can_modify: None,
        }
    }

    /// The contents of the pull request.
    pub fn body<A: Into<String>>(mut self, body: impl Into<Option<A>>) -> Self {
        self.body = body.into().map(A::into);
        self
    }

    /// The contents of the pull request.
    pub fn title<A: Into<String>>(mut self, title: impl Into<Option<A>>) -> Self {
        self.title = title.into().map(A::into);
        self
    }

    /// The contents of the pull request.
    pub fn base<A: Into<String>>(mut self, base: impl Into<Option<A>>) -> Self {
        self.base = base.into().map(A::into);
        self
    }

    /// The contents of the pull request.
    pub fn state<A: Into<crate::params::pulls::State>>(
        mut self,
        state: impl Into<Option<A>>,
    ) -> Self {
        self.state = state.into().map(A::into);
        self
    }

    /// Indicates whether `maintainers` can modify the pull request.
    pub fn maintainer_can_modify(mut self, maintainer_can_modify: impl Into<Option<bool>>) -> Self {
        self.maintainer_can_modify = maintainer_can_modify.into();
        self
    }

    /// Sends the request to update the pull request.
    pub async fn send(self) -> crate::Result<crate::models::pulls::PullRequest> {
        let route = format!(
            "/repos/{owner}/{repo}/pulls/{pr}",
            owner = self.handler.owner,
            repo = self.handler.repo,
            pr = self.pull_number,
        );

        self.handler.http_patch(route, Some(&self)).await
    }
}

#[cfg(test)]
mod tests {

    #[tokio::test]
    async fn serialize() {
        let octocrab = crate::Octocrab::default();
        let handler = octocrab.pulls("rust-lang", "rust");
        let list = handler
            .update(1)
            .title("title")
            .body(String::from("testing..."))
            .state(crate::params::pulls::State::Open)
            .maintainer_can_modify(true);

        assert_eq!(
            serde_json::to_value(list).unwrap(),
            serde_json::json!({
                "pull_number": 1,
                "title": "title",
                "body": "testing...",
                "state": "open",
                "maintainer_can_modify": true,
            })
        )
    }
}