Skip to content

Commit

Permalink
Merge pull request #18 from ddotthomas/fix-custom-proton-version
Browse files Browse the repository at this point in the history
Removes extra github requests for each tag by reusing data from the List Releases requests
  • Loading branch information
auyer committed Dec 17, 2023
2 parents 666fa26 + 8c2c605 commit d3bf27b
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 440 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/enforce_label.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ jobs:
- uses: yogevbd/enforce-label-action@2.2.2
with:
REQUIRED_LABELS_ANY: "🚀 Feature,💅 Improvement,🐛 Bug,📚 Docs,🧪 Tests,☁️ CI,🚨 Security,🤖 Dependencies"
REQUIRED_LABELS_ANY_DESCRIPTION: "It is necessary to add a label to your PR. This will help to categorize it and add a note on the release. Chose one of [🚀 Feature,💅 Improvement,🐛 Bug,📚 Docs,🧪 Tests,☁️ CI,🚨 Security,🤖 Dependencies]"
REQUIRED_LABELS_ANY_DESCRIPTION: "It is necessary to add a label to your PR. This will help to categorize it and add a note on the release. Choose one of [🚀 Feature,💅 Improvement,🐛 Bug,📚 Docs,🧪 Tests,☁️ CI,🚨 Security,🤖 Dependencies]"
9 changes: 1 addition & 8 deletions libprotonup/src/apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ impl fmt::Display for App {
}

impl App {
pub fn app_available_variants(&self) -> Vec<Variant> {
match *self {
Self::Steam => vec![Variant::GEProton],
Self::Lutris => vec![Variant::WineGE, Variant::GEProton],
}
}

pub fn app_default_variant(&self) -> Variant {
pub fn app_wine_version(&self) -> Variant {
match *self {
Self::Steam => Variant::GEProton,
Self::Lutris => Variant::WineGE,
Expand Down
154 changes: 49 additions & 105 deletions libprotonup/src/github.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
use crate::constants;
use crate::variants::VariantParameters;
use crate::variants::VariantGithubParameters;
use anyhow::Result;
use serde::{Deserialize, Serialize};

pub type ReleaseList = Vec<Release>;

#[derive(Serialize, Deserialize, Debug)]
pub struct Release {
url: String,
/// API URL of the Release
url: Option<String>,
/// Tag name of the Release, examples "8.7-GE-1-Lol" "GE-Proton8-5"
pub tag_name: String,
name: String,
published_at: String,
/// Asset list for each Release, usually the tar.gz/tar.xz file and a sha512sum file for integrity checking
assets: Vec<Asset>,
}

impl std::fmt::Display for Release {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{}", self.tag_name)
}
}

impl Release {
/// Returns a Download struct corresponding to the Release
pub fn get_download_info(&self) -> Download {
let mut download: Download = Download::default();
download.version = self.tag_name.clone();
for asset in &self.assets {
if asset.name.ends_with("sha512sum") {
download.sha512sum_url = asset.browser_download_url.clone();
} else if asset.name.ends_with("tar.gz") || asset.name.ends_with("tar.xz") {
download.download_url = asset.browser_download_url.clone();
download.size = asset.size as u64;
}
}

download
}
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Asset {
url: String,
id: i64,
name: String,
size: i64,
created_at: String,
updated_at: String,
browser_download_url: String,
}

pub async fn list_releases(source: &VariantParameters) -> Result<ReleaseList, reqwest::Error> {
/// Returns a Vec of Releases from a GitHub repository, the URL used for the request is built from the passed in VariantParameters
pub async fn list_releases(
source: &VariantGithubParameters,
) -> Result<ReleaseList, reqwest::Error> {
let agent = format!("{}/v{}", constants::USER_AGENT, constants::VERSION,);

let url = format!(
Expand All @@ -43,57 +71,9 @@ pub async fn list_releases(source: &VariantParameters) -> Result<ReleaseList, re
#[derive(Default, Debug, PartialEq, Clone)]
pub struct Download {
pub version: String,
pub sha512sum: String,
pub download: String,
pub sha512sum_url: String,
pub download_url: String,
pub size: u64,
pub created_at: String,
}

pub async fn fetch_data_from_tag(
tag: &str,
source: &VariantParameters,
) -> Result<Download, reqwest::Error> {
let agent = format!("{}/v{}", constants::USER_AGENT, constants::VERSION,);

let client = reqwest::Client::builder().user_agent(agent).build()?;

let mut download = Download::default();
let release = match tag {
"latest" => {
let url = format!(
"{}/{}/{}/releases/latest",
source.repository_url, source.repository_account, source.repository_name,
);
let rel: Release = client.get(url).send().await?.json().await?;
rel
}
_ => {
let url = format!(
"{}/{}/{}/releases/tags/{}",
source.repository_url, source.repository_account, source.repository_name, &tag
);
let rel: Release = client.get(url).send().await?.json().await?;
rel
}
};

download.version = release.tag_name;
for asset in &release.assets {
if asset.name.ends_with("sha512sum") {
download.sha512sum = asset.browser_download_url.as_str().to_string();
}
if asset.name.ends_with("tar.gz") {
download.created_at = asset.created_at.clone();
download.download = asset.browser_download_url.as_str().to_string();
download.size = asset.size as u64;
}
if asset.name.ends_with("tar.xz") {
download.created_at = asset.created_at.clone();
download.download = asset.browser_download_url.as_str().to_string();
download.size = asset.size as u64;
}
}
Ok(download)
}
#[cfg(test)]
mod tests {
Expand All @@ -102,66 +82,24 @@ mod tests {
use super::*;

#[tokio::test]
async fn test_fetch_data_from_tag() {
async fn test_list_releases() {
let conditions = &[
(
variants::Variant::WineGE.parameters(),
"latest",
"Get Steam",
variants::Variant::WineGE.get_github_parameters(),
"List WineGE",
),
(
variants::Variant::GEProton.parameters(),
"latest",
"Download Lutris",
variants::Variant::GEProton.get_github_parameters(),
"List GEProton",
),
];
for (source_parameters, tag, desc) in conditions {
let result = fetch_data_from_tag(tag, source_parameters).await;

assert!(
result.is_ok(),
"case :{} test: fetch_data_from_tag returned error",
desc
);

let result = result.unwrap();

assert!(
result.download.len() > 5,
"case : '{}' test: fetch_data_from_tag returned an wrong download link",
desc
);
assert!(
result.sha512sum.len() > 5,
"case : '{}' test: fetch_data_from_tag returned an wrong sha512sum",
desc
);
assert!(
result.size > 100,
"case : '{}' test: fetch_data_from_tag returned an wrong sha512sum",
desc
);
assert!(
result.version.len() > 2,
"case : '{}' test: fetch_data_from_tag returned an wrong version",
desc
);
}
}

#[tokio::test]
async fn test_list_releases() {
let conditions = &[
(variants::Variant::WineGE.parameters(), "List WineGE"),
(variants::Variant::GEProton.parameters(), "List GEProton"),
];

for (source_parameters, desc) in conditions {
let result = list_releases(source_parameters).await;

assert!(
result.is_ok(),
"case : '{}' test: fetch_data_from_tag returned error",
"case : '{}' test: list_releases returned error",
desc
);

Expand All @@ -188,8 +126,14 @@ mod tests {
};

let conditions = &[
(variants::Variant::WineGE.parameters(), "Get WineGE"),
(variants::Variant::GEProton.parameters(), "Get GEProton"),
(
variants::Variant::WineGE.get_github_parameters(),
"Get WineGE",
),
(
variants::Variant::GEProton.get_github_parameters(),
"Get GEProton",
),
];
for (source_parameters, desc) in conditions {
let url = format!(
Expand Down
20 changes: 11 additions & 9 deletions libprotonup/src/variants.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::constants::*;
use std::{fmt, str::FromStr};
// VariantParameters stores the parameters for a variant of Proton
pub struct VariantParameters {

/// Struct used to build GitHub api request URLs.
/// Contains the GitHub URL, username for GE, the repository name for either Wine GE or Proton GE, and a Variant Enum for identifying the parameters type
pub struct VariantGithubParameters {
/// this is a link back to the enum variant
variant_ref: Variant,
/// URL of the repository server (GitHub compatible URL only at the moment)
Expand All @@ -12,15 +14,15 @@ pub struct VariantParameters {
pub repository_name: String,
}

impl VariantParameters {
impl VariantGithubParameters {
/// new_custom is a generator for custom VariantParameters
pub fn new_custom(
variant: Variant,
repository_url: String,
repository_account: String,
repository_name: String,
) -> VariantParameters {
VariantParameters {
) -> VariantGithubParameters {
VariantGithubParameters {
variant_ref: variant,
repository_url,
repository_account,
Expand Down Expand Up @@ -71,16 +73,16 @@ impl Variant {
}
}

/// returns the default parameters for this Variant.
pub fn parameters(&self) -> VariantParameters {
/// Returns the default parameters for this Variant, used to build the GitHub URL
pub fn get_github_parameters(&self) -> VariantGithubParameters {
match self {
Variant::GEProton => VariantParameters {
Variant::GEProton => VariantGithubParameters {
variant_ref: Variant::GEProton,
repository_url: GITHUB_URL.to_owned(),
repository_name: GEPROTON_GITHUB_REPO.to_owned(),
repository_account: GE_GITHUB_ACCOUNT.to_owned(),
},
Variant::WineGE => VariantParameters {
Variant::WineGE => VariantGithubParameters {
variant_ref: Variant::WineGE,
repository_url: GITHUB_URL.to_owned(),
repository_name: WINEGE_GITHUB_REPO.to_owned(),
Expand Down
Loading

0 comments on commit d3bf27b

Please sign in to comment.