File tree Expand file tree Collapse file tree 2 files changed +48
-2
lines changed Expand file tree Collapse file tree 2 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { setOutput } from '@actions/core'
5
5
6
6
import github from './github.js'
7
7
import { getActionContext } from './action-context.js'
8
+ import { octoSecondaryRatelimitRetry } from './secondary-ratelimit-retry.js'
8
9
9
10
async function main ( ) {
10
11
const sha = await getBuiltSHA ( )
@@ -17,9 +18,10 @@ async function main() {
17
18
let number = ''
18
19
19
20
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
+ )
21
24
for ( const issue of data . items ) {
22
- // console.log(issue)
23
25
console . log ( 'ID:' , issue . id )
24
26
console . log ( 'Number:' , issue . number )
25
27
console . log ( 'URL:' , issue . html_url )
Original file line number Diff line number Diff line change
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
+ / Y o u h a v e e x c e e d e d a s e c o n d a r y r a t e l i m i t / . 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
+ }
You can’t perform that action at this time.
0 commit comments