Skip to content

Commit

Permalink
Implement GitHub release with asset
Browse files Browse the repository at this point in the history
  • Loading branch information
jadelily18 committed Oct 17, 2023
1 parent 53af6e2 commit ba00d70
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 22 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# mrpack distributor

**NOTE: This code is very, _very_ messy right now. There are likely lots of bugs! I am fully aware of this,
I'm just doing my best to get it working as fast as possible so I can use it in my modpacks**

Builds from a packwiz project and distributes it to Modrinth and/or GitHub Releases.

## License

Licensed under both the [MIT License](/LICENSE-MIT) and [Apache 2.0](LICENSE-APACHE).
89 changes: 80 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use glob::glob;
use crate::{
models::pack::PackFile
};
use crate::models::github::ReleaseResponse;
use crate::models::github::{CreateReleaseRequest, ReleaseResponse};
use crate::models::meta::Config;
use crate::models::modrinth::{VersionRequest, VersionStatus, VersionType};

Expand Down Expand Up @@ -208,6 +208,13 @@ async fn main() -> Result<(), anyhow::Error> {
.replace("%mc_version%", &pack_file.versions.minecraft)
.replace("%loader%", loader);

let mrpack_file_contents = match fs::read(&*mrpack_path) {
Ok(file) => file,
Err(err) => return Err(anyhow!(
"Failed to read .mrpack file: {}", err
))
};

// Changelog

println!("Generating changelog...");
Expand Down Expand Up @@ -268,7 +275,77 @@ async fn main() -> Result<(), anyhow::Error> {

// TODO: Create GitHub release

println!("Successfully created GitHub release!");
let github_token = match env::var("GITHUB_TOKEN") {
Ok(token) => token,
Err(err) => return Err(anyhow!(
"Failed to get `GITHUB_TOKEN`: {}", err
))
};

let new_release_req_body = CreateReleaseRequest {
tag_name: pack_file.version.clone(),
name: Some(version_name.clone()),
body: Some(changelog_markdown.clone())
};

let new_release_response = match reqwest::Client::new()
.post(
format!(
"https://api.github.com/repos/{}/{}/releases",
config_file.github.repo_owner.clone(),
config_file.github.repo_name.clone()
)
)
.json(&new_release_req_body)
.header("User-Agent", env!("CARGO_PKG_NAME"))
.header("Accept", "application/vnd.github+json")
.bearer_auth(github_token.clone())
.send().await {
Ok(res) => {
match res.json::<ReleaseResponse>().await {
Ok(json) => Ok(json),
Err(err) => Err(err)
}
},
Err(err) => {
return Err(anyhow::Error::from(err))
}
};




match new_release_response {
Ok(release_res) => {
println!("Successfully created GitHub release!");
println!("Uploading release asset to GitHub release...");

match reqwest::Client::new()
.post(
format!(
"https://uploads.github.com/repos/{}/{}/releases/{}/assets?name=\"{}\"",
config_file.github.repo_owner,
config_file.github.repo_name,
release_res.id,
file_name.replace(" ", "%20")
)
)
.header("User-Agent", env!("CARGO_PKG_NAME"))
.header("Accept", "application/vnd.github+json")
.header("Content-Type", "application/zip")
.bearer_auth(github_token)
.body(mrpack_file_contents.clone())
.send().await {
Ok(_) => println!("Successfully uploaded release asset!"),
Err(_) => println!("Failed to upload release asset.")
};

},
Err(err) => println!("Failed to create GitHub release: {}", err)
}




// Modrinth Release

Expand Down Expand Up @@ -298,13 +375,7 @@ async fn main() -> Result<(), anyhow::Error> {
))
};

let mrpack_file = match fs::read(&*mrpack_path) {
Ok(file) => file,
Err(err) => return Err(anyhow!(
"Failed to read .mrpack file: {}", err
))
};
let file_part = match reqwest::multipart::Part::bytes(mrpack_file)
let file_part = match reqwest::multipart::Part::bytes(mrpack_file_contents)
.file_name(file_name)
.mime_str("application/zip") {
Ok(part) => part,
Expand Down
20 changes: 7 additions & 13 deletions src/models/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@ use serde::{Serialize, Deserialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateReleaseRequest {
pub tag_name: String,
pub target_commitish: Option<String>,
pub name: Option<String>,
pub body: Option<String>,
pub draft: Option<bool>,
pub prerelease: Option<bool>,
pub discussion_category_name: Option<String>,
pub generate_release_notes: Option<bool>,
pub make_latest: Option<String>
pub body: Option<String>
}

#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -33,11 +27,11 @@ pub struct ReleaseResponse {
pub published_at: Option<String>,
pub author: GithubAuthor,
pub assets: Vec<GithubAsset>,
pub body_html: String,
pub body_text: String,
pub mentions_count: i32,
pub discussion_url: String,
pub reactions: GithubReactions
pub body_html: Option<String>,
pub body_text: Option<String>,
pub mentions_count: Option<i32>,
pub discussion_url: Option<String>,
pub reactions: Option<GithubReactions>
}

#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -63,7 +57,7 @@ pub struct GithubAuthor {
#[serde(rename = "type")]
pub type_string: String,
pub site_admin: bool,
pub starred_at: String
pub starred_at: Option<String>
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down

0 comments on commit ba00d70

Please sign in to comment.