Skip to content

Commit 077cdf8

Browse files
committed
Use commits API instead of search API
The search API is rate limited much more than the normal API. Also, the commits endpoint works on forks, and is simpler (returns an empty list for unknown users, for example). Both APIs only work on the default branch, but I think that should be fine.
1 parent 210178d commit 077cdf8

File tree

1 file changed

+11
-25
lines changed

1 file changed

+11
-25
lines changed

src/github.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -960,11 +960,6 @@ pub struct IssueSearchResult {
960960
pub items: Vec<Issue>,
961961
}
962962

963-
#[derive(Debug, serde::Deserialize)]
964-
struct CommitSearchResult {
965-
total_count: u32,
966-
}
967-
968963
#[derive(Clone, Debug, serde::Deserialize)]
969964
pub struct Repository {
970965
pub full_name: String,
@@ -1534,33 +1529,24 @@ impl GithubClient {
15341529
/// Returns whether or not the given GitHub login has made any commits to
15351530
/// the given repo.
15361531
pub async fn is_new_contributor(&self, repo: &Repository, author: &str) -> bool {
1537-
if repo.fork {
1538-
// GitHub always returns 0 results in forked repos, so this cannot
1539-
// work for them.
1540-
return false;
1541-
}
15421532
let url = format!(
1543-
"{}/search/commits?q=repo:{}+author:{}",
1533+
"{}/repos/{}/commits?author={}",
15441534
Repository::GITHUB_API_URL,
15451535
repo.full_name,
15461536
author,
15471537
);
15481538
let req = self.get(&url);
1549-
match self.json::<CommitSearchResult>(req).await {
1550-
Ok(res) => res.total_count == 0,
1539+
match self.json::<Vec<GithubCommit>>(req).await {
1540+
// Note: This only returns results for the default branch.
1541+
// That should be fine in most cases since I think it is rare for
1542+
// new users to make their first commit to a different branch.
1543+
Ok(res) => res.is_empty(),
15511544
Err(e) => {
1552-
// 422 is returned for unknown user
1553-
if e.downcast_ref::<reqwest::Error>().map_or(false, |e| {
1554-
e.status() == Some(StatusCode::UNPROCESSABLE_ENTITY)
1555-
}) {
1556-
true
1557-
} else {
1558-
log::warn!(
1559-
"failed to search for user commits in {} for author {author}: {e}",
1560-
repo.full_name
1561-
);
1562-
false
1563-
}
1545+
log::warn!(
1546+
"failed to search for user commits in {} for author {author}: {e}",
1547+
repo.full_name
1548+
);
1549+
false
15641550
}
15651551
}
15661552
}

0 commit comments

Comments
 (0)