Skip to content

A tiny friend in your Mac's notch and on your iPhone that keeps an eye on your AI coding agents: Claude Code, Codex, Cursor, Gemini CLI, Antigravity and more. Approve from the notch or your Lock Screen.

fix(github): paginate user repos to count total stars beyond 100 (#862)

unknownaffaedb84b4b
diff --git a/NotchBuddy/Sources/App/GithubPoller.swift b/NotchBuddy/Sources/App/GithubPoller.swift
index 2435a179..b75c8a5f 100644
--- a/NotchBuddy/Sources/App/GithubPoller.swift
+++ b/NotchBuddy/Sources/App/GithubPoller.swift
@@ -69,20 +69,34 @@ final class GithubPoller: @unchecked Sendable {
}.resume()
}
- private func fetchStars(token: String, totalRepos: Int) {
- guard let url = URL(string: "https://api.github.com/user/repos?per_page=100&affiliation=owner&sort=pushed") else { return }
+ private func fetchStars(token: String, totalRepos: Int, page: Int = 1, accumulatedStars: Int = 0) {
+ guard let url = URL(string: "https://api.github.com/user/repos?per_page=100&affiliation=owner&sort=pushed&page=\(page)") else { return }
var req = URLRequest(url: url, timeoutInterval: 15)
req.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
req.setValue("application/vnd.github+json", forHTTPHeaderField: "Accept")
- URLSession.shared.dataTask(with: req) { data, response, _ in
+ URLSession.shared.dataTask(with: req) { [weak self] data, response, _ in
let code = (response as? HTTPURLResponse)?.statusCode ?? 0
guard let data, code == 200,
- let repos = try? JSONSerialization.jsonObject(with: data) as? [[String: Any]] else { return }
+ let repos = try? JSONSerialization.jsonObject(with: data) as? [[String: Any]] else {
+ if accumulatedStars > 0 {
+ DispatchQueue.main.async {
+ AppState.shared.githubStats = GitHubStats(totalRepos: totalRepos, totalStars: accumulatedStars)
+ }
+ }
+ return
+ }
- let totalStars = repos.reduce(0) { $0 + (($1["stargazers_count"] as? Int) ?? 0) }
- DispatchQueue.main.async {
- AppState.shared.githubStats = GitHubStats(totalRepos: totalRepos, totalStars: totalStars)
+ let pageStars = repos.reduce(0) { $0 + (($1["stargazers_count"] as? Int) ?? 0) }
+ let totalSoFar = accumulatedStars + pageStars
+ let maxPages = max(1, min(50, (totalRepos + 99) / 100))
+
+ if repos.count >= 100 && page < maxPages {
+ self?.fetchStars(token: token, totalRepos: totalRepos, page: page + 1, accumulatedStars: totalSoFar)
+ } else {
+ DispatchQueue.main.async {
+ AppState.shared.githubStats = GitHubStats(totalRepos: totalRepos, totalStars: totalSoFar)
+ }
}
}.resume()
}
diff --git a/windows/src-tauri/src/integrations.rs b/windows/src-tauri/src/integrations.rs
index e41e2265..ae324657 100644
--- a/windows/src-tauri/src/integrations.rs
+++ b/windows/src-tauri/src/integrations.rs
@@ -549,32 +549,53 @@ async fn poll_github(app: AppHandle) {
.and_then(Value::as_i64)
.unwrap_or(0);
- let repos = http
- .get("https://api.github.com/user/repos?per_page=100&affiliation=owner&sort=pushed")
- .header("Authorization", format!("Bearer {token}"))
- .header("Accept", "application/vnd.github+json")
- .header("User-Agent", "Coucou")
- .send()
- .await;
- let stars: i64 = match repos {
- Ok(r) if r.status().is_success() => r
- .json::<Value>()
- .await
- .ok()
- .and_then(|v| v.as_array().cloned())
- .map(|list| {
- list.iter()
- .filter_map(|r| r.get("stargazers_count").and_then(Value::as_i64))
- .sum()
- })
- .unwrap_or(0),
- _ => 0,
- };
+ let total_repos = public + private;
+ let max_pages = calculate_repo_pages(total_repos);
+ let mut stars: i64 = 0;
+
+ for page in 1..=max_pages {
+ let url = format!(
+ "https://api.github.com/user/repos?per_page=100&affiliation=owner&sort=pushed&page={page}"
+ );
+ let resp = http
+ .get(&url)
+ .header("Authorization", format!("Bearer {token}"))
+ .header("Accept", "application/vnd.github+json")
+ .header("User-Agent", "Coucou")
+ .send()
+ .await;
+ match resp {
+ Ok(r) if r.status().is_success() => {
+ let Ok(list) = r.json::<Vec<Value>>().await else { break };
+ let count = list.len();
+ stars += sum_repo_stars(&list);
+ if count < 100 {
+ break;
+ }
+ }
+ _ => break,
+ }
+ }
- GITHUB.cache.lock().unwrap().stats = Some((public + private, stars));
+ GITHUB.cache.lock().unwrap().stats = Some((total_repos, stars));
emit_github(&app);
}
+pub(crate) fn calculate_repo_pages(total_repos: i64) -> usize {
+ if total_repos <= 0 {
+ 1
+ } else {
+ usize::try_from((total_repos + 99) / 100).unwrap_or(1).clamp(1, 50)
+ }
+}
+
+pub(crate) fn sum_repo_stars(repos: &[Value]) -> i64 {
+ repos
+ .iter()
+ .filter_map(|r| r.get("stargazers_count").and_then(Value::as_i64))
+ .sum()
+}
+
// ── GitHub pulse and activity (GithubPoller.swift) ────────────────────────────
//
// Two more loops next to the stats above, with the Mac cadence: the pulse (my
@@ -1312,4 +1333,27 @@ mod tests {
assert_eq!(data["activity"], json!({ "total": 5, "weeks": [], "fetchedAt": 9 }));
assert!(data.get("pulse").is_none());
}
+
+ #[test]
+ fn calculate_repo_pages_bounds_correctly() {
+ assert_eq!(calculate_repo_pages(0), 1);
+ assert_eq!(calculate_repo_pages(1), 1);
+ assert_eq!(calculate_repo_pages(100), 1);
+ assert_eq!(calculate_repo_pages(101), 2);
+ assert_eq!(calculate_repo_pages(200), 2);
+ assert_eq!(calculate_repo_pages(500), 5);
+ assert_eq!(calculate_repo_pages(5000), 50);
+ assert_eq!(calculate_repo_pages(9999), 50);
+ }
+
+ #[test]
+ fn sum_repo_stars_sums_stargazers_count() {
+ let repos = vec![
+ json!({ "name": "repo1", "stargazers_count": 42 }),
+ json!({ "name": "repo2", "stargazers_count": 10 }),
+ json!({ "name": "repo3" }),
+ json!({ "name": "repo4", "stargazers_count": 0 }),
+ ];
+ assert_eq!(sum_repo_stars(&repos), 52);
+ }
}