automatons_github/resource/pull_request/
mod.rs

1use std::fmt::{Display, Formatter};
2
3use serde::{Deserialize, Serialize};
4use url::Url;
5
6use crate::id;
7
8pub use self::branch::PullRequestBranch;
9
10mod branch;
11
12id!(
13    /// Pull request id
14    ///
15    /// The [`PullRequestId`] is a unique, numerical id that is used to interact with a pull request
16    /// through [GitHub's REST API](https://docs.github.com/en/rest).
17    PullRequestId
18);
19
20id!(
21    /// Pull request number
22    ///
23    /// Every [`PullRequest`] has a unique, human-readable, monotonically increasing number assigned
24    /// to it. This number identifies the pull request on GitHub's website.
25    PullRequestNumber
26);
27
28/// Pull request
29///
30/// Pull requests are a feature of GitHub to merge two branches. Users can create, review, and merge
31/// pull requests using GitHub's platform. Each pull request has a unique `id`, a human-readable
32/// `number`, and references to the two branches.
33#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
34pub struct PullRequest {
35    id: PullRequestId,
36    number: PullRequestNumber,
37    url: Url,
38    head: PullRequestBranch,
39    base: PullRequestBranch,
40}
41
42impl PullRequest {
43    /// Returns the pull request's id.
44    #[cfg_attr(feature = "tracing", tracing::instrument)]
45    pub fn id(&self) -> PullRequestId {
46        self.id
47    }
48
49    /// Returns the pull request's number.
50    #[cfg_attr(feature = "tracing", tracing::instrument)]
51    pub fn number(&self) -> PullRequestNumber {
52        self.number
53    }
54
55    /// Returns the API endpoint to query the pull request.
56    #[cfg_attr(feature = "tracing", tracing::instrument)]
57    pub fn url(&self) -> &Url {
58        &self.url
59    }
60
61    /// Returns the pull request's head branch
62    #[cfg_attr(feature = "tracing", tracing::instrument)]
63    pub fn head(&self) -> &PullRequestBranch {
64        &self.head
65    }
66
67    /// Returns the pull request's base branch
68    #[cfg_attr(feature = "tracing", tracing::instrument)]
69    pub fn base(&self) -> &PullRequestBranch {
70        &self.base
71    }
72}
73
74impl Display for PullRequest {
75    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
76        write!(f, "#{}", self.number)
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::PullRequest;
83
84    #[test]
85    fn trait_deserialize() {
86        let pr: PullRequest = serde_json::from_str(include_str!(
87            "../../../tests/fixtures/resource/pull_request.json"
88        ))
89        .unwrap();
90
91        assert_eq!(27, pr.number().get());
92    }
93
94    #[test]
95    fn trait_display() {
96        let pr: PullRequest = serde_json::from_str(include_str!(
97            "../../../tests/fixtures/resource/pull_request.json"
98        ))
99        .unwrap();
100
101        assert_eq!("#27", pr.to_string());
102    }
103
104    #[test]
105    fn trait_send() {
106        fn assert_send<T: Send>() {}
107        assert_send::<PullRequest>();
108    }
109
110    #[test]
111    fn trait_sync() {
112        fn assert_sync<T: Sync>() {}
113        assert_sync::<PullRequest>();
114    }
115}