jinxapi_github/v1_1_4/request/
git_create_commit.rs

1//! Create a commit
2//! 
3//! Creates a new Git [commit object](https://git-scm.com/book/en/v1/Git-Internals-Git-Objects#Commit-Objects).
4//! 
5//! **Signature verification object**
6//! 
7//! The response will include a `verification` object that describes the result of verifying the commit's signature. The following fields are included in the `verification` object:
8//! 
9//! | Name | Type | Description |
10//! | ---- | ---- | ----------- |
11//! | `verified` | `boolean` | Indicates whether GitHub considers the signature in this commit to be verified. |
12//! | `reason` | `string` | The reason for verified value. Possible values and their meanings are enumerated in the table below. |
13//! | `signature` | `string` | The signature that was extracted from the commit. |
14//! | `payload` | `string` | The value that was signed. |
15//! 
16//! These are the possible values for `reason` in the `verification` object:
17//! 
18//! | Value | Description |
19//! | ----- | ----------- |
20//! | `expired_key` | The key that made the signature is expired. |
21//! | `not_signing_key` | The "signing" flag is not among the usage flags in the GPG key that made the signature. |
22//! | `gpgverify_error` | There was an error communicating with the signature verification service. |
23//! | `gpgverify_unavailable` | The signature verification service is currently unavailable. |
24//! | `unsigned` | The object does not include a signature. |
25//! | `unknown_signature_type` | A non-PGP signature was found in the commit. |
26//! | `no_user` | No user was associated with the `committer` email address in the commit. |
27//! | `unverified_email` | The `committer` email address in the commit was associated with a user, but the email address is not verified on her/his account. |
28//! | `bad_email` | The `committer` email address in the commit is not included in the identities of the PGP key that made the signature. |
29//! | `unknown_key` | The key that made the signature has not been registered with any user's account. |
30//! | `malformed_signature` | There was an error parsing the signature. |
31//! | `invalid` | The signature could not be cryptographically verified using the key whose key-id was found in the signature. |
32//! | `valid` | None of the above errors applied, so the signature is considered to be verified. |
33//! 
34//! [API method documentation](https://docs.github.com/rest/reference/git#create-a-commit)
35
36pub struct Content<Body>
37{
38    body: Body,
39    content_type_value: Option<::std::borrow::Cow<'static, [u8]>>,
40}
41
42impl<Body> Content<Body> {
43    pub fn new(body: Body) -> Self {
44        Self { body, content_type_value: None }
45    }
46
47    #[must_use]
48    pub fn with_content_type(mut self, content_type: impl Into<::std::borrow::Cow<'static, [u8]>>) -> Self {
49        self.content_type_value = Some(content_type.into());
50        self
51    }
52
53    fn content_type(&self) -> Option<&[u8]> {
54        self.content_type_value.as_deref()
55    }
56
57    fn into_body(self) -> Body {
58        self.body
59    }
60}
61
62fn url_string(
63    base_url: &str,
64    p_owner: &str,
65    p_repo: &str,
66) -> Result<String, crate::v1_1_4::ApiError> {
67    let trimmed = if base_url.is_empty() {
68        "https://api.github.com"
69    } else {
70        base_url.trim_end_matches('/')
71    };
72    let mut url = String::with_capacity(trimmed.len() + 38);
73    url.push_str(trimmed);
74    url.push_str("/repos/");
75    ::querylizer::Simple::extend(&mut url, &p_owner, false, &::querylizer::encode_path)?;
76    url.push('/');
77    ::querylizer::Simple::extend(&mut url, &p_repo, false, &::querylizer::encode_path)?;
78    url.push_str("/git/commits");
79    Ok(url)
80}
81
82#[cfg(feature = "hyper")]
83pub fn http_builder(
84    base_url: &str,
85    p_owner: &str,
86    p_repo: &str,
87    h_user_agent: &str,
88    h_accept: ::std::option::Option<&str>,
89) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
90    let url = url_string(
91        base_url,
92        p_owner,
93        p_repo,
94    )?;
95    let mut builder = ::http::request::Request::post(url);
96    builder = builder.header(
97        "User-Agent",
98        &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
99    );
100    if let Some(value) = &h_accept {
101        builder = builder.header(
102            "Accept",
103            &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
104        );
105    }
106    Ok(builder)
107}
108
109#[cfg(feature = "hyper")]
110pub fn hyper_request(
111    mut builder: ::http::request::Builder,
112    content: Content<::hyper::Body>,
113) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError>
114{
115    if let Some(content_type) = content.content_type() {
116        builder = builder.header(::http::header::CONTENT_TYPE, content_type);
117    }
118    Ok(builder.body(content.into_body())?)
119}
120
121#[cfg(feature = "hyper")]
122impl From<::hyper::Body> for Content<::hyper::Body> {
123    fn from(body: ::hyper::Body) -> Self {
124        Self::new(body)
125    }
126}
127
128#[cfg(feature = "reqwest")]
129pub fn reqwest_builder(
130    base_url: &str,
131    p_owner: &str,
132    p_repo: &str,
133    h_user_agent: &str,
134    h_accept: ::std::option::Option<&str>,
135) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
136    let url = url_string(
137        base_url,
138        p_owner,
139        p_repo,
140    )?;
141    let reqwest_url = ::reqwest::Url::parse(&url)?;
142    let mut request = ::reqwest::Request::new(::reqwest::Method::POST, reqwest_url);
143    let headers = request.headers_mut();
144    headers.append(
145        "User-Agent",
146        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
147    );
148    if let Some(value) = &h_accept {
149        headers.append(
150            "Accept",
151            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
152        );
153    }
154    Ok(request)
155}
156
157#[cfg(feature = "reqwest")]
158pub fn reqwest_request(
159    mut builder: ::reqwest::Request,
160    content: Content<::reqwest::Body>,
161) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
162    if let Some(content_type) = content.content_type() {
163        builder.headers_mut().append(
164            ::reqwest::header::HeaderName::from_static("content-type"),
165            ::reqwest::header::HeaderValue::try_from(content_type)?,
166        );
167    }
168    *builder.body_mut() = Some(content.into_body());
169    Ok(builder)
170}
171
172#[cfg(feature = "reqwest")]
173impl From<::reqwest::Body> for Content<::reqwest::Body> {
174    fn from(body: ::reqwest::Body) -> Self {
175        Self::new(body)
176    }
177}
178
179#[cfg(feature = "reqwest-blocking")]
180pub fn reqwest_blocking_builder(
181    base_url: &str,
182    p_owner: &str,
183    p_repo: &str,
184    h_user_agent: &str,
185    h_accept: ::std::option::Option<&str>,
186) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
187    let url = url_string(
188        base_url,
189        p_owner,
190        p_repo,
191    )?;
192    let reqwest_url = ::reqwest::Url::parse(&url)?;
193    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::POST, reqwest_url);
194    let headers = request.headers_mut();
195    headers.append(
196        "User-Agent",
197        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
198    );
199    if let Some(value) = &h_accept {
200        headers.append(
201            "Accept",
202            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
203        );
204    }
205    Ok(request)
206}
207
208#[cfg(feature = "reqwest-blocking")]
209pub fn reqwest_blocking_request(
210    mut builder: ::reqwest::blocking::Request,
211    content: Content<::reqwest::blocking::Body>,
212) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
213    if let Some(content_type) = content.content_type() {
214        builder.headers_mut().append(
215            ::reqwest::header::HeaderName::from_static("content-type"),
216            ::reqwest::header::HeaderValue::try_from(content_type)?,
217        );
218    }
219    *builder.body_mut() = Some(content.into_body());
220    Ok(builder)
221}
222
223#[cfg(feature = "reqwest-blocking")]
224impl From<::reqwest::blocking::Body> for Content<::reqwest::blocking::Body> {
225    fn from(body: ::reqwest::blocking::Body) -> Self {
226        Self::new(body)
227    }
228}
229
230/// Types for body parameter in [`super::git_create_commit`]
231pub mod body {
232    #[allow(non_snake_case)]
233    #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
234    pub struct Json<'a> {
235        /// The commit message
236        pub message: ::std::borrow::Cow<'a, str>,
237
238        /// The SHA of the tree object this commit points to
239        pub tree: ::std::borrow::Cow<'a, str>,
240
241        /// The SHAs of the commits that were the parents of this commit. If omitted or empty, the commit will be written as a root commit. For a single parent, an array of one SHA should be provided; for a merge commit, an array of more than one should be provided.
242        #[serde(skip_serializing_if = "Option::is_none", default)]
243        pub parents: ::std::option::Option<::std::borrow::Cow<'a, [::std::borrow::Cow<'a, str>]>>,
244
245        #[serde(skip_serializing_if = "Option::is_none", default)]
246        pub author: ::std::option::Option<crate::v1_1_4::request::git_create_commit::body::json::Author<'a>>,
247
248        #[serde(skip_serializing_if = "Option::is_none", default)]
249        pub committer: ::std::option::Option<crate::v1_1_4::request::git_create_commit::body::json::Committer<'a>>,
250
251        /// The [PGP signature](https://en.wikipedia.org/wiki/Pretty_Good_Privacy) of the commit. GitHub adds the signature to the `gpgsig` header of the created commit. For a commit signature to be verifiable by Git or GitHub, it must be an ASCII-armored detached PGP signature over the string commit as it would be written to the object database. To pass a `signature` parameter, you need to first manually create a valid PGP signature, which can be complicated. You may find it easier to [use the command line](https://git-scm.com/book/id/v2/Git-Tools-Signing-Your-Work) to create signed commits.
252        #[serde(skip_serializing_if = "Option::is_none", default)]
253        pub signature: ::std::option::Option<::std::borrow::Cow<'a, str>>,
254
255        #[serde(flatten)]
256        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
257    }
258
259    /// Types for fields in [`Json`]
260    pub mod json {
261        /// Information about the author of the commit. By default, the `author` will be the authenticated user and the current date. See the `author` and `committer` object below for details.
262        #[allow(non_snake_case)]
263        #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
264        pub struct Author<'a> {
265            /// The name of the author (or committer) of the commit
266            pub name: ::std::borrow::Cow<'a, str>,
267
268            /// The email of the author (or committer) of the commit
269            pub email: ::std::borrow::Cow<'a, str>,
270
271            /// Indicates when this commit was authored (or committed). This is a timestamp in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format: `YYYY-MM-DDTHH:MM:SSZ`.
272            #[serde(skip_serializing_if = "Option::is_none", default)]
273            pub date: ::std::option::Option<::std::borrow::Cow<'a, str>>,
274
275            #[serde(flatten)]
276            pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
277        }
278
279        /// Information about the person who is making the commit. By default, `committer` will use the information set in `author`. See the `author` and `committer` object below for details.
280        #[allow(non_snake_case)]
281        #[derive(Clone, Eq, PartialEq, Debug, Default, ::serde::Serialize, ::serde::Deserialize)]
282        pub struct Committer<'a> {
283            /// The name of the author (or committer) of the commit
284            #[serde(skip_serializing_if = "Option::is_none", default)]
285            pub name: ::std::option::Option<::std::borrow::Cow<'a, str>>,
286
287            /// The email of the author (or committer) of the commit
288            #[serde(skip_serializing_if = "Option::is_none", default)]
289            pub email: ::std::option::Option<::std::borrow::Cow<'a, str>>,
290
291            /// Indicates when this commit was authored (or committed). This is a timestamp in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format: `YYYY-MM-DDTHH:MM:SSZ`.
292            #[serde(skip_serializing_if = "Option::is_none", default)]
293            pub date: ::std::option::Option<::std::borrow::Cow<'a, str>>,
294
295            #[serde(flatten)]
296            pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
297        }
298    }
299
300    #[cfg(feature = "hyper")]
301    impl<'a> TryFrom<&Json<'a>> for super::Content<::hyper::Body> {
302        type Error = crate::v1_1_4::ApiError;
303
304        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
305            Ok(
306                Self::new(::serde_json::to_vec(value)?.into())
307                .with_content_type(&b"application/json"[..])
308            )
309        }
310    }
311
312    #[cfg(feature = "reqwest")]
313    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::Body> {
314        type Error = crate::v1_1_4::ApiError;
315
316        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
317            Ok(
318                Self::new(::serde_json::to_vec(value)?.into())
319                .with_content_type(&b"application/json"[..])
320            )
321        }
322    }
323
324    #[cfg(feature = "reqwest-blocking")]
325    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::blocking::Body> {
326        type Error = crate::v1_1_4::ApiError;
327
328        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
329            Ok(
330                Self::new(::serde_json::to_vec(value)?.into())
331                .with_content_type(&b"application/json"[..])
332            )
333        }
334    }
335}