hubcaps_ex/
branches.rs

1//! Repo branches interface
2//!
3//! For more information, visit the official
4//! [Github docs](https://developer.github.com/v3/repos/branches/)
5use serde::{Deserialize, Serialize};
6
7use crate::{Future, Github, MediaType, Stream};
8
9/// reference to gists associated with a github user
10pub struct Branches {
11    github: Github,
12    owner: String,
13    repo: String,
14}
15
16impl Branches {
17    #[doc(hidden)]
18    pub fn new<U, R>(github: Github, owner: U, repo: R) -> Self
19    where
20        U: Into<String>,
21        R: Into<String>,
22    {
23        Branches {
24            github,
25            owner: owner.into(),
26            repo: repo.into(),
27        }
28    }
29
30    /// list of branches for this repo
31    pub fn list(&self) -> Future<Vec<Branch>> {
32        self.github.get(&format!(
33            "/repos/{owner}/{repo}/branches",
34            owner = self.owner,
35            repo = self.repo
36        ))
37    }
38
39    /// provides an stream over branches for this repo
40    pub fn iter(&self) -> Stream<Branch> {
41        self.github.get_stream(&format!(
42            "/repos/{owner}/{repo}/branches",
43            owner = self.owner,
44            repo = self.repo
45        ))
46    }
47
48    /// gets a branch for this repo by name
49    pub fn get<B>(&self, branch: B) -> Future<Branch>
50    where
51        B: Into<String>,
52    {
53        self.github.get(&format!(
54            "/repos/{owner}/{repo}/branches/{branch}",
55            owner = self.owner,
56            repo = self.repo,
57            branch = branch.into()
58        ))
59    }
60
61    /// update branch production for a given branch
62    ///
63    /// https://developer.github.com/v3/repos/branches/#update-branch-protection
64    pub fn protection<B>(&self, branch: B, pro: &Protection) -> Future<ProtectionState>
65    where
66        B: Into<String>,
67    {
68        self.github.put_media(
69            &format!(
70                "/repos/{owner}/{repo}/branches/{branch}/protection",
71                owner = self.owner,
72                repo = self.repo,
73                branch = branch.into()
74            ),
75            json!(pro),
76            MediaType::Preview("luke-cage"),
77        )
78    }
79}
80
81// representations
82
83#[derive(Debug, Deserialize)]
84pub struct Branch {
85    pub name: String,
86    pub protected: Option<bool>,
87    pub protection_url: Option<String>,
88    // todo: commit ref
89}
90
91#[derive(Debug, Deserialize, Serialize)]
92pub struct ProtectionState {
93    pub required_status_checks: Option<StatusChecks>,
94    pub enforce_admins: Option<EnforceAdmins>,
95    //pub required_pull_request_reviews: Option<RequiredPullRequestReviews>,
96    //pub restrictions: Option<Restrictions>,
97}
98
99#[derive(Debug, Deserialize, Serialize)]
100pub struct EnforceAdmins {
101    pub url: String,
102    pub enabled: bool,
103}
104
105#[derive(Debug, Deserialize, Serialize)]
106pub struct Protection {
107    pub required_status_checks: Option<StatusChecks>,
108    pub enforce_admins: bool,
109    pub required_pull_request_reviews: Option<RequiredPullRequestReviews>,
110    pub restrictions: Option<Restrictions>,
111}
112
113#[derive(Debug, Deserialize, Serialize)]
114pub struct Restrictions {
115    pub users: Vec<String>,
116    pub teams: Vec<String>,
117}
118
119#[derive(Debug, Deserialize, Serialize)]
120pub struct RequiredPullRequestReviews {
121    #[serde(skip_serializing_if = "Option::is_none")]
122    pub dismissal_restrictions: Option<Restrictions>,
123    pub dismiss_stale_reviews: bool,
124    pub require_code_owner_reviews: bool,
125    pub required_approving_review_count: u8,
126}
127
128#[derive(Debug, Deserialize, Serialize)]
129pub struct StatusChecks {
130    pub strict: bool,
131    pub contexts: Vec<String>,
132}