Skip to content

Repo sync #32075

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

Merged
merged 4 commits into from
Mar 13, 2024
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
6 changes: 4 additions & 2 deletions src/workflows/find-past-built-pr.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { setOutput } from '@actions/core'

import github from './github.js'
import { getActionContext } from './action-context.js'
import { octoSecondaryRatelimitRetry } from './secondary-ratelimit-retry.js'

async function main() {
const sha = await getBuiltSHA()
Expand All @@ -17,9 +18,10 @@ async function main() {
let number = ''

const q = `${sha} repo:"${owner}/${repo}"`
const { data } = await octokit.rest.search.issuesAndPullRequests({ q })
const { data } = await octoSecondaryRatelimitRetry(() =>
octokit.rest.search.issuesAndPullRequests({ q }),
)
for (const issue of data.items) {
// console.log(issue)
console.log('ID:', issue.id)
console.log('Number:', issue.number)
console.log('URL:', issue.html_url)
Expand Down
44 changes: 44 additions & 0 deletions src/workflows/secondary-ratelimit-retry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { RequestError } from '@octokit/request-error'

const DEFAULT_SLEEPTIME = parseInt(process.env.SECONDARY_RATELIMIT_RETRY_SLEEPTIME, 10) || 30_000
const DEFAULT_ATTEMPTS = parseInt(process.env.SECONDARY_RATELIMIT_RETRY_ATTEMPTS, 10) || 5

// Secondary rate limits are responded with a 403. The message will contain
// "You have exceeded a secondary rate limit".
// More info about what they are here:
// https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#about-secondary-rate-limits
export async function octoSecondaryRatelimitRetry(
fn,
{ attempts = DEFAULT_ATTEMPTS, sleepTime = DEFAULT_SLEEPTIME } = {},
) {
let tries = 0
while (true) {
try {
return await fn()
} catch (error) {
if (
error instanceof RequestError &&
error.status === 403 &&
/You have exceeded a secondary rate limit/.test(error.message)
) {
if (tries < attempts) {
console.warn(
`Sleeping for ${(sleepTime / 1000).toFixed(1)}s before retrying after ${tries + 1} try`,
)
await sleep(sleepTime)
tries++
continue
} else {
console.warn(`Giving up on retries after ${tries + 1} attempts`)
}
}
throw error
}
}
}

async function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}