Add sleeps to github api to prevent rate limiting

This commit is contained in:
Martin Fournier 2022-03-07 17:27:29 -05:00
parent e80190e687
commit 1e55715c6b

@ -1,3 +1,4 @@
/* eslint-disable no-await-in-loop */
import { Octokit } from "@octokit/rest"; import { Octokit } from "@octokit/rest";
import commandLineArgs from "command-line-args"; import commandLineArgs from "command-line-args";
@ -78,10 +79,9 @@ class MergeChangelog {
searchResults.push(...entries); searchResults.push(...entries);
} }
const pullRequestPromises = []; const pulls = [];
for (const entry of searchResults) { for (const entry of searchResults) {
pullRequestPromises.push( const r = await (this.octokit.rest.pulls.get({
this.octokit.rest.pulls.get({
owner, repo, owner, repo,
pull_number: entry.number, pull_number: entry.number,
}).then((response) => ({ }).then((response) => ({
@ -89,9 +89,9 @@ class MergeChangelog {
merge_commit_sha: response.data.merge_commit_sha, merge_commit_sha: response.data.merge_commit_sha,
head_commit_sha: response.data.head.sha, head_commit_sha: response.data.head.sha,
}))); })));
pulls.push(r);
await sleep(1000);
} }
const pulls = await Promise.all(pullRequestPromises);
return pulls; return pulls;
} }
@ -128,8 +128,9 @@ class MergeChangelog {
const pullQuery = `user:${owner} repo:${repo} is:pr is:merged merged:"${from.date.toISOString()}..${to.date.toISOString()}"`; const pullQuery = `user:${owner} repo:${repo} is:pr is:merged merged:"${from.date.toISOString()}..${to.date.toISOString()}"`;
const commits = await this.getCommitsSearchResults(commitQuery); const commits = await this.getCommitsSearchResults(commitQuery);
await sleep(5000)
const pulls = await this.getPullsSearchResults(pullQuery); const pulls = await this.getPullsSearchResults(pullQuery);
await sleep(5000)
// We only have the merge commit sha & the HEAD sha in this data, but it can exclude some entries // We only have the merge commit sha & the HEAD sha in this data, but it can exclude some entries
const pullsCommitSha = pulls. const pullsCommitSha = pulls.
map((p) => [p.merge_commit_sha, p.head_commit_sha]). map((p) => [p.merge_commit_sha, p.head_commit_sha]).
@ -234,6 +235,12 @@ ${commitLines.join('\n')}
} }
} }
const sleep = async (wait) => {
return new Promise((resolve) => {
setTimeout(resolve, wait)
})
}
const api = new MergeChangelog({ auth: process.env.GITHUB_API_TOKEN }); const api = new MergeChangelog({ auth: process.env.GITHUB_API_TOKEN });
api.getChangelog(cliArgs.from, cliArgs.to, cliArgs.detailed).then((data) => { api.getChangelog(cliArgs.from, cliArgs.to, cliArgs.detailed).then((data) => {
console.log(data.log); console.log(data.log);