Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement mechanism to avoid secondary rate limit on PR creation #164

Merged
merged 2 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ Push each repository's local branch to the remote. If a branch already exists on
</table>

### Pull Requests
If you are planning to raise PRs, please add your github personal access token to the githubAccessToken object in `~/.config/nori-workspace/config.json`. Authenticating increases the [secondary rate limit](https://docs.github.com/en/rest/guides/best-practices-for-integrators#dealing-with-secondary-rate-limits) of GitHub API, which increases your chance to raise multiple PRs without being blocked by the limit.

##### `nori prs --templates.title --templates.body`

Expand Down
79 changes: 40 additions & 39 deletions src/commands/prs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const styles = require('../lib/styles')
const getConfig = require('../lib/config')
const promiseAllErrors = require('../lib/promise-all-errors')

const PR_CREATION_TIMEOUT_MS = 2000

exports.command = 'prs'
exports.desc = 'create Github pull requests for pushed branches'

Expand Down Expand Up @@ -45,47 +47,46 @@ exports.handler = async ({ templates: { title, body } }, state) => {

const octokit = getOctokit(githubAccessToken)

await promiseAllErrors(
state.repos.map(async repo => {
if (repo.remoteBranch) {
const [existingPr] = await octokit.paginate(
octokit.pulls.list.endpoint.merge({
owner: repo.owner,
repo: repo.name,
head: `${repo.owner}:${repo.remoteBranch}`,
}),
)
for (const repo of state.repos) {
serena97 marked this conversation as resolved.
Show resolved Hide resolved
if (repo.remoteBranch) {
const [existingPr] = await octokit.paginate(
octokit.pulls.list.endpoint.merge({
owner: repo.owner,
repo: repo.name,
head: `${repo.owner}:${repo.remoteBranch}`,
}),
)

if (existingPr) {
logger.log(`${repo.owner}/${repo.name} PR`, {
status: 'info',
message: `using existing PR ${styles.url(
existingPr.html_url,
)} for ${styles.branch(repo.remoteBranch)} on ${styles.repo(
`${repo.owner}/${repo.name}`,
)}`,
})
}
repo.pr =
existingPr ||
(await logger
.logPromise(
octokit.pulls.create({
owner: repo.owner,
repo: repo.name,
head: repo.remoteBranch,
base: repo.centralBranch,
title: titleTemplate(repo),
body: bodyTemplate(repo),
}),
`creating PR for ${styles.branch(
repo.remoteBranch,
)} on ${styles.repo(`${repo.owner}/${repo.name}`)}`,
)
.then(response => response.data))
if (existingPr) {
logger.log(`${repo.owner}/${repo.name} PR`, {
status: 'info',
message: `using existing PR ${styles.url(
existingPr.html_url,
)} for ${styles.branch(repo.remoteBranch)} on ${styles.repo(
`${repo.owner}/${repo.name}`,
)}`,
})
}
}),
)
repo.pr =
existingPr ||
(await logger
.logPromise(
octokit.pulls.create({
owner: repo.owner,
repo: repo.name,
head: repo.remoteBranch,
base: repo.centralBranch,
title: titleTemplate(repo),
body: bodyTemplate(repo),
}),
`creating PR for ${styles.branch(
repo.remoteBranch,
)} on ${styles.repo(`${repo.owner}/${repo.name}`)}`,
)
.then(response => response.data))
await new Promise(r => setTimeout(r, PR_CREATION_TIMEOUT_MS))
}
}
}

exports.undo = async (_, state) => {
Expand Down
31 changes: 23 additions & 8 deletions src/lib/octokit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,37 @@ const { Octokit } = require('@octokit/rest')
const { throttling } = require('@octokit/plugin-throttling')
const { retry } = require('@octokit/plugin-retry')
const OctokitInstance = Octokit.plugin(throttling, retry)
const logger = require('../lib/logger')

const RETRY_LIMIT = 5

let client

const retryWrapper = (retryAfter, options, message) => {
if (options.request.retryCount === RETRY_LIMIT) {
return
}
logger.log(`${message}, retrying after ${retryAfter}s`, {
status: 'pending',
message: `${message}, retrying after ${retryAfter}s`,
})
return true
}

module.exports = token => {
if (!client) {
client = new OctokitInstance({
previews: ['inertia-preview'],
auth: `token ${token}`,
auth: token,
throttle: {
onRateLimit: (retryAfter, options) => {
// Only retry once.
if (options.request.retryCount === 0) {
return true
}
},
onAbuseLimit: () => {},
onRateLimit: (retryAfter, options) =>
retryWrapper(retryAfter, options, 'Hit GitHub API rate limit'),
onAbuseLimit: (retryAfter, options) =>
retryWrapper(
retryAfter,
options,
'Hit secondary GitHub API rate limit',
),
},
})
}
Expand Down