automatons_github/resource/pull_request/
branch.rs

1use std::fmt::{Display, Formatter};
2
3use serde::{Deserialize, Serialize};
4
5use crate::resource::{GitRef, GitSha, MinimalRepository};
6
7/// Pull request branch reference
8///
9/// Pull requests have a `base` branch against which they are opened, and a `head` branch that
10/// contains the changes that should be merged. The [`PullRequest`] resource references these using
11/// the [`PullRequestBranch`] data type.
12#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
13pub struct PullRequestBranch {
14    #[serde(rename = "ref")]
15    git_ref: GitRef,
16
17    #[serde(rename = "sha")]
18    git_sha: GitSha,
19
20    repo: MinimalRepository,
21}
22
23impl PullRequestBranch {
24    /// Returns the pull request branch's git ref.
25    #[cfg_attr(feature = "tracing", tracing::instrument)]
26    pub fn git_ref(&self) -> &GitRef {
27        &self.git_ref
28    }
29
30    /// Returns the pull request branch's git sha.
31    #[cfg_attr(feature = "tracing", tracing::instrument)]
32    pub fn git_sha(&self) -> &GitSha {
33        &self.git_sha
34    }
35
36    /// Returns the repository in which the pull request was created.
37    #[cfg_attr(feature = "tracing", tracing::instrument)]
38    pub fn repository(&self) -> &MinimalRepository {
39        &self.repo
40    }
41}
42
43impl Display for PullRequestBranch {
44    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
45        write!(f, "{}", self.git_ref)
46    }
47}
48
49#[cfg(test)]
50mod test {
51    use super::PullRequestBranch;
52
53    const JSON: &str = r#"
54    {
55        "ref": "main",
56        "sha": "3de05046636de664eff97823e24c92d382fa6607",
57        "repo": {
58            "id": 518377950,
59            "url": "https://api.github.com/repos/devxbots/automatons",
60            "name": "automatons"
61        }
62    }
63    "#;
64
65    #[test]
66    fn trait_deserialize() {
67        let branch: PullRequestBranch = serde_json::from_str(JSON).unwrap();
68
69        assert_eq!("main", branch.git_ref().get());
70    }
71
72    #[test]
73    fn trait_display() {
74        let branch: PullRequestBranch = serde_json::from_str(JSON).unwrap();
75
76        assert_eq!("main", branch.to_string());
77    }
78
79    #[test]
80    fn trait_send() {
81        fn assert_send<T: Send>() {}
82        assert_send::<PullRequestBranch>();
83    }
84
85    #[test]
86    fn trait_sync() {
87        fn assert_sync<T: Sync>() {}
88        assert_sync::<PullRequestBranch>();
89    }
90}