jinxapi_github/v1_1_4/request/
pulls_update_branch.rs

1//! Update a pull request branch
2//! 
3//! Updates the pull request branch with the latest upstream changes by merging HEAD from the base branch into the pull request branch.
4//! 
5//! [API method documentation](https://docs.github.com/rest/reference/pulls#update-a-pull-request-branch)
6
7pub struct Content<Body>
8{
9    body: Body,
10    content_type_value: Option<::std::borrow::Cow<'static, [u8]>>,
11}
12
13impl<Body> Content<Body> {
14    pub fn new(body: Body) -> Self {
15        Self { body, content_type_value: None }
16    }
17
18    #[must_use]
19    pub fn with_content_type(mut self, content_type: impl Into<::std::borrow::Cow<'static, [u8]>>) -> Self {
20        self.content_type_value = Some(content_type.into());
21        self
22    }
23
24    fn content_type(&self) -> Option<&[u8]> {
25        self.content_type_value.as_deref()
26    }
27
28    fn into_body(self) -> Body {
29        self.body
30    }
31}
32
33fn url_string(
34    base_url: &str,
35    p_owner: &str,
36    p_repo: &str,
37    p_pull_number: i64,
38) -> Result<String, crate::v1_1_4::ApiError> {
39    let trimmed = if base_url.is_empty() {
40        "https://api.github.com"
41    } else {
42        base_url.trim_end_matches('/')
43    };
44    let mut url = String::with_capacity(trimmed.len() + 48);
45    url.push_str(trimmed);
46    url.push_str("/repos/");
47    ::querylizer::Simple::extend(&mut url, &p_owner, false, &::querylizer::encode_path)?;
48    url.push('/');
49    ::querylizer::Simple::extend(&mut url, &p_repo, false, &::querylizer::encode_path)?;
50    url.push_str("/pulls/");
51    ::querylizer::Simple::extend(&mut url, &p_pull_number, false, &::querylizer::encode_path)?;
52    url.push_str("/update-branch");
53    Ok(url)
54}
55
56#[cfg(feature = "hyper")]
57pub fn http_builder(
58    base_url: &str,
59    p_owner: &str,
60    p_repo: &str,
61    p_pull_number: i64,
62    h_user_agent: &str,
63    h_accept: ::std::option::Option<&str>,
64) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
65    let url = url_string(
66        base_url,
67        p_owner,
68        p_repo,
69        p_pull_number,
70    )?;
71    let mut builder = ::http::request::Request::put(url);
72    builder = builder.header(
73        "User-Agent",
74        &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
75    );
76    if let Some(value) = &h_accept {
77        builder = builder.header(
78            "Accept",
79            &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
80        );
81    }
82    Ok(builder)
83}
84
85#[cfg(feature = "hyper")]
86pub fn hyper_request(
87    mut builder: ::http::request::Builder,
88    content: Content<::hyper::Body>,
89) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
90{
91    if let Some(content_type) = content.content_type() {
92        builder = builder.header(::http::header::CONTENT_TYPE, content_type);
93    }
94    Ok(builder.body(content.into_body())?)
95}
96
97#[cfg(feature = "hyper")]
98impl From<::hyper::Body> for Content<::hyper::Body> {
99    fn from(body: ::hyper::Body) -> Self {
100        Self::new(body)
101    }
102}
103
104#[cfg(feature = "reqwest")]
105pub fn reqwest_builder(
106    base_url: &str,
107    p_owner: &str,
108    p_repo: &str,
109    p_pull_number: i64,
110    h_user_agent: &str,
111    h_accept: ::std::option::Option<&str>,
112) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
113    let url = url_string(
114        base_url,
115        p_owner,
116        p_repo,
117        p_pull_number,
118    )?;
119    let reqwest_url = ::reqwest::Url::parse(&url)?;
120    let mut request = ::reqwest::Request::new(::reqwest::Method::PUT, reqwest_url);
121    let headers = request.headers_mut();
122    headers.append(
123        "User-Agent",
124        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
125    );
126    if let Some(value) = &h_accept {
127        headers.append(
128            "Accept",
129            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
130        );
131    }
132    Ok(request)
133}
134
135#[cfg(feature = "reqwest")]
136pub fn reqwest_request(
137    mut builder: ::reqwest::Request,
138    content: Content<::reqwest::Body>,
139) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
140    if let Some(content_type) = content.content_type() {
141        builder.headers_mut().append(
142            ::reqwest::header::HeaderName::from_static("content-type"),
143            ::reqwest::header::HeaderValue::try_from(content_type)?,
144        );
145    }
146    *builder.body_mut() = Some(content.into_body());
147    Ok(builder)
148}
149
150#[cfg(feature = "reqwest")]
151impl From<::reqwest::Body> for Content<::reqwest::Body> {
152    fn from(body: ::reqwest::Body) -> Self {
153        Self::new(body)
154    }
155}
156
157#[cfg(feature = "reqwest-blocking")]
158pub fn reqwest_blocking_builder(
159    base_url: &str,
160    p_owner: &str,
161    p_repo: &str,
162    p_pull_number: i64,
163    h_user_agent: &str,
164    h_accept: ::std::option::Option<&str>,
165) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
166    let url = url_string(
167        base_url,
168        p_owner,
169        p_repo,
170        p_pull_number,
171    )?;
172    let reqwest_url = ::reqwest::Url::parse(&url)?;
173    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::PUT, reqwest_url);
174    let headers = request.headers_mut();
175    headers.append(
176        "User-Agent",
177        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
178    );
179    if let Some(value) = &h_accept {
180        headers.append(
181            "Accept",
182            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
183        );
184    }
185    Ok(request)
186}
187
188#[cfg(feature = "reqwest-blocking")]
189pub fn reqwest_blocking_request(
190    mut builder: ::reqwest::blocking::Request,
191    content: Content<::reqwest::blocking::Body>,
192) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
193    if let Some(content_type) = content.content_type() {
194        builder.headers_mut().append(
195            ::reqwest::header::HeaderName::from_static("content-type"),
196            ::reqwest::header::HeaderValue::try_from(content_type)?,
197        );
198    }
199    *builder.body_mut() = Some(content.into_body());
200    Ok(builder)
201}
202
203#[cfg(feature = "reqwest-blocking")]
204impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
205    fn from(body: ::reqwest::blocking::Body) -> Self {
206        Self::new(body)
207    }
208}
209
210/// Types for body parameter in [`super::pulls_update_branch`]
211pub mod body {
212    #[allow(non_snake_case)]
213    #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
214    pub struct Json<'a> {
215        /// The expected SHA of the pull request's HEAD ref. This is the most recent commit on the pull request's branch. If the expected SHA does not match the pull request's HEAD, you will receive a `422 Unprocessable Entity` status. You can use the "[List commits](https://docs.github.com/rest/reference/repos#list-commits)" endpoint to find the most recent commit SHA. Default: SHA of the pull request's current HEAD ref.
216        #[serde(skip_serializing_if = "Option::is_none", default)]
217        pub expected_head_sha: ::std::option::Option<::std::borrow::Cow<'a, str>>,
218
219        #[serde(flatten)]
220        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
221    }
222
223    #[cfg(feature = "hyper")]
224    impl<'a> TryFrom<&::std::option::Option<crate::v1_1_4::request::pulls_update_branch::body::Json<'a>>> for super::Content<::hyper::Body> {
225        type Error = crate::v1_1_4::ApiError;
226
227        fn try_from(value: &::std::option::Option<crate::v1_1_4::request::pulls_update_branch::body::Json<'a>>) -> Result<Self, Self::Error> {
228            Ok(
229                Self::new(::serde_json::to_vec(value)?.into())
230                .with_content_type(&b"application/json"[..])
231            )
232        }
233    }
234
235    #[cfg(feature = "reqwest")]
236    impl<'a> TryFrom<&::std::option::Option<crate::v1_1_4::request::pulls_update_branch::body::Json<'a>>> for super::Content<::reqwest::Body> {
237        type Error = crate::v1_1_4::ApiError;
238
239        fn try_from(value: &::std::option::Option<crate::v1_1_4::request::pulls_update_branch::body::Json<'a>>) -> Result<Self, Self::Error> {
240            Ok(
241                Self::new(::serde_json::to_vec(value)?.into())
242                .with_content_type(&b"application/json"[..])
243            )
244        }
245    }
246
247    #[cfg(feature = "reqwest-blocking")]
248    impl<'a> TryFrom<&::std::option::Option<crate::v1_1_4::request::pulls_update_branch::body::Json<'a>>> for super::Content<::reqwest::blocking::Body> {
249        type Error = crate::v1_1_4::ApiError;
250
251        fn try_from(value: &::std::option::Option<crate::v1_1_4::request::pulls_update_branch::body::Json<'a>>) -> Result<Self, Self::Error> {
252            Ok(
253                Self::new(::serde_json::to_vec(value)?.into())
254                .with_content_type(&b"application/json"[..])
255            )
256        }
257    }
258}