Skip to content

Commit

Permalink
Perform less unnecessary github requests
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Mar 25, 2024
1 parent 8f068ab commit 229b0a4
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions lib/sources/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

use http_body_util::BodyExt;
use octocrab::{models::repos::Release, Error, Octocrab, OctocrabBuilder, Result};
use reqwest::header::ACCEPT;
use reqwest::{header::ACCEPT, StatusCode};
use secrecy::{ExposeSecret, SecretString};
use semver::Version;
use tokio::time::sleep;
Expand Down Expand Up @@ -151,20 +151,18 @@ impl GitHubSource {
let releases = repository.releases();

let tag_with_prefix = format!("v{}", tool_spec.version());
let tag_without_prefix = tool_spec.version().to_string();
let (response_with_prefix, response_without_prefix) = tokio::join!(
releases.get_by_tag(&tag_with_prefix),
releases.get_by_tag(&tag_without_prefix),
);

if response_with_prefix.is_err() && response_without_prefix.is_err() {
#[allow(clippy::unnecessary_unwrap)]
return Err(response_with_prefix.unwrap_err());
match releases.get_by_tag(&tag_with_prefix).await {
Err(err) if is_github_not_found(&err) => {}
Err(err) => return Err(err),
Ok(release) => return Ok(Some(release)),
}

let opt_with_prefix = response_with_prefix.ok();
let opt_without_prefix = response_without_prefix.ok();
Ok(opt_with_prefix.or(opt_without_prefix))
let tag_without_prefix = tool_spec.version().to_string();
match releases.get_by_tag(&tag_without_prefix).await {
Err(err) if is_github_not_found(&err) => Ok(None),
Err(err) => Err(err),
Ok(release) => Ok(Some(release)),
}
}

/**
Expand Down Expand Up @@ -263,3 +261,11 @@ fn crab_builder() -> OctocrabBuilder<NoSvc, DefaultOctocrabBuilderConfig, NoAuth
.unwrap()
.add_header(ACCEPT, String::from("application/json"))
}

fn is_github_not_found(err: &Error) -> bool {
if let Error::GitHub { source, .. } = err {
source.status_code == StatusCode::NOT_FOUND
} else {
false
}
}

0 comments on commit 229b0a4

Please sign in to comment.