{"slug": "code-review-the-actual-git-branch-using-github-copilot-sdk", "title": "Code review the actual git branch using github copilot sdk.", "summary": "A developer has built a script that uses the GitHub Copilot SDK to automatically review the actual git branch by sending the diff between main and HEAD to a large language model for analysis. The script generates a JSON file with review comments, including file paths and line numbers, following enterprise standards. The tool is designed to streamline code review processes by leveraging AI.", "body_md": "| import { execSync } from \"node:child_process\"; | |\n| import fs from \"node:fs\"; | |\n| import { CopilotClient } from \"@github/copilot-sdk\"; | |\n| // Get diff Git | |\n| function getBranchDiff(projectlocation = \"./\") { | |\n| if (projectlocation !== \"./\") { | |\n| execSync(`cd ${projectlocation}`); | |\n| } | |\n| return execSync(\"git diff main...HEAD\", { | |\n| encoding: \"utf8\", | |\n| maxBuffer: 10 * 1024 * 1024, | |\n| }); | |\n| } | |\n| // Send the diff to Copilot for review | |\n| async function reviewDiff(diff) { | |\n| // Initialize the client with your GitHub token | |\n| const client = new CopilotClient({ | |\n| gitHubToken: process.env.GITHUB_TOKEN, | |\n| useLoggedInUser: false, | |\n| }); | |\n| await client.start(); | |\n| // Create a session with the desired model and streaming enabled | |\n| const session = await client.createSession({ | |\n| model: process.env.MODEL || \"claude-sonnet-5\", | |\n| streaming: true | |\n| }); | |\n| const prompt = ` | |\n| You are a senior software reviewer. Perform an in-depth code review following enterprise standards based on the provided context. | |\n| CRITICAL OUTPUT INSTRUCTIONS: | |\n| Return ONLY a valid JSON array. | |\n| Do not include markdown. | |\n| Do not include code fences. | |\n| Do not include explanations outside the JSON. | |\n| Do not include any text before or after the JSON. | |\n| Each item in the array must follow exactly this structure: | |\n| [ | |\n| { | |\n| \"body\": \"Clear and actionable review comment\", | |\n| \"labels\": [\"code-review\"], | |\n| \"assignee_id\": 456, | |\n| \"id\": 0, | |\n| \"file_path\": \"path/to/file.js\", | |\n| \"line_number\": 42 | |\n| } | |\n| ] | |\n| Rules: | |\n| - Focus only on meaningful issues | |\n| - Keep comments concise and useful | |\n| - Do not invent file paths or line numbers | |\n| - Do not include fields other than \"body\" | |\n| - Return [] if there are no important review comments | |\n| <git_diff> | |\n| ${diff} | |\n| </git_diff>`; | |\n| // Send the prompt to the session and wait for the response | |\n| const response = await session.sendAndWait(prompt,300000); | |\n| await client.stop(); | |\n| return response; | |\n| } | |\n| async function main() { | |\n| const diff = getBranchDiff('./'); // Adjust the path to your project if needed | |\n| const review = await reviewDiff(diff); | |\n| // create a JSON file with the review result | |\n| fs.writeFileSync('review.json', review.data.content); | |\n| } | |\n| main().catch((error) => { | |\n| console.error(\"Script failed:\", error); | |\n| process.exit(1); | |\n| }); |", "url": "https://wpnews.pro/news/code-review-the-actual-git-branch-using-github-copilot-sdk", "canonical_source": "https://gist.github.com/MariemChaabeni/aa044efa3d4c5f2cde305679527d8a63", "published_at": "2026-07-29 10:53:39+00:00", "updated_at": "2026-08-03 10:00:09.808190+00:00", "lang": "en", "topics": ["developer-tools", "large-language-models", "ai-tools"], "entities": ["GitHub Copilot SDK", "GitHub", "CopilotClient"], "alternates": {"html": "https://wpnews.pro/news/code-review-the-actual-git-branch-using-github-copilot-sdk", "markdown": "https://wpnews.pro/news/code-review-the-actual-git-branch-using-github-copilot-sdk.md", "text": "https://wpnews.pro/news/code-review-the-actual-git-branch-using-github-copilot-sdk.txt", "jsonld": "https://wpnews.pro/news/code-review-the-actual-git-branch-using-github-copilot-sdk.jsonld"}}