mesa_dev/low_level/
commits.rs1use mesa_dev_oapi::apis::configuration::Configuration;
15use mesa_dev_oapi::apis::{Error, ResponseContent};
16use serde::{Deserialize, Serialize};
17
18pub use mesa_dev_oapi::apis::commits_api::PostByOrgByRepoCommitsError as CreateCommitError;
20
21pub use mesa_dev_oapi::models::post_by_org_by_repo_commits_request_files_inner_any_of::Encoding;
23
24pub type CommitAuthor =
28 mesa_dev_oapi::models::GetByOrgByRepoCommits200ResponseCommitsInnerCommitter;
29
30#[derive(Clone, Debug, PartialEq)]
34pub enum CommitFile {
35 Upsert {
37 path: String,
39 content: String,
41 encoding: Option<Encoding>,
43 },
44 Delete {
46 path: String,
48 },
49 Lfs {
51 path: String,
53 oid: String,
55 size: i64,
57 },
58}
59
60impl Serialize for CommitFile {
61 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
62 match self {
63 Self::Upsert {
64 path,
65 content,
66 encoding,
67 } => {
68 #[derive(Serialize)]
69 struct Wire<'a> {
70 action: &'a str,
71 path: &'a str,
72 content: &'a str,
73 #[serde(skip_serializing_if = "Option::is_none")]
74 encoding: &'a Option<Encoding>,
75 }
76 Wire {
77 action: "upsert",
78 path,
79 content,
80 encoding,
81 }
82 .serialize(serializer)
83 }
84 Self::Delete { path } => {
85 #[derive(Serialize)]
86 struct Wire<'a> {
87 action: &'a str,
88 path: &'a str,
89 }
90 Wire {
91 action: "delete",
92 path,
93 }
94 .serialize(serializer)
95 }
96 Self::Lfs { path, oid, size } => {
97 #[derive(Serialize)]
98 struct Lfs<'a> {
99 oid: &'a str,
100 size: i64,
101 }
102 #[derive(Serialize)]
103 struct Wire<'a> {
104 path: &'a str,
105 lfs: Lfs<'a>,
106 }
107 Wire {
108 path,
109 lfs: Lfs { oid, size: *size },
110 }
111 .serialize(serializer)
112 }
113 }
114 }
115}
116
117#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
122pub struct CommitResponse {
123 pub sha: String,
125 pub branch: String,
127 pub message: String,
129}
130
131#[allow(clippy::too_many_arguments)]
142pub async fn create_commit(
143 configuration: &Configuration,
144 org: &str,
145 repo: &str,
146 branch: &str,
147 message: &str,
148 author: &CommitAuthor,
149 files: &[CommitFile],
150 base_sha: Option<&str>,
151) -> Result<CommitResponse, Error<CreateCommitError>> {
152 let mut body = serde_json::json!({
153 "branch": branch,
154 "message": message,
155 "author": {
156 "name": author.name,
157 "email": author.email,
158 },
159 "files": files,
160 });
161
162 if let Some(sha) = base_sha {
163 body.as_object_mut()
164 .map(|m| m.insert("base_sha".to_string(), serde_json::json!(sha)));
165 }
166
167 let uri_str = format!(
168 "{}/{org}/{repo}/commits",
169 configuration.base_path,
170 org = mesa_dev_oapi::apis::urlencode(org),
171 repo = mesa_dev_oapi::apis::urlencode(repo),
172 );
173 let mut req_builder = configuration
174 .client
175 .request(reqwest::Method::POST, &uri_str);
176
177 if let Some(ref user_agent) = configuration.user_agent {
178 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
179 }
180 if let Some(ref token) = configuration.bearer_access_token {
181 req_builder = req_builder.bearer_auth(token.to_owned());
182 }
183 req_builder = req_builder.json(&body);
184
185 let req = req_builder.build()?;
186 let resp = configuration.client.execute(req).await?;
187
188 let status = resp.status();
189
190 if !status.is_client_error() && !status.is_server_error() {
191 let text = resp.text().await?;
192 serde_path_to_error::deserialize(&mut serde_json::Deserializer::from_str(&text))
193 .map_err(Error::from)
194 } else {
195 let text = resp.text().await?;
196 let entity: Option<CreateCommitError> = serde_json::from_str(&text).ok();
197 Err(Error::ResponseError(ResponseContent {
198 status,
199 content: text,
200 entity,
201 }))
202 }
203}