Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .github/cr-gpt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_API_KEY: sk-dtIzk2nobuSuTdnw7G3nT3BlbkFJNCjTGHzgnaskUeB11M1HJ6111
35 changes: 33 additions & 2 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Probot } from 'probot';
import { Context, Probot } from 'probot';
import { Chat } from './chat.js';

const OPENAI_API_KEY = 'OPENAI_API_KEY';

export const robot = (app: Probot) => {
const chat = new Chat();
// const getDiff = async (context: Context, pullRequestNumber: number) => {
// const repo = context.repo();

Expand All @@ -26,7 +27,31 @@ export const robot = (app: Probot) => {
// return diff as unknown as string;
// };

const loadChat = async (context: Context) => {
const repo = context.repo();
const { data } = (await context.octokit.request(
'GET /repos/{owner}/{repo}/actions/variables/{name}',
{
owner: repo.owner,
repo: repo.repo,
name: OPENAI_API_KEY,
}
)) as any;

if (!data?.value) {
return null;
}

return new Chat(data.value);
};

app.on('pull_request.opened', async (context) => {
const chat = await loadChat(context);

if (!chat) {
return 'no chat initialized';
}

async function cr() {
const issueComment = context.issue({
body: await chat.codeReview(
Expand Down Expand Up @@ -56,6 +81,12 @@ export const robot = (app: Probot) => {
return;
}

const chat = await loadChat(context);

if (!chat) {
return 'no chat initialized';
}

async function cr() {
// const diff = await getDiff(context, context.payload.issue.number);
const issueComment = context.issue({
Expand Down
15 changes: 10 additions & 5 deletions src/chat.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { ChatGPTAPI } from 'chatgpt';
export class Chat {
private chatAPI: ChatGPTAPI;

const chatAPI = new ChatGPTAPI({
apiKey: process.env.OPENAI_API_KEY as string,
});
constructor(apikey: string) {
this.chatAPI = new ChatGPTAPI({
apiKey: apikey,
});
}

export class Chat {
private generatePrompt = (_: string, patch: string) => {
return `
Act as a code reviewer of a Pull Request, providing feedback on the code changes below.
Expand Down Expand Up @@ -33,7 +36,9 @@ export class Chat {
console.time('code-review cost');
console.log('start query chatGPT');

const res = await chatAPI.sendMessage(this.generatePrompt(title, patch));
const res = await this.chatAPI.sendMessage(
this.generatePrompt(title, patch)
);

console.log('end query chatGPT');
console.timeEnd('code-review cost');
Expand Down