Skip to content

Commit

Permalink
feat: support review & refactor code in PR
Browse files Browse the repository at this point in the history
  • Loading branch information
oceanlvr committed Dec 7, 2022
1 parent ba7e27a commit 864fd24
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 32 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
# ChatGPT ProBot

[![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)

[![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)

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

A ChatGPT-based GitHub APP. Type `/chatgpt` to chat with robot 🤖️.

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

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

## Deploy your own APP
## Usage

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

**Try Review/Refactor on [PR#7](https://github.com/oceanlvr/ChatGPT-ProBot/pull/7)**

| event | description | example |
| ----------- | --------------------------------- | --------------------------------------- |
| `/ping` | ping robot status | |
| `/chatgpt` | chat with bot on issue/PR comment | /chatgpt who are you? |
| `/review` | auto review code in the PR | /review fix the callback hell problem |
| `/refactor` | refactor the code | /refactor fix the callback hell problem |

## Deploy your own APP

1. [Install & Configure the GitHub App](https://github.com/apps/chatgptbot)
2. Create `.env` file following `example.env`, please check config section in your [GitHub apps page](https://github.com/settings/apps)
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.
2. **`PRIVATE_KEY` is required, it should be encoded by `base64`**.(`console.log(Buffer.from(<PRIVATE_KEY>).toString('base64'))`).
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).
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`)
4. Edit the webhooks URL to `${vercelAPPURL}/api/github/webhooks`. For me it's https://chatgptbot.vercel.app/api/github/webhooks
4. Edit the webhooks URL to `${vercelAPPURL}/api/github/webhooks`. For me it's <https://chatgptbot.vercel.app/api/github/webhooks>
5. Type `/chatgpt` in an issue, chat with the bot

*step4: update webhook URL to your vercel app domain.*
Expand Down
Binary file added assets/review.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 18 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,48 @@ const commands = require('probot-commands-pro')
module.exports = (app) => {
commands(app, 'ping', async (context) => {
const issueComment = context.issue({
body: 'pong',
body: '🤖️: pong',
})
return await context.octokit.issues.createComment(issueComment)
})

commands(app, 'chatgpt', async (context) => {
if (context.isBot)
return
const { comment, issue } = context.payload
const { comment, issue, sender } = context.payload
const { body } = comment || issue
const prompt = body.replace('/chatgpt', '').trim()
const response = await search(prompt)
const issueComment = context.issue({
body: response,
body: `@${sender.login} 🤖️: ${response}`,
})
return await context.octokit.issues.createComment(issueComment)
})

// wip: review code from code & add test
// WIP: review code from code & add test
// https://docs.github.com/en/issues/tracking-your-work-with-issues/creating-an-issue
app.on('issues.opened', async (context) => { })
//['issue_comment.created', 'issue_comment.edited', 'issues.opened', 'issues.edited']

// configure something
app.on(['installation'], async (context) => { })

// add test && review && refactor
app.on(['pull_request_review_comment'], async (context) => {
app.on(['pull_request_review_comment.created'], async (context) => {
if (context.isBot)
return
const { comment } = context.payload
const { body, diff_hunk } = comment || issue
const { comment, sender } = context.payload
const { body, diff_hunk } = comment
const eventHandlerMap = {
'/review': review,
'/refactor': refactor,
}
const event = Object.keys(eventHandlerMap).find((key) => body.includes(key))
if (!event) return

if (!body.includes(`/review`)) return
const prompt = body.replace('/review', '').trim()
const response = await review({ prompt, lang: 'javascript', code: diff_hunk })
const prompt = body.replace(event, '').trim()
const response = await eventHandlerMap[event]({ prompt, lang: 'javascript', code: diff_hunk })
const issueComment = context.issue({
body: response,
body: `@${sender.login} 🤖️: ${response}`,
})
return await context.octokit.issues.createComment(issueComment)
})

// app.on(['issue_comment.created', 'issue_comment.edited', 'issues.opened', 'issues.edited'], async (context) => {
// if (context.isBot)
// return
// const { comment, issue } = context.payload
// const { body } = comment || issue
// if (!body.includes(`/chatgpt`)) return
// const prompt = body.replace('/chatgpt', '').trim()
// const response = await search(prompt)
// const issueComment = context.issue({
// body: response,
// })
// return await context.octokit.issues.createComment(issueComment)
// })
};
22 changes: 22 additions & 0 deletions rollup.config-1670388714434.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var resolve = require('@rollup/plugin-node-resolve');
var commonjs = require('@rollup/plugin-commonjs');
var terser = require('@rollup/plugin-terser');

var rollup_config = {
input: 'index.js',
output: {
dir: './dist',
format: 'cjs',
},
plugins: [
resolve({ preferBuiltins: true, }),
commonjs(),
terser()
],
};

exports.default = rollup_config;
6 changes: 5 additions & 1 deletion src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ const { ChatGPTAPI } = require('@oceanlvr/chatgpt')
const client = new ChatGPTAPI({ sessionToken: process.env.SESSION_TOKEN })

async function search(searchPrompt) {
// await client.ensureAuth()
await client.ensureAuth()
const reponse = await client.sendMessage(searchPrompt)
return reponse
}

async function refactor({ code, lang, prompt }) {
await client.ensureAuth()

const searchPrompt = `Refactor folloing ${lang} code. Do not include example usage. ${prompt}.
${code}
`
Expand All @@ -17,6 +19,8 @@ async function refactor({ code, lang, prompt }) {
}

async function review({ code, lang, prompt }) {
await client.ensureAuth()

const searchPrompt = `Review folloing ${lang} code. ${prompt}.
${code}
`
Expand Down

1 comment on commit 864fd24

@vercel
Copy link

@vercel vercel bot commented on 864fd24 Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

chatgptbot – ./

chatgptbot.vercel.app
chatgptbot-oceanlvr.vercel.app
chatgptbot-git-main-oceanlvr.vercel.app

Please sign in to comment.