jinxapi_github/v1_1_4/request/
repos_update_branch_protection.rs

1//! Update branch protection
2//! 
3//! Protected branches are available in public repositories with GitHub Free and GitHub Free for organizations, and in public and private repositories with GitHub Pro, GitHub Team, GitHub Enterprise Cloud, and GitHub Enterprise Server. For more information, see [GitHub's products](https://docs.github.com/github/getting-started-with-github/githubs-products) in the GitHub Help documentation.
4//! 
5//! Protecting a branch requires admin or owner permissions to the repository.
6//! 
7//! **Note**: Passing new arrays of `users` and `teams` replaces their previous values.
8//! 
9//! **Note**: The list of users, apps, and teams in total is limited to 100 items.
10//! 
11//! [API method documentation](https://docs.github.com/rest/reference/repos#update-branch-protection)
12
13pub struct Content<Body>
14{
15    body: Body,
16    content_type_value: Option<::std::borrow::Cow<'static, [u8]>>,
17}
18
19impl<Body> Content<Body> {
20    pub fn new(body: Body) -> Self {
21        Self { body, content_type_value: None }
22    }
23
24    #[must_use]
25    pub fn with_content_type(mut self, content_type: impl Into<::std::borrow::Cow<'static, [u8]>>) -> Self {
26        self.content_type_value = Some(content_type.into());
27        self
28    }
29
30    fn content_type(&self) -> Option<&[u8]> {
31        self.content_type_value.as_deref()
32    }
33
34    fn into_body(self) -> Body {
35        self.body
36    }
37}
38
39fn url_string(
40    base_url: &str,
41    p_owner: &str,
42    p_repo: &str,
43    p_branch: &str,
44) -> Result<String, crate::v1_1_4::ApiError> {
45    let trimmed = if base_url.is_empty() {
46        "https://api.github.com"
47    } else {
48        base_url.trim_end_matches('/')
49    };
50    let mut url = String::with_capacity(trimmed.len() + 48);
51    url.push_str(trimmed);
52    url.push_str("/repos/");
53    ::querylizer::Simple::extend(&mut url, &p_owner, false, &::querylizer::encode_path)?;
54    url.push('/');
55    ::querylizer::Simple::extend(&mut url, &p_repo, false, &::querylizer::encode_path)?;
56    url.push_str("/branches/");
57    ::querylizer::Simple::extend(&mut url, &p_branch, false, &::querylizer::encode_path)?;
58    url.push_str("/protection");
59    Ok(url)
60}
61
62#[cfg(feature = "hyper")]
63pub fn http_builder(
64    base_url: &str,
65    p_owner: &str,
66    p_repo: &str,
67    p_branch: &str,
68    h_user_agent: &str,
69    h_accept: ::std::option::Option<&str>,
70) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
71    let url = url_string(
72        base_url,
73        p_owner,
74        p_repo,
75        p_branch,
76    )?;
77    let mut builder = ::http::request::Request::put(url);
78    builder = builder.header(
79        "User-Agent",
80        &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
81    );
82    if let Some(value) = &h_accept {
83        builder = builder.header(
84            "Accept",
85            &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
86        );
87    }
88    Ok(builder)
89}
90
91#[cfg(feature = "hyper")]
92pub fn hyper_request(
93    mut builder: ::http::request::Builder,
94    content: Content<::hyper::Body>,
95) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
96{
97    if let Some(content_type) = content.content_type() {
98        builder = builder.header(::http::header::CONTENT_TYPE, content_type);
99    }
100    Ok(builder.body(content.into_body())?)
101}
102
103#[cfg(feature = "hyper")]
104impl From<::hyper::Body> for Content<::hyper::Body> {
105    fn from(body: ::hyper::Body) -> Self {
106        Self::new(body)
107    }
108}
109
110#[cfg(feature = "reqwest")]
111pub fn reqwest_builder(
112    base_url: &str,
113    p_owner: &str,
114    p_repo: &str,
115    p_branch: &str,
116    h_user_agent: &str,
117    h_accept: ::std::option::Option<&str>,
118) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
119    let url = url_string(
120        base_url,
121        p_owner,
122        p_repo,
123        p_branch,
124    )?;
125    let reqwest_url = ::reqwest::Url::parse(&url)?;
126    let mut request = ::reqwest::Request::new(::reqwest::Method::PUT, reqwest_url);
127    let headers = request.headers_mut();
128    headers.append(
129        "User-Agent",
130        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
131    );
132    if let Some(value) = &h_accept {
133        headers.append(
134            "Accept",
135            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
136        );
137    }
138    Ok(request)
139}
140
141#[cfg(feature = "reqwest")]
142pub fn reqwest_request(
143    mut builder: ::reqwest::Request,
144    content: Content<::reqwest::Body>,
145) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
146    if let Some(content_type) = content.content_type() {
147        builder.headers_mut().append(
148            ::reqwest::header::HeaderName::from_static("content-type"),
149            ::reqwest::header::HeaderValue::try_from(content_type)?,
150        );
151    }
152    *builder.body_mut() = Some(content.into_body());
153    Ok(builder)
154}
155
156#[cfg(feature = "reqwest")]
157impl From<::reqwest::Body> for Content<::reqwest::Body> {
158    fn from(body: ::reqwest::Body) -> Self {
159        Self::new(body)
160    }
161}
162
163#[cfg(feature = "reqwest-blocking")]
164pub fn reqwest_blocking_builder(
165    base_url: &str,
166    p_owner: &str,
167    p_repo: &str,
168    p_branch: &str,
169    h_user_agent: &str,
170    h_accept: ::std::option::Option<&str>,
171) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
172    let url = url_string(
173        base_url,
174        p_owner,
175        p_repo,
176        p_branch,
177    )?;
178    let reqwest_url = ::reqwest::Url::parse(&url)?;
179    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::PUT, reqwest_url);
180    let headers = request.headers_mut();
181    headers.append(
182        "User-Agent",
183        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
184    );
185    if let Some(value) = &h_accept {
186        headers.append(
187            "Accept",
188            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
189        );
190    }
191    Ok(request)
192}
193
194#[cfg(feature = "reqwest-blocking")]
195pub fn reqwest_blocking_request(
196    mut builder: ::reqwest::blocking::Request,
197    content: Content<::reqwest::blocking::Body>,
198) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
199    if let Some(content_type) = content.content_type() {
200        builder.headers_mut().append(
201            ::reqwest::header::HeaderName::from_static("content-type"),
202            ::reqwest::header::HeaderValue::try_from(content_type)?,
203        );
204    }
205    *builder.body_mut() = Some(content.into_body());
206    Ok(builder)
207}
208
209#[cfg(feature = "reqwest-blocking")]
210impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
211    fn from(body: ::reqwest::blocking::Body) -> Self {
212        Self::new(body)
213    }
214}
215
216/// Types for body parameter in [`super::repos_update_branch_protection`]
217pub mod body {
218    #[allow(non_snake_case)]
219    #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
220    pub struct Json<'a> {
221        pub required_status_checks: ::std::option::Option<crate::v1_1_4::request::repos_update_branch_protection::body::json::RequiredStatusChecks<'a>>,
222
223        /// Enforce all configured restrictions for administrators. Set to `true` to enforce required status checks for repository administrators. Set to `null` to disable.
224        pub enforce_admins: ::std::option::Option<bool>,
225
226        pub required_pull_request_reviews: ::std::option::Option<crate::v1_1_4::request::repos_update_branch_protection::body::json::RequiredPullRequestReviews<'a>>,
227
228        pub restrictions: ::std::option::Option<crate::v1_1_4::request::repos_update_branch_protection::body::json::Restrictions<'a>>,
229
230        /// Enforces a linear commit Git history, which prevents anyone from pushing merge commits to a branch. Set to `true` to enforce a linear commit history. Set to `false` to disable a linear commit Git history. Your repository must allow squash merging or rebase merging before you can enable a linear commit history. Default: `false`. For more information, see "[Requiring a linear commit history](https://docs.github.com/github/administering-a-repository/requiring-a-linear-commit-history)" in the GitHub Help documentation.
231        #[serde(skip_serializing_if = "Option::is_none", default)]
232        pub required_linear_history: ::std::option::Option<bool>,
233
234        /// Permits force pushes to the protected branch by anyone with write access to the repository. Set to `true` to allow force pushes. Set to `false` or `null` to block force pushes. Default: `false`. For more information, see "[Enabling force pushes to a protected branch](https://docs.github.com/en/github/administering-a-repository/enabling-force-pushes-to-a-protected-branch)" in the GitHub Help documentation."
235        #[serde(skip_serializing_if = "Option::is_none", default, deserialize_with = "crate::v1_1_4::support::deserialize_some")]
236        pub allow_force_pushes: ::std::option::Option<::std::option::Option<bool>>,
237
238        /// Allows deletion of the protected branch by anyone with write access to the repository. Set to `false` to prevent deletion of the protected branch. Default: `false`. For more information, see "[Enabling force pushes to a protected branch](https://docs.github.com/en/github/administering-a-repository/enabling-force-pushes-to-a-protected-branch)" in the GitHub Help documentation.
239        #[serde(skip_serializing_if = "Option::is_none", default)]
240        pub allow_deletions: ::std::option::Option<bool>,
241
242        /// Blocks creation of new branches which match the branch protection pattern. Set to `true` to prohibit new branch creation. Default: `false`.
243        #[serde(skip_serializing_if = "Option::is_none", default)]
244        pub block_creations: ::std::option::Option<bool>,
245
246        /// Requires all conversations on code to be resolved before a pull request can be merged into a branch that matches this rule. Set to `false` to disable. Default: `false`.
247        #[serde(skip_serializing_if = "Option::is_none", default)]
248        pub required_conversation_resolution: ::std::option::Option<bool>,
249
250        #[serde(flatten)]
251        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
252    }
253
254    /// Types for fields in [`Json`]
255    pub mod json {
256        /// Require status checks to pass before merging. Set to `null` to disable.
257        #[allow(non_snake_case)]
258        #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
259        pub struct RequiredStatusChecks<'a> {
260            /// Require branches to be up to date before merging.
261            pub strict: bool,
262
263            /// **Deprecated**: The list of status checks to require in order to merge into this branch. If any of these checks have recently been set by a particular GitHub App, they will be required to come from that app in future for the branch to merge. Use `checks` instead of `contexts` for more fine-grained control.
264            pub contexts: ::std::borrow::Cow<'a, [::std::borrow::Cow<'a, str>]>,
265
266            /// The list of status checks to require in order to merge into this branch.
267            #[serde(skip_serializing_if = "Option::is_none", default)]
268            pub checks: ::std::option::Option<::std::borrow::Cow<'a, [crate::v1_1_4::request::repos_update_branch_protection::body::json::required_status_checks::Checks<'a>]>>,
269
270            #[serde(flatten)]
271            pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
272        }
273
274        /// Types for fields in [`RequiredStatusChecks`]
275        pub mod required_status_checks {
276            #[allow(non_snake_case)]
277            #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
278            pub struct Checks<'a> {
279                /// The name of the required check
280                pub context: ::std::borrow::Cow<'a, str>,
281
282                /// The ID of the GitHub App that must provide this check. Omit this field to automatically select the GitHub App that has recently provided this check, or any app if it was not set by a GitHub App. Pass -1 to explicitly allow any app to set the status.
283                #[serde(skip_serializing_if = "Option::is_none", default)]
284                pub app_id: ::std::option::Option<i64>,
285
286                #[serde(flatten)]
287                pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
288            }
289        }
290
291        /// Require at least one approving review on a pull request, before merging. Set to `null` to disable.
292        #[allow(non_snake_case)]
293        #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
294        pub struct RequiredPullRequestReviews<'a> {
295            #[serde(skip_serializing_if = "Option::is_none", default)]
296            pub dismissal_restrictions: ::std::option::Option<crate::v1_1_4::request::repos_update_branch_protection::body::json::required_pull_request_reviews::DismissalRestrictions<'a>>,
297
298            /// Set to `true` if you want to automatically dismiss approving reviews when someone pushes a new commit.
299            #[serde(skip_serializing_if = "Option::is_none", default)]
300            pub dismiss_stale_reviews: ::std::option::Option<bool>,
301
302            /// Blocks merging pull requests until [code owners](https://docs.github.com/articles/about-code-owners/) review them.
303            #[serde(skip_serializing_if = "Option::is_none", default)]
304            pub require_code_owner_reviews: ::std::option::Option<bool>,
305
306            /// Specify the number of reviewers required to approve pull requests. Use a number between 1 and 6 or 0 to not require reviewers.
307            #[serde(skip_serializing_if = "Option::is_none", default)]
308            pub required_approving_review_count: ::std::option::Option<i64>,
309
310            #[serde(skip_serializing_if = "Option::is_none", default)]
311            pub bypass_pull_request_allowances: ::std::option::Option<crate::v1_1_4::request::repos_update_branch_protection::body::json::required_pull_request_reviews::BypassPullRequestAllowances<'a>>,
312
313            #[serde(flatten)]
314            pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
315        }
316
317        /// Types for fields in [`RequiredPullRequestReviews`]
318        pub mod required_pull_request_reviews {
319            /// Specify which users and teams can dismiss pull request reviews. Pass an empty `dismissal_restrictions` object to disable. User and team `dismissal_restrictions` are only available for organization-owned repositories. Omit this parameter for personal repositories.
320            #[allow(non_snake_case)]
321            #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
322            pub struct DismissalRestrictions<'a> {
323                /// The list of user `login`s with dismissal access
324                #[serde(skip_serializing_if = "Option::is_none", default)]
325                pub users: ::std::option::Option<::std::borrow::Cow<'a, [::std::borrow::Cow<'a, str>]>>,
326
327                /// The list of team `slug`s with dismissal access
328                #[serde(skip_serializing_if = "Option::is_none", default)]
329                pub teams: ::std::option::Option<::std::borrow::Cow<'a, [::std::borrow::Cow<'a, str>]>>,
330
331                #[serde(flatten)]
332                pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
333            }
334
335            /// Allow specific users or teams to bypass pull request requirements.
336            #[allow(non_snake_case)]
337            #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
338            pub struct BypassPullRequestAllowances<'a> {
339                /// The list of user `login`s allowed to bypass pull request requirements.
340                #[serde(skip_serializing_if = "Option::is_none", default)]
341                pub users: ::std::option::Option<::std::borrow::Cow<'a, [::std::borrow::Cow<'a, str>]>>,
342
343                /// The list of team `slug`s allowed to bypass pull request requirements.
344                #[serde(skip_serializing_if = "Option::is_none", default)]
345                pub teams: ::std::option::Option<::std::borrow::Cow<'a, [::std::borrow::Cow<'a, str>]>>,
346
347                #[serde(flatten)]
348                pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
349            }
350        }
351
352        /// Restrict who can push to the protected branch. User, app, and team `restrictions` are only available for organization-owned repositories. Set to `null` to disable.
353        #[allow(non_snake_case)]
354        #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
355        pub struct Restrictions<'a> {
356            /// The list of user `login`s with push access
357            pub users: ::std::borrow::Cow<'a, [::std::borrow::Cow<'a, str>]>,
358
359            /// The list of team `slug`s with push access
360            pub teams: ::std::borrow::Cow<'a, [::std::borrow::Cow<'a, str>]>,
361
362            /// The list of app `slug`s with push access
363            #[serde(skip_serializing_if = "Option::is_none", default)]
364            pub apps: ::std::option::Option<::std::borrow::Cow<'a, [::std::borrow::Cow<'a, str>]>>,
365
366            #[serde(flatten)]
367            pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
368        }
369    }
370
371    #[cfg(feature = "hyper")]
372    impl<'a> TryFrom<&Json<'a>> for super::Content<::hyper::Body> {
373        type Error = crate::v1_1_4::ApiError;
374
375        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
376            Ok(
377                Self::new(::serde_json::to_vec(value)?.into())
378                .with_content_type(&b"application/json"[..])
379            )
380        }
381    }
382
383    #[cfg(feature = "reqwest")]
384    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::Body> {
385        type Error = crate::v1_1_4::ApiError;
386
387        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
388            Ok(
389                Self::new(::serde_json::to_vec(value)?.into())
390                .with_content_type(&b"application/json"[..])
391            )
392        }
393    }
394
395    #[cfg(feature = "reqwest-blocking")]
396    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::blocking::Body> {
397        type Error = crate::v1_1_4::ApiError;
398
399        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
400            Ok(
401                Self::new(::serde_json::to_vec(value)?.into())
402                .with_content_type(&b"application/json"[..])
403            )
404        }
405    }
406}