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 1 commit
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
96 changes: 52 additions & 44 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
47 changes: 5 additions & 42 deletions src/review-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {Octokit} from '@octokit/action'
import {Bot} from './bot.js'
import {
Commenter,
COMMENT_GREETING,
COMMENT_REPLY_TAG,
COMMENT_TAG,
SUMMARIZE_TAG
Expand Down Expand Up @@ -175,48 +174,12 @@ export const handleReviewComment = async (bot: Bot, prompts: Prompts) => {
next_comment_ids
)

const message = `${COMMENT_GREETING}

${reply}

${COMMENT_REPLY_TAG}
`
if (topLevelComment) {
const topLevelCommentId = topLevelComment.id
try {
// Post the reply to the user comment
await octokit.pulls.createReplyForReviewComment({
owner: repo.owner,
repo: repo.repo,
pull_number,
body: message,
comment_id: topLevelCommentId
})
// replace COMMENT_TAG with COMMENT_REPLY_TAG in topLevelComment
const newBody = topLevelComment.body.replace(
COMMENT_TAG,
COMMENT_REPLY_TAG
)
await octokit.pulls.updateReviewComment({
owner: repo.owner,
repo: repo.repo,
comment_id: topLevelCommentId,
body: newBody
})
} catch (error) {
core.warning(`Failed to reply to the top-level comment`)
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: topLevelCommentId
})
} catch (error) {
core.warning(`Failed to reply to the top-level comment`)
}
}
await commenter.review_comment_reply(
pull_number,
topLevelComment,
reply
)
} else {
core.warning(`Failed to find the top-level comment to reply to`)
}
Expand Down