# Code review the actual git branch using github copilot sdk.

> Source: <https://gist.github.com/MariemChaabeni/aa044efa3d4c5f2cde305679527d8a63>
> Published: 2026-07-29 10:53:39+00:00

| import { execSync } from "node:child_process"; | |
| import fs from "node:fs"; | |
| import { CopilotClient } from "@github/copilot-sdk"; | |
| // Get diff Git | |
| function getBranchDiff(projectlocation = "./") { | |
| if (projectlocation !== "./") { | |
| execSync(`cd ${projectlocation}`); | |
| } | |
| return execSync("git diff main...HEAD", { | |
| encoding: "utf8", | |
| maxBuffer: 10 * 1024 * 1024, | |
| }); | |
| } | |
| // Send the diff to Copilot for review | |
| async function reviewDiff(diff) { | |
| // Initialize the client with your GitHub token | |
| const client = new CopilotClient({ | |
| gitHubToken: process.env.GITHUB_TOKEN, | |
| useLoggedInUser: false, | |
| }); | |
| await client.start(); | |
| // Create a session with the desired model and streaming enabled | |
| const session = await client.createSession({ | |
| model: process.env.MODEL || "claude-sonnet-5", | |
| streaming: true | |
| }); | |
| const prompt = ` | |
| You are a senior software reviewer. Perform an in-depth code review following enterprise standards based on the provided context. | |
| CRITICAL OUTPUT INSTRUCTIONS: | |
| Return ONLY a valid JSON array. | |
| Do not include markdown. | |
| Do not include code fences. | |
| Do not include explanations outside the JSON. | |
| Do not include any text before or after the JSON. | |
| Each item in the array must follow exactly this structure: | |
| [ | |
| { | |
| "body": "Clear and actionable review comment", | |
| "labels": ["code-review"], | |
| "assignee_id": 456, | |
| "id": 0, | |
| "file_path": "path/to/file.js", | |
| "line_number": 42 | |
| } | |
| ] | |
| Rules: | |
| - Focus only on meaningful issues | |
| - Keep comments concise and useful | |
| - Do not invent file paths or line numbers | |
| - Do not include fields other than "body" | |
| - Return [] if there are no important review comments | |
| <git_diff> | |
| ${diff} | |
| </git_diff>`; | |
| // Send the prompt to the session and wait for the response | |
| const response = await session.sendAndWait(prompt,300000); | |
| await client.stop(); | |
| return response; | |
| } | |
| async function main() { | |
| const diff = getBranchDiff('./'); // Adjust the path to your project if needed | |
| const review = await reviewDiff(diff); | |
| // create a JSON file with the review result | |
| fs.writeFileSync('review.json', review.data.content); | |
| } | |
| main().catch((error) => { | |
| console.error("Script failed:", error); | |
| process.exit(1); | |
| }); |
