-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/houseabsolute/ubi?shareId=XXXX-XXXX-XXXX-XXXX).
- Loading branch information
Showing
5 changed files
with
123 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,4 @@ zip = { version = "2.2.1", default-features = false, features = [ | |
"lzma", | ||
"zstd", | ||
] } | ||
async-trait = "0.1.50" |
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,13 @@ | ||
use anyhow::Result; | ||
Check failure on line 1 in ubi/src/forge.rs GitHub Actions / Check that code is lint clean using precious
|
||
use async_trait::async_trait; | ||
use reqwest::Client; | ||
use url::Url; | ||
|
||
use crate::release::{Asset, Release}; | ||
|
||
#[async_trait] | ||
pub(crate) trait Forge { | ||
fn new(project_name: String, tag: Option<String>, url: Option<Url>, api_base: Option<String>) -> Self; | ||
async fn fetch_assets(&self, client: &Client) -> Result<Vec<Asset>>; | ||
async fn release_info(&self, client: &Client) -> Result<Release>; | ||
} |
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,75 @@ | ||
use crate::forge::Forge; | ||
use crate::release::{Asset, Release}; | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use reqwest::{header::HeaderValue, header::ACCEPT, Client}; | ||
use url::Url; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct GitLabAssetFetcher { | ||
project_name: String, | ||
tag: Option<String>, | ||
url: Option<Url>, | ||
gitlab_api_base: String, | ||
} | ||
|
||
const GITLAB_API_BASE: &str = "https://gitlab.com/api/v4"; | ||
|
||
#[async_trait] | ||
impl Forge for GitLabAssetFetcher { | ||
fn new( | ||
project_name: String, | ||
tag: Option<String>, | ||
url: Option<Url>, | ||
gitlab_api_base: Option<String>, | ||
) -> Self { | ||
Self { | ||
project_name, | ||
tag, | ||
url, | ||
gitlab_api_base: gitlab_api_base.unwrap_or(GITLAB_API_BASE.to_string()), | ||
} | ||
} | ||
|
||
async fn fetch_assets(&self, client: &Client) -> Result<Vec<Asset>> { | ||
if let Some(url) = &self.url { | ||
return Ok(vec![Asset { | ||
name: url.path().split('/').last().unwrap().to_string(), | ||
url: url.clone(), | ||
}]); | ||
} | ||
|
||
Ok(self.release_info(client).await?.assets) | ||
} | ||
|
||
async fn release_info(&self, client: &Client) -> Result<Release> { | ||
let mut parts = self.project_name.split('/'); | ||
let owner = parts.next().unwrap(); | ||
let repo = parts.next().unwrap(); | ||
|
||
let url = match &self.tag { | ||
Some(tag) => format!( | ||
"{}/projects/{}/releases/{}", | ||
self.gitlab_api_base, | ||
urlencoding::encode(&format!("{}/{}", owner, repo)), | ||
tag, | ||
), | ||
None => format!( | ||
"{}/projects/{}/releases", | ||
self.gitlab_api_base, | ||
urlencoding::encode(&format!("{}/{}", owner, repo)), | ||
), | ||
}; | ||
let req = client | ||
.get(url) | ||
.header(ACCEPT, HeaderValue::from_str("application/json")?) | ||
.build()?; | ||
let resp = client.execute(req).await?; | ||
|
||
if let Err(e) = resp.error_for_status_ref() { | ||
return Err(anyhow::Error::new(e)); | ||
} | ||
|
||
Ok(resp.json::<Release>().await?) | ||
} | ||
} |
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