jinxapi_github/v1_1_4/request/
repos_create_commit_status.rs

1//! Create a commit status
2//! 
3//! Users with push access in a repository can create commit statuses for a given SHA.
4//! 
5//! Note: there is a limit of 1000 statuses per `sha` and `context` within a repository. Attempts to create more than 1000 statuses will result in a validation error.
6//! 
7//! [API method documentation](https://docs.github.com/rest/reference/repos#create-a-commit-status)
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_sha: &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() + 37);
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("/statuses/");
53    ::querylizer::Simple::extend(&mut url, &p_sha, 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_sha: &str,
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_sha,
71    )?;
72    let mut builder = ::http::request::Request::post(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_sha: &str,
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_sha,
119    )?;
120    let reqwest_url = ::reqwest::Url::parse(&url)?;
121    let mut request = ::reqwest::Request::new(::reqwest::Method::POST, 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_sha: &str,
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_sha,
172    )?;
173    let reqwest_url = ::reqwest::Url::parse(&url)?;
174    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::POST, 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::repos_create_commit_status`]
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 state of the status. Can be one of `error`, `failure`, `pending`, or `success`.
217        pub state: ::std::borrow::Cow<'a, str>,
218
219        /// The target URL to associate with this status. This URL will be linked from the GitHub UI to allow users to easily see the source of the status.  
220        /// For example, if your continuous integration system is posting build status, you would want to provide the deep link for the build output for this specific SHA:  
221        /// `http://ci.example.com/user/repo/build/sha`
222        #[serde(skip_serializing_if = "Option::is_none", default)]
223        pub target_url: ::std::option::Option<::std::borrow::Cow<'a, str>>,
224
225        /// A short description of the status.
226        #[serde(skip_serializing_if = "Option::is_none", default)]
227        pub description: ::std::option::Option<::std::borrow::Cow<'a, str>>,
228
229        /// A string label to differentiate this status from the status of other systems. This field is case-insensitive.
230        #[serde(skip_serializing_if = "Option::is_none", default)]
231        pub context: ::std::option::Option<::std::borrow::Cow<'a, str>>,
232
233        #[serde(flatten)]
234        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
235    }
236
237    #[cfg(feature = "hyper")]
238    impl<'a> TryFrom<&Json<'a>> for super::Content<::hyper::Body> {
239        type Error = crate::v1_1_4::ApiError;
240
241        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
242            Ok(
243                Self::new(::serde_json::to_vec(value)?.into())
244                .with_content_type(&b"application/json"[..])
245            )
246        }
247    }
248
249    #[cfg(feature = "reqwest")]
250    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::Body> {
251        type Error = crate::v1_1_4::ApiError;
252
253        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
254            Ok(
255                Self::new(::serde_json::to_vec(value)?.into())
256                .with_content_type(&b"application/json"[..])
257            )
258        }
259    }
260
261    #[cfg(feature = "reqwest-blocking")]
262    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::blocking::Body> {
263        type Error = crate::v1_1_4::ApiError;
264
265        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
266            Ok(
267                Self::new(::serde_json::to_vec(value)?.into())
268                .with_content_type(&b"application/json"[..])
269            )
270        }
271    }
272}