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

Commit c0e847f

Browse files
authored
Don't summarize large diffs (#8)
New Feature: This pull request adds new methods to the `Commenter` class that allow for adding and removing a summary from the description field of a PR. It also modifies the `codeReview` function in `src/review.ts` to use these new methods. The changes are well-implemented and make the code more modular.
1 parent 7b63dd6 commit c0e847f

File tree

3 files changed

+148
-128
lines changed

3 files changed

+148
-128
lines changed

dist/index.js

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

src/commenter.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ const COMMENT_GREETING = `:robot: ChatGPT`
1313

1414
const DEFAULT_TAG = '<!-- This is an auto-generated comment by ChatGPT -->'
1515

16+
const description_tag =
17+
'<!-- This is an auto-generated comment: release notes by chatgpt -->'
18+
const description_tag_end =
19+
'<!-- end of auto-generated comment: release notes by chatgpt -->'
20+
1621
export class Commenter {
1722
/**
1823
* @param mode Can be "create", "replace", "append" and "prepend". Default is "replace".
@@ -21,6 +26,61 @@ export class Commenter {
2126
await comment(message, tag, mode)
2227
}
2328

29+
get_description(description: string) {
30+
// remove our summary from description by looking for description_tag and description_tag_end
31+
const start = description.indexOf(description_tag)
32+
const end = description.indexOf(description_tag_end)
33+
if (start >= 0 && end >= 0) {
34+
return (
35+
description.slice(0, start) +
36+
description.slice(end + description_tag_end.length)
37+
)
38+
}
39+
return description
40+
}
41+
42+
async update_description(
43+
pull_number: number,
44+
description: string,
45+
message: string
46+
) {
47+
// add this response to the description field of the PR as release notes by looking
48+
// for the tag (marker)
49+
try {
50+
// find the tag in the description and replace the content between the tag and the tag_end
51+
// if not found, add the tag and the content to the end of the description
52+
const tag_index = description.indexOf(description_tag)
53+
const tag_end_index = description.indexOf(description_tag_end)
54+
const comment = `\n\n${description_tag}\n${message}\n${description_tag_end}`
55+
if (tag_index === -1 || tag_end_index === -1) {
56+
let new_description = description
57+
new_description += comment
58+
await octokit.pulls.update({
59+
owner: repo.owner,
60+
repo: repo.repo,
61+
pull_number,
62+
body: new_description
63+
})
64+
} else {
65+
let new_description = description.substring(0, tag_index)
66+
new_description += comment
67+
new_description += description.substring(
68+
tag_end_index + description_tag_end.length
69+
)
70+
await octokit.pulls.update({
71+
owner: repo.owner,
72+
repo: repo.repo,
73+
pull_number,
74+
body: new_description
75+
})
76+
}
77+
} catch (e: any) {
78+
core.warning(
79+
`Failed to get PR: ${e}, skipping adding release notes to description.`
80+
)
81+
}
82+
}
83+
2484
async review_comment(
2585
pull_number: number,
2686
commit_id: string,

src/review.ts

Lines changed: 25 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ const token = core.getInput('token')
1212
const octokit = new Octokit({auth: `token ${token}`})
1313
const context = github.context
1414
const repo = context.repo
15-
const description_tag =
16-
'<!-- This is an auto-generated comment: release notes by chatgpt -->'
17-
const description_tag_end =
18-
'<!-- end of auto-generated comment: release notes by chatgpt -->'
1915

2016
const MAX_TOKENS_FOR_EXTRA_CONTENT = 2500
2117

@@ -39,18 +35,14 @@ export const codeReview = async (
3935
return
4036
}
4137

38+
const commenter: Commenter = new Commenter()
39+
4240
const inputs: Inputs = new Inputs()
4341
inputs.title = context.payload.pull_request.title
4442
if (context.payload.pull_request.body) {
45-
inputs.description = context.payload.pull_request.body
46-
// remove our summary from description by looking for description_tag and description_tag_end
47-
const start = inputs.description.indexOf(description_tag)
48-
const end = inputs.description.indexOf(description_tag_end)
49-
if (start >= 0 && end >= 0) {
50-
inputs.description =
51-
inputs.description.slice(0, start) +
52-
inputs.description.slice(end + description_tag_end.length)
53-
}
43+
inputs.description = commenter.get_description(
44+
context.payload.pull_request.body
45+
)
5446
}
5547
// as gpt-3.5-turbo isn't paying attention to system message, add to inputs for now
5648
inputs.system_message = options.system_message
@@ -115,8 +107,6 @@ export const codeReview = async (
115107
}
116108

117109
if (files_to_review.length > 0) {
118-
const commenter: Commenter = new Commenter()
119-
120110
// Summary Stage
121111
const [, summarize_begin_ids] = await bot.chat(
122112
prompts.render_summarize_beginning(inputs),
@@ -128,15 +118,18 @@ export const codeReview = async (
128118
inputs.file_content = file_content
129119
inputs.file_diff = file_diff
130120
if (file_diff.length > 0) {
131-
// summarize diff
132-
const [summarize_resp, summarize_diff_ids] = await bot.chat(
133-
prompts.render_summarize_file_diff(inputs),
134-
next_summarize_ids
135-
)
136-
if (!summarize_resp) {
137-
core.info('summarize: nothing obtained from chatgpt')
138-
} else {
139-
next_summarize_ids = summarize_diff_ids
121+
const file_diff_tokens = tokenizer.get_token_count(file_diff)
122+
if (file_diff_tokens < MAX_TOKENS_FOR_EXTRA_CONTENT) {
123+
// summarize diff
124+
const [summarize_resp, summarize_diff_ids] = await bot.chat(
125+
prompts.render_summarize_file_diff(inputs),
126+
next_summarize_ids
127+
)
128+
if (!summarize_resp) {
129+
core.info('summarize: nothing obtained from chatgpt')
130+
} else {
131+
next_summarize_ids = summarize_diff_ids
132+
}
140133
}
141134
}
142135
}
@@ -163,50 +156,14 @@ export const codeReview = async (
163156
core.info('release notes: nothing obtained from chatgpt')
164157
} else {
165158
next_summarize_ids = release_notes_ids
166-
// add this response to the description field of the PR as release notes by looking
167-
// for the tag (marker)
168-
try {
169-
const description = inputs.description
170-
171-
// find the tag in the description and replace the content between the tag and the tag_end
172-
// if not found, add the tag and the content to the end of the description
173-
const tag_index = description.indexOf(description_tag)
174-
const tag_end_index = description.indexOf(description_tag_end)
175-
if (tag_index === -1 || tag_end_index === -1) {
176-
let new_description = description
177-
new_description += description_tag
178-
new_description += '\n### Summary by ChatGPT\n'
179-
new_description += release_notes_response
180-
new_description += '\n'
181-
new_description += description_tag_end
182-
await octokit.pulls.update({
183-
owner: repo.owner,
184-
repo: repo.repo,
185-
pull_number: context.payload.pull_request.number,
186-
body: new_description
187-
})
188-
} else {
189-
let new_description = description.substring(0, tag_index)
190-
new_description += description_tag
191-
new_description += '\n### Summary by ChatGPT\n'
192-
new_description += release_notes_response
193-
new_description += '\n'
194-
new_description += description_tag_end
195-
new_description += description.substring(
196-
tag_end_index + description_tag_end.length
197-
)
198-
await octokit.pulls.update({
199-
owner: repo.owner,
200-
repo: repo.repo,
201-
pull_number: context.payload.pull_request.number,
202-
body: new_description
203-
})
204-
}
205-
} catch (e: any) {
206-
core.warning(
207-
`Failed to get PR: ${e}, skipping adding release notes to description.`
208-
)
209-
}
159+
const description = inputs.description
160+
let message = '### Summary by ChatGPT\n\n'
161+
message += release_notes_response
162+
commenter.update_description(
163+
context.payload.pull_request.number,
164+
description,
165+
message
166+
)
210167
}
211168

212169
// Review Stage

0 commit comments

Comments
 (0)