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

Commit 31c2116

Browse files
authored
refactor code (#69)
<!-- This is an auto-generated comment: release notes by openai --> ### Summary by OpenAI **Release Notes:** - Refactor: Improves readability and maintainability of the codebase. - New Feature: Adds a new method to reply to top-level comments and update them. - Bug fix: Removes console error logs for unhandled rejections and uncaught exceptions. - Documentation: Updates the `Options` class with a new property and its value based on the `openai_model` property. - Style: Replaces a constant with an options object. - Test: No changes made. - Chore: No changes made. This pull request improves the codebase by refactoring it, adding a new feature to reply to top-level comments, and removing console error logs. It also updates the documentation and makes style changes. <!-- end of auto-generated comment: release notes by openai -->
1 parent 81273ab commit 31c2116

File tree

6 files changed

+148
-109
lines changed

6 files changed

+148
-109
lines changed

dist/index.js

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

src/commenter.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,59 @@ ${COMMENT_TAG}`
140140
}
141141
}
142142

143+
async review_comment_reply(
144+
pull_number: number,
145+
top_level_comment: any,
146+
message: string
147+
) {
148+
const reply = `${COMMENT_GREETING}
149+
150+
${message}
151+
152+
${COMMENT_REPLY_TAG}
153+
`
154+
try {
155+
// Post the reply to the user comment
156+
await octokit.pulls.createReplyForReviewComment({
157+
owner: repo.owner,
158+
repo: repo.repo,
159+
pull_number,
160+
body: reply,
161+
comment_id: top_level_comment.id
162+
})
163+
} catch (error) {
164+
core.warning(`Failed to reply to the top-level comment ${error}`)
165+
try {
166+
await octokit.pulls.createReplyForReviewComment({
167+
owner: repo.owner,
168+
repo: repo.repo,
169+
pull_number,
170+
body: `Could not post the reply to the top-level comment due to the following error: ${error}`,
171+
comment_id: top_level_comment.id
172+
})
173+
} catch (e) {
174+
core.warning(`Failed to reply to the top-level comment ${e}`)
175+
}
176+
}
177+
try {
178+
if (top_level_comment.body.includes(COMMENT_TAG)) {
179+
// replace COMMENT_TAG with COMMENT_REPLY_TAG in topLevelComment
180+
const newBody = top_level_comment.body.replace(
181+
COMMENT_TAG,
182+
COMMENT_REPLY_TAG
183+
)
184+
await octokit.pulls.updateReviewComment({
185+
owner: repo.owner,
186+
repo: repo.repo,
187+
comment_id: top_level_comment.id,
188+
body: newBody
189+
})
190+
}
191+
} catch (error) {
192+
core.warning(`Failed to update the top-level comment ${error}`)
193+
}
194+
}
195+
143196
async get_comments_at_line(pull_number: number, path: string, line: number) {
144197
const comments = await this.list_review_comments(pull_number)
145198
return comments.filter(

src/main.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async function run(): Promise<void> {
5454
} else if (
5555
process.env.GITHUB_EVENT_NAME === 'pull_request_review_comment'
5656
) {
57-
await handleReviewComment(bot, prompts)
57+
await handleReviewComment(bot, options, prompts)
5858
} else {
5959
core.warning('Skipped: this action only works on push event')
6060
}
@@ -69,11 +69,9 @@ async function run(): Promise<void> {
6969

7070
process
7171
.on('unhandledRejection', (reason, p) => {
72-
console.error(reason, 'Unhandled Rejection at Promise', p)
7372
core.warning(`Unhandled Rejection at Promise: ${reason}, promise is ${p}`)
7473
})
7574
.on('uncaughtException', (e: any) => {
76-
console.error(e, 'Uncaught Exception thrown')
7775
core.warning(`Uncaught Exception thrown: ${e}, backtrace: ${e.stack}`)
7876
})
7977

src/options.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ export class Options {
202202
openai_retries: number
203203
openai_timeout_ms: number
204204
openai_concurrency_limit: number
205+
max_tokens_for_extra_content: number
205206

206207
constructor(
207208
debug: boolean,
@@ -225,6 +226,14 @@ export class Options {
225226
this.openai_retries = parseInt(openai_retries)
226227
this.openai_timeout_ms = parseInt(openai_timeout_ms)
227228
this.openai_concurrency_limit = parseInt(openai_concurrency_limit)
229+
230+
if (this.openai_model === 'gpt-4') {
231+
this.max_tokens_for_extra_content = 5000
232+
} else if (this.openai_model === 'gpt-3.5-turbo') {
233+
this.max_tokens_for_extra_content = 2500
234+
} else {
235+
this.max_tokens_for_extra_content = 1000
236+
}
228237
}
229238

230239
check_path(path: string): boolean {

0 commit comments

Comments
 (0)