From fbe8236249b08895dd5d3b523e77939139cef1cc Mon Sep 17 00:00:00 2001 From: Emanuel Braz Date: Wed, 8 Nov 2023 00:07:39 -0300 Subject: [PATCH 1/6] feat: add code review functionality --- .github/workflows/code-review.yaml | 24 +++ code-review/README.md | 66 ++++++++ code-review/action.js | 231 ++++++++++++++++++++++++++++ code-review/action.yml | 31 ++++ services/simple_chat_gpt_service.js | 12 ++ 5 files changed, 364 insertions(+) create mode 100644 .github/workflows/code-review.yaml create mode 100644 code-review/README.md create mode 100644 code-review/action.js create mode 100644 code-review/action.yml diff --git a/.github/workflows/code-review.yaml b/.github/workflows/code-review.yaml new file mode 100644 index 0000000..ceb0e80 --- /dev/null +++ b/.github/workflows/code-review.yaml @@ -0,0 +1,24 @@ +name: Code Reviewer +run-name: Action started by ${{ github.actor }} + +on: + pull_request: + types: + - opened + - synchronize + +permissions: write-all + +jobs: + review: + runs-on: ubuntu-latest + steps: + - name: Checkout Repo + uses: actions/checkout@v3 + + - name: AI Code Reviewer + uses: ./code-review + with: + token: ${{ secrets.GITHUB_TOKEN }} + openai_key: ${{ secrets.OPENAI_KEY }} + exclude: "**/*.json, **/*.md, **/*.g.dart" \ No newline at end of file diff --git a/code-review/README.md b/code-review/README.md new file mode 100644 index 0000000..cf94bbe --- /dev/null +++ b/code-review/README.md @@ -0,0 +1,66 @@ +# Code Reviewer + +Code Reviewer is a GitHub Action that leverages OpenAI's GPT API to provide intelligent feedback and suggestions on +your pull requests. This powerful tool helps improve code quality and saves developers time by automating the code +review process. + +## Features + +- Reviews pull requests using OpenAI's chat GPT API. +- Provides intelligent comments and suggestions for improving your code. +- Filters out files that match specified exclude patterns. +- Easy to set up and integrate into your GitHub workflow. + +## Setup + +1. To use this GitHub Action, you need an OpenAI API key. If you don't have one, sign up for an API key + at [OpenAI](https://beta.openai.com/signup). + +2. Add the OpenAI API key as a GitHub Secret in your repository with the name `openai_key`. You can find more + information about GitHub Secrets [here](https://docs.github.com/en/actions/reference/encrypted-secrets). + +3. Create a `.github/workflows/main.yml` file in your repository and add the following content: + +```yaml +name: Code Reviewer +run-name: Action started by ${{ github.actor }} + +on: + pull_request: + types: + - opened + - synchronize + +permissions: write-all + +jobs: + review: + runs-on: ubuntu-latest + steps: + - name: Checkout Repo + uses: actions/checkout@v3 + + - name: AI Code Reviewer + uses: emanuel-braz/github-actions/code-review@0.1.0 + with: + token: ${{ secrets.GITHUB_TOKEN }} + openai_key: ${{ secrets.OPENAI_KEY }} + exclude: "**/*.json, **/*.md, **/*.g.dart" # Optional: exclude patterns separated by commas +``` + +- Customize the `exclude` input if you want to ignore certain file patterns from being reviewed. + +- Commit the changes to your repository, and AI Code Reviewer will start working on your future pull requests. + +## How It Works + +The AI Code Reviewer GitHub Action retrieves the pull request diff, filters out excluded files, and sends code chunks to +the OpenAI API. It then generates review comments based on the AI's response and adds them to the pull request. + +## Contributing + +Contributions are welcome! Please feel free to submit issues or pull requests to improve the GitHub Actions. + +## License + +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information. \ No newline at end of file diff --git a/code-review/action.js b/code-review/action.js new file mode 100644 index 0000000..720eb97 --- /dev/null +++ b/code-review/action.js @@ -0,0 +1,231 @@ +require('child_process') + .execSync( + 'npm install @actions/core @actions/github parse-diff minimatch fs', + { cwd: __dirname } + ); + +let parse = require('parse-diff'); +const minimatch = require('minimatch'); +const core = require('@actions/core'); +const github = require('@actions/github'); +const fs = require('fs'); +const SimpleChatGptService = require('../services/simple_chat_gpt_service.js'); +const ChatCompletionParams = require('../services/gpt/chat_completion_params.js'); +const Logger = require('../utils/logger.js'); + +const logger = new Logger(true, core); +const GITHUB_TOKEN = core.getInput("token"); +const OPENAI_API_KEY = core.getInput("openai_key"); +const OPENAI_API_MODEL = core.getInput("openai_key_model"); +const overridePrompt = core.getInput("override_prompt"); +const appendPrompt = core.getInput("append_prompt"); +const excludePatterns = core + .getInput("exclude") + .split(",") + .map((s) => s.trim()); + +const octokit = github.getOctokit(GITHUB_TOKEN); + +async function getPRDetails() { + const { repository, number } = JSON.parse( + fs.readFileSync(process.env.GITHUB_EVENT_PATH || "", "utf8") + ); + const prResponse = await octokit.pulls.get({ + owner: repository.owner.login, + repo: repository.name, + pull_number: number, + }); + return { + owner: repository.owner.login, + repo: repository.name, + pull_number: number, + title: prResponse.data.title ?? "", + description: prResponse.data.body ?? "", + }; +} + +async function getDiff(owner, repo, pull_number) { + const response = await octokit.pulls.get({ + owner, + repo, + pull_number, + mediaType: { format: "diff" }, + }); + // @ts-expect-error - response.data is a string + return response.data; +} + +async function analyzeCode(parsedDiff, prDetails) { + const comments = []; //Array<{ body: string; path: string; line: number }> + + for (const file of parsedDiff) { + if (file.to === "/dev/null") continue; // Ignore deleted files + for (const chunk of file.chunks) { + const messages = createMessages(file, chunk, prDetails); + const aiResponse = await getAIResponse(messages); + if (aiResponse) { + const newComments = createComment(file, chunk, aiResponse); + if (newComments) { + comments.push(...newComments); + } + } + } + } + return comments; +} + +function createMessages(file, chunk, prDetails) { + let systemPrompt = + { + content: `You are a senior software engineer and your task is to review pull requests. Folow the instructions below. +- Provide the response in following JSON format: [{"lineNumber": , "reviewComment": ""}] +- You NEVER give positive comments or compliments. +- You NEVER consider removing empty line. +- You NEVER consider to remove trailing whitespace +- You NEVER consider adding comment to describe the purpose of methods +- You will Provide comments and suggestions ONLY if there is something to improve, otherwise return an empty array. +- You must write the comment in GitHub Markdown format. +- Do use the given description only for the overall context and only comment the code.`, + role: "system", + }; + + let userPrompt = + { + content: `Review the following code diff in the file "${file.to}" and take the pull request title and description into account when writing the response. + +Pull request title: ${prDetails.title} +Pull request description: + +--- +${prDetails.description} +--- + +Git diff to review: + +\`\`\`diff +${chunk.content} +${chunk.changes + // @ts-expect-error - ln and ln2 exists where needed + .map((c) => `${c.ln ? c.ln : c.ln2} ${c.content}`) + .join("\n")} +\`\`\` + `, + role: "user", + }; + + return [systemPrompt, userPrompt]; +} + +async function getAIResponse(messages) { + + try { + const chatCompletionParams = new ChatCompletionParams({ + messages: messages, + model: OPENAI_API_MODEL, + temperature: 0.2, + max_tokens: 700, + top_p: 1, + frequency_penalty: 0, + presence_penalty: 0, + }); + + const simpleChatGptService = new SimpleChatGptService(OPENAI_API_KEY); + const response = await simpleChatGptService.fromParams({ chatCompletionParams }); + + const result = response?.trim() || "[]"; + logger.log(`AI response: ${result}`); + return JSON.parse(result); + } catch (error) { + console.error("Error:", error); + return null; + } +} + +// Array<{ body: string; path: string; line: number }> +function createComment(file, chunk, aiResponses) { + return aiResponses.flatMap((aiResponse) => { + if (!file.to) { + return []; + } + return { + body: aiResponse.reviewComment, + path: file.to, + line: Number(aiResponse.lineNumber), + }; + }); +} + +// comments: Array<{ body: string; path: string; line: number }> +// return Promise +async function createReviewComment(owner, repo, pull_number, comments) { + await octokit.pulls.createReview({ + owner, + repo, + pull_number, + comments, + event: "COMMENT", + }); +} + +async function main() { + + const prDetails = await getPRDetails(); + let diff; // string | null + const eventData = JSON.parse( + fs.readFileSync(process.env.GITHUB_EVENT_PATH ?? "", "utf8") + ); + + if (eventData.action === "opened") { + diff = await getDiff( + prDetails.owner, + prDetails.repo, + prDetails.pull_number + ); + } else if (eventData.action === "synchronize") { + const newBaseSha = eventData.before; + const newHeadSha = eventData.after; + + const response = await octokit.repos.compareCommits({ + headers: { + accept: "application/vnd.github.v3.diff", + }, + owner: prDetails.owner, + repo: prDetails.repo, + base: newBaseSha, + head: newHeadSha, + }); + + diff = String(response.data); + } else { + console.log("Unsupported event:", process.env.GITHUB_EVENT_NAME); + return; + } + + if (!diff) { + console.log("No diff found"); + return; + } + + const parsedDiff = parse(diff); + + const filteredDiff = parsedDiff.filter((file) => { + return !excludePatterns.some((pattern) => + minimatch.minimatch(file.to ?? "", pattern) + ); + }); + + const comments = await analyzeCode(filteredDiff, prDetails); + if (comments.length > 0) { + await createReviewComment( + prDetails.owner, + prDetails.repo, + prDetails.pull_number, + comments + ); + } +} + +main().catch((error) => { + console.error("Error:", error); + process.exit(1); +}); diff --git a/code-review/action.yml b/code-review/action.yml new file mode 100644 index 0000000..91fbcb4 --- /dev/null +++ b/code-review/action.yml @@ -0,0 +1,31 @@ +name: emanuel-braz/code-review +description: "Perform code reviews and comment on diffs using OpenAI API." +author: Emanuel Braz +branding: + icon: send + color: gray-dark +inputs: + token: + description: "GitHub token to interact with the repository." + required: true + openai_key: + description: "OpenAI API key for GPT." + required: true + openai_key_model: + description: "OpenAI API model." + required: false + default: "gpt-3.5-turbo" + exclude: + description: "Glob patterns to exclude files from the diff analysis" + required: false + default: "" + override_prompt: + description: "The text to be used to override the default prompt." + required: false + append_prompt: + description: "The text to be used to append to the default prompt." + required: false + +runs: + using: "node16" + main: ./action.js diff --git a/services/simple_chat_gpt_service.js b/services/simple_chat_gpt_service.js index 0ceb1ed..b84e466 100644 --- a/services/simple_chat_gpt_service.js +++ b/services/simple_chat_gpt_service.js @@ -29,6 +29,18 @@ class SimpleChatGptService { throw error; } } + + // chatCompletionParams: ChatCompletionParams + async fromParams({ chatCompletionParams }) { + try { + const response = await this.service.chatCompletions(chatCompletionParams); + const message = response.choices[0].message.content; + return message; + } catch (error) { + console.error('[SimpleChatGptService]', error); + throw error; + } + } } module.exports = SimpleChatGptService; \ No newline at end of file From 99824af027123c309a74e644382d299c6cae3bfb Mon Sep 17 00:00:00 2001 From: Emanuel Braz Date: Wed, 8 Nov 2023 14:34:22 -0300 Subject: [PATCH 2/6] feat: prompt improvements --- .github/workflows/code-review.yaml | 9 ++++--- code-review/action.js | 40 +++++++++++++++++++----------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/.github/workflows/code-review.yaml b/.github/workflows/code-review.yaml index ceb0e80..d128e55 100644 --- a/.github/workflows/code-review.yaml +++ b/.github/workflows/code-review.yaml @@ -10,15 +10,16 @@ on: permissions: write-all jobs: - review: + code-review: runs-on: ubuntu-latest steps: - name: Checkout Repo - uses: actions/checkout@v3 + uses: actions/checkout@v4 - - name: AI Code Reviewer + - name: Code Review the pull request uses: ./code-review with: token: ${{ secrets.GITHUB_TOKEN }} openai_key: ${{ secrets.OPENAI_KEY }} - exclude: "**/*.json, **/*.md, **/*.g.dart" \ No newline at end of file + exclude: "**/*.json, **/*.md, **/*.g.dart" + append_prompt: "Give a maximum of 3 suggestions" \ No newline at end of file diff --git a/code-review/action.js b/code-review/action.js index 720eb97..e25172f 100644 --- a/code-review/action.js +++ b/code-review/action.js @@ -75,17 +75,31 @@ async function analyzeCode(parsedDiff, prDetails) { } function createMessages(file, chunk, prDetails) { - let systemPrompt = - { - content: `You are a senior software engineer and your task is to review pull requests. Folow the instructions below. -- Provide the response in following JSON format: [{"lineNumber": , "reviewComment": ""}] -- You NEVER give positive comments or compliments. -- You NEVER consider removing empty line. -- You NEVER consider to remove trailing whitespace -- You NEVER consider adding comment to describe the purpose of methods -- You will Provide comments and suggestions ONLY if there is something to improve, otherwise return an empty array. + const instructionJsonFormat = `- Provide the response in following JSON format: [{"lineNumber": , "reviewComment": ""}]`; + + var contentSystemMessage = `You are a senior software engineer and your task is to review pull requests for possible bugs or bad development practices. Follow the instructions below: +- You should NEVER give positive comments or compliments. +- You should NEVER suggest removing empty line. +- You should NEVER suggest to remove trailing or leading whitespace. +- You should NEVER suggest adding comment to describe the purpose of methods or functions. +- You ONLY will provide comments and suggestions if there is something to improve, otherwise return an empty array. - You must write the comment in GitHub Markdown format. -- Do use the given description only for the overall context and only comment the code.`, +- Do use the given pull request title and description only for the overall context and only comment the code.`; + + if (overridePrompt) { + contentSystemMessage = overridePrompt; + } + + contentSystemMessage = `${contentSystemMessage}\n${instructionJsonFormat}`; + + if (appendPrompt) { + contentSystemMessage = `${contentSystemMessage}\n\n${appendPrompt}`; + } + + + var systemPrompt = + { + content: contentSystemMessage, role: "system", }; @@ -109,7 +123,7 @@ ${chunk.changes .map((c) => `${c.ln ? c.ln : c.ln2} ${c.content}`) .join("\n")} \`\`\` - `, + `, role: "user", }; @@ -123,7 +137,7 @@ async function getAIResponse(messages) { messages: messages, model: OPENAI_API_MODEL, temperature: 0.2, - max_tokens: 700, + max_tokens: 900, top_p: 1, frequency_penalty: 0, presence_penalty: 0, @@ -155,8 +169,6 @@ function createComment(file, chunk, aiResponses) { }); } -// comments: Array<{ body: string; path: string; line: number }> -// return Promise async function createReviewComment(owner, repo, pull_number, comments) { await octokit.pulls.createReview({ owner, From 078c2a347795b4eaaacd0cfd6754d5f76757a34d Mon Sep 17 00:00:00 2001 From: Emanuel Braz Date: Wed, 8 Nov 2023 14:48:56 -0300 Subject: [PATCH 3/6] feat: reduce temperature --- code-review/action.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code-review/action.js b/code-review/action.js index e25172f..2c0c5c3 100644 --- a/code-review/action.js +++ b/code-review/action.js @@ -136,7 +136,7 @@ async function getAIResponse(messages) { const chatCompletionParams = new ChatCompletionParams({ messages: messages, model: OPENAI_API_MODEL, - temperature: 0.2, + temperature: 0, max_tokens: 900, top_p: 1, frequency_penalty: 0, From 4ea08b51edc221a135f6c32213f6ea9176f8b5a0 Mon Sep 17 00:00:00 2001 From: Emanuel Braz Date: Wed, 8 Nov 2023 15:49:49 -0300 Subject: [PATCH 4/6] chore: using portuguese to sugestions --- .github/workflows/code-review.yaml | 3 ++- code-review/action.js | 16 ++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.github/workflows/code-review.yaml b/.github/workflows/code-review.yaml index d128e55..e35666a 100644 --- a/.github/workflows/code-review.yaml +++ b/.github/workflows/code-review.yaml @@ -22,4 +22,5 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} openai_key: ${{ secrets.OPENAI_KEY }} exclude: "**/*.json, **/*.md, **/*.g.dart" - append_prompt: "Give a maximum of 3 suggestions" \ No newline at end of file + append_prompt: | + - Translate the comment in all "reviewComment" properties to portuguese (pt-br). \ No newline at end of file diff --git a/code-review/action.js b/code-review/action.js index 2c0c5c3..11460da 100644 --- a/code-review/action.js +++ b/code-review/action.js @@ -75,15 +75,16 @@ async function analyzeCode(parsedDiff, prDetails) { } function createMessages(file, chunk, prDetails) { - const instructionJsonFormat = `- Provide the response in following JSON format: [{"lineNumber": , "reviewComment": ""}]`; + const instructionJsonFormat = `- Always provide the response in following JSON format: [{"lineNumber": , "reviewComment": ""}]`; var contentSystemMessage = `You are a senior software engineer and your task is to review pull requests for possible bugs or bad development practices. Follow the instructions below: -- You should NEVER give positive comments or compliments. -- You should NEVER suggest removing empty line. -- You should NEVER suggest to remove trailing or leading whitespace. -- You should NEVER suggest adding comment to describe the purpose of methods or functions. -- You ONLY will provide comments and suggestions if there is something to improve, otherwise return an empty array. -- You must write the comment in GitHub Markdown format. +- You should never give positive comments or compliments. +- You should never suggest removing empty line. +- You should not suggest adding a new line at the end of the file. +- You should never suggest to remove trailing or leading whitespace. +- You should never suggest adding comment to code. +- Avoid giving an excessive amount of suggestions for a single file. Prioritize the most important suggestions. +- You will provide suggestions only if there is possible issues or bugs in the code, otherwise return an empty array. - Do use the given pull request title and description only for the overall context and only comment the code.`; if (overridePrompt) { @@ -96,7 +97,6 @@ function createMessages(file, chunk, prDetails) { contentSystemMessage = `${contentSystemMessage}\n\n${appendPrompt}`; } - var systemPrompt = { content: contentSystemMessage, From f7e89f5593f979867a6770b1d00ffc4be460dd67 Mon Sep 17 00:00:00 2001 From: Emanuel Braz Date: Wed, 8 Nov 2023 19:19:43 -0300 Subject: [PATCH 5/6] feat: add max_tokens params --- .github/workflows/code-review.yaml | 2 ++ README.md | 2 ++ code-review/README.md | 10 +++++++--- code-review/action.js | 5 ++++- code-review/action.yml | 4 ++++ package.json | 2 +- 6 files changed, 20 insertions(+), 5 deletions(-) diff --git a/.github/workflows/code-review.yaml b/.github/workflows/code-review.yaml index e35666a..faeea9c 100644 --- a/.github/workflows/code-review.yaml +++ b/.github/workflows/code-review.yaml @@ -21,6 +21,8 @@ jobs: with: token: ${{ secrets.GITHUB_TOKEN }} openai_key: ${{ secrets.OPENAI_KEY }} + max_tokens: 900 exclude: "**/*.json, **/*.md, **/*.g.dart" append_prompt: | + - Give a minimum of 0 suggestions and a maximum of 5 suggestions. - Translate the comment in all "reviewComment" properties to portuguese (pt-br). \ No newline at end of file diff --git a/README.md b/README.md index b5884e6..c299c40 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ #### [Generate chat GPT message](simple-chat-gpt/README.md) +#### [Code review Pull Requests](code-review/README.md) + #### Soon more actions will be added to have a complete Gitflow utilities.: - [ ] Manage PR - [ ] Manage Issue diff --git a/code-review/README.md b/code-review/README.md index cf94bbe..0855313 100644 --- a/code-review/README.md +++ b/code-review/README.md @@ -38,23 +38,27 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout Repo - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: AI Code Reviewer uses: emanuel-braz/github-actions/code-review@0.1.0 with: token: ${{ secrets.GITHUB_TOKEN }} openai_key: ${{ secrets.OPENAI_KEY }} + max_tokens: 900 exclude: "**/*.json, **/*.md, **/*.g.dart" # Optional: exclude patterns separated by commas + append_prompt: | + - Give a minimum of 0 suggestions and a maximum of 5 suggestions. + - Translate the comment in all "reviewComment" properties to portuguese (pt-br). ``` - Customize the `exclude` input if you want to ignore certain file patterns from being reviewed. -- Commit the changes to your repository, and AI Code Reviewer will start working on your future pull requests. +- Commit the changes to your repository, and Code Reviewer Actions will start working on your future pull requests. ## How It Works -The AI Code Reviewer GitHub Action retrieves the pull request diff, filters out excluded files, and sends code chunks to +The Code Reviewer GitHub Action retrieves the pull request diff, filters out excluded files, and sends code chunks to the OpenAI API. It then generates review comments based on the AI's response and adds them to the pull request. ## Contributing diff --git a/code-review/action.js b/code-review/action.js index 11460da..7ed435b 100644 --- a/code-review/action.js +++ b/code-review/action.js @@ -19,6 +19,7 @@ const OPENAI_API_KEY = core.getInput("openai_key"); const OPENAI_API_MODEL = core.getInput("openai_key_model"); const overridePrompt = core.getInput("override_prompt"); const appendPrompt = core.getInput("append_prompt"); +const maxTokens = core.getInput("max_tokens"); const excludePatterns = core .getInput("exclude") .split(",") @@ -132,12 +133,14 @@ ${chunk.changes async function getAIResponse(messages) { + logger.log(`Max tokens: ${maxTokens}`); + try { const chatCompletionParams = new ChatCompletionParams({ messages: messages, model: OPENAI_API_MODEL, temperature: 0, - max_tokens: 900, + max_tokens: parseInt(maxTokens), top_p: 1, frequency_penalty: 0, presence_penalty: 0, diff --git a/code-review/action.yml b/code-review/action.yml index 91fbcb4..771dafa 100644 --- a/code-review/action.yml +++ b/code-review/action.yml @@ -15,6 +15,10 @@ inputs: description: "OpenAI API model." required: false default: "gpt-3.5-turbo" + max_tokens: + description: "OpenAI API max tokens." + default: "900" + required: false exclude: description: "Glob patterns to exclude files from the diff analysis" required: false diff --git a/package.json b/package.json index e726d7d..d9fb33c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "braz-actions", - "version": "0.0.7", + "version": "0.1.0", "private": true, "description": "GitHub Actions", "main": "create-update-release/action.js", From 7449956ea2a0d516ee40de1ef33e88dc1aad9077 Mon Sep 17 00:00:00 2001 From: Emanuel Braz Date: Wed, 8 Nov 2023 19:51:30 -0300 Subject: [PATCH 6/6] feat: testing different prompts --- .github/workflows/code-review.yaml | 5 +++-- CHANGELOG.md | 11 +++++++---- code-review/action.js | 15 ++++++++------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/.github/workflows/code-review.yaml b/.github/workflows/code-review.yaml index faeea9c..72a2bf3 100644 --- a/.github/workflows/code-review.yaml +++ b/.github/workflows/code-review.yaml @@ -24,5 +24,6 @@ jobs: max_tokens: 900 exclude: "**/*.json, **/*.md, **/*.g.dart" append_prompt: | - - Give a minimum of 0 suggestions and a maximum of 5 suggestions. - - Translate the comment in all "reviewComment" properties to portuguese (pt-br). \ No newline at end of file + - Give a maximum of 4 suggestions + - Do not suggest code formatting issues. + - Do not suggest imports issues. \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d708b0..ca78738 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ -## 0.0.2 (2023-09-10) -* chore: add example and fix typo ([a0cff7b](https://github.com/emanuel-braz/action-release/commit/a0cff7b)) +## 0.1.0 (2023-11-09) +* feat: add ai code review -## 0.0.1 (2023-09-10) -* initial commit, migrate repo ([769459d](https://github.com/emanuel-braz/action-release/commit/769459d)) \ No newline at end of file +## 0.0.2 (2023-09-10) +* chore: add example and fix typo + +## 0.0.1 (2023-09-10) +* initial commit \ No newline at end of file diff --git a/code-review/action.js b/code-review/action.js index 7ed435b..f7bb873 100644 --- a/code-review/action.js +++ b/code-review/action.js @@ -62,6 +62,7 @@ async function analyzeCode(parsedDiff, prDetails) { for (const file of parsedDiff) { if (file.to === "/dev/null") continue; // Ignore deleted files for (const chunk of file.chunks) { + const messages = createMessages(file, chunk, prDetails); const aiResponse = await getAIResponse(messages); if (aiResponse) { @@ -79,13 +80,13 @@ function createMessages(file, chunk, prDetails) { const instructionJsonFormat = `- Always provide the response in following JSON format: [{"lineNumber": , "reviewComment": ""}]`; var contentSystemMessage = `You are a senior software engineer and your task is to review pull requests for possible bugs or bad development practices. Follow the instructions below: -- You should never give positive comments or compliments. -- You should never suggest removing empty line. -- You should not suggest adding a new line at the end of the file. -- You should never suggest to remove trailing or leading whitespace. -- You should never suggest adding comment to code. -- Avoid giving an excessive amount of suggestions for a single file. Prioritize the most important suggestions. -- You will provide suggestions only if there is possible issues or bugs in the code, otherwise return an empty array. +- You will provide suggestions only if there are issues or bugs in the code, otherwise return an empty array. +- Do not give positive comments or compliments. +- Don't suggest removing empty line or adding line to end of file. +- Ignore "\\ No newline at end of file" information. +- Don't suggest to remove trailing or leading whitespace. +- Don't suggest to remove the spaces. +- Don't suggest adding comment to code. - Do use the given pull request title and description only for the overall context and only comment the code.`; if (overridePrompt) {