diff --git a/README.md b/README.md index 60d6333..3cb8d3f 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,29 @@ # 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) @@ -20,7 +31,7 @@ Powered by [Probot](https://github.com/probot/probot) & [chatgpt-api](https://gi 2. **`PRIVATE_KEY` is required, it should be encoded by `base64`**.(`console.log(Buffer.from().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 5. Type `/chatgpt` in an issue, chat with the bot *step4: update webhook URL to your vercel app domain.* diff --git a/assets/review.jpg b/assets/review.jpg new file mode 100644 index 0000000..9680fa1 Binary files /dev/null and b/assets/review.jpg differ diff --git a/index.js b/index.js index 1b6b59b..67fa59f 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ 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) }) @@ -12,49 +12,40 @@ module.exports = (app) => { 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) - // }) }; diff --git a/rollup.config-1670388714434.cjs b/rollup.config-1670388714434.cjs new file mode 100644 index 0000000..a20861d --- /dev/null +++ b/rollup.config-1670388714434.cjs @@ -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; diff --git a/src/client.js b/src/client.js index 982fd66..a927eb1 100644 --- a/src/client.js +++ b/src/client.js @@ -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} ` @@ -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} `