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

refactor code #69

Merged
merged 2 commits into from
Mar 20, 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
124 changes: 69 additions & 55 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions src/commenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,59 @@ ${COMMENT_TAG}`
}
}

async review_comment_reply(
pull_number: number,
top_level_comment: any,
message: string
) {
const reply = `${COMMENT_GREETING}

${message}

${COMMENT_REPLY_TAG}
`
try {
// Post the reply to the user comment
await octokit.pulls.createReplyForReviewComment({
owner: repo.owner,
repo: repo.repo,
pull_number,
body: reply,
comment_id: top_level_comment.id
})
} catch (error) {
core.warning(`Failed to reply to the top-level comment ${error}`)
try {
await octokit.pulls.createReplyForReviewComment({
owner: repo.owner,
repo: repo.repo,
pull_number,
body: `Could not post the reply to the top-level comment due to the following error: ${error}`,
comment_id: top_level_comment.id
})
} catch (e) {
core.warning(`Failed to reply to the top-level comment ${e}`)
}
}
try {
if (top_level_comment.body.includes(COMMENT_TAG)) {
// replace COMMENT_TAG with COMMENT_REPLY_TAG in topLevelComment
const newBody = top_level_comment.body.replace(
COMMENT_TAG,
COMMENT_REPLY_TAG
)
await octokit.pulls.updateReviewComment({
owner: repo.owner,
repo: repo.repo,
comment_id: top_level_comment.id,
body: newBody
})
}
} catch (error) {
core.warning(`Failed to update the top-level comment ${error}`)
}
}

async get_comments_at_line(pull_number: number, path: string, line: number) {
const comments = await this.list_review_comments(pull_number)
return comments.filter(
Expand Down
4 changes: 1 addition & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function run(): Promise<void> {
} else if (
process.env.GITHUB_EVENT_NAME === 'pull_request_review_comment'
) {
await handleReviewComment(bot, prompts)
await handleReviewComment(bot, options, prompts)
} else {
core.warning('Skipped: this action only works on push event')
}
Expand All @@ -69,11 +69,9 @@ async function run(): Promise<void> {

process
.on('unhandledRejection', (reason, p) => {
console.error(reason, 'Unhandled Rejection at Promise', p)
core.warning(`Unhandled Rejection at Promise: ${reason}, promise is ${p}`)
})
.on('uncaughtException', (e: any) => {
console.error(e, 'Uncaught Exception thrown')
core.warning(`Uncaught Exception thrown: ${e}, backtrace: ${e.stack}`)
})

Expand Down
9 changes: 9 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export class Options {
openai_retries: number
openai_timeout_ms: number
openai_concurrency_limit: number
max_tokens_for_extra_content: number

constructor(
debug: boolean,
Expand All @@ -225,6 +226,14 @@ export class Options {
this.openai_retries = parseInt(openai_retries)
this.openai_timeout_ms = parseInt(openai_timeout_ms)
this.openai_concurrency_limit = parseInt(openai_concurrency_limit)

if (this.openai_model === 'gpt-4') {
this.max_tokens_for_extra_content = 5000
} else if (this.openai_model === 'gpt-3.5-turbo') {
this.max_tokens_for_extra_content = 2500
} else {
this.max_tokens_for_extra_content = 1000
}
}

check_path(path: string): boolean {
Expand Down
Loading