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

Commit 1dfd2f0

Browse files
authored
limit event to created comments (#46)
<!-- This is an auto-generated comment: release notes by openai --> ### Summary by OpenAI **Release Notes** This pull request introduces a new feature that limits the event to created comments only. The changes include adding a new event type "created" to trigger the workflow only when a new pull request review comment is created, updating the pull request event to trigger only on "created" comments, refactoring getConversationChain method to use getTopLevelComment method, adding getTopLevelComment method to get top-level comment of a conversation chain, adding COMMENT_GREETING constant for greeting message, and modifying handleReviewComment function to skip events that are not created and include the new comment in the conversation chain. <!-- end of auto-generated comment: release notes by openai -->
1 parent 1eb31e7 commit 1dfd2f0

File tree

5 files changed

+108
-51
lines changed

5 files changed

+108
-51
lines changed

.github/workflows/chatgpt-review.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ permissions:
44
contents: read
55
pull-requests: write
66

7-
on: [pull_request, pull_request_review_comment]
7+
on:
8+
pull_request:
9+
pull_request_review_comment:
10+
types: [created]
811

912
concurrency:
1013
group:

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ permissions:
3232
contents: read
3333
pull-requests: write
3434

35-
on: [pull_request, pull_request_review_comment]
35+
on:
36+
pull_request:
37+
pull_request_review_comment:
38+
types: [created]
3639

3740
concurrency:
3841
group:

dist/index.js

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

src/commenter.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,18 @@ ${tag}`
141141
async getConversationChain(pull_number: number, comment: any) {
142142
try {
143143
const reviewComments = await list_review_comments(pull_number)
144-
const conversationChain: string[] = [
145-
`${comment.user.login}-(${comment.id}): ${comment.body}`
146-
]
147-
148-
let in_reply_to_id = comment.in_reply_to_id
149-
let topLevelComment: any | null
144+
const topLevelComment = await this.getTopLevelComment(
145+
reviewComments,
146+
comment
147+
)
150148

151-
while (in_reply_to_id) {
152-
const parentComment = reviewComments.find(
153-
(cmt: any) => cmt.id === in_reply_to_id
154-
)
149+
const conversationChain = reviewComments
150+
.filter((cmt: any) => cmt.in_reply_to_id === topLevelComment.id)
151+
.map((cmt: any) => `${cmt.user.login}-(${cmt.id}): ${cmt.body}`)
155152

156-
if (parentComment) {
157-
conversationChain.unshift(
158-
`${parentComment.user.login}-(${parentComment.id}): ${parentComment.body}`
159-
)
160-
in_reply_to_id = parentComment.in_reply_to_id
161-
topLevelComment = parentComment
162-
} else {
163-
break
164-
}
165-
}
153+
conversationChain.unshift(
154+
`${topLevelComment.user.login}: ${topLevelComment.body}`
155+
)
166156

167157
return {
168158
chain: conversationChain.join('\n---\n'),
@@ -176,6 +166,24 @@ ${tag}`
176166
}
177167
}
178168
}
169+
170+
async getTopLevelComment(reviewComments: any[], comment: any) {
171+
let topLevelComment = comment
172+
173+
while (topLevelComment.in_reply_to_id) {
174+
const parentComment = reviewComments.find(
175+
(cmt: any) => cmt.id === topLevelComment.in_reply_to_id
176+
)
177+
178+
if (parentComment) {
179+
topLevelComment = parentComment
180+
} else {
181+
break
182+
}
183+
}
184+
185+
return topLevelComment
186+
}
179187
}
180188

181189
const list_review_comments = async (target: number, page: number = 1) => {

src/review-comment.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import * as core from '@actions/core'
22
import * as github from '@actions/github'
33
import {Octokit} from '@octokit/action'
44
import {Bot} from './bot.js'
5-
import {Commenter, COMMENT_REPLY_TAG, COMMENT_TAG} from './commenter.js'
5+
import {
6+
Commenter,
7+
COMMENT_GREETING,
8+
COMMENT_REPLY_TAG,
9+
COMMENT_TAG
10+
} from './commenter.js'
611

712
const token = core.getInput('token')
813
? core.getInput('token')
@@ -11,7 +16,7 @@ const token = core.getInput('token')
1116
const octokit = new Octokit({auth: `token ${token}`})
1217
const context = github.context
1318
const repo = context.repo
14-
const BOT_INVITE = '@openai'
19+
const ASK_BOT = '@openai'
1520

1621
export const handleReviewComment = async (bot: Bot) => {
1722
const commenter: Commenter = new Commenter()
@@ -38,6 +43,12 @@ export const handleReviewComment = async (bot: Bot) => {
3843
return
3944
}
4045

46+
// check if the comment was created and not edited or deleted
47+
if (context.payload.action !== 'created') {
48+
core.warning(`Skipped: ${context.eventName} event is not created`)
49+
return
50+
}
51+
4152
// Check if the comment is not from the bot itself
4253
if (
4354
!comment.body.includes(COMMENT_TAG) &&
@@ -50,11 +61,12 @@ export const handleReviewComment = async (bot: Bot) => {
5061
pull_number,
5162
comment
5263
)
64+
core.info(`Conversation chain: ${chain}`)
5365
// check whether this chain contains replies from the bot
5466
if (
5567
chain.includes(COMMENT_TAG) ||
5668
chain.includes(COMMENT_REPLY_TAG) ||
57-
comment.body.startsWith(BOT_INVITE)
69+
comment.body.startsWith(ASK_BOT)
5870
) {
5971
const prompt = `I would like you to reply to the new comment made on a conversation chain on a code review diff.
6072
@@ -63,16 +75,24 @@ Diff:
6375
${diffHunk}
6476
\`\`\`
6577
66-
Conversation chain:
78+
Conversation chain (including the new comment):
6779
\`\`\`
6880
${chain}
6981
\`\`\`
7082
71-
Please reply to the latest comment in the conversation chain without extra prose as that reply will be posted as-is.`
83+
Please reply to the new comment in the conversation chain without extra prose as that reply will be posted as-is. Make sure to tag the user in your reply. Providing below the new comment again as reference:
84+
\`\`\`
85+
${comment.user.login}: ${comment.body}
86+
\`\`\`
87+
`
7288

7389
const [reply] = await bot.chat(prompt, {})
74-
const message = `${COMMENT_REPLY_TAG}\n${reply}`
90+
const message = `${COMMENT_GREETING}
91+
92+
${reply}
7593
94+
${COMMENT_REPLY_TAG}
95+
`
7696
if (topLevelComment) {
7797
const topLevelCommentId = topLevelComment.id
7898
try {
@@ -102,5 +122,7 @@ Please reply to the latest comment in the conversation chain without extra prose
102122
core.warning(`Failed to find the top-level comment to reply to`)
103123
}
104124
}
125+
} else {
126+
core.info(`Skipped: ${context.eventName} event is from the bot itself`)
105127
}
106128
}

0 commit comments

Comments
 (0)