Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/fetcher/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ impl<'a> SimpleFetcher<'a, 1> for Fetchgit {
}
}

impl<'a> Fetchgit {
impl Fetchgit {
fn fetch(
&'a self,
&self,
values @ [url]: &[&str; 1],
rev: &str,
submodules: bool,
Expand Down
17 changes: 16 additions & 1 deletion src/fetcher/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ struct Commit {
sha: String,
}

fn token() -> Option<String> {
["GH_TOKEN", "GITHUB_TOKEN", "GITHUB_API_TOKEN"]
.iter()
.find_map(|key| std::env::var(key).ok())
}

impl SimpleFetcher<'_, 2> for FetchFromGitHub<'_> {
const HOST_KEY: &'static str = "githubBase";
const KEYS: [&'static str; 2] = ["owner", "repo"];
Expand All @@ -28,7 +34,16 @@ impl SimpleFetcher<'_, 2> for FetchFromGitHub<'_> {
let host = self.0.unwrap_or("github.com");
let url = format!("https://api.{host}/repos/{owner}/{repo}/commits?per_page=1");

let [Commit { sha }] = ureq::get(&url)
// https://docs.github.com/en/rest/authentication/authenticating-to-the-rest-api
let mut request = ureq::get(&url)
.set("Accept", "application/vnd.github+json")
.set("X-GitHub-Api-Version", "2022-11-28");

if let Some(token) = token() {
request = request.set("Authorization", &format!("Bearer {token}"));
}

let [Commit { sha }] = request
.call()?
.into_json::<[_; 1]>()
.with_context(|| format!("no commits found for https://{host}/{owner}/{repo}"))?;
Expand Down
2 changes: 1 addition & 1 deletion src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub trait SimpleFetcher<'a, const N: usize> {
}

fn resolve_submodules(&self, submodules: Option<bool>) -> bool {
submodules.map_or(false, |submodules| submodules ^ Self::SUBMODULES_DEFAULT)
submodules.is_some_and(|submodules| submodules ^ Self::SUBMODULES_DEFAULT)
}

fn fetch_rev(&self, _: &[&str; N]) -> Result<String> {
Expand Down