Skip to content

Commit b323db5

Browse files
authored
Merge pull request #12 from mvtavares/adding_logs
adding some logs
2 parents cc303c1 + 1f5528d commit b323db5

File tree

7 files changed

+81
-19
lines changed

7 files changed

+81
-19
lines changed

.github/workflows/review.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
on: [pull_request]
1+
on:
2+
pull_request:
3+
types: [ opened, review_requested, synchronize ]
4+
paths-ignore:
5+
- 'dist/**'
26

37
name: ChatGPT CodeReview
4-
8+
env:
9+
ACTIONS_RUNNER_DEBUG: true
10+
511
jobs:
612
chatgpt_comment:
13+
#if: contains(github.event.pull_request.requested_reviewers.*.login, 'mvtavares')
714
runs-on: ubuntu-latest
815
name: Let ChatGPT comment on your PR.
916
steps:

dist/index.js

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

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/chatgpt-api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {ChatGPTAPI, ChatMessage} from 'chatgpt'
22
import * as core from '@actions/core'
33

4+
export const MODEL_MAX_LENGTH = 4097;
5+
46
export async function createChatGPTAPI(): Promise<ChatGPTAPI> {
57
const api = new ChatGPTAPI({
68
apiKey: process.env.OPENAI_API_KEY ?? '',

src/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import {readFileSync} from 'fs'
66

77
async function run(): Promise<void> {
88
try {
9-
9+
core.debug("Initializando...")
1010
const ev = JSON.parse(
1111
readFileSync(`${process.env.GITHUB_EVENT_PATH}`, 'utf8')
1212
)
1313
const prNum = ev.pull_request.number
14+
core.debug(`PR number is: ${prNum}`)
1415

1516
const mode = core.getInput('mode')
17+
core.debug(`Running mode: ${mode}`)
18+
1619
const split = 'yolo'
1720
// Get current repo.
1821
const [owner, repo] = process.env.GITHUB_REPOSITORY

src/mode/pr_review.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import {genReviewPRPrompt, genReviewPRSplitedPrompt} from '../prompt'
33
import {callChatGPT, startConversation} from '../chatgpt-api'
44
import {Octokit} from '@octokit/action'
55
import * as github from '@actions/github'
6+
import * as utils from '../utils/string-utils'
67

78
const octokit = new Octokit()
89
const context = github.context
10+
const MAX_BODY_LENGTH = 500
911

1012
/* eslint-disable @typescript-eslint/no-explicit-any */
1113
export async function runPRReview(
@@ -32,19 +34,32 @@ export async function runPRReview(
3234
})
3335
let reply: string
3436
if (split === 'yolo') {
37+
core.debug(`Diff is: ${diff}`)
3538
// check this line
36-
const prompt = genReviewPRPrompt(title, body ?? '', JSON.stringify(diff))
39+
let promptBody = body
40+
if (body && body.length > MAX_BODY_LENGTH) {
41+
promptBody = utils.truncate(body, MAX_BODY_LENGTH)
42+
}
43+
core.debug(`title length: ${title.length}`)
44+
core.debug(`body length: ${promptBody?.length}`)
45+
const prompt = genReviewPRPrompt(title, promptBody ?? '', String(diff))
3746
core.info(`The prompt is: ${prompt}`)
47+
core.debug(`prompt length: ${title.length}`)
48+
3849
const response = await callChatGPT(api, prompt, 5)
39-
reply = response
50+
reply = response.text
4051
} else {
4152
reply = ''
4253
const {welcomePrompts, diffPrompts, endPrompt} = genReviewPRSplitedPrompt(
4354
title,
4455
body ?? '',
45-
JSON.stringify(diff),
56+
String(diff),
4657
65536
4758
)
59+
core.debug(`title length: ${title.length}`)
60+
core.debug(`body length: ${body?.length}`)
61+
core.debug(`diff length: ${title.length}`)
62+
4863
const conversation = startConversation(api, 5)
4964
let cnt = 0
5065
const prompts = welcomePrompts.concat(diffPrompts)
@@ -54,14 +69,14 @@ export async function runPRReview(
5469
core.info(`Sending ${prompt}`)
5570
response = await conversation.sendMessage(prompt, response)
5671
core.info(`Received ${response}`)
57-
reply += `**ChatGPT#${++cnt}**: ${response}\n\n`
72+
reply += `**ChatGPT#${++cnt}**: ${response.text}\n\n`
5873
// Wait for 10s
5974
await new Promise(r => setTimeout(r, 10000))
6075
}
61-
await octokit.issues.createComment({
62-
...context.repo,
63-
issue_number: pull_number,
64-
body: reply
65-
})
6676
}
77+
await octokit.issues.createComment({
78+
...context.repo,
79+
issue_number: pull_number,
80+
body: reply
81+
})
6782
}

src/utils/string-utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function truncate(str: string, n: number): string {
2+
return str.length > n ? `${str.slice(0, n - 1)}&hellip;` : str
3+
}

0 commit comments

Comments
 (0)