Skip to content

Instantly share code, notes, and snippets.

@rm3l
Last active February 27, 2025 08:12
Show Gist options
  • Save rm3l/ad641d214a3e4fb2379a48a81f42e139 to your computer and use it in GitHub Desktop.
Save rm3l/ad641d214a3e4fb2379a48a81f42e139 to your computer and use it in GitHub Desktop.
Simple NodeJS script that polls GH using node-fetch
const readline = require('readline');
const URL = 'https://api.github.com';
const TIMEOUT_MS = 10000; // Aligning with the default timeout settings in RHDH/Backstage
let requestInterval = 3000;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let GITHUB_TOKEN = process.env.GITHUB_TOKEN;
async function promptForToken() {
return new Promise((resolve) => {
rl.question("Enter GitHub Token (or press Enter to skip): ", (token) => {
resolve(token.trim() || null);
});
});
}
async function fetchGitHub() {
const fetch = (await import('node-fetch')).default;
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), TIMEOUT_MS);
try {
const headers = {
'User-Agent': 'RHDH on OSD on GCP (test)'
};
if (GITHUB_TOKEN) {
headers['Authorization'] = `Bearer ${GITHUB_TOKEN}`;
}
const response = await fetch(URL, {
method: 'GET',
headers,
signal: controller.signal
});
clearTimeout(timeout);
if (response.status === 403) {
const rateLimitRemaining = response.headers.get('x-ratelimit-remaining');
const rateLimitReset = response.headers.get('x-ratelimit-reset');
if (rateLimitRemaining === '0') {
const resetTime = new Date(rateLimitReset * 1000);
console.warn(`Rate limit exceeded. Next reset at: ${resetTime}`);
console.warn(`Increasing request interval to avoid limits.`);
requestInterval = Math.max(requestInterval * 2, 60000); // Increase interval to avoid hitting limits
return;
}
}
console.log(`Success: ${response.status}`);
requestInterval = 3000; // Reset
} catch (error) {
console.error(`Fetch error: ${error.message}`);
}
}
(async () => {
if (!GITHUB_TOKEN) {
GITHUB_TOKEN = await promptForToken();
rl.close();
}
// Repeat interval is set dynamically to avoid hitting rate limit errors
setInterval(fetchGitHub, requestInterval);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment