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

use octokit throttling plugin instead of retry plugin #192

Merged
merged 4 commits into from
Apr 18, 2023
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,442 changes: 3,294 additions & 3,148 deletions dist/index.js

Large diffs are not rendered by default.

380 changes: 164 additions & 216 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@actions/github": "^5.1.1",
"@dqbd/tiktoken": "^1.0.6",
"@octokit/action": "^5.0.2",
"@octokit/plugin-retry": "^4.1.3",
"@octokit/plugin-throttling": "^5.0.1",
"minimatch": "^9.0.0",
"node-fetch": "^3.3.1",
"p-limit": "^4.0.0"
Expand Down
76 changes: 31 additions & 45 deletions src/commenter.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import * as core from '@actions/core'
import * as github from '@actions/github'
import {Octokit} from '@octokit/action'
import {retry} from '@octokit/plugin-retry'

const token = core.getInput('token') || process.env.GITHUB_TOKEN

const RetryOctokit = Octokit.plugin(retry)
const octokit = new RetryOctokit({
auth: `token ${token}`,
request: {
retries: 10,
retryAfter: 30
}
})
import {octokit} from './octokit.js'

const context = github.context
const repo = context.repo
Expand Down Expand Up @@ -167,40 +155,38 @@ ${tag}`
`Submitting review for PR #${pull_number}, total comments: ${this.reviewCommentsBuffer.length}`
)
try {
let batchNumber = 1
while (this.reviewCommentsBuffer.length > 0) {
const commentsBatch = this.reviewCommentsBuffer.splice(0, 30)
core.info(
`Posting batch #${batchNumber} with ${commentsBatch.length} comments`
)

await octokit.pulls.createReview({
owner: repo.owner,
repo: repo.repo,
pull_number,
commit_id,
event: 'COMMENT',
comments: commentsBatch.map(comment => {
const commentData: any = {
path: comment.path,
body: comment.message,
line: comment.end_line,
start_side: 'RIGHT'
}

if (comment.start_line !== comment.end_line) {
commentData.start_line = comment.start_line
}

return commentData
let commentCounter = 0
for (const comment of this.reviewCommentsBuffer) {
core.info(`Posting comment: ${comment.message}`)

if (comment.start_line !== comment.end_line) {
await octokit.pulls.createReviewComment({
owner: repo.owner,
repo: repo.repo,
pull_number,
commit_id,
body: comment.message,
path: comment.path,
line: comment.end_line,
start_side: 'RIGHT',
start_line: comment.start_line
})
} else {
await octokit.pulls.createReviewComment({
owner: repo.owner,
repo: repo.repo,
pull_number,
commit_id,
body: comment.message,
path: comment.path,
line: comment.end_line
})
})

if (this.reviewCommentsBuffer.length > 0) {
core.info(`Waiting 10 seconds before posting next batch`)
await new Promise(resolve => setTimeout(resolve, 10000))
}
batchNumber++

commentCounter++
core.info(
`Comment ${commentCounter}/${this.reviewCommentsBuffer.length} posted`
)
}
} catch (e) {
core.warning(`Failed to submit review: ${e}`)
Expand Down
32 changes: 32 additions & 0 deletions src/octokit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as core from '@actions/core'
import {Octokit} from '@octokit/action'
import {throttling} from '@octokit/plugin-throttling'

const token = core.getInput('token') || process.env.GITHUB_TOKEN

const ThrottlingOctokit = Octokit.plugin(throttling)
export const octokit = new ThrottlingOctokit({
auth: `token ${token}`,
throttle: {
onRateLimit: (
retryAfter: number,
options: any,
o: Octokit,
retryCount: number
) => {
core.warning(
`Request quota exhausted for request ${options.method} ${options.url}
Retry after: ${retryAfter} seconds
Retry count: ${retryCount}
`
)
return true
},
onSecondaryRateLimit: (retryAfter: number, options: any, o: Octokit) => {
core.warning(
`SecondaryRateLimit detected for request ${options.method} ${options.url}`
)
return true
}
}
})
16 changes: 1 addition & 15 deletions src/review-comment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import {Octokit} from '@octokit/action'
import {retry} from '@octokit/plugin-retry'
import {Bot} from './bot.js'
import {
Commenter,
Expand All @@ -10,22 +8,10 @@ import {
EXTRA_CONTENT_TAG,
SUMMARIZE_TAG
} from './commenter.js'
import {octokit} from './octokit.js'
import {Inputs, Options, Prompts} from './options.js'
import * as tokenizer from './tokenizer.js'

const token = core.getInput('token')
? core.getInput('token')
: process.env.GITHUB_TOKEN

const RetryOctokit = Octokit.plugin(retry)
const octokit = new RetryOctokit({
auth: `token ${token}`,
request: {
retries: 10,
retryAfter: 30
}
})

const context = github.context
const repo = context.repo
const ASK_BOT = '@openai'
Expand Down
16 changes: 1 addition & 15 deletions src/review.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import {Octokit} from '@octokit/action'
import {retry} from '@octokit/plugin-retry'
import pLimit from 'p-limit'
import {Bot} from './bot.js'
import {
Expand All @@ -10,22 +8,10 @@ import {
EXTRA_CONTENT_TAG,
SUMMARIZE_TAG
} from './commenter.js'
import {octokit} from './octokit.js'
import {Inputs, Options, Prompts} from './options.js'
import * as tokenizer from './tokenizer.js'

const token = core.getInput('token')
? core.getInput('token')
: process.env.GITHUB_TOKEN

const RetryOctokit = Octokit.plugin(retry)
const octokit = new RetryOctokit({
auth: `token ${token}`,
request: {
retries: 10,
retryAfter: 30
}
})

const context = github.context
const repo = context.repo

Expand Down