Skip to content
This repository was archived by the owner on Mar 6, 2024. It is now read-only.

Commit b78b1ab

Browse files
authored
use octokit throttling plugin instead of retry plugin (#192)
1 parent 954909d commit b78b1ab

File tree

7 files changed

+3524
-3440
lines changed

7 files changed

+3524
-3440
lines changed

dist/index.js

Lines changed: 3294 additions & 3148 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 164 additions & 216 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"@actions/github": "^5.1.1",
3030
"@dqbd/tiktoken": "^1.0.6",
3131
"@octokit/action": "^5.0.2",
32-
"@octokit/plugin-retry": "^4.1.3",
32+
"@octokit/plugin-throttling": "^5.0.1",
3333
"minimatch": "^9.0.0",
3434
"node-fetch": "^3.3.1",
3535
"p-limit": "^4.0.0"

src/commenter.ts

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
/* eslint-disable @typescript-eslint/explicit-function-return-type */
22
import * as core from '@actions/core'
33
import * as github from '@actions/github'
4-
import {Octokit} from '@octokit/action'
5-
import {retry} from '@octokit/plugin-retry'
6-
7-
const token = core.getInput('token') || process.env.GITHUB_TOKEN
8-
9-
const RetryOctokit = Octokit.plugin(retry)
10-
const octokit = new RetryOctokit({
11-
auth: `token ${token}`,
12-
request: {
13-
retries: 10,
14-
retryAfter: 30
15-
}
16-
})
4+
import {octokit} from './octokit.js'
175

186
const context = github.context
197
const repo = context.repo
@@ -167,40 +155,38 @@ ${tag}`
167155
`Submitting review for PR #${pull_number}, total comments: ${this.reviewCommentsBuffer.length}`
168156
)
169157
try {
170-
let batchNumber = 1
171-
while (this.reviewCommentsBuffer.length > 0) {
172-
const commentsBatch = this.reviewCommentsBuffer.splice(0, 30)
173-
core.info(
174-
`Posting batch #${batchNumber} with ${commentsBatch.length} comments`
175-
)
176-
177-
await octokit.pulls.createReview({
178-
owner: repo.owner,
179-
repo: repo.repo,
180-
pull_number,
181-
commit_id,
182-
event: 'COMMENT',
183-
comments: commentsBatch.map(comment => {
184-
const commentData: any = {
185-
path: comment.path,
186-
body: comment.message,
187-
line: comment.end_line,
188-
start_side: 'RIGHT'
189-
}
190-
191-
if (comment.start_line !== comment.end_line) {
192-
commentData.start_line = comment.start_line
193-
}
194-
195-
return commentData
158+
let commentCounter = 0
159+
for (const comment of this.reviewCommentsBuffer) {
160+
core.info(`Posting comment: ${comment.message}`)
161+
162+
if (comment.start_line !== comment.end_line) {
163+
await octokit.pulls.createReviewComment({
164+
owner: repo.owner,
165+
repo: repo.repo,
166+
pull_number,
167+
commit_id,
168+
body: comment.message,
169+
path: comment.path,
170+
line: comment.end_line,
171+
start_side: 'RIGHT',
172+
start_line: comment.start_line
173+
})
174+
} else {
175+
await octokit.pulls.createReviewComment({
176+
owner: repo.owner,
177+
repo: repo.repo,
178+
pull_number,
179+
commit_id,
180+
body: comment.message,
181+
path: comment.path,
182+
line: comment.end_line
196183
})
197-
})
198-
199-
if (this.reviewCommentsBuffer.length > 0) {
200-
core.info(`Waiting 10 seconds before posting next batch`)
201-
await new Promise(resolve => setTimeout(resolve, 10000))
202184
}
203-
batchNumber++
185+
186+
commentCounter++
187+
core.info(
188+
`Comment ${commentCounter}/${this.reviewCommentsBuffer.length} posted`
189+
)
204190
}
205191
} catch (e) {
206192
core.warning(`Failed to submit review: ${e}`)

src/octokit.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as core from '@actions/core'
2+
import {Octokit} from '@octokit/action'
3+
import {throttling} from '@octokit/plugin-throttling'
4+
5+
const token = core.getInput('token') || process.env.GITHUB_TOKEN
6+
7+
const ThrottlingOctokit = Octokit.plugin(throttling)
8+
export const octokit = new ThrottlingOctokit({
9+
auth: `token ${token}`,
10+
throttle: {
11+
onRateLimit: (
12+
retryAfter: number,
13+
options: any,
14+
o: Octokit,
15+
retryCount: number
16+
) => {
17+
core.warning(
18+
`Request quota exhausted for request ${options.method} ${options.url}
19+
Retry after: ${retryAfter} seconds
20+
Retry count: ${retryCount}
21+
`
22+
)
23+
return true
24+
},
25+
onSecondaryRateLimit: (retryAfter: number, options: any, o: Octokit) => {
26+
core.warning(
27+
`SecondaryRateLimit detected for request ${options.method} ${options.url}`
28+
)
29+
return true
30+
}
31+
}
32+
})

src/review-comment.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import * as core from '@actions/core'
22
import * as github from '@actions/github'
3-
import {Octokit} from '@octokit/action'
4-
import {retry} from '@octokit/plugin-retry'
53
import {Bot} from './bot.js'
64
import {
75
Commenter,
@@ -10,22 +8,10 @@ import {
108
EXTRA_CONTENT_TAG,
119
SUMMARIZE_TAG
1210
} from './commenter.js'
11+
import {octokit} from './octokit.js'
1312
import {Inputs, Options, Prompts} from './options.js'
1413
import * as tokenizer from './tokenizer.js'
1514

16-
const token = core.getInput('token')
17-
? core.getInput('token')
18-
: process.env.GITHUB_TOKEN
19-
20-
const RetryOctokit = Octokit.plugin(retry)
21-
const octokit = new RetryOctokit({
22-
auth: `token ${token}`,
23-
request: {
24-
retries: 10,
25-
retryAfter: 30
26-
}
27-
})
28-
2915
const context = github.context
3016
const repo = context.repo
3117
const ASK_BOT = '@openai'

src/review.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import * as core from '@actions/core'
22
import * as github from '@actions/github'
3-
import {Octokit} from '@octokit/action'
4-
import {retry} from '@octokit/plugin-retry'
53
import pLimit from 'p-limit'
64
import {Bot} from './bot.js'
75
import {
@@ -10,22 +8,10 @@ import {
108
EXTRA_CONTENT_TAG,
119
SUMMARIZE_TAG
1210
} from './commenter.js'
11+
import {octokit} from './octokit.js'
1312
import {Inputs, Options, Prompts} from './options.js'
1413
import * as tokenizer from './tokenizer.js'
1514

16-
const token = core.getInput('token')
17-
? core.getInput('token')
18-
: process.env.GITHUB_TOKEN
19-
20-
const RetryOctokit = Octokit.plugin(retry)
21-
const octokit = new RetryOctokit({
22-
auth: `token ${token}`,
23-
request: {
24-
retries: 10,
25-
retryAfter: 30
26-
}
27-
})
28-
2915
const context = github.context
3016
const repo = context.repo
3117

0 commit comments

Comments
 (0)