http_client: Replace build_tarball_url
with a more extensible function (#15332)
This PR replaces the `build_tarball_url` with `build_asset_url` that accepts an `AssetKind` enum to support downloading different kinds of assets from GitHub. Right now the only asset kind we support is still `.tar.gz`, but the new structure is more amenable to adding more asset kinds. Release Notes: - N/A
This commit is contained in:
parent
64add2f222
commit
138c3fcfdd
2 changed files with 31 additions and 13 deletions
|
@ -117,32 +117,41 @@ pub async fn get_release_by_tag_name(
|
|||
Ok(release)
|
||||
}
|
||||
|
||||
pub fn build_tarball_url(repo_name_with_owner: &str, tag: &str) -> Result<String> {
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub enum AssetKind {
|
||||
TarGz,
|
||||
}
|
||||
|
||||
pub fn build_asset_url(repo_name_with_owner: &str, tag: &str, kind: AssetKind) -> Result<String> {
|
||||
let mut url = Url::parse(&format!(
|
||||
"https://github.com/{repo_name_with_owner}/archive/refs/tags",
|
||||
))?;
|
||||
// We're pushing this here, because tags may contain `/` and other characters
|
||||
// that need to be escaped.
|
||||
let tarball_filename = format!("{}.tar.gz", tag);
|
||||
let asset_filename = format!(
|
||||
"{tag}.{extension}",
|
||||
extension = match kind {
|
||||
AssetKind::TarGz => "tar.gz",
|
||||
}
|
||||
);
|
||||
url.path_segments_mut()
|
||||
.map_err(|_| anyhow!("cannot modify url path segments"))?
|
||||
.push(&tarball_filename);
|
||||
.push(&asset_filename);
|
||||
Ok(url.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::github::build_tarball_url;
|
||||
use crate::github::{build_asset_url, AssetKind};
|
||||
|
||||
#[test]
|
||||
fn test_build_tarball_url() {
|
||||
fn test_build_asset_url() {
|
||||
let tag = "release/2.3.5";
|
||||
let repo_name_with_owner = "microsoft/vscode-eslint";
|
||||
|
||||
let have = build_tarball_url(repo_name_with_owner, tag).unwrap();
|
||||
|
||||
let tarball = build_asset_url(repo_name_with_owner, tag, AssetKind::TarGz).unwrap();
|
||||
assert_eq!(
|
||||
have,
|
||||
tarball,
|
||||
"https://github.com/microsoft/vscode-eslint/archive/refs/tags/release%2F2.3.5.tar.gz"
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue