Skip to content

Commit d4f6adb

Browse files
authored
Merge pull request #32075 from github/repo-sync
Repo sync
2 parents 6e7e41e + 76eec36 commit d4f6adb

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/workflows/find-past-built-pr.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { setOutput } from '@actions/core'
55

66
import github from './github.js'
77
import { getActionContext } from './action-context.js'
8+
import { octoSecondaryRatelimitRetry } from './secondary-ratelimit-retry.js'
89

910
async function main() {
1011
const sha = await getBuiltSHA()
@@ -17,9 +18,10 @@ async function main() {
1718
let number = ''
1819

1920
const q = `${sha} repo:"${owner}/${repo}"`
20-
const { data } = await octokit.rest.search.issuesAndPullRequests({ q })
21+
const { data } = await octoSecondaryRatelimitRetry(() =>
22+
octokit.rest.search.issuesAndPullRequests({ q }),
23+
)
2124
for (const issue of data.items) {
22-
// console.log(issue)
2325
console.log('ID:', issue.id)
2426
console.log('Number:', issue.number)
2527
console.log('URL:', issue.html_url)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { RequestError } from '@octokit/request-error'
2+
3+
const DEFAULT_SLEEPTIME = parseInt(process.env.SECONDARY_RATELIMIT_RETRY_SLEEPTIME, 10) || 30_000
4+
const DEFAULT_ATTEMPTS = parseInt(process.env.SECONDARY_RATELIMIT_RETRY_ATTEMPTS, 10) || 5
5+
6+
// Secondary rate limits are responded with a 403. The message will contain
7+
// "You have exceeded a secondary rate limit".
8+
// More info about what they are here:
9+
// https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#about-secondary-rate-limits
10+
export async function octoSecondaryRatelimitRetry(
11+
fn,
12+
{ attempts = DEFAULT_ATTEMPTS, sleepTime = DEFAULT_SLEEPTIME } = {},
13+
) {
14+
let tries = 0
15+
while (true) {
16+
try {
17+
return await fn()
18+
} catch (error) {
19+
if (
20+
error instanceof RequestError &&
21+
error.status === 403 &&
22+
/You have exceeded a secondary rate limit/.test(error.message)
23+
) {
24+
if (tries < attempts) {
25+
console.warn(
26+
`Sleeping for ${(sleepTime / 1000).toFixed(1)}s before retrying after ${tries + 1} try`,
27+
)
28+
await sleep(sleepTime)
29+
tries++
30+
continue
31+
} else {
32+
console.warn(`Giving up on retries after ${tries + 1} attempts`)
33+
}
34+
}
35+
throw error
36+
}
37+
}
38+
}
39+
40+
async function sleep(ms) {
41+
return new Promise((resolve) => {
42+
setTimeout(resolve, ms)
43+
})
44+
}

0 commit comments

Comments
 (0)