Skip to content

Commit

Permalink
improve github module organization
Browse files Browse the repository at this point in the history
  • Loading branch information
andyschwab committed Jan 28, 2025
1 parent 659a166 commit fe7c562
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 265 deletions.
22 changes: 11 additions & 11 deletions modules/github/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class GitHubAPI {
const now = Math.floor(Date.now() / 1000); // Convert to seconds
if (this.rateLimits[limitType].remaining <= 1 && now < this.rateLimits[limitType].reset) {
const waitTime = (this.rateLimits[limitType].reset - now) * 1000; // Convert to milliseconds
console.warn(`Rate limit for ${limitType} API nearly exhausted. Waiting ${waitTime/1000}s for reset...`);
await new Promise(resolve => setTimeout(resolve, waitTime + 1000)); // Add 1s buffer
}

Expand All @@ -28,18 +29,21 @@ export class GitHubAPI {
});

// Update rate limit info from response headers
this.rateLimits[limitType] = {
remaining: parseInt(response.headers.get('x-ratelimit-remaining')),
reset: parseInt(response.headers.get('x-ratelimit-reset'))
};
const remaining = parseInt(response.headers.get('x-ratelimit-remaining'));
const reset = parseInt(response.headers.get('x-ratelimit-reset'));
if (!isNaN(remaining) && !isNaN(reset)) {
this.rateLimits[limitType] = { remaining, reset };
}

if (!response.ok) {
throw new Error(`GitHubAPI request error:
const error = new Error(`GitHubAPI request error:
Endpoint: ${endpoint}
Status: ${response.status}
Status Text: ${response.statusText}
Rate Limit Remaining: ${response.headers.get('x-ratelimit-remaining')}
Rate Limit Reset: ${response.headers.get('x-ratelimit-reset')}`);
Rate Limit Remaining: ${remaining}
Rate Limit Reset: ${reset}`);
error.status = response.status;
throw error;
}

const data = await response.json();
Expand Down Expand Up @@ -104,10 +108,6 @@ export class GitHubAPI {
return this.request(`/users/${username}`, false);
}

async loadUserEvents(username) {
return this.request(`/users/${username}/events/public?per_page=30`, false);
}

async loadUserRecentCommits(username) {
// Search across all orgs to reduce API calls
return this.request(`/search/commits?q=author:${username}&sort=author-date&order=desc&per_page=5`, false);
Expand Down
Loading

0 comments on commit fe7c562

Please sign in to comment.