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

Commit 373123c

Browse files
authored
type fixes (#112)
<!-- This is an auto-generated comment: release notes by openai --> ### Summary by OpenAI Release Notes: - Bug fix: Improves error handling and logging in `Bot` class's `chat` and `chat_` methods. - Refactor: Changes the function signature of `codeReview` to return a Promise. - Documentation: Updates some formatting in the generated comment. > "Type fixes galore, > Bugs squashed forevermore. > Code now more robust, > Celebrate with a joyful thrust!" <!-- end of auto-generated comment: release notes by openai -->
1 parent eaddf5e commit 373123c

File tree

4 files changed

+66
-56
lines changed

4 files changed

+66
-56
lines changed

src/bot.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ export class Bot {
3838
}
3939

4040
chat = async (message: string, ids: Ids): Promise<[string, Ids]> => {
41-
let new_ids: Ids = {}
42-
let response = ''
41+
let res: [string, Ids] = ['', {}]
4342
try {
44-
;[response, new_ids] = await this.chat_(message, ids)
45-
} catch (e: any) {
46-
core.warning(`Failed to chat: ${e}, backtrace: ${e.stack}`)
47-
} finally {
48-
return [response, new_ids]
43+
res = await this.chat_(message, ids)
44+
return res
45+
} catch (e: unknown) {
46+
if (e instanceof openai.ChatGPTError)
47+
core.warning(`Failed to chat: ${e}, backtrace: ${e.stack}`)
48+
return res
4949
}
5050
}
5151

@@ -59,7 +59,7 @@ export class Bot {
5959
core.info(`sending to openai: ${message}`)
6060
}
6161

62-
let response: openai.ChatMessage | null = null
62+
let response: openai.ChatMessage | undefined
6363

6464
if (this.api) {
6565
const opts: openai.SendMessageOptions = {
@@ -69,15 +69,16 @@ export class Bot {
6969
opts.parentMessageId = ids.parentMessageId
7070
}
7171
try {
72-
response = await utils.retry(
72+
response = await utils.retry<openai.ChatMessage>(
7373
this.api.sendMessage.bind(this.api),
7474
[message, opts],
7575
this.options.openai_retries
7676
)
77-
} catch (e: any) {
78-
core.info(
79-
`response: ${response}, failed to send message to openai: ${e}, backtrace: ${e.stack}`
80-
)
77+
} catch (e: unknown) {
78+
if (e instanceof openai.ChatGPTError)
79+
core.info(
80+
`response: ${response}, failed to send message to openai: ${e}, backtrace: ${e.stack}`
81+
)
8182
}
8283
const end = Date.now()
8384
core.info(`response: ${JSON.stringify(response)}`)

src/commenter.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
12
import * as core from '@actions/core'
23
import * as github from '@actions/github'
34
import {Octokit} from '@octokit/action'
5+
import {ChatGPTError} from 'chatgpt'
46

57
const token = core.getInput('token')
68
? core.getInput('token')
@@ -121,7 +123,7 @@ ${tag}`
121123
body: new_description
122124
})
123125
}
124-
} catch (e: any) {
126+
} catch (e) {
125127
core.warning(
126128
`Failed to get PR: ${e}, skipping adding release notes to description.`
127129
)
@@ -169,7 +171,7 @@ ${tag}`
169171
line
170172
})
171173
}
172-
} catch (e: any) {
174+
} catch (e) {
173175
core.warning(`Failed to post review comment: ${e}`)
174176
}
175177
}
@@ -239,7 +241,7 @@ ${COMMENT_REPLY_TAG}
239241
pull_number: number,
240242
path: string,
241243
line: number,
242-
tag: string = ''
244+
tag = ''
243245
) {
244246
const existing_comments = await this.get_comments_at_line(
245247
pull_number,
@@ -300,7 +302,7 @@ ${chain}
300302
top_level_comment
301303
)
302304
return {chain, topLevelComment: top_level_comment}
303-
} catch (e: any) {
305+
} catch (e) {
304306
core.warning(`Failed to get conversation chain: ${e}`)
305307
return {
306308
chain: '',
@@ -347,8 +349,8 @@ ${chain}
347349
}
348350

349351
return all_comments
350-
} catch (e: any) {
351-
console.warn(`Failed to list review comments: ${e}`)
352+
} catch (e) {
353+
core.warning(`Failed to list review comments: ${e}`)
352354
return all_comments
353355
}
354356
}
@@ -361,7 +363,7 @@ ${chain}
361363
issue_number: target,
362364
body
363365
})
364-
} catch (e: any) {
366+
} catch (e) {
365367
core.warning(`Failed to create comment: ${e}`)
366368
}
367369
}
@@ -379,7 +381,7 @@ ${chain}
379381
} else {
380382
await this.create(body, target)
381383
}
382-
} catch (e: any) {
384+
} catch (e) {
383385
core.warning(`Failed to replace comment: ${e}`)
384386
}
385387
}
@@ -397,7 +399,7 @@ ${chain}
397399
} else {
398400
await this.create(body, target)
399401
}
400-
} catch (e: any) {
402+
} catch (e) {
401403
core.warning(`Failed to append comment: ${e}`)
402404
}
403405
}
@@ -415,7 +417,7 @@ ${chain}
415417
} else {
416418
await this.create(body, target)
417419
}
418-
} catch (e: any) {
420+
} catch (e) {
419421
core.warning(`Failed to prepend comment: ${e}`)
420422
}
421423
}
@@ -430,7 +432,7 @@ ${chain}
430432
}
431433

432434
return null
433-
} catch (e: any) {
435+
} catch (e: unknown) {
434436
core.warning(`Failed to find comment with tag: ${e}`)
435437
return null
436438
}
@@ -456,8 +458,10 @@ ${chain}
456458
}
457459

458460
return all_comments
459-
} catch (e: any) {
460-
console.warn(`Failed to list comments: ${e}`)
461+
} catch (e: unknown) {
462+
if (e instanceof ChatGPTError) {
463+
core.warning(`Failed to list comments: ${e}`)
464+
}
461465
return all_comments
462466
}
463467
}

src/review.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {Bot} from './bot.js'
66
import {Commenter, COMMENT_REPLY_TAG, SUMMARIZE_TAG} from './commenter.js'
77
import {Inputs, Options, Prompts} from './options.js'
88
import * as tokenizer from './tokenizer.js'
9+
import {ChatGPTError} from 'chatgpt'
910

1011
const token = core.getInput('token')
1112
? core.getInput('token')
@@ -18,7 +19,7 @@ export const codeReview = async (
1819
bot: Bot,
1920
options: Options,
2021
prompts: Prompts
21-
) => {
22+
): Promise<void> => {
2223
const commenter: Commenter = new Commenter()
2324

2425
const openai_concurrency_limit = pLimit(options.openai_concurrency_limit)
@@ -122,12 +123,7 @@ export const codeReview = async (
122123
patches.push([line, patch])
123124
}
124125
if (patches.length > 0) {
125-
return [file.filename, file_content, file_diff, patches] as [
126-
string,
127-
string,
128-
string,
129-
[number, string][]
130-
]
126+
return [file.filename, file_content, file_diff, patches]
131127
} else {
132128
return null
133129
}
@@ -233,8 +229,9 @@ ${filename}: ${summary}
233229
234230
---
235231
236-
${filter_ignored_files.length > 0
237-
? `
232+
${
233+
filter_ignored_files.length > 0
234+
? `
238235
<details>
239236
<summary>Files ignored due to filter (${filter_ignored_files.length})</summary>
240237
@@ -244,23 +241,25 @@ ${filter_ignored_files.length > 0
244241
245242
</details>
246243
`
247-
: ''
248-
}
244+
: ''
245+
}
249246
250-
${skipped_files_to_summarize.length > 0
251-
? `
247+
${
248+
skipped_files_to_summarize.length > 0
249+
? `
252250
<details>
253-
<summary>Files not summarized due to max files limit (${skipped_files_to_summarize.length
254-
})</summary>
251+
<summary>Files not summarized due to max files limit (${
252+
skipped_files_to_summarize.length
253+
})</summary>
255254
256255
### Not summarized
257256
258257
* ${skipped_files_to_summarize.join('\n* ')}
259258
260259
</details>
261260
`
262-
: ''
263-
}
261+
: ''
262+
}
264263
`
265264

266265
next_summarize_ids = summarize_final_response_ids
@@ -388,10 +387,12 @@ ${skipped_files_to_summarize.length > 0
388387
} else {
389388
ins.comment_chain = 'no previous comments'
390389
}
391-
} catch (e: any) {
392-
core.warning(
393-
`Failed to get comments: ${e}, skipping. backtrace: ${e.stack}`
394-
)
390+
} catch (e: unknown) {
391+
if (e instanceof ChatGPTError) {
392+
core.warning(
393+
`Failed to get comments: ${e}, skipping. backtrace: ${e.stack}`
394+
)
395+
}
395396
}
396397

397398
try {
@@ -414,12 +415,14 @@ ${skipped_files_to_summarize.length > 0
414415
line,
415416
`${response}`
416417
)
417-
} catch (e: any) {
418-
core.warning(`Failed to comment: ${e}, skipping.
418+
} catch (e: unknown) {
419+
if (e instanceof ChatGPTError) {
420+
core.warning(`Failed to comment: ${e}, skipping.
419421
backtrace: ${e.stack}
420422
filename: ${filename}
421423
line: ${line}
422424
patch: ${patch}`)
425+
}
423426
}
424427
}
425428
}
@@ -457,10 +460,12 @@ ${skipped_files_to_summarize.length > 0
457460
if (skipped_files_to_review.length > 0) {
458461
// make bullet points for skipped files
459462
const comment = `
460-
${skipped_files_to_review.length > 0
463+
${
464+
skipped_files_to_review.length > 0
461465
? `<details>
462-
<summary>Files not reviewed due to max files limit (${skipped_files_to_review.length
463-
})</summary>
466+
<summary>Files not reviewed due to max files limit (${
467+
skipped_files_to_review.length
468+
})</summary>
464469
465470
### Not reviewed
466471
@@ -469,7 +474,7 @@ ${skipped_files_to_summarize.length > 0
469474
</details>
470475
`
471476
: ''
472-
}
477+
}
473478
`
474479
if (comment.length > 0) {
475480
await commenter.comment(comment, SUMMARIZE_TAG, 'append')

src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as core from '@actions/core'
22

3-
export const retry = async (
3+
export const retry = async <T = unknown>(
44
fn: Function,
5-
args: any[],
5+
args: unknown[],
66
times: number
7-
): Promise<any> => {
7+
): Promise<T | undefined> => {
88
for (let i = 0; i < times; i++) {
99
try {
1010
return await fn(...args)

0 commit comments

Comments
 (0)