-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Adam Perkowski <[email protected]> Signed-off-by: Adam Perkowski <[email protected]>
- Loading branch information
1 parent
9e115ac
commit 4b015d5
Showing
6 changed files
with
126 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use crate::{api, error}; | ||
use reqwest::{header::HeaderValue, Response}; | ||
|
||
#[derive(serde::Deserialize)] | ||
#[serde(transparent)] | ||
struct GiteaTagResponse { | ||
tags: Vec<GiteaTag>, | ||
} | ||
|
||
#[derive(serde::Deserialize)] | ||
struct GiteaTag { | ||
name: String, | ||
} | ||
|
||
#[derive(serde::Deserialize)] | ||
#[serde(transparent)] | ||
struct GiteaReleaseResponse { | ||
releases: Vec<GiteaRelease>, | ||
} | ||
|
||
#[derive(serde::Deserialize)] | ||
struct GiteaRelease { | ||
tag_name: String, | ||
html_url: String, | ||
} | ||
|
||
/// get the latest version of a package from Gitea | ||
pub fn get_latest(args: api::ApiArgs) -> api::ReleaseFuture { | ||
Box::pin(async move { | ||
let host = if !args.args[1].is_empty() { | ||
&args.args[1] | ||
} else { | ||
"gitea.com" | ||
}; | ||
let repo_url = format!("https://{}/api/v1/repos/{}", host, args.args[0]); | ||
|
||
if args.use_max_tag.is_some_and(|x| x) { | ||
let url = format!("{}/tags", repo_url); | ||
|
||
let result = request(url, &args).await?; | ||
let json: &GiteaTag = &result.json::<GiteaTagResponse>().await?.tags[0]; | ||
|
||
Ok(api::Release { | ||
name: json.name.clone(), | ||
tag: Some(json.name.clone()), | ||
url: format!("{}/releases/tag/{}", repo_url, json.name), | ||
}) | ||
} else { | ||
let url = format!("{}/releases", repo_url); | ||
let result = request(url, &args).await?; | ||
let json: &GiteaRelease = &result.json::<GiteaReleaseResponse>().await?.releases[0]; | ||
|
||
let tag = json.tag_name.to_owned(); | ||
|
||
Ok(api::Release { | ||
name: tag.clone(), | ||
tag: Some(tag), | ||
url: json.html_url.clone(), | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
async fn request(url: String, args: &api::ApiArgs) -> error::Result<Response> { | ||
let mut headers = api::setup_headers(); | ||
if !args.api_key.is_empty() { | ||
headers.insert( | ||
"PRIVATE-TOKEN", | ||
HeaderValue::from_str(&args.api_key).unwrap(), | ||
); | ||
}; | ||
let client = &args.request_client; | ||
|
||
let result = client.get(url).headers(headers).send().await?; | ||
api::match_statuscode(&result.status(), args.package.clone())?; | ||
|
||
Ok(result) | ||
} | ||
|
||
#[tokio::test] | ||
async fn request_test() { | ||
let package = "maandree/libkeccak".to_string(); | ||
let args = api::ApiArgs { | ||
request_client: reqwest::Client::new(), | ||
package: package.clone(), | ||
use_max_tag: Some(true), | ||
args: vec![package, "codeberg.org".to_string()], | ||
api_key: String::new(), | ||
}; | ||
|
||
assert!(get_latest(args).await.is_ok()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters