use crate::github::GithubRelease;
use crate::http;
use crate::http::HTTP_FETCH;
use eyre::bail;
use mise_settings::Settings;
use mise_sigstore::Attestation;
use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT};
use std::{
collections::HashSet,
sync::{
LazyLock,
atomic::{AtomicBool, Ordering},
},
};
pub static VERSIONS_HOST_HEADERS: LazyLock<HeaderMap> =
LazyLock::new(|| versions_host_headers(*crate::env::IS_CI, crate::user_agent::get()));
fn versions_host_headers(ci: bool, user_agent: &str) -> HeaderMap {
let mut headers = HeaderMap::new();
if ci {
headers.insert("x-mise-ci", HeaderValue::from_static("true"));
if let Ok(user_agent) = HeaderValue::from_str(&format!("{user_agent} ci")) {
headers.insert(USER_AGENT, user_agent);
}
}
headers
}
pub const GITHUB_RELEASES_MAX_PAGES: usize = 10;
#[derive(serde::Deserialize)]
pub struct GithubReleasesPage {
pub releases: Vec<GithubRelease>,
pub next_page: Option<u32>,
#[serde(default)]
pub truncated: bool,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct GithubVersionsList {
pub source: String,
pub fetched_at: Option<String>,
#[serde(default)]
pub releases: Vec<GithubListedRelease>,
#[serde(default)]
pub tags: Vec<GithubListedTag>,
#[serde(default)]
pub truncated: bool,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct GithubListedRelease {
pub tag: String,
pub date: Option<String>,
pub prerelease: bool,
pub asset_count: u32,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct GithubListedTag {
pub tag: String,
pub date: Option<String>,
}
#[derive(Debug, Clone, Copy)]
pub enum GithubList<'a> {
Releases,
Tags,
ReleasesWithAsset(&'a str),
}
#[derive(serde::Deserialize)]
pub struct GithubTagsPage {
pub tags: Vec<GithubMirroredTag>,
pub next_page: Option<u32>,
#[serde(default)]
pub truncated: bool,
}
#[derive(serde::Deserialize)]
pub struct GithubMirroredTag {
pub name: String,
#[serde(default)]
pub date: Option<String>,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct GithubContentsEntry {
pub name: String,
#[serde(rename = "type")]
pub kind: String,
}
#[derive(serde::Deserialize)]
struct AttestationsResponse {
attestations: Vec<Attestation>,
}
#[derive(Clone, Copy)]
pub struct VersionsHostLogContext<'a> {
endpoint: &'static str,
tool: Option<&'a str>,
repo: Option<&'a str>,
tag: Option<&'a str>,
digest: Option<&'a str>,
page: Option<u32>,
full: Option<&'a str>,
version: Option<&'a str>,
}
impl<'a> VersionsHostLogContext<'a> {
pub fn version_list(tool: &'a str) -> Self {
Self {
endpoint: "version_list",
tool: Some(tool),
repo: None,
tag: None,
digest: None,
page: None,
full: None,
version: None,
}
}
fn github_release(repo: &'a str, tag: &'a str) -> Self {
Self {
endpoint: "github_release",
tool: None,
repo: Some(repo),
tag: Some(tag),
digest: None,
page: None,
full: None,
version: None,
}
}
fn github_releases(repo: &'a str, page: u32) -> Self {
Self {
endpoint: "github_releases",
tool: None,
repo: Some(repo),
tag: None,
digest: None,
page: Some(page),
full: None,
version: None,
}
}
fn github_tags(repo: &'a str, page: u32) -> Self {
Self {
endpoint: "github_tags",
tool: None,
repo: Some(repo),
tag: None,
digest: None,
page: Some(page),
full: None,
version: None,
}
}
fn github_repo(endpoint: &'static str, repo: &'a str) -> Self {
Self {
endpoint,
tool: None,
repo: Some(repo),
tag: None,
digest: None,
page: None,
full: None,
version: None,
}
}
fn github_attestations(repo: &'a str, digest: &'a str) -> Self {
Self {
endpoint: "github_attestations",
tool: None,
repo: Some(repo),
tag: None,
digest: Some(digest),
page: None,
full: None,
version: None,
}
}
pub fn install_track(tool: &'a str, full: &'a str, version: &'a str) -> Self {
Self {
endpoint: "install_track",
tool: Some(tool),
repo: None,
tag: None,
digest: None,
page: None,
full: Some(full),
version: Some(version),
}
}
fn fields(&self) -> String {
let mut fields = format!("endpoint={}", self.endpoint);
if let Some(tool) = self.tool {
fields.push_str(&format!(" tool={}", log_value(tool)));
}
if let Some(repo) = self.repo {
fields.push_str(&format!(" repo={}", log_value(repo)));
}
if let Some(tag) = self.tag {
fields.push_str(&format!(" tag={}", log_value(tag)));
}
if let Some(digest) = self.digest {
fields.push_str(&format!(" digest={}", log_value(digest)));
}
if let Some(page) = self.page {
fields.push_str(&format!(" page={page}"));
}
if let Some(full) = self.full {
fields.push_str(&format!(
" full={}",
log_value(&sanitize_full_for_log(full))
));
}
if let Some(version) = self.version {
fields.push_str(&format!(" version={}", log_value(version)));
}
fields
}
}
pub fn log_value(value: &str) -> String {
if value
.bytes()
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-' | b'/' | b':' | b'@'))
{
value.to_string()
} else {
format!("{:?}", value)
}
}
pub fn sanitize_full_for_log(full: &str) -> String {
sanitize_full(full).unwrap_or_else(|| "[redacted]".to_string())
}
pub fn sanitize_full(full: &str) -> Option<String> {
fn strip(mut url: url::Url) -> url::Url {
let _ = url.set_username("");
let _ = url.set_password(None);
url.set_query(None);
url.set_fragment(None);
url
}
let with_host = |s: &str| url::Url::parse(s).ok().filter(|url| url.has_host());
if let Some((backend, value)) = full.split_once(':') {
if value.starts_with("//") {
return with_host(full).map(|url| strip(url).to_string());
}
if let Some(url) = with_host(value) {
return Some(format!("{backend}:{}", strip(url)));
}
}
if full.contains("://") {
return None;
}
Some(full.to_string())
}
pub fn log_versions_host_trace(ctx: VersionsHostLogContext<'_>, outcome: &str, extra: &str) {
if extra.is_empty() {
trace!("mise-versions {} outcome={outcome}", ctx.fields());
} else {
trace!("mise-versions {} outcome={outcome} {extra}", ctx.fields());
}
}
pub fn log_versions_host_warn(ctx: VersionsHostLogContext<'_>, outcome: &str, extra: &str) {
if extra.is_empty() {
warn!("mise-versions {} outcome={outcome}", ctx.fields());
} else {
warn!("mise-versions {} outcome={outcome} {extra}", ctx.fields());
}
}
pub async fn github_release(repo: &str, tag: &str) -> eyre::Result<Option<GithubRelease>> {
if !enabled_for_github_repo(repo) || github_release_is_url_replaced(repo, tag) {
return Ok(None);
}
let Some((owner, repo_name)) = split_github_repo(repo) else {
return Ok(None);
};
let url = format!(
"https://mise-versions.jdx.dev/api/github/repos/{}/{}/releases/{}",
encode_path_segment(owner),
encode_path_segment(repo_name),
encode_path_segment(tag)
);
let ctx = VersionsHostLogContext::github_release(repo, tag);
let Some(mut release) = fetch_json_unless_not_found::<GithubRelease>(&url, ctx).await? else {
return Ok(None);
};
if !valid_github_release_tag(&release, tag) {
log_versions_host_trace(
ctx,
"tag_mismatch",
&format!("returned_tag={}", log_value(&release.tag_name)),
);
bail!(
"mise-versions answered for {repo}@{tag} with release {}",
release.tag_name
);
}
if let Err(asset) = pin_github_release_assets(&mut release, owner, repo_name) {
log_versions_host_trace(
ctx,
"invalid_asset_url",
&format!("asset={}", log_value(&asset)),
);
bail!("mise-versions listed asset {asset} of {repo}@{tag} at an unexpected URL");
}
log_versions_host_trace(ctx, "success", "");
Ok(Some(release))
}
pub async fn github_releases(repo: &str, page: u32) -> eyre::Result<Option<GithubReleasesPage>> {
if !enabled_for_github_repo(repo) {
return Ok(None);
}
let Some((owner, repo_name)) = split_github_repo(repo) else {
return Ok(None);
};
let url = github_releases_url(owner, repo_name, page);
let ctx = VersionsHostLogContext::github_releases(repo, page);
let Some(mut list) = fetch_json_unless_not_found::<GithubReleasesPage>(&url, ctx).await? else {
return Ok(None);
};
if list.next_page.is_some_and(|next| next != page + 1) {
log_versions_host_trace(ctx, "invalid_next_page", "");
bail!("mise-versions pointed page {page} of {repo}'s releases at an unexpected next page");
}
if let Err(tag) = pin_listed_releases(&mut list, owner, repo_name) {
log_versions_host_trace(ctx, "invalid_release", &format!("tag={}", log_value(&tag)));
bail!("mise-versions listed release {tag} of {repo} with an unexpected asset URL");
}
log_versions_host_trace(ctx, "success", &format!("releases={}", list.releases.len()));
Ok(Some(list))
}
pub async fn github_versions_list(
repo: &str,
list: GithubList<'_>,
) -> eyre::Result<Option<GithubVersionsList>> {
if !enabled_for_github_repo(repo) {
return Ok(None);
}
let Some(url) = github_versions_list_url(repo, list) else {
return Ok(None);
};
let ctx = VersionsHostLogContext::github_repo("github_versions_list", repo);
let Some(found) = fetch_json_unless_not_found::<GithubVersionsList>(&url, ctx).await? else {
return Ok(None);
};
let expected = match list {
GithubList::Tags => "tags",
GithubList::Releases | GithubList::ReleasesWithAsset(_) => "releases",
};
if found.source != expected {
bail!(
"mise-versions answered {url} with a list of {}",
found.source
);
}
log_versions_host_trace(
ctx,
"success",
&format!(
"entries={} truncated={}",
found.releases.len() + found.tags.len(),
found.truncated
),
);
Ok(Some(found))
}
fn github_versions_list_url(repo: &str, list: GithubList<'_>) -> Option<String> {
let (owner, name) = split_github_repo(repo)?;
let part = |s: &str| {
let s = s.to_ascii_lowercase();
let valid = !s.is_empty()
&& s != "."
&& s != ".."
&& s.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | '-'));
valid.then_some(s)
};
let (owner, name) = (part(owner)?, part(name)?);
let file = match list {
GithubList::Releases => "releases.json".to_string(),
GithubList::Tags => "tags.json".to_string(),
GithubList::ReleasesWithAsset(asset) => {
let valid = (1..=255).contains(&asset.len())
&& asset.chars().all(|c| {
c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '+' | '@' | '~' | '-')
});
if !valid {
return None;
}
format!("assets/{asset}/releases.json")
}
};
Some(format!(
"https://mise-versions.jdx.dev/data/github/{owner}/{name}/{file}"
))
}
pub async fn github_tags(
repo: &str,
page: u32,
dates: bool,
) -> eyre::Result<Option<GithubTagsPage>> {
if !enabled_for_github_repo(repo) {
return Ok(None);
}
let Some((owner, repo_name)) = split_github_repo(repo) else {
return Ok(None);
};
let url = github_tags_url(owner, repo_name, page, dates);
let ctx = VersionsHostLogContext::github_tags(repo, page);
let Some(list) = fetch_json_unless_not_found::<GithubTagsPage>(&url, ctx).await? else {
return Ok(None);
};
if list.next_page.is_some_and(|next| next != page + 1) {
log_versions_host_trace(ctx, "invalid_next_page", "");
bail!("mise-versions pointed page {page} of {repo}'s tags at an unexpected next page");
}
log_versions_host_trace(ctx, "success", &format!("tags={}", list.tags.len()));
Ok(Some(list))
}
fn github_tags_url(owner: &str, repo: &str, page: u32, dates: bool) -> String {
format!(
"https://mise-versions.jdx.dev/api/github/repos/{}/{}/tags?page={page}{}",
encode_path_segment(owner),
encode_path_segment(repo),
if dates { "&dates=1" } else { "" }
)
}
pub async fn github_contents(
repo: &str,
commit: &str,
path: &str,
) -> eyre::Result<Option<Vec<GithubContentsEntry>>> {
if !enabled_for_github_repo(repo) {
return Ok(None);
}
let Some((owner, repo_name)) = split_github_repo(repo) else {
return Ok(None);
};
let url = github_contents_url(owner, repo_name, commit, path);
let ctx = VersionsHostLogContext::github_repo("github_contents", repo);
let entries = fetch_json_unless_not_found(&url, ctx).await?;
if let Some(entries) = &entries {
let entries: &Vec<GithubContentsEntry> = entries;
log_versions_host_trace(ctx, "success", &format!("entries={}", entries.len()));
}
Ok(entries)
}
fn github_contents_url(owner: &str, repo: &str, commit: &str, path: &str) -> String {
let path = path
.split('/')
.filter(|segment| !segment.is_empty())
.map(encode_path_segment)
.collect::<Vec<_>>()
.join("/");
format!(
"https://mise-versions.jdx.dev/api/github/repos/{}/{}/contents/{path}?ref={}",
encode_path_segment(owner),
encode_path_segment(repo),
encode_path_segment(commit),
)
}
#[derive(Debug, PartialEq, Eq)]
pub enum PackslipList {
Absent,
Present(String),
}
#[derive(serde::Deserialize)]
struct PackslipListResponse {
present: bool,
text: Option<String>,
}
pub async fn github_packslip_list(
repo: &str,
sub: Option<&str>,
) -> eyre::Result<Option<PackslipList>> {
if !enabled_for_github_repo(repo) {
return Ok(None);
}
let Some((owner, repo_name)) = split_github_repo(repo) else {
return Ok(None);
};
let url = github_packslip_list_url(owner, repo_name, sub);
let ctx = VersionsHostLogContext::github_repo("github_packslip_list", repo);
let Some(response) = fetch_json_unless_not_found::<PackslipListResponse>(&url, ctx).await?
else {
return Ok(None);
};
let list = match (response.present, response.text) {
(false, _) => PackslipList::Absent,
(true, Some(text)) => PackslipList::Present(text),
(true, None) => bail!("invalid response from {url}: a list with no text"),
};
log_versions_host_trace(
ctx,
"success",
&format!("present={}", matches!(list, PackslipList::Present(_))),
);
Ok(Some(list))
}
fn github_packslip_list_url(owner: &str, repo: &str, sub: Option<&str>) -> String {
let mut url = format!(
"https://mise-versions.jdx.dev/api/github/repos/{}/{}/packslip-list",
encode_path_segment(owner),
encode_path_segment(repo),
);
for segment in sub.into_iter().flat_map(|sub| sub.split('/')) {
url.push('/');
url.push_str(&encode_path_segment(segment));
}
url
}
fn github_releases_url(owner: &str, repo: &str, page: u32) -> String {
format!(
"https://mise-versions.jdx.dev/api/github/repos/{}/{}/releases?page={page}",
encode_path_segment(owner),
encode_path_segment(repo),
)
}
fn pin_listed_releases(
list: &mut GithubReleasesPage,
owner: &str,
repo: &str,
) -> Result<(), String> {
for release in &mut list.releases {
if release.draft || pin_github_release_assets(release, owner, repo).is_err() {
return Err(release.tag_name.clone());
}
}
Ok(())
}
pub async fn github_attestations(
repo: &str,
digest: &str,
) -> eyre::Result<Option<Vec<Attestation>>> {
if !enabled_for_github_repo(repo) {
return Ok(None);
}
let Some((owner, repo_name)) = split_github_repo(repo) else {
return Ok(None);
};
let url = format!(
"https://mise-versions.jdx.dev/api/github/repos/{}/{}/attestations/{}",
encode_path_segment(owner),
encode_path_segment(repo_name),
encode_digest_path_segment(digest)
);
let ctx = VersionsHostLogContext::github_attestations(repo, digest);
let response: Option<AttestationsResponse> = fetch_json_unless_not_found(&url, ctx).await?;
if let Some(response) = &response {
log_versions_host_trace(
ctx,
"success",
&format!("attestations={}", response.attestations.len()),
);
}
Ok(response.map(|r| r.attestations))
}
async fn fetch_json_unless_not_found<T>(
url: &str,
ctx: VersionsHostLogContext<'_>,
) -> eyre::Result<Option<T>>
where
T: serde::de::DeserializeOwned,
{
if crate::network::offline(&Settings::get()) {
bail!("offline mode is enabled");
}
debug!("GET {url}");
let resp = match HTTP_FETCH
.get_async_with_headers_allow_error_status(url, &VERSIONS_HOST_HEADERS)
.await
{
Ok(resp) => resp,
Err(err) => {
let status = transport_error_status(&err);
log_versions_host_trace(
ctx,
"failed",
&format!(
"status={} error={}",
status.unwrap_or(0),
log_value(&err.to_string())
),
);
return Err(VersionsHostUnavailable {
status,
message: format!("{err:#}"),
}
.into());
}
};
let status = resp.status();
debug!("GET {url} {status}");
if status == reqwest::StatusCode::NOT_FOUND {
log_versions_host_trace(ctx, "not_found", "status=404");
return Ok(None);
}
if !status.is_success() {
let body = http::read_bounded_error_body(
resp,
crate::network::fetch_remote_versions_timeout(&Settings::get()),
)
.await;
let message = versions_host_error_message(status.as_u16(), &body);
log_versions_host_trace(
ctx,
"failed",
&format!("status={} error={}", status.as_u16(), log_value(&message)),
);
return Err(VersionsHostUnavailable {
status: Some(status.as_u16()),
message,
}
.into());
}
let body = match http::buffer_body(resp).await {
Ok(resp) => resp.bytes().await?,
Err(err)
if err.downcast_ref::<reqwest::Error>().is_some()
|| err.downcast_ref::<http::BodyTimedOut>().is_some() =>
{
return Err(VersionsHostUnavailable {
status: None,
message: format!("reading {url}: {err:#}"),
}
.into());
}
Err(err) => return Err(err),
};
serde_json::from_slice(&body)
.map(Some)
.map_err(|err| eyre::eyre!("invalid response from {url}: {err}"))
}
fn transport_error_status(err: &eyre::Report) -> Option<u16> {
err.chain()
.filter_map(|cause| cause.downcast_ref::<reqwest::Error>())
.find_map(reqwest::Error::status)
.map(|status| status.as_u16())
}
#[derive(Debug, thiserror::Error)]
#[error("{message}")]
pub struct VersionsHostUnavailable {
pub status: Option<u16>,
pub(crate) message: String,
}
impl VersionsHostUnavailable {
pub fn is_temporary(&self) -> bool {
self.status != Some(403)
}
}
pub fn is_temporarily_unavailable(err: &eyre::Report) -> bool {
err.chain().any(|cause| {
cause.is::<GithubRetryFailed>()
|| cause
.downcast_ref::<VersionsHostUnavailable>()
.is_some_and(VersionsHostUnavailable::is_temporary)
})
}
pub fn retries_on_github(err: &eyre::Report) -> bool {
err.chain().any(|cause| {
cause
.downcast_ref::<VersionsHostUnavailable>()
.is_some_and(|e| match e.status {
None => true,
Some(status) => status == 429 || (500..=599).contains(&status),
})
})
}
#[derive(Debug, thiserror::Error)]
#[error(
"failed to read {what} from mise-versions.jdx.dev ({mirror}), and from api.github.com after it"
)]
pub struct GithubRetryFailed {
what: String,
mirror: String,
#[source]
github: Box<dyn std::error::Error + Send + Sync + 'static>,
}
impl GithubRetryFailed {
pub fn new(what: &str, mirror: &eyre::Report, github: eyre::Report) -> Self {
Self {
what: what.to_string(),
mirror: format!("{mirror:#}"),
github: github.into(),
}
}
}
pub fn is_not_found(err: &eyre::Report) -> bool {
fn names_404(cause: &(dyn std::error::Error + 'static)) -> bool {
cause
.downcast_ref::<reqwest::Error>()
.and_then(reqwest::Error::status)
.is_some_and(|status| status == reqwest::StatusCode::NOT_FOUND)
|| {
let message = cause.to_string();
message.starts_with("HTTP status client error (404")
|| message.contains("(404 from mise-versions.jdx.dev")
}
}
err.chain().any(|cause| {
names_404(cause)
|| cause
.downcast_ref::<GithubRetryFailed>()
.is_some_and(|failed| {
let mut source = std::error::Error::source(failed);
while let Some(inner) = source {
if names_404(inner) {
return true;
}
source = inner.source();
}
false
})
})
}
fn versions_host_error_message(status: u16, body: &str) -> String {
let body = body.trim();
let status_code = reqwest::StatusCode::from_u16(status);
let label = match status_code {
Ok(status) if status.is_client_error() => "HTTP status client error",
Ok(status) if status.is_server_error() => "HTTP status server error",
_ => "HTTP status error",
};
let status = status_code
.map(|status| status.to_string())
.unwrap_or_else(|_| status.to_string());
if body.is_empty() {
return format!("{label} ({status})");
}
format!(
"{label} ({status}): {}",
body.chars().take(200).collect::<String>()
)
}
pub fn enabled_for_github_metadata() -> bool {
let settings = Settings::get();
settings.use_versions_host && !github_is_url_replaced(None)
}
pub fn enabled_for_github_repo(repo: &str) -> bool {
enabled_for_github_metadata()
&& !github_is_url_replaced(Some(repo))
&& !github_repo_excluded(repo)
}
pub fn github_repo_excluded(repo: &str) -> bool {
let settings = Settings::get();
github_repo_matches_any(&settings.versions_host_exclude, repo)
}
pub fn invalid_exclude_pattern(patterns: &[String]) -> Option<(&str, String)> {
patterns.iter().find_map(|pattern| {
globset::Glob::new(pattern.trim())
.err()
.map(|err| (pattern.as_str(), err.to_string()))
})
}
pub fn backend_excluded(full: &str) -> bool {
let Some(rest) = full.strip_prefix("github:") else {
return false;
};
let repo = rest.split(['[', '@']).next().unwrap_or(rest);
github_repo_excluded(repo)
}
fn github_repo_matches_any(patterns: &[String], repo: &str) -> bool {
patterns.iter().any(|pattern| {
globset::GlobBuilder::new(pattern.trim())
.case_insensitive(true)
.literal_separator(true)
.build()
.is_ok_and(|glob| glob.compile_matcher().is_match(repo))
})
}
pub fn github_is_url_replaced(repo: Option<&str>) -> bool {
static LOGGED: AtomicBool = AtomicBool::new(false);
if Settings::get().url_replacements.is_none() {
return false;
}
let mut urls = vec!["https://api.github.com/".to_string()];
if let Some(repo) = repo {
urls.extend([
format!("https://api.github.com/repos/{repo}/releases"),
format!("https://api.github.com/repos/{repo}/releases?per_page=100"),
format!("https://api.github.com/repos/{repo}/releases/latest"),
format!("https://api.github.com/repos/{repo}/releases/tags/v1.0.0"),
format!("https://api.github.com/repos/{repo}/attestations/sha256:0"),
]);
}
let replaced = urls.iter().any(|original| {
let Ok(original) = url::Url::parse(original) else {
return false;
};
let mut replaced = original.clone();
http::apply_url_replacements(&mut replaced);
replaced != original
});
if replaced && !LOGGED.swap(true, Ordering::Relaxed) {
debug!("url_replacements reroutes GitHub; not using mise-versions for GitHub metadata");
}
replaced
}
pub fn github_api_url_is_replaced(url: &str) -> bool {
if Settings::get().url_replacements.is_none() {
return false;
}
let Ok(original) = url::Url::parse(url) else {
return false;
};
let mut replaced = original.clone();
http::apply_url_replacements(&mut replaced);
replaced != original
}
pub fn github_release_is_url_replaced(repo: &str, tag: &str) -> bool {
if Settings::get().url_replacements.is_none() {
return false;
}
let path = if tag == "latest" {
"latest".to_string()
} else {
format!("tags/{}", encode_path_segment(tag))
};
let Ok(original) = url::Url::parse(&format!(
"https://api.github.com/repos/{repo}/releases/{path}"
)) else {
return false;
};
let mut replaced = original.clone();
http::apply_url_replacements(&mut replaced);
replaced != original
}
fn split_github_repo(repo: &str) -> Option<(&str, &str)> {
let (owner, name) = repo.split_once('/')?;
(!owner.is_empty() && !name.is_empty() && !name.contains('/')).then_some((owner, name))
}
fn encode_path_segment(segment: &str) -> String {
urlencoding::encode(segment).into_owned()
}
fn encode_digest_path_segment(digest: &str) -> String {
encode_path_segment(digest).replace("%3A", ":")
}
fn pin_github_release_assets(
release: &mut GithubRelease,
owner: &str,
repo: &str,
) -> Result<(), String> {
for asset in &mut release.assets {
let browser = pinned_browser_download_url(
&asset.browser_download_url,
owner,
repo,
&release.tag_name,
&asset.name,
);
let api = pinned_asset_api_url(&asset.url, owner, repo);
let (Some(browser), Some(api)) = (browser, api) else {
return Err(asset.name.clone());
};
if browser != asset.browser_download_url {
trace!(
"mise-versions lists {} under another repository; pinning it to {owner}/{repo}",
asset.browser_download_url
);
}
asset.browser_download_url = browser;
asset.url = api;
asset.from_versions_host = true;
remember_mirrored_asset_api_url(&asset.url);
}
Ok(())
}
static MIRRORED_ASSET_API_URLS: LazyLock<std::sync::Mutex<HashSet<String>>> =
LazyLock::new(Default::default);
pub fn remember_mirrored_asset_api_url(url: &str) {
MIRRORED_ASSET_API_URLS
.lock()
.unwrap()
.insert(url.to_string());
}
pub fn remember_locked_asset_api_url(url: &str) {
let is_asset = is_plain_asset_api_url(url)
|| url::Url::parse(url).is_ok_and(|parsed| {
let segments: Vec<_> = parsed.path_segments().into_iter().flatten().collect();
parsed.host_str() == Some("api.github.com")
&& matches!(segments[..], ["repos", _, _, "releases", "assets", _])
});
if is_asset {
remember_mirrored_asset_api_url(url);
}
}
fn is_plain_asset_api_url(url: &str) -> bool {
let plain = |segment: &str| {
!segment.is_empty()
&& segment != "."
&& segment != ".."
&& segment
.bytes()
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.'))
};
let Some(rest) = url.strip_prefix("https://api.github.com/repos/") else {
return false;
};
let mut segments = rest.split('/');
let (Some(owner), Some(repo), Some("releases"), Some("assets"), Some(id), None) = (
segments.next(),
segments.next(),
segments.next(),
segments.next(),
segments.next(),
segments.next(),
) else {
return false;
};
plain(owner) && plain(repo) && !id.is_empty() && id.bytes().all(|b| b.is_ascii_digit())
}
pub fn is_mirrored_asset_api_url(url: &str) -> bool {
MIRRORED_ASSET_API_URLS.lock().unwrap().contains(url)
}
pub fn digest_download_pin(digest: Option<&str>, url_api: Option<&str>) -> Option<String> {
digest
.filter(|_| !url_api.is_some_and(is_mirrored_asset_api_url))
.map(str::to_string)
}
fn valid_github_release_tag(release: &GithubRelease, tag: &str) -> bool {
tag == "latest" || release.tag_name == tag
}
fn pinned_browser_download_url(
url: &str,
owner: &str,
repo: &str,
tag: &str,
asset: &str,
) -> Option<String> {
let parsed = url::Url::parse(url).ok()?;
if parsed.scheme() != "https" || parsed.host_str() != Some("github.com") {
return None;
}
let segments: Vec<_> = parsed.path_segments()?.collect();
let [o, r, "releases", "download", tag_segments @ .., file] = segments.as_slice() else {
return None;
};
if o.is_empty()
|| r.is_empty()
|| !path_segments_match(tag_segments, tag)
|| !path_segment_matches(file, asset)
{
return None;
}
if github_repo_segment_matches(o, owner) && github_repo_segment_matches(r, repo) {
return Some(format!("https://github.com/{}", segments.join("/")));
}
Some(format!(
"https://github.com/{}/{}/{}",
encode_path_segment(owner),
encode_path_segment(repo),
segments[2..].join("/")
))
}
fn pinned_asset_api_url(url: &str, owner: &str, repo: &str) -> Option<String> {
let parsed = url::Url::parse(url).ok()?;
if parsed.scheme() != "https" || parsed.host_str() != Some("api.github.com") {
return None;
}
let segments: Vec<_> = parsed.path_segments()?.collect();
let ["repos", o, r, "releases", "assets", id] = segments.as_slice() else {
return None;
};
if o.is_empty() || r.is_empty() || id.is_empty() || !id.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
if github_repo_segment_matches(o, owner) && github_repo_segment_matches(r, repo) {
return Some(format!(
"https://api.github.com/repos/{o}/{r}/releases/assets/{id}"
));
}
Some(format!(
"https://api.github.com/repos/{}/{}/releases/assets/{id}",
encode_path_segment(owner),
encode_path_segment(repo),
))
}
fn github_repo_segment_matches(segment: &str, expected: &str) -> bool {
segment.eq_ignore_ascii_case(expected)
}
fn path_segment_matches(segment: &str, expected: &str) -> bool {
segment == expected || urlencoding::decode(segment).is_ok_and(|decoded| decoded == expected)
}
fn path_segments_match(segments: &[&str], expected: &str) -> bool {
!segments.is_empty() && path_segment_matches(&segments.join("/"), expected)
}
#[cfg(test)]
mod tests {
#[test]
fn test_versions_host_exclude_patterns() {
let patterns = vec!["My-Org/*".to_string(), "other/internal-cli".to_string()];
assert!(super::github_repo_matches_any(&patterns, "my-org/tool"));
assert!(super::github_repo_matches_any(
&patterns,
"other/internal-cli"
));
assert!(!super::github_repo_matches_any(&patterns, "other/public"));
assert!(!super::github_repo_matches_any(&patterns, "my-org/a/b"));
assert!(!super::github_repo_matches_any(&[], "my-org/tool"));
assert!(super::invalid_exclude_pattern(&["my-org/[abc".to_string()]).is_some());
assert!(super::invalid_exclude_pattern(&patterns).is_none());
}
use super::*;
#[test]
fn test_versions_host_headers_mark_ci() {
let headers = versions_host_headers(true, "mise/2026.10.10 linux-x64 (2026-10-11) bash");
assert_eq!(headers["x-mise-ci"], "true");
assert_eq!(
headers[USER_AGENT],
"mise/2026.10.10 linux-x64 (2026-10-11) bash ci"
);
}
#[test]
fn test_versions_host_headers_outside_ci_are_empty() {
assert!(versions_host_headers(false, "mise/2026.10.10").is_empty());
}
#[test]
fn test_versions_host_log_context_fields_quotes_spaces() {
let fields =
VersionsHostLogContext::install_track("some tool", "github:jdx/mise", "1.2.3").fields();
assert_eq!(
fields,
r#"endpoint=install_track tool="some tool" full=github:jdx/mise version=1.2.3"#
);
}
#[test]
fn test_versions_host_log_context_redacts_full_url_credentials() {
let fields = VersionsHostLogContext::install_track(
"private",
"github:https://user:token@example.com/org/repo?token=secret#frag",
"1.2.3",
)
.fields();
assert_eq!(
fields,
r#"endpoint=install_track tool=private full=github:https://example.com/org/repo version=1.2.3"#
);
}
#[test]
fn test_sanitize_full() {
assert_eq!(
sanitize_full("aqua:BurntSushi/ripgrep").unwrap(),
"aqua:BurntSushi/ripgrep"
);
assert_eq!(sanitize_full("npm:@scope/pkg").unwrap(), "npm:@scope/pkg");
assert_eq!(
sanitize_full("http:https://user:pass@example.com/a.tar.gz?token=x#f").unwrap(),
"http:https://example.com/a.tar.gz"
);
assert_eq!(
sanitize_full("https://user:pass@example.com/a.tar.gz?token=x").unwrap(),
"https://example.com/a.tar.gz"
);
assert_eq!(sanitize_full("http:https://user:pass@[bad/a"), None);
assert_eq!(
sanitize_full_for_log("http:https://user:pass@[bad/a"),
"[redacted]"
);
}
#[test]
fn test_split_github_repo() {
assert_eq!(split_github_repo("cli/cli"), Some(("cli", "cli")));
assert_eq!(split_github_repo("cli"), None);
assert_eq!(split_github_repo("cli/cli/extra"), None);
}
#[test]
fn test_encode_path_segment_encodes_digest() {
assert_eq!(encode_path_segment("sha256:abc/def"), "sha256%3Aabc%2Fdef");
}
#[test]
fn test_encode_digest_path_segment_preserves_algorithm_separator() {
assert_eq!(
encode_digest_path_segment("sha256:abc/def"),
"sha256:abc%2Fdef"
);
}
#[test]
fn test_github_releases_url_encodes_repo() {
assert_eq!(
github_releases_url("jdx", "mise.test", 3),
"https://mise-versions.jdx.dev/api/github/repos/jdx/mise.test/releases?page=3"
);
}
#[test]
fn test_github_versions_list_url() {
assert_eq!(
github_versions_list_url("BurntSushi/ripgrep", GithubList::Releases).as_deref(),
Some("https://mise-versions.jdx.dev/data/github/burntsushi/ripgrep/releases.json")
);
assert_eq!(
github_versions_list_url("apache/tomcat", GithubList::Tags).as_deref(),
Some("https://mise-versions.jdx.dev/data/github/apache/tomcat/tags.json")
);
assert_eq!(
github_versions_list_url(
"jdx/mise",
GithubList::ReleasesWithAsset("packslip.sigstore.json")
)
.as_deref(),
Some(
"https://mise-versions.jdx.dev/data/github/jdx/mise/assets/packslip.sigstore.json/releases.json"
)
);
assert!(github_versions_list_url("o/..", GithubList::Releases).is_none());
assert!(github_versions_list_url("o/r", GithubList::ReleasesWithAsset("a/b")).is_none());
assert!(github_versions_list_url("o/r%20", GithubList::Releases).is_none());
}
#[test]
fn test_github_versions_list_deserializes() {
let releases: GithubVersionsList = serde_json::from_str(
r#"{"source":"releases","fetched_at":"2026-10-14T21:20:41Z","releases":[{"tag":"v2.102.0","date":"2026-09-30T02:40:02Z","prerelease":false,"asset_count":22}],"truncated":false}"#,
)
.unwrap();
assert_eq!(releases.releases[0].asset_count, 22);
assert!(releases.tags.is_empty());
let tags: GithubVersionsList = serde_json::from_str(
r#"{"source":"tags","fetched_at":"2026-10-14T21:20:43Z","tags":[{"tag":"11.0.27","sha":"52f3b36","date":null}],"truncated":true}"#,
)
.unwrap();
assert_eq!(tags.tags[0].tag, "11.0.27");
assert_eq!(tags.tags[0].date, None);
assert!(tags.truncated);
}
#[test]
fn test_github_tags_url() {
assert_eq!(
github_tags_url("apache", "tomcat", 2, false),
"https://mise-versions.jdx.dev/api/github/repos/apache/tomcat/tags?page=2"
);
assert_eq!(
github_tags_url("apache", "tomcat", 1, true),
"https://mise-versions.jdx.dev/api/github/repos/apache/tomcat/tags?page=1&dates=1"
);
}
#[test]
fn test_github_tags_page_deserializes() {
let page: GithubTagsPage = serde_json::from_str(
r#"{"tags":[{"name":"11.0.27","commit":{"sha":"52f3b36","url":"https://api.github.com/repos/apache/tomcat/commits/52f3b36"},"date":"2026-10-06T18:22:36Z"},{"name":"11.0.26","commit":{"sha":"a1"}}],"next_page":2,"truncated":false}"#,
)
.unwrap();
assert_eq!(page.tags[0].name, "11.0.27");
assert_eq!(page.tags[0].date.as_deref(), Some("2026-10-06T18:22:36Z"));
assert_eq!(page.tags[1].date, None);
assert_eq!(page.next_page, Some(2));
}
#[test]
fn test_github_contents_url_encodes_each_segment() {
assert_eq!(
github_contents_url("jdx", "fnox", "cc06c75", "skills/fnox tool/"),
"https://mise-versions.jdx.dev/api/github/repos/jdx/fnox/contents/skills/fnox%20tool?ref=cc06c75"
);
}
#[test]
fn test_github_packslip_list_url() {
assert_eq!(
github_packslip_list_url("jdx", "hk", None),
"https://mise-versions.jdx.dev/api/github/repos/jdx/hk/packslip-list"
);
assert_eq!(
github_packslip_list_url("o", "r", Some("tools/my tool")),
"https://mise-versions.jdx.dev/api/github/repos/o/r/packslip-list/tools/my%20tool"
);
}
#[test]
fn test_packslip_list_response_deserialize() {
let absent: PackslipListResponse =
serde_json::from_str(r#"{"present":false,"fetched_at":"2026-10-26T12:00:00Z"}"#)
.unwrap();
assert!(!absent.present && absent.text.is_none());
let present: PackslipListResponse =
serde_json::from_str(r#"{"present":true,"text":"{}\n","fetched_at":"x"}"#).unwrap();
assert!(present.present);
assert_eq!(present.text.as_deref(), Some("{}\n"));
}
#[test]
fn test_github_contents_deserialize() {
let entries: Vec<GithubContentsEntry> = serde_json::from_str(
r#"[{"name":"SKILL.md","path":"skills/fnox/SKILL.md","type":"file","size":12,"sha":"a"},{"name":"refs","path":"skills/fnox/refs","type":"dir","sha":"b"}]"#,
)
.unwrap();
assert_eq!(entries[1].name, "refs");
assert_eq!(entries[1].kind, "dir");
}
#[tokio::test(flavor = "current_thread")]
async fn test_slow_body_is_temporarily_unavailable() {
use confique::Layer;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
struct SettingsGuard {
_lock: std::sync::MutexGuard<'static, ()>,
}
impl Drop for SettingsGuard {
fn drop(&mut self) {
crate::testing::reset_settings(None);
}
}
let _settings = SettingsGuard {
_lock: crate::testing::lock_ignoring_poison(&crate::testing::SETTINGS_LOCK),
};
let mut settings = mise_settings::SettingsPartial::empty();
settings.http_download_timeout = Some("200ms".to_string());
settings.http_retries = Some(0);
crate::testing::reset_settings(Some(settings));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
tokio::spawn(async move {
let Ok((mut socket, _)) = listener.accept().await else {
return;
};
let mut request = [0u8; 4096];
let _ = socket.read(&mut request).await;
let _ = socket
.write_all(
b"HTTP/1.1 200 OK\r\nContent-Length: 1000000\r\nConnection: close\r\n\r\n",
)
.await;
while socket.write_all(b" ").await.is_ok() {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
});
let url = format!("http://127.0.0.1:{port}/gh/v1/repos/jdx/mise/releases/v1.0.0");
let result = tokio::time::timeout(
std::time::Duration::from_secs(10),
fetch_json_unless_not_found::<serde_json::Value>(
&url,
VersionsHostLogContext::github_release("jdx/mise", "v1.0.0"),
),
)
.await
.expect("the body deadline did not end the read");
let err = result.unwrap_err();
assert!(is_temporarily_unavailable(&err), "{err:#}");
assert!(retries_on_github(&err), "{err:#}");
}
#[tokio::test(flavor = "current_thread")]
async fn test_github_packslip_list_answers() {
use confique::Layer;
struct SettingsGuard {
_lock: std::sync::MutexGuard<'static, ()>,
}
impl Drop for SettingsGuard {
fn drop(&mut self) {
crate::testing::reset_settings(None);
}
}
let _settings = SettingsGuard {
_lock: crate::testing::lock_ignoring_poison(&crate::testing::SETTINGS_LOCK),
};
let mut server = mockito::Server::new_async().await;
let mut replacements = indexmap::IndexMap::new();
replacements.insert("https://mise-versions.jdx.dev".to_string(), server.url());
let mut settings = mise_settings::SettingsPartial::empty();
settings.url_replacements = Some(replacements);
settings.http_retries = Some(0);
crate::testing::reset_settings(Some(settings));
let route = |path: &str| format!("/api/github/repos/o/{path}");
server
.mock("GET", route("none/packslip-list").as_str())
.with_body(r#"{"present":false,"fetched_at":"2026-10-26T12:00:00Z"}"#)
.create_async()
.await;
server
.mock("GET", route("listed/packslip-list/tools/x").as_str())
.with_body(
r#"{"present":true,"text":"{\"a\":1}\n","fetched_at":"2026-10-26T12:00:00Z"}"#,
)
.create_async()
.await;
server
.mock("GET", route("private/packslip-list").as_str())
.with_status(404)
.create_async()
.await;
server
.mock("GET", route("limited/packslip-list").as_str())
.with_status(503)
.create_async()
.await;
server
.mock("GET", route("empty/packslip-list").as_str())
.with_body(r#"{"present":true,"fetched_at":"x"}"#)
.create_async()
.await;
assert_eq!(
github_packslip_list("o/none", None).await.unwrap(),
Some(PackslipList::Absent)
);
assert_eq!(
github_packslip_list("o/listed", Some("tools/x"))
.await
.unwrap(),
Some(PackslipList::Present("{\"a\":1}\n".to_string()))
);
assert_eq!(github_packslip_list("o/private", None).await.unwrap(), None);
let err = github_packslip_list("o/limited", None).await.unwrap_err();
assert!(retries_on_github(&err), "{err:#}");
let err = github_packslip_list("o/empty", None).await.unwrap_err();
assert!(!retries_on_github(&err), "{err:#}");
}
#[test]
fn test_temporarily_unavailable() {
let unavailable = |status| -> eyre::Report {
VersionsHostUnavailable {
status,
message: "x".to_string(),
}
.into()
};
for status in [Some(429), Some(502), Some(503), None] {
assert!(is_temporarily_unavailable(
&unavailable(status).wrap_err("failed to read from mise-versions")
));
}
assert!(!is_temporarily_unavailable(&unavailable(Some(403))));
assert!(!is_temporarily_unavailable(&eyre::eyre!(
"invalid response"
)));
}
#[test]
fn test_github_releases_page_deserializes() {
let page: GithubReleasesPage = serde_json::from_str(
r#"{"releases":[{"tag_name":"v1.0.0","draft":false,"prerelease":false,
"created_at":"2026-01-01T00:00:00Z","published_at":"2026-01-02T00:00:00Z",
"assets":[]}],"next_page":null,"truncated":false}"#,
)
.unwrap();
assert_eq!(page.releases.len(), 1);
assert_eq!(
page.releases[0].published_at.as_deref(),
Some("2026-01-02T00:00:00Z")
);
assert_eq!(page.next_page, None);
}
#[test]
fn test_pinned_browser_download_url() {
let pin = |url: &str, tag: &str, asset: &str| {
pinned_browser_download_url(url, "jdx", "mise-test-fixtures", tag, asset)
};
let fixture =
"https://github.com/jdx/mise-test-fixtures/releases/download/v1.0.0/hello-world.tar.gz";
assert_eq!(
pin(fixture, "v1.0.0", "hello-world.tar.gz").as_deref(),
Some(fixture)
);
let upper =
"https://github.com/JDX/Mise-Test-Fixtures/releases/download/v1.0.0/hello-world.tar.gz";
assert_eq!(
pin(upper, "v1.0.0", "hello-world.tar.gz").as_deref(),
Some(upper)
);
let encoded = "https://github.com/jdx/mise-test-fixtures/releases/download/release%2F2026/hello%20world.tar.gz";
assert_eq!(
pin(encoded, "release/2026", "hello world.tar.gz").as_deref(),
Some(encoded)
);
assert!(
pin(
"https://github.com/jdx/mise-test-fixtures/releases/download/%40biomejs/biome%402.5.2/biome-linux-x64",
"@biomejs/biome@2.5.2",
"biome-linux-x64"
)
.is_some()
);
assert_eq!(
pin(
"https://github.com/new-owner/new-name/releases/download/v1.0.0/hello-world.tar.gz",
"v1.0.0",
"hello-world.tar.gz"
)
.as_deref(),
Some(fixture)
);
assert!(
pin(
"https://github.com/jdx/mise-test-fixtures/releases/download/v0.9.0/hello-world.tar.gz",
"v1.0.0",
"hello-world.tar.gz"
)
.is_none()
);
assert!(pin(fixture, "v1.0.0", "hello-world-windows.zip").is_none());
assert!(
pin(
"https://evil.example.com/jdx/mise-test-fixtures/releases/download/v1.0.0/hello-world.tar.gz",
"v1.0.0",
"hello-world.tar.gz"
)
.is_none()
);
assert!(
pin(
"http://github.com/jdx/mise-test-fixtures/releases/download/v1.0.0/hello-world.tar.gz",
"v1.0.0",
"hello-world.tar.gz"
)
.is_none()
);
assert!(
pin(
"https://github.com/jdx/mise-test-fixtures/releases/download",
"v1.0.0",
"download"
)
.is_none()
);
assert!(
pin(
"https://github.com/jdx/mise-test-fixtures/archive/v1.0.0/hello-world.tar.gz",
"v1.0.0",
"hello-world.tar.gz"
)
.is_none()
);
}
#[test]
fn test_pinned_asset_api_url() {
let pin = |url: &str| pinned_asset_api_url(url, "jdx", "mise-test-fixtures");
let fixture = "https://api.github.com/repos/jdx/mise-test-fixtures/releases/assets/1";
assert_eq!(pin(fixture).as_deref(), Some(fixture));
assert_eq!(
pin("https://api.github.com/repos/other/renamed/releases/assets/1").as_deref(),
Some(fixture)
);
assert!(pin("https://github.com/jdx/mise-test-fixtures/releases/assets/1").is_none());
assert!(
pin("https://api.github.com/repos/jdx/mise-test-fixtures/releases/assets/1/extra")
.is_none()
);
assert!(
pin("https://api.github.com/repos/jdx/mise-test-fixtures/releases/assets/x").is_none()
);
assert!(pin("https://api.github.com/repos/jdx/mise-test-fixtures/git/blobs/1").is_none());
for odd in [
"HTTPS://api.github.com/repos/jdx/mise-test-fixtures/releases/assets/1",
" https://api.github.com/repos/jdx/mise-test-fixtures/releases/assets/1",
"https://API.github.com/repos/jdx/mise-test-fixtures/releases/assets/1",
"https://x@api.github.com/repos/jdx/mise-test-fixtures/releases/assets/1",
"https://api.github.com:443/repos/jdx/mise-test-fixtures/releases/assets/1?a=b#c",
] {
assert_eq!(pin(odd).as_deref(), Some(fixture), "{odd:?}");
}
}
#[test]
fn test_remember_locked_asset_api_url() {
let asset = "https://api.github.com/repos/jdx/locked/releases/assets/1";
assert!(!is_mirrored_asset_api_url(asset));
remember_locked_asset_api_url(asset);
assert!(is_mirrored_asset_api_url(asset));
for other in [
"https://api.github.com/repos/jdx/locked/contents/releases/assets/1?ref=v1.0.0",
"https://api.github.com/repos/jdx/locked/tarball/v1.0.0",
"https://github.example.com/api/v3/repos/jdx/locked/releases/assets/1",
"https://gitlab.com/api/v4/projects/jdx%2Flocked/releases/assets/1",
] {
remember_locked_asset_api_url(other);
assert!(!is_mirrored_asset_api_url(other), "{other}");
assert!(!is_plain_asset_api_url(other), "{other}");
}
assert!(is_plain_asset_api_url(asset));
for odd in [
"HTTPS://API.GITHUB.COM/repos/jdx/locked/releases/assets/2",
"https://api.github.com:443/repos/jdx/locked/releases/assets/3",
"https://api.github.com/repos/jdx/locked/releases/assets/4?x=1",
] {
assert!(!is_plain_asset_api_url(odd), "{odd}");
remember_locked_asset_api_url(odd);
assert!(is_mirrored_asset_api_url(odd), "{odd}");
}
for other in [
"https://api.github.com/repos/jdx/../releases/assets/5",
"https://api.github.com/repos/jdx/locked/releases/assets/",
"https://api.github.com/repos/jdx/locked/releases/assets/6/extra",
"https://api.github.com.example.com/repos/jdx/locked/releases/assets/7",
] {
assert!(!is_plain_asset_api_url(other), "{other}");
}
}
#[test]
fn test_pinned_browser_download_url_is_canonical() {
let fixture =
"https://github.com/jdx/mise-test-fixtures/releases/download/v1.0.0/hello-world.tar.gz";
for odd in [
"HTTPS://github.com/jdx/mise-test-fixtures/releases/download/v1.0.0/hello-world.tar.gz",
" https://github.com/jdx/mise-test-fixtures/releases/download/v1.0.0/hello-world.tar.gz",
"https://GitHub.com/jdx/mise-test-fixtures/releases/download/v1.0.0/hello-world.tar.gz",
"https://x@github.com/jdx/mise-test-fixtures/releases/download/v1.0.0/hello-world.tar.gz",
] {
assert_eq!(
pinned_browser_download_url(
odd,
"jdx",
"mise-test-fixtures",
"v1.0.0",
"hello-world.tar.gz"
)
.as_deref(),
Some(fixture),
"{odd:?}"
);
}
}
#[test]
fn test_pin_listed_releases() {
let release = |tag: &str, draft: bool, asset_owner: &str, file: &str| GithubRelease {
tag_name: tag.into(),
draft,
prerelease: false,
created_at: "2026-01-01T00:00:00Z".into(),
published_at: None,
assets: vec![crate::github::GithubAsset {
from_versions_host: false,
name: "tool.tar.gz".into(),
browser_download_url: format!(
"https://github.com/{asset_owner}/mise/releases/download/{tag}/{file}"
),
url: format!("https://api.github.com/repos/{asset_owner}/mise/releases/assets/1"),
digest: None,
updated_at: None,
}],
};
let page = |releases| GithubReleasesPage {
releases,
next_page: None,
truncated: false,
};
let mut ok = page(vec![
release("v1.0.0", false, "jdx", "tool.tar.gz"),
GithubRelease {
assets: vec![],
..release("v0.1.0", false, "jdx", "tool.tar.gz")
},
release("v0.9.0", false, "renamed-from", "tool.tar.gz"),
]);
assert_eq!(pin_listed_releases(&mut ok, "jdx", "mise"), Ok(()));
assert_eq!(
ok.releases[2].assets[0].browser_download_url,
"https://github.com/jdx/mise/releases/download/v0.9.0/tool.tar.gz"
);
assert_eq!(
ok.releases[2].assets[0].url,
"https://api.github.com/repos/jdx/mise/releases/assets/1"
);
assert!(is_mirrored_asset_api_url(&ok.releases[2].assets[0].url));
assert!(!is_mirrored_asset_api_url(
"https://api.github.com/repos/jdx/private-tool/releases/assets/1"
));
let mut draft = page(vec![release("v1.0.0", true, "jdx", "tool.tar.gz")]);
assert_eq!(
pin_listed_releases(&mut draft, "jdx", "mise"),
Err("v1.0.0".to_string())
);
let mut swapped = page(vec![release("v1.0.0", false, "jdx", "other.tar.gz")]);
assert_eq!(
pin_listed_releases(&mut swapped, "jdx", "mise"),
Err("v1.0.0".to_string())
);
}
#[test]
fn test_valid_github_release_tag() {
let release = GithubRelease {
tag_name: "v1.0.0".into(),
draft: false,
prerelease: false,
created_at: "2026-01-01T00:00:00Z".into(),
published_at: None,
assets: vec![],
};
assert!(valid_github_release_tag(&release, "v1.0.0"));
assert!(valid_github_release_tag(&release, "latest"));
assert!(!valid_github_release_tag(&release, "v2.0.0"));
}
#[test]
fn test_versions_host_error_message_includes_body() {
assert_eq!(
versions_host_error_message(403, "GitHub repo is not in the mise registry\n"),
"HTTP status client error (403 Forbidden): GitHub repo is not in the mise registry"
);
}
#[test]
fn test_versions_host_error_message_caps_body() {
let body = "x".repeat(250);
let message = versions_host_error_message(502, &body);
assert_eq!(
message.len(),
"HTTP status server error (502 Bad Gateway): ".len() + 200
);
}
#[test]
fn test_versions_host_error_message_uses_generic_label_for_other_statuses() {
assert_eq!(
versions_host_error_message(302, ""),
"HTTP status error (302 Found)"
);
}
#[test]
fn test_attestations_response_requires_attestations_field() {
assert!(serde_json::from_str::<AttestationsResponse>("{}").is_err());
assert!(
serde_json::from_str::<AttestationsResponse>(r#"{"attestations":[]}"#)
.unwrap()
.attestations
.is_empty()
);
}
#[test]
fn test_transport_error_status_ignores_404_in_the_message() {
let err = eyre::eyre!(
"error sending request for url (https://mise-versions.jdx.dev/api/github/repos/o/r/releases/v1.404.0): operation timed out"
);
assert_eq!(transport_error_status(&err), None);
let unavailable: eyre::Report = VersionsHostUnavailable {
status: transport_error_status(&err),
message: format!("{err:#}"),
}
.into();
assert!(retries_on_github(&unavailable));
}
}