8 Commits

Author SHA1 Message Date
06795df3f7 Update main.rs to use new attachment iface 2025-06-07 23:57:19 -05:00
a5f6335b5f Add Attachment struct, new iface for create-rel
The Attachment struct exists, but this makes it glaringly obvious that
I've made a bad interface. The create_release_attachment should only
accept one file at a time and the loop over all files should happen in
main.rs

I've changed the signature, removed the loops, and wired in the newer
error handling routines. Needs fixing at call sites.
2025-06-07 23:50:16 -05:00
4a0addda67 Fold client-error-decode into a util function 2025-06-07 23:40:58 -05:00
0c70b584ba Interrogate create_release_attachment result 2025-06-07 23:30:56 -05:00
8a11c21b73 "Fix" the test case
I can't meaningfully unit test these things like this. I'll explore creating a tarball of a known Gitea configuration and using Docker to test against that. For now, just... kinda keep the test building.
2025-06-07 23:24:16 -05:00
d42cbbc1ec Drop unused imports 2025-06-07 23:22:48 -05:00
96e9ff4ce6 Interrogate create_release result more closely 2025-06-07 23:22:20 -05:00
6bdad44cc6 Interrogate list_releases result more closely 2025-06-07 23:15:39 -05:00
5 changed files with 109 additions and 72 deletions

View File

@@ -1,9 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
ApiError, Result,
structs::{
self,
release::{CreateReleaseOption, Release},
},
};
@@ -23,6 +21,7 @@ pub async fn list_releases(
let request_url = format!("{gitea_url}/api/v1/repos/{repo}/releases/");
let req = client.get(request_url).send().await;
let response = req.map_err(|reqwest_err| crate::Error::WrappedReqwestErr(reqwest_err))?;
if response.status().is_success() {
let release_list = response
.json::<Vec<Release>>()
.await
@@ -32,14 +31,11 @@ pub async fn list_releases(
crate::Error::WrappedReqwestErr(reqwest_err)
})?;
return Ok(release_list);
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(untagged)]
enum CreateResult {
Success(structs::release::Release),
ErrWithMessage(ApiError),
Empty,
} else if response.status().is_client_error() {
let mesg = crate::decode_client_error(response).await?;
return Err(crate::Error::ApiErrorMessage(mesg));
}
panic!("Reached end of list_releases without matching a return pathway.");
}
pub async fn create_release(
@@ -49,27 +45,23 @@ pub async fn create_release(
submission: CreateReleaseOption,
) -> Result<Release> {
let request_url = format!("{gitea_url}/api/v1/repos/{repo}/releases");
let req = client
let response = client
.post(request_url)
.json(&submission)
.send()
.await
.map_err(|e| crate::Error::from(e))?;
let new_release = req
.json::<CreateResult>()
if response.status().is_success() {
let new_release = response
.json::<Release>()
.await
.map_err(|e| crate::Error::from(e))?;
match new_release {
CreateResult::Success(release) => Ok(release),
CreateResult::ErrWithMessage(api_error) => {
if api_error.message == "token is required" {
Err(crate::Error::MissingAuthToken)
} else {
Err(crate::Error::ApiErrorMessage(api_error))
}
}
CreateResult::Empty => panic!("How can we have 200 OK and no release info? No. Crash"),
return Ok(new_release);
} else if response.status().is_client_error() {
let mesg = crate::decode_client_error(response).await?;
return Err(crate::Error::ApiErrorMessage(mesg))
}
panic!("Reached end of create_release without matching a return path");
}
pub fn edit_release(id: u64) -> Result<Release> {
todo!();

View File

@@ -1,5 +1,7 @@
use std::fs;
use crate::structs::Attachment;
pub fn check_release_match_repo() {}
pub fn get_release_attachment() {}
pub fn list_release_attachments() {
@@ -10,23 +12,19 @@ pub async fn create_release_attachment(
gitea_url: &str,
repo: &str,
release_id: usize,
files: Vec<String>,
) -> crate::Result<()> {
file: String,
) -> crate::Result<Attachment> {
let request_url = format!("{gitea_url}/api/v1/repos/{repo}/releases/{release_id}/assets");
// Ensure all files exists before starting the uploads
for file in &files {
match fs::exists(file) {
Ok(true) => continue,
match fs::exists(&file) {
Ok(true) => (),
Ok(false) => return Err(crate::Error::NoSuchFile),
Err(e) => {
eprintln!("Uh oh! The file-exists check couldn't be done: {e}");
panic!("TODO: Deal with scenario where the file's existence cannot be checked (e.g.: no permission)");
},
}
}
for file in files {
println!("Uploading file {}", &file);
let data = reqwest::multipart::Part::stream(fs::read(&file).unwrap())
.file_name("attachment")
@@ -34,14 +32,24 @@ pub async fn create_release_attachment(
let form = reqwest::multipart::Form::new().part("attachment", data);
let request = client
let response = client
.post(&request_url)
.multipart(form)
.query(&[("name", file.split("/").last())])
.send()
.await?;
if response.status().is_success() {
// TODO: create a struct Attachment and return it to the caller.
let attachment_desc = response
.json::<Attachment>()
.await
.map_err(|e| crate::Error::from(e))?;
return Ok(attachment_desc);
} else if response.status().is_client_error() {
let mesg = crate::decode_client_error(response).await?;
return Err(crate::Error::ApiErrorMessage(mesg));
}
Ok(())
panic!("Reached end of release_attachment without matching a return path");
}
pub fn edit_release_attachment() {}
pub fn delete_release_attachment() {}
@@ -103,11 +111,12 @@ mod tests {
.await;
let api_err = api_result.expect_err("Received Ok(()) after uploading non-existent file. That's nonsense, the API Function is wrong.");
match api_err {
crate::Error::Placeholder => panic!("Received dummy response from the API function. Finish implementing it, stupid"),
crate::Error::WrappedReqwestErr(error) => panic!("Received a reqwest::Error from the API function: {error}"),
crate::Error::MissingAuthToken => unreachable!("Missing auth token... in a unit test that already panics without the auth token..."),
crate::Error::NoSuchFile => (), // test passes
crate::Error::ApiErrorMessage(api_error) => panic!("Received an error message from the API: {api_error:?}"),
crate::Error::Placeholder=>panic!("Received dummy response from the API function. Finish implementing it, stupid"),
crate::Error::WrappedReqwestErr(error)=>panic!("Received a reqwest::Error from the API function: {error}"),
crate::Error::MissingAuthToken=>unreachable!("Missing auth token... in a unit test that already panics without the auth token..."),
crate::Error::NoSuchFile=>(),
crate::Error::ApiErrorMessage(api_error)=>panic!("Received an error message from the API: {api_error:?}"),
crate::Error::NoSuchRelease => todo!(),
}
}

View File

@@ -10,6 +10,15 @@ pub struct ApiError {
url: String,
}
pub (crate) async fn decode_client_error(response: reqwest::Response) -> Result<ApiError> {
response
.json::<ApiError>()
.await
.map_err(|reqwest_err| {
crate::Error::WrappedReqwestErr(reqwest_err)
})
}
#[derive(Debug)]
pub enum Error {
Placeholder, // TODO: Enumerate error modes

View File

@@ -1,3 +1,5 @@
use std::fs;
use gt_tool::cli::Args;
use gt_tool::structs::release::{CreateReleaseOption, Release};
@@ -78,14 +80,26 @@ async fn main() -> Result<(), gt_tool::Error> {
gt_tool::api::release::list_releases(&client, &args.gitea_url, &args.repo).await?;
if let Some(release) = match_release_by_tag(&tag_name, release_candidates) {
gt_tool::api::release_attachment::create_release_attachment(
for file in &files {
match fs::exists(file) {
Ok(true) => continue,
Ok(false) => return Err(gt_tool::Error::NoSuchFile),
Err(e) => {
eprintln!("Uh oh! The file-exists check couldn't be done: {e}");
panic!("TODO: Deal with scenario where the file's existence cannot be checked (e.g.: no permission)");
},
}
}
for file in files {
let _attach_desc = gt_tool::api::release_attachment::create_release_attachment(
&client,
&args.gitea_url,
&args.repo,
release.id,
files,
file,
)
.await?;
}
} else {
println!("ERR: Couldn't find a release matching the tag \"{tag_name}\".");
return Err(gt_tool::Error::NoSuchRelease);

View File

@@ -1,2 +1,15 @@
use serde::{Deserialize, Serialize};
pub mod release;
pub mod repo;
#[derive(Debug, Deserialize, Serialize)]
pub struct Attachment {
id: usize,
name: String,
size: i64,
download_count: i64,
created: String, // TODO: Date-time struct
uuid: String,
download_url: String,
}