Skip to content

Commit 864fd24

Browse files
committed
feat: support review & refactor code in PR
1 parent ba7e27a commit 864fd24

File tree

5 files changed

+60
-32
lines changed

5 files changed

+60
-32
lines changed

README.md

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
# ChatGPT ProBot
2+
23
[![Release Version](https://img.shields.io/github/release/oceanlvr/ChatGPTBot.svg)](https://github.com/oceanlvr/ChatGPTBot/releases/latest) ![Twitter](https://img.shields.io/twitter/follow/AdaMeta1?style=social)
34

45
[![GitHub](https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white)](https://github.com/apps/chatgptbot) [![Deploy to Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/oceanlvr/ChatGPTBot)
56

6-
A ChatGPT-based GitHub APP. Type `/chatgpt` to chat with robot 🤖️. **Try on [issue#1](https://github.com/oceanlvr/ChatGPT-ProBot/issues/1)**
7-
7+
A ChatGPT-based GitHub APP. Type `/chatgpt` to chat with robot 🤖️.
88

99
![hello](./assets/Hi.jpg)
1010

1111
Powered by [Probot](https://github.com/probot/probot) & [chatgpt-api](https://github.com/transitive-bullshit/chatgpt-api)
1212

13-
## Deploy your own APP
13+
## Usage
1414

15+
**Try on [issue#1](https://github.com/oceanlvr/ChatGPT-ProBot/issues/1)**
1516

17+
**Try Review/Refactor on [PR#7](https://github.com/oceanlvr/ChatGPT-ProBot/pull/7)**
18+
19+
| event | description | example |
20+
| ----------- | --------------------------------- | --------------------------------------- |
21+
| `/ping` | ping robot status | |
22+
| `/chatgpt` | chat with bot on issue/PR comment | /chatgpt who are you? |
23+
| `/review` | auto review code in the PR | /review fix the callback hell problem |
24+
| `/refactor` | refactor the code | /refactor fix the callback hell problem |
25+
26+
## Deploy your own APP
1627

1728
1. [Install & Configure the GitHub App](https://github.com/apps/chatgptbot)
1829
2. Create `.env` file following `example.env`, please check config section in your [GitHub apps page](https://github.com/settings/apps)
1930
1. `APP_ID/PRIVATE_KEY/GITHUB_CLIENT_SECRET/GITHUB_CLIENT_ID` is required, please check [chatgptbot settings](https://github.com/settings/apps/chatgptbot) and fill them.
2031
2. **`PRIVATE_KEY` is required, it should be encoded by `base64`**.(`console.log(Buffer.from(<PRIVATE_KEY>).toString('base64'))`).
2132
3. `SESSION_TOKEN` is required, it is generated by `ChatGPT` [website](https://chat.openai.com/chat). You can get it following [this step](https://github.com/transitive-bullshit/chatgpt-api#how-it-works).
2233
3. Vercel Deploy (**recommend**), click [![Deploy to Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/oceanlvr/ChatGPTBot) to clone deploy. Copy `.env` file environment to vercel app [environment-variables](https://vercel.com/docs/concepts/projects/environment-variables) in setting page (for me it's `https://vercel.com/oceanlvr/chatgptbot/settings/environment-variables`)
23-
4. Edit the webhooks URL to `${vercelAPPURL}/api/github/webhooks`. For me it's https://chatgptbot.vercel.app/api/github/webhooks
34+
4. Edit the webhooks URL to `${vercelAPPURL}/api/github/webhooks`. For me it's <https://chatgptbot.vercel.app/api/github/webhooks>
2435
5. Type `/chatgpt` in an issue, chat with the bot
2536

2637
*step4: update webhook URL to your vercel app domain.*

assets/review.jpg

223 KB
Loading

index.js

+18-27
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,48 @@ const commands = require('probot-commands-pro')
44
module.exports = (app) => {
55
commands(app, 'ping', async (context) => {
66
const issueComment = context.issue({
7-
body: 'pong',
7+
body: '🤖️: pong',
88
})
99
return await context.octokit.issues.createComment(issueComment)
1010
})
1111

1212
commands(app, 'chatgpt', async (context) => {
1313
if (context.isBot)
1414
return
15-
const { comment, issue } = context.payload
15+
const { comment, issue, sender } = context.payload
1616
const { body } = comment || issue
1717
const prompt = body.replace('/chatgpt', '').trim()
1818
const response = await search(prompt)
1919
const issueComment = context.issue({
20-
body: response,
20+
body: `@${sender.login} 🤖️: ${response}`,
2121
})
2222
return await context.octokit.issues.createComment(issueComment)
2323
})
2424

25-
// wip: review code from code & add test
25+
// WIP: review code from code & add test
2626
// https://docs.github.com/en/issues/tracking-your-work-with-issues/creating-an-issue
27-
app.on('issues.opened', async (context) => { })
27+
//['issue_comment.created', 'issue_comment.edited', 'issues.opened', 'issues.edited']
28+
2829
// configure something
2930
app.on(['installation'], async (context) => { })
3031

31-
// add test && review && refactor
32-
app.on(['pull_request_review_comment'], async (context) => {
32+
app.on(['pull_request_review_comment.created'], async (context) => {
3333
if (context.isBot)
3434
return
35-
const { comment } = context.payload
36-
const { body, diff_hunk } = comment || issue
35+
const { comment, sender } = context.payload
36+
const { body, diff_hunk } = comment
37+
const eventHandlerMap = {
38+
'/review': review,
39+
'/refactor': refactor,
40+
}
41+
const event = Object.keys(eventHandlerMap).find((key) => body.includes(key))
42+
if (!event) return
3743

38-
if (!body.includes(`/review`)) return
39-
const prompt = body.replace('/review', '').trim()
40-
const response = await review({ prompt, lang: 'javascript', code: diff_hunk })
44+
const prompt = body.replace(event, '').trim()
45+
const response = await eventHandlerMap[event]({ prompt, lang: 'javascript', code: diff_hunk })
4146
const issueComment = context.issue({
42-
body: response,
47+
body: `@${sender.login} 🤖️: ${response}`,
4348
})
4449
return await context.octokit.issues.createComment(issueComment)
4550
})
46-
47-
// app.on(['issue_comment.created', 'issue_comment.edited', 'issues.opened', 'issues.edited'], async (context) => {
48-
// if (context.isBot)
49-
// return
50-
// const { comment, issue } = context.payload
51-
// const { body } = comment || issue
52-
// if (!body.includes(`/chatgpt`)) return
53-
// const prompt = body.replace('/chatgpt', '').trim()
54-
// const response = await search(prompt)
55-
// const issueComment = context.issue({
56-
// body: response,
57-
// })
58-
// return await context.octokit.issues.createComment(issueComment)
59-
// })
6051
};

rollup.config-1670388714434.cjs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, '__esModule', { value: true });
4+
5+
var resolve = require('@rollup/plugin-node-resolve');
6+
var commonjs = require('@rollup/plugin-commonjs');
7+
var terser = require('@rollup/plugin-terser');
8+
9+
var rollup_config = {
10+
input: 'index.js',
11+
output: {
12+
dir: './dist',
13+
format: 'cjs',
14+
},
15+
plugins: [
16+
resolve({ preferBuiltins: true, }),
17+
commonjs(),
18+
terser()
19+
],
20+
};
21+
22+
exports.default = rollup_config;

src/client.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ const { ChatGPTAPI } = require('@oceanlvr/chatgpt')
33
const client = new ChatGPTAPI({ sessionToken: process.env.SESSION_TOKEN })
44

55
async function search(searchPrompt) {
6-
// await client.ensureAuth()
6+
await client.ensureAuth()
77
const reponse = await client.sendMessage(searchPrompt)
88
return reponse
99
}
1010

1111
async function refactor({ code, lang, prompt }) {
12+
await client.ensureAuth()
13+
1214
const searchPrompt = `Refactor folloing ${lang} code. Do not include example usage. ${prompt}.
1315
${code}
1416
`
@@ -17,6 +19,8 @@ async function refactor({ code, lang, prompt }) {
1719
}
1820

1921
async function review({ code, lang, prompt }) {
22+
await client.ensureAuth()
23+
2024
const searchPrompt = `Review folloing ${lang} code. ${prompt}.
2125
${code}
2226
`

0 commit comments

Comments
 (0)