Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(turbo-updater): notifications per channel #3028

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/turbo-updater/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ publish = false
[dependencies]
colored = "2.0"
is-terminal = "0.4"
semver = "1.0"
serde = { version = "1.0.126", features = ["derive"] }
strip-ansi-escapes = "0.1.1"
terminal_size = "0.2"
Expand Down
55 changes: 45 additions & 10 deletions crates/turbo-updater/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::time::Duration;
use std::{fmt, time::Duration};

use colored::*;
use is_terminal::IsTerminal;
use semver::Version as SemVerVersion;
use serde::Deserialize;
use thiserror::Error as ThisError;
use update_informer::{Check, Package, Registry, Result as UpdateResult, Version};
Expand All @@ -20,32 +21,50 @@ const ENVIRONMENTAL_DISABLE_VARS: [&str; 1] = ["CI"];
pub enum UpdateNotifierError {
#[error("Failed to write to terminal")]
RenderError(#[from] ui::utils::GetDisplayLengthError),
#[error("Failed to parse current version")]
VersionError(#[from] semver::Error),
}

#[derive(Deserialize)]
#[derive(Deserialize, Debug)]
struct NpmVersionData {
version: String,
}

#[derive(Debug)]
enum VersionTag {
Latest,
Canary,
Next,
tknickman marked this conversation as resolved.
Show resolved Hide resolved
}

impl fmt::Display for VersionTag {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
VersionTag::Latest => write!(f, "latest"),
VersionTag::Canary => write!(f, "canary"),
VersionTag::Next => write!(f, "next"),
tknickman marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

struct NPMRegistry;

impl Registry for NPMRegistry {
const NAME: &'static str = "npm_registry";
const NAME: &'static str = "npm-registry";
fn get_latest_version(
pkg: &Package,
version: &Version,
timeout: Duration,
) -> UpdateResult<Option<String>> {
// determine tag to request
let tag = match &version.get().pre {
t if t.contains("canary") => "canary",
t if t.contains("next") => "next",
_ => "latest",
};

let tag = get_tag_from_version(&version.get().pre);
// since we're overloading tag within name, we need to split it back out
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static, he says.

let full_name = pkg.to_string();
let split_name: Vec<&str> = full_name.split('/').collect();
let name = split_name[1];
let url = format!(
"https://turbo.build/api/binaries/version?name={name}&tag={tag}",
name = pkg,
name = name,
tag = tag
);
let resp = ureq::get(&url).timeout(timeout).call()?;
Expand All @@ -54,6 +73,14 @@ impl Registry for NPMRegistry {
}
}

fn get_tag_from_version(pre: &semver::Prerelease) -> VersionTag {
match pre {
t if t.contains("canary") => VersionTag::Canary,
t if t.contains("next") => VersionTag::Next,
tknickman marked this conversation as resolved.
Show resolved Hide resolved
_ => VersionTag::Latest,
}
}

fn should_skip_notification() -> bool {
if NOTIFIER_DISABLE_VARS
.iter()
Expand Down Expand Up @@ -89,6 +116,14 @@ pub fn check_for_updates(
return Ok(());
}

// we want notifications per channel (latest, canary, etc) so we need to ensure
// we have one cached latest version per channel. UpdateInformer does not
// support this out of the box, so we hack it into the name by overloading
// owner (in the supported owner/name format) to be channel/name.
let parsed_version = SemVerVersion::parse(&current_version)?;
let tag = get_tag_from_version(&parsed_version.pre);
let package_name = format!("{}/{}", tag, package_name);

let timeout = timeout.unwrap_or(DEFAULT_TIMEOUT);
let interval = interval.unwrap_or(DEFAULT_INTERVAL);
let informer = update_informer::new(NPMRegistry, package_name, current_version)
Expand Down