jinxapi_github/v1_1_4/request/
pulls_update.rs

1//! Update a pull request
2//! 
3//! Draft pull requests are available in public repositories with GitHub Free and GitHub Free for organizations, GitHub Pro, and legacy per-repository billing plans, and in public and private repositories with GitHub Team and GitHub Enterprise Cloud. 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//! To open or update a pull request in a public repository, you must have write access to the head or the source branch. For organization-owned repositories, you must be a member of the organization that owns the repository to open or update a pull request.
6//! 
7//! [API method documentation](https://docs.github.com/rest/reference/pulls/#update-a-pull-request)
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_pull_number: i64,
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() + 34);
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("/pulls/");
53    ::querylizer::Simple::extend(&mut url, &p_pull_number, false, &::querylizer::encode_path)?;
54    Ok(url)
55}
56
57#[cfg(feature = "hyper")]
58pub fn http_builder(
59    base_url: &str,
60    p_owner: &str,
61    p_repo: &str,
62    p_pull_number: i64,
63    h_user_agent: &str,
64    h_accept: ::std::option::Option<&str>,
65) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
66    let url = url_string(
67        base_url,
68        p_owner,
69        p_repo,
70        p_pull_number,
71    )?;
72    let mut builder = ::http::request::Request::patch(url);
73    builder = builder.header(
74        "User-Agent",
75        &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
76    );
77    if let Some(value) = &h_accept {
78        builder = builder.header(
79            "Accept",
80            &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
81        );
82    }
83    Ok(builder)
84}
85
86#[cfg(feature = "hyper")]
87pub fn hyper_request(
88    mut builder: ::http::request::Builder,
89    content: Content<::hyper::Body>,
90) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
91{
92    if let Some(content_type) = content.content_type() {
93        builder = builder.header(::http::header::CONTENT_TYPE, content_type);
94    }
95    Ok(builder.body(content.into_body())?)
96}
97
98#[cfg(feature = "hyper")]
99impl From<::hyper::Body> for Content<::hyper::Body> {
100    fn from(body: ::hyper::Body) -> Self {
101        Self::new(body)
102    }
103}
104
105#[cfg(feature = "reqwest")]
106pub fn reqwest_builder(
107    base_url: &str,
108    p_owner: &str,
109    p_repo: &str,
110    p_pull_number: i64,
111    h_user_agent: &str,
112    h_accept: ::std::option::Option<&str>,
113) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
114    let url = url_string(
115        base_url,
116        p_owner,
117        p_repo,
118        p_pull_number,
119    )?;
120    let reqwest_url = ::reqwest::Url::parse(&url)?;
121    let mut request = ::reqwest::Request::new(::reqwest::Method::PATCH, reqwest_url);
122    let headers = request.headers_mut();
123    headers.append(
124        "User-Agent",
125        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
126    );
127    if let Some(value) = &h_accept {
128        headers.append(
129            "Accept",
130            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
131        );
132    }
133    Ok(request)
134}
135
136#[cfg(feature = "reqwest")]
137pub fn reqwest_request(
138    mut builder: ::reqwest::Request,
139    content: Content<::reqwest::Body>,
140) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
141    if let Some(content_type) = content.content_type() {
142        builder.headers_mut().append(
143            ::reqwest::header::HeaderName::from_static("content-type"),
144            ::reqwest::header::HeaderValue::try_from(content_type)?,
145        );
146    }
147    *builder.body_mut() = Some(content.into_body());
148    Ok(builder)
149}
150
151#[cfg(feature = "reqwest")]
152impl From<::reqwest::Body> for Content<::reqwest::Body> {
153    fn from(body: ::reqwest::Body) -> Self {
154        Self::new(body)
155    }
156}
157
158#[cfg(feature = "reqwest-blocking")]
159pub fn reqwest_blocking_builder(
160    base_url: &str,
161    p_owner: &str,
162    p_repo: &str,
163    p_pull_number: i64,
164    h_user_agent: &str,
165    h_accept: ::std::option::Option<&str>,
166) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
167    let url = url_string(
168        base_url,
169        p_owner,
170        p_repo,
171        p_pull_number,
172    )?;
173    let reqwest_url = ::reqwest::Url::parse(&url)?;
174    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::PATCH, reqwest_url);
175    let headers = request.headers_mut();
176    headers.append(
177        "User-Agent",
178        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
179    );
180    if let Some(value) = &h_accept {
181        headers.append(
182            "Accept",
183            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
184        );
185    }
186    Ok(request)
187}
188
189#[cfg(feature = "reqwest-blocking")]
190pub fn reqwest_blocking_request(
191    mut builder: ::reqwest::blocking::Request,
192    content: Content<::reqwest::blocking::Body>,
193) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
194    if let Some(content_type) = content.content_type() {
195        builder.headers_mut().append(
196            ::reqwest::header::HeaderName::from_static("content-type"),
197            ::reqwest::header::HeaderValue::try_from(content_type)?,
198        );
199    }
200    *builder.body_mut() = Some(content.into_body());
201    Ok(builder)
202}
203
204#[cfg(feature = "reqwest-blocking")]
205impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
206    fn from(body: ::reqwest::blocking::Body) -> Self {
207        Self::new(body)
208    }
209}
210
211/// Types for body parameter in [`super::pulls_update`]
212pub mod body {
213    #[allow(non_snake_case)]
214    #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
215    pub struct Json<'a> {
216        /// The title of the pull request.
217        #[serde(skip_serializing_if = "Option::is_none", default)]
218        pub title: ::std::option::Option<::std::borrow::Cow<'a, str>>,
219
220        /// The contents of the pull request.
221        #[serde(skip_serializing_if = "Option::is_none", default)]
222        pub body: ::std::option::Option<::std::borrow::Cow<'a, str>>,
223
224        /// State of this Pull Request. Either `open` or `closed`.
225        #[serde(skip_serializing_if = "Option::is_none", default)]
226        pub state: ::std::option::Option<::std::borrow::Cow<'a, str>>,
227
228        /// The name of the branch you want your changes pulled into. This should be an existing branch on the current repository. You cannot update the base branch on a pull request to point to another repository.
229        #[serde(skip_serializing_if = "Option::is_none", default)]
230        pub base: ::std::option::Option<::std::borrow::Cow<'a, str>>,
231
232        /// Indicates whether [maintainers can modify](https://docs.github.com/articles/allowing-changes-to-a-pull-request-branch-created-from-a-fork/) the pull request.
233        #[serde(skip_serializing_if = "Option::is_none", default)]
234        pub maintainer_can_modify: ::std::option::Option<bool>,
235
236        #[serde(flatten)]
237        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
238    }
239
240    #[cfg(feature = "hyper")]
241    impl<'a> TryFrom<&Json<'a>> for super::Content<::hyper::Body> {
242        type Error = crate::v1_1_4::ApiError;
243
244        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
245            Ok(
246                Self::new(::serde_json::to_vec(value)?.into())
247                .with_content_type(&b"application/json"[..])
248            )
249        }
250    }
251
252    #[cfg(feature = "reqwest")]
253    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::Body> {
254        type Error = crate::v1_1_4::ApiError;
255
256        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
257            Ok(
258                Self::new(::serde_json::to_vec(value)?.into())
259                .with_content_type(&b"application/json"[..])
260            )
261        }
262    }
263
264    #[cfg(feature = "reqwest-blocking")]
265    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::blocking::Body> {
266        type Error = crate::v1_1_4::ApiError;
267
268        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
269            Ok(
270                Self::new(::serde_json::to_vec(value)?.into())
271                .with_content_type(&b"application/json"[..])
272            )
273        }
274    }
275}