jinxapi_github/v1_1_4/request/
repos_create_commit_comment.rs

1//! Create a commit comment
2//! 
3//! Create a comment for a commit using its `:commit_sha`.
4//! 
5//! This endpoint triggers [notifications](https://docs.github.com/en/github/managing-subscriptions-and-notifications-on-github/about-notifications). Creating content too quickly using this endpoint may result in secondary rate limiting. See "[Secondary rate limits](https://docs.github.com/rest/overview/resources-in-the-rest-api#secondary-rate-limits)" and "[Dealing with secondary rate limits](https://docs.github.com/rest/guides/best-practices-for-integrators#dealing-with-secondary-rate-limits)" for details.
6//! 
7//! [API method documentation](https://docs.github.com/rest/reference/repos#create-a-commit-comment)
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_commit_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() + 45);
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("/commits/");
53    ::querylizer::Simple::extend(&mut url, &p_commit_sha, false, &::querylizer::encode_path)?;
54    url.push_str("/comments");
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_commit_sha: &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_commit_sha,
72    )?;
73    let mut builder = ::http::request::Request::post(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_commit_sha: &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_commit_sha,
120    )?;
121    let reqwest_url = ::reqwest::Url::parse(&url)?;
122    let mut request = ::reqwest::Request::new(::reqwest::Method::POST, 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_commit_sha: &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_commit_sha,
173    )?;
174    let reqwest_url = ::reqwest::Url::parse(&url)?;
175    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::POST, 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_create_commit_comment`]
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        /// The contents of the comment.
218        pub body: ::std::borrow::Cow<'a, str>,
219
220        /// Relative path of the file to comment on.
221        #[serde(skip_serializing_if = "Option::is_none", default)]
222        pub path: ::std::option::Option<::std::borrow::Cow<'a, str>>,
223
224        /// Line index in the diff to comment on.
225        #[serde(skip_serializing_if = "Option::is_none", default)]
226        pub position: ::std::option::Option<i64>,
227
228        /// **Deprecated**. Use **position** parameter instead. Line number in the file to comment on.
229        #[serde(skip_serializing_if = "Option::is_none", default)]
230        pub line: ::std::option::Option<i64>,
231
232        #[serde(flatten)]
233        pub additionalProperties: ::std::collections::HashMap<::std::borrow::Cow<'a, str>, ::serde_json::value::Value>
234    }
235
236    #[cfg(feature = "hyper")]
237    impl<'a> TryFrom<&Json<'a>> for super::Content<::hyper::Body> {
238        type Error = crate::v1_1_4::ApiError;
239
240        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
241            Ok(
242                Self::new(::serde_json::to_vec(value)?.into())
243                .with_content_type(&b"application/json"[..])
244            )
245        }
246    }
247
248    #[cfg(feature = "reqwest")]
249    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::Body> {
250        type Error = crate::v1_1_4::ApiError;
251
252        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
253            Ok(
254                Self::new(::serde_json::to_vec(value)?.into())
255                .with_content_type(&b"application/json"[..])
256            )
257        }
258    }
259
260    #[cfg(feature = "reqwest-blocking")]
261    impl<'a> TryFrom<&Json<'a>> for super::Content<::reqwest::blocking::Body> {
262        type Error = crate::v1_1_4::ApiError;
263
264        fn try_from(value: &Json<'a>) -> Result<Self, Self::Error> {
265            Ok(
266                Self::new(::serde_json::to_vec(value)?.into())
267                .with_content_type(&b"application/json"[..])
268            )
269        }
270    }
271}