jinxapi_github/v1_1_4/request/
repos_update_status_check_protection.rs

1//! Update status check 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//! Updating required status checks requires admin or owner permissions to the repository and branch protection to be enabled.
6//! 
7//! [API method documentation](https://docs.github.com/rest/reference/repos#update-status-check-protection)
8
9pub struct Content<Body>
10{
11    body: Body,
12    content_type_value: Option<::std::borrow::Cow<'static, [u8]>>,
13}
14
15impl<Body> Content<Body> {
16    pub fn new(body: Body) -> Self {
17        Self { body, content_type_value: None }
18    }
19
20    #[must_use]
21    pub fn with_content_type(mut self, content_type: impl Into<::std::borrow::Cow<'static, [u8]>>) -> Self {
22        self.content_type_value = Some(content_type.into());
23        self
24    }
25
26    fn content_type(&self) -> Option<&[u8]> {
27        self.content_type_value.as_deref()
28    }
29
30    fn into_body(self) -> Body {
31        self.body
32    }
33}
34
35fn url_string(
36    base_url: &str,
37    p_owner: &str,
38    p_repo: &str,
39    p_branch: &str,
40) -> Result<String, crate::v1_1_4::ApiError> {
41    let trimmed = if base_url.is_empty() {
42        "https://api.github.com"
43    } else {
44        base_url.trim_end_matches('/')
45    };
46    let mut url = String::with_capacity(trimmed.len() + 71);
47    url.push_str(trimmed);
48    url.push_str("/repos/");
49    ::querylizer::Simple::extend(&mut url, &p_owner, false, &::querylizer::encode_path)?;
50    url.push('/');
51    ::querylizer::Simple::extend(&mut url, &p_repo, false, &::querylizer::encode_path)?;
52    url.push_str("/branches/");
53    ::querylizer::Simple::extend(&mut url, &p_branch, false, &::querylizer::encode_path)?;
54    url.push_str("/protection/required_status_checks");
55    Ok(url)
56}
57
58#[cfg(feature = "hyper")]
59pub fn http_builder(
60    base_url: &str,
61    p_owner: &str,
62    p_repo: &str,
63    p_branch: &str,
64    h_user_agent: &str,
65    h_accept: ::std::option::Option<&str>,
66) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
67    let url = url_string(
68        base_url,
69        p_owner,
70        p_repo,
71        p_branch,
72    )?;
73    let mut builder = ::http::request::Request::patch(url);
74    builder = builder.header(
75        "User-Agent",
76        &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
77    );
78    if let Some(value) = &h_accept {
79        builder = builder.header(
80            "Accept",
81            &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
82        );
83    }
84    Ok(builder)
85}
86
87#[cfg(feature = "hyper")]
88pub fn hyper_request(
89    mut builder: ::http::request::Builder,
90    content: Content<::hyper::Body>,
91) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
92{
93    if let Some(content_type) = content.content_type() {
94        builder = builder.header(::http::header::CONTENT_TYPE, content_type);
95    }
96    Ok(builder.body(content.into_body())?)
97}
98
99#[cfg(feature = "hyper")]
100impl From<::hyper::Body> for Content<::hyper::Body> {
101    fn from(body: ::hyper::Body) -> Self {
102        Self::new(body)
103    }
104}
105
106#[cfg(feature = "reqwest")]
107pub fn reqwest_builder(
108    base_url: &str,
109    p_owner: &str,
110    p_repo: &str,
111    p_branch: &str,
112    h_user_agent: &str,
113    h_accept: ::std::option::Option<&str>,
114) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
115    let url = url_string(
116        base_url,
117        p_owner,
118        p_repo,
119        p_branch,
120    )?;
121    let reqwest_url = ::reqwest::Url::parse(&url)?;
122    let mut request = ::reqwest::Request::new(::reqwest::Method::PATCH, reqwest_url);
123    let headers = request.headers_mut();
124    headers.append(
125        "User-Agent",
126        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
127    );
128    if let Some(value) = &h_accept {
129        headers.append(
130            "Accept",
131            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
132        );
133    }
134    Ok(request)
135}
136
137#[cfg(feature = "reqwest")]
138pub fn reqwest_request(
139    mut builder: ::reqwest::Request,
140    content: Content<::reqwest::Body>,
141) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
142    if let Some(content_type) = content.content_type() {
143        builder.headers_mut().append(
144            ::reqwest::header::HeaderName::from_static("content-type"),
145            ::reqwest::header::HeaderValue::try_from(content_type)?,
146        );
147    }
148    *builder.body_mut() = Some(content.into_body());
149    Ok(builder)
150}
151
152#[cfg(feature = "reqwest")]
153impl From<::reqwest::Body> for Content<::reqwest::Body> {
154    fn from(body: ::reqwest::Body) -> Self {
155        Self::new(body)
156    }
157}
158
159#[cfg(feature = "reqwest-blocking")]
160pub fn reqwest_blocking_builder(
161    base_url: &str,
162    p_owner: &str,
163    p_repo: &str,
164    p_branch: &str,
165    h_user_agent: &str,
166    h_accept: ::std::option::Option<&str>,
167) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
168    let url = url_string(
169        base_url,
170        p_owner,
171        p_repo,
172        p_branch,
173    )?;
174    let reqwest_url = ::reqwest::Url::parse(&url)?;
175    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::PATCH, reqwest_url);
176    let headers = request.headers_mut();
177    headers.append(
178        "User-Agent",
179        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
180    );
181    if let Some(value) = &h_accept {
182        headers.append(
183            "Accept",
184            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
185        );
186    }
187    Ok(request)
188}
189
190#[cfg(feature = "reqwest-blocking")]
191pub fn reqwest_blocking_request(
192    mut builder: ::reqwest::blocking::Request,
193    content: Content<::reqwest::blocking::Body>,
194) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
195    if let Some(content_type) = content.content_type() {
196        builder.headers_mut().append(
197            ::reqwest::header::HeaderName::from_static("content-type"),
198            ::reqwest::header::HeaderValue::try_from(content_type)?,
199        );
200    }
201    *builder.body_mut() = Some(content.into_body());
202    Ok(builder)
203}
204
205#[cfg(feature = "reqwest-blocking")]
206impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
207    fn from(body: ::reqwest::blocking::Body) -> Self {
208        Self::new(body)
209    }
210}
211
212/// Types for body parameter in [`super::repos_update_status_check_protection`]
213pub mod body {
214    #[allow(non_snake_case)]
215    #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
216    pub struct Json<'a> {
217        /// Require branches to be up to date before merging.
218        #[serde(skip_serializing_if = "Option::is_none", default)]
219        pub strict: ::std::option::Option<bool>,
220
221        /// **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.
222        #[serde(skip_serializing_if = "Option::is_none", default)]
223        pub contexts: ::std::option::Option<::std::borrow::Cow<'a, [::std::borrow::Cow<'a, str>]>>,
224
225        /// The list of status checks to require in order to merge into this branch.
226        #[serde(skip_serializing_if = "Option::is_none", default)]
227        pub checks: ::std::option::Option<::std::borrow::Cow<'a, [crate::v1_1_4::request::repos_update_status_check_protection::body::json::Checks<'a>]>>,
228
229        #[serde(flatten)]
230        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
231    }
232
233    /// Types for fields in [`Json`]
234    pub mod json {
235        #[allow(non_snake_case)]
236        #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
237        pub struct Checks<'a> {
238            /// The name of the required check
239            pub context: ::std::borrow::Cow<'a, str>,
240
241            /// 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.
242            #[serde(skip_serializing_if = "Option::is_none", default)]
243            pub app_id: ::std::option::Option<i64>,
244
245            #[serde(flatten)]
246            pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
247        }
248    }
249
250    #[cfg(feature = "hyper")]
251    impl<'a> TryFrom<&Json<'a>> for super::Content<::hyper::Body> {
252        type Error = crate::v1_1_4::ApiError;
253
254        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
255            Ok(
256                Self::new(::serde_json::to_vec(value)?.into())
257                .with_content_type(&b"application/json"[..])
258            )
259        }
260    }
261
262    #[cfg(feature = "reqwest")]
263    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::Body> {
264        type Error = crate::v1_1_4::ApiError;
265
266        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
267            Ok(
268                Self::new(::serde_json::to_vec(value)?.into())
269                .with_content_type(&b"application/json"[..])
270            )
271        }
272    }
273
274    #[cfg(feature = "reqwest-blocking")]
275    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::blocking::Body> {
276        type Error = crate::v1_1_4::ApiError;
277
278        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
279            Ok(
280                Self::new(::serde_json::to_vec(value)?.into())
281                .with_content_type(&b"application/json"[..])
282            )
283        }
284    }
285}