MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / fetch_latest

Function fetch_latest

atomic-cli/src/commands/update.rs:265–315  ·  view source on GitHub ↗
(env: &dyn EnvLookup)

Source from the content-addressed store, hash-verified

263// GitHub fetch — kept thin, not unit-tested
264
265async fn fetch_latest(env: &dyn EnvLookup) -> Result<String, CliError> {
266 let url = "https://api.github.com/repos/atomicdotdev/atomic/releases/latest";
267 let ua = concat!("atomic-cli/", env!("CARGO_PKG_VERSION"));
268
269 let client = reqwest::Client::builder()
270 .timeout(std::time::Duration::from_secs(10))
271 .build()
272 .map_err(|e| CliError::RemoteError {
273 message: format!("HTTP client init failed: {e}"),
274 url: Some(url.to_string()),
275 })?;
276
277 let mut req = client
278 .get(url)
279 .header(reqwest::header::USER_AGENT, ua)
280 .header(reqwest::header::ACCEPT, "application/vnd.github+json");
281
282 // Authenticated requests get 5000/hr instead of 60/hr. Useful for CI
283 // running `atomic update --check`. Empty/whitespace tokens are
284 // ignored so a stray `export GITHUB_TOKEN=` doesn't break auth.
285 if let Some(token) = env.var("GITHUB_TOKEN") {
286 if !token.trim().is_empty() {
287 req = req.bearer_auth(token);
288 }
289 }
290
291 let resp = req.send().await.map_err(|e| CliError::RemoteError {
292 message: format!("Failed to reach GitHub: {e}"),
293 url: Some(url.to_string()),
294 })?;
295
296 if resp.status() == reqwest::StatusCode::FORBIDDEN {
297 return Err(CliError::RemoteError {
298 message: "GitHub API rate limit hit (60/hr unauthenticated, 5000/hr with GITHUB_TOKEN). Try again later or export GITHUB_TOKEN.".to_string(),
299 url: Some(url.to_string()),
300 });
301 }
302 if !resp.status().is_success() {
303 return Err(CliError::RemoteError {
304 message: format!("GitHub returned HTTP {}", resp.status()),
305 url: Some(url.to_string()),
306 });
307 }
308
309 let release: GithubReleaseLatest = resp.json().await.map_err(|e| CliError::RemoteError {
310 message: format!("Failed to parse GitHub response: {e}"),
311 url: Some(url.to_string()),
312 })?;
313
314 Ok(release.tag_name)
315}
316
317// Message formatting — all printing helpers are pure so tests can capture output
318

Callers 1

executeMethod · 0.85

Calls 9

timeoutMethod · 0.80
varMethod · 0.80
getMethod · 0.65
buildMethod · 0.45
headerMethod · 0.45
is_emptyMethod · 0.45
statusMethod · 0.45
is_successMethod · 0.45
jsonMethod · 0.45

Tested by

no test coverage detected