mirror of
https://github.com/JoshiCodes/knock.git
synced 2026-12-04 15:29:40 +01:00
Opt-in update notices
- Asked once after the first start; switch and Check now in Settings - At most one request a day to GitHub's latest-release endpoint, nothing else is sent; knock never updates itself - A new version shows a notice with download link and release notes, plus one desktop notification per version; versions can be skipped
This commit is contained in:
@@ -10,6 +10,7 @@ pub mod prepare;
|
||||
pub mod run;
|
||||
pub mod runner;
|
||||
pub mod secrets;
|
||||
pub mod update;
|
||||
pub mod vars;
|
||||
pub mod ws;
|
||||
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
//! Asks GitHub for the latest release. Only runs when the user opted in;
|
||||
//! the request carries nothing but the version in the User-Agent.
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use serde::Serialize;
|
||||
|
||||
pub const RELEASES_API: &str = "https://api.github.com/repos/JoshiCodes/knock/releases/latest";
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct Release {
|
||||
pub version: String,
|
||||
pub url: String,
|
||||
pub notes: String,
|
||||
pub published_at: String,
|
||||
pub newer: bool,
|
||||
}
|
||||
|
||||
/// Parses `1.2.3` or `v1.2.3-rc1`; pre-releases sort before their release.
|
||||
fn parse(v: &str) -> Option<(u64, u64, u64, Option<String>)> {
|
||||
let v = v.trim().trim_start_matches('v');
|
||||
let (core, pre) = match v.split_once('-') {
|
||||
Some((c, p)) => (c, Some(p.to_string())),
|
||||
None => (v, None),
|
||||
};
|
||||
let mut it = core.split('.').map(|x| x.parse::<u64>().ok());
|
||||
Some((it.next()??, it.next().flatten().unwrap_or(0), it.next().flatten().unwrap_or(0), pre))
|
||||
}
|
||||
|
||||
pub fn is_newer(latest: &str, current: &str) -> bool {
|
||||
match (parse(latest), parse(current)) {
|
||||
(Some(l), Some(c)) => {
|
||||
let (lk, ck) = ((l.0, l.1, l.2), (c.0, c.1, c.2));
|
||||
if lk != ck {
|
||||
return lk > ck;
|
||||
}
|
||||
match (&l.3, &c.3) {
|
||||
(None, Some(_)) => true,
|
||||
(Some(a), Some(b)) => a > b,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn latest(current: &str) -> Result<Release> {
|
||||
let client = reqwest::Client::builder()
|
||||
.user_agent(format!("knock/{current}"))
|
||||
.timeout(std::time::Duration::from_secs(10))
|
||||
.build()?;
|
||||
let resp = client
|
||||
.get(RELEASES_API)
|
||||
.header("Accept", "application/vnd.github+json")
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| anyhow!("could not reach GitHub: {e}"))?;
|
||||
match resp.status().as_u16() {
|
||||
200 => {}
|
||||
404 => return Err(anyhow!("no published release found (the repository may be private or have no releases yet)")),
|
||||
403 | 429 => return Err(anyhow!("GitHub rate limit reached; try again later")),
|
||||
s => return Err(anyhow!("GitHub answered HTTP {s}")),
|
||||
}
|
||||
let v: serde_json::Value = serde_json::from_slice(&resp.bytes().await?)?;
|
||||
let tag = v.get("tag_name").and_then(|t| t.as_str()).unwrap_or_default().to_string();
|
||||
let version = tag.trim_start_matches('v').to_string();
|
||||
Ok(Release {
|
||||
newer: is_newer(&version, current),
|
||||
url: v.get("html_url").and_then(|t| t.as_str()).unwrap_or_default().to_string(),
|
||||
notes: v.get("body").and_then(|t| t.as_str()).unwrap_or_default().to_string(),
|
||||
published_at: v.get("published_at").and_then(|t| t.as_str()).unwrap_or_default().to_string(),
|
||||
version,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::is_newer;
|
||||
|
||||
#[test]
|
||||
fn compares_versions() {
|
||||
assert!(is_newer("0.2.0", "0.1.0"));
|
||||
assert!(is_newer("v1.0.0", "0.9.9"));
|
||||
assert!(is_newer("0.2.0", "0.2.0-rc1"));
|
||||
assert!(is_newer("0.2.0-rc2", "0.2.0-rc1"));
|
||||
assert!(!is_newer("0.1.0", "0.1.0"));
|
||||
assert!(!is_newer("0.2.0-rc1", "0.2.0"));
|
||||
assert!(!is_newer("0.1.9", "0.2.0"));
|
||||
assert!(!is_newer("garbage", "0.1.0"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user