{"id":71821,"date":"2025-05-05T16:01:11","date_gmt":"2025-05-05T10:31:11","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=71821"},"modified":"2025-05-06T15:09:14","modified_gmt":"2025-05-06T09:39:14","slug":"automating-code-reviews-using-openai-and-github","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/automating-code-reviews-using-openai-and-github\/","title":{"rendered":"Automating Code Reviews Using OpenAI and GitHub"},"content":{"rendered":"<h2>The State of Code Reviews in Today\u2019s Development Landscape:<\/h2>\n<p>In today\u2019s fast-moving world of software development, AI has made remarkable progress. It can write code, debug errors, and even help design architectures. But let\u2019s be honest, we\u2019re not quite at a point where AI can take over the entire development process. Human developers are still essential, not just for their coding skills, but because they bring something AI can&#8217;t replicate (yet): context, intuition, and a deep understanding of the business problem they&#8217;re solving.<\/p>\n<p>That said, even experienced developers make mistakes. Under tight deadlines and with growing code complexity, things slip through, logic bugs, performance issues, or even security gaps. This is why code reviews are so critical. They act as a second pair of eyes before any code is merged. Typically, platforms like GitHub are used for this. A developer raises a Pull Request (PR), and a teammate reviews the changes before they\u2019re approved. Below is how the current process works.<\/p>\n<div id=\"attachment_71841\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-71841\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-71841 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-12.10.31\u202fPM-1-1024x381.png\" alt=\"Current process of code review - TO THE NEW Blog\" width=\"625\" height=\"233\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-12.10.31\u202fPM-1-1024x381.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-12.10.31\u202fPM-1-300x112.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-12.10.31\u202fPM-1-768x286.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-12.10.31\u202fPM-1-1536x572.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-12.10.31\u202fPM-1-2048x763.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-12.10.31\u202fPM-1-624x232.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-71841\" class=\"wp-caption-text\">Current process of code review<\/p><\/div>\n<p>But code reviews themselves aren\u2019t perfect. Reviewers might be overloaded with tasks, unfamiliar with the specific part of the codebase, or just miss something. In teams handling dozens of PRs daily, giving every one of them enough attention is tough. And that\u2019s where AI can lend a hand.<\/p>\n<h2><\/h2>\n<h2>Where Generative AI Fits In<\/h2>\n<p>Generative AI \u2014 like OpenAI\u2019s models can serve as a helpful assistant during the code review process. Not as a replacement for human reviewers, but as an extra layer of insight. Imagine an AI that instantly looks at your PR, summarizes the changes, points out issues, and suggests better approaches, all within seconds of opening the PR.<\/p>\n<p>Here\u2019s how that helps:<\/p>\n<ul>\n<li>Every PR gets at least a baseline review.<\/li>\n<li>Reviewers can focus on complex logic or business rules, rather than spotting typos or missed edge cases.<\/li>\n<li>Developers get quicker feedback, which means faster iterations and fewer bugs.<\/li>\n<\/ul>\n<p>In this blog, I\u2019ll show you how to build an automated code review pipeline using:<\/p>\n<ul>\n<li>GitHub Actions (to trigger reviews when a PR is raised)<\/li>\n<li>Python (to extract code changes and communicate with OpenAI)<\/li>\n<li>OpenAI (for generating the review)<\/li>\n<li>GitHub PR Comments (to post the AI\u2019s feedback right where the developer needs it)<\/li>\n<\/ul>\n<h2><\/h2>\n<h2>Architecture Overview<\/h2>\n<p>Here\u2019s a simple architecture diagram showing how everything connects:<\/p>\n<h2><\/h2>\n<div id=\"attachment_71816\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-71816\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-71816 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-12.18.36\u202fPM-1024x415.png\" alt=\"Automated code review workflow | TO THE NEW blog\" width=\"625\" height=\"253\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-12.18.36\u202fPM-1024x415.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-12.18.36\u202fPM-300x122.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-12.18.36\u202fPM-768x312.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-12.18.36\u202fPM-1536x623.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-12.18.36\u202fPM-2048x831.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-12.18.36\u202fPM-624x253.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-71816\" class=\"wp-caption-text\">Automated code review workflow<\/p><\/div>\n<h2>Getting Started &#8211; What You\u2019ll Need<\/h2>\n<h3>A GitHub Repository<\/h3>\n<p>You can use any existing GitHub repo or create a new one for this. The AI will review future PRs raised in this repository.<\/p>\n<h3>An OpenAI Account<\/h3>\n<p>If you don\u2019t have one yet, create it here: <a href=\"https:\/\/platform.openai.com\/\">OpenAI Platform<\/a> Then:<\/p>\n<ul>\n<li>Check your available free credits <a href=\"https:\/\/platform.openai.com\/settings\/organization\/billing\/overview\">here<\/a>.<\/li>\n<li>If your credits are exhausted, add a payment method (minimum $5) <a href=\"https:\/\/platform.openai.com\/settings\/organization\/billing\/payment-methods\">here<\/a>.<\/li>\n<li>Generate an API key and save it in your GitHub repo under:<br \/>\nSettings \u2192 Secrets and variables \u2192 Actions \u2192 Add Secret \u2192 OPENAI_API_KEY<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>How It Works \u2014 Step by Step<\/h2>\n<h2><\/h2>\n<h3>1. Developer Raises a PR<\/h3>\n<p>This is your usual development flow, a feature or bugfix branch is pushed and a PR is created against the main branch.<\/p>\n<h3>2. GitHub Action Is Triggered<\/h3>\n<p>GitHub lets you run automated workflows on events like PR creation. You can set this up by adding a YAML file under <strong>.github\/workflows\/ai-review.yml<\/strong>.<\/p>\n<p>The workflow does the following:<\/p>\n<ul>\n<li>Checks out the code<\/li>\n<li>Installs dependencies (like the OpenAI Python client)<\/li>\n<li>Runs a Python script that triggers the review<\/li>\n<\/ul>\n<h3>3. Get the Git Diff<\/h3>\n<p>The Python script compares the current branch with the target (usually main) to find out what changed:<\/p>\n<pre>subprocess.check_output([\"git\", \"diff\", \"origin\/main...HEAD\"], text=True)<\/pre>\n<p>This gives us the exact changes the developer made.<\/p>\n<h3>4. Send the Diff to OpenAI<\/h3>\n<p>We send this diff to OpenAI using a chat completion API, like this:<\/p>\n<pre>client.chat.completions.create(\r\n\u00a0 \u00a0 model=\"gpt-3.5-turbo\",\r\n\u00a0 \u00a0 messages=[\r\n\u00a0 \u00a0 \u00a0 \u00a0 {\"role\": \"system\", \"content\": \"...\"},\r\n\u00a0 \u00a0 \u00a0 \u00a0 {\"role\": \"user\", \"content\": diff}\r\n\u00a0 \u00a0 ]\r\n)<\/pre>\n<p>We use the system role to tell the AI what kind of response we expect. In this case, a detailed code review. Here\u2019s a sample prompt:<\/p>\n<pre>\"You are a senior software engineer and an expert code reviewer. \"\r\n\"When provided with code diffs, you will perform a detailed and structured review. \"\r\n\"Break your feedback into the following sections:\"\r\n\"1. Summary of Code Changes \u2013 Describe in simple terms what the changes are trying to do.\"\r\n\"2. Code Quality Issues \u2013 Point out bugs, code smells, or inefficiencies.\"\r\n\"3. Suggestions for Improvement \u2013 Offer clear, better alternatives (with code snippets) for problematic parts.\"\r\n\"4. Overall Assessment \u2013 Summarize how good or bad the changes are and if they meet clean code standards.\"\r\n\"Be constructive, concise, and professional.\"<\/pre>\n<p>The user message is the actual code diff.<\/p>\n<blockquote><p>Why GPT-3.5 Turbo?<br \/>\nIt\u2019s fast, affordable, and surprisingly good at spotting bugs and recommending better code. If you want even deeper insights, you could upgrade to GPT-4 or GPT-4o \u2014 but for basic reviews, 3.5 is great.<\/p><\/blockquote>\n<h3>5. Post Review Back to the PR<\/h3>\n<p>Finally, we take OpenAI\u2019s response and post it as a comment in the PR using GitHub\u2019s REST API and the <strong>GITHUB_TOKEN<\/strong> secret.<\/p>\n<h2><\/h2>\n<h2>Example: What an AI-Powered Code Review Looks Like<\/h2>\n<p>To see this in action, here\u2019s a real-world example of an automated code review performed by OpenAI.<\/p>\n<p>We\u2019ve created a sample GitHub repository that demonstrates how this setup works: <a href=\"https:\/\/github.com\/kednat87\/ai-code-reviewed-repo\/tree\/dev\">\ud83d\udd17 View the repo on GitHub<\/a><\/p>\n<p>In that repo, we include a deliberately flawed SQL snippet to test how OpenAI responds during a pull request:<\/p>\n<pre>-- No so good SQL Code for Review\r\nSELECT * \r\nFROM customers \r\nWHERE status = 'active'\r\nAND register_date &gt; '2022-01-01'\r\nOR status = 'inactive'\r\nORDER BY customer_name;<\/pre>\n<p>When this code is committed and a PR is raised, OpenAI automatically analyzes the changes and posts a review comment directly in the pull request, like this:<\/p>\n<div id=\"attachment_71817\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-71817\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-71817 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-04-at-9.24.14\u202fPM-1024x781.png\" alt=\"Summary of code changes | TO THE NEW blog\" width=\"625\" height=\"477\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-04-at-9.24.14\u202fPM-1024x781.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-04-at-9.24.14\u202fPM-300x229.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-04-at-9.24.14\u202fPM-768x586.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-04-at-9.24.14\u202fPM-1536x1172.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-04-at-9.24.14\u202fPM-624x476.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-04-at-9.24.14\u202fPM.png 1856w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-71817\" class=\"wp-caption-text\">Summary of code changes<\/p><\/div>\n<p>As shown in the comment above, the AI reviewer provides structured and valuable insights, including:<\/p>\n<ul>\n<li><strong>Clear summary of changes:<\/strong> It recognized the addition of the AI review script, GitHub workflow, and a sample SQL file.<\/li>\n<li><strong>Code quality feedback:<\/strong> It pointed out security gaps, missing error handling, and suggested more descriptive variable names.<\/li>\n<li><strong>Precise analysis of the SQL query: <\/strong>It correctly identified a logical flaw in the SQL query: the condition<\/li>\n<\/ul>\n<pre>WHERE status = 'active' \r\nAND register_date &gt; '2022-01-01' \r\nOR status = 'inactive'<\/pre>\n<p>was flagged due to missing parentheses. The AI understood that this could lead to incorrect filtering, a common mistake in SQL where the precedence of AND and OR isn&#8217;t properly controlled. This shows its ability to reason through syntax and logic in SQL, not just surface-level issues.<\/p>\n<ul>\n<li><strong>Actionable suggestions:<\/strong> From improving variable names to correcting the SQL logic, it offered practical, ready-to-use fixes.<\/li>\n<li><strong>Professional assessment:<\/strong> The AI provided a balanced review, highlighting the innovation while recommending improvements to make the solution more robust.<\/li>\n<\/ul>\n<p>This kind of feedback not only improves the quality of the code but also helps developers learn better practices over time without waiting for human reviewers to step in.<\/p>\n<h3>Tailoring the response as per needs:<\/h3>\n<p>To focus solely on reviewing SQL code, we can simply revise the prompt sent to OpenAI. By tailoring the instructions to limit the review scope to SQL logic and best practices, we ensure that the feedback remains targeted and relevant.<\/p>\n<p>Below is the revised prompt used:<\/p>\n<pre>\"You are a senior SQL expert and code reviewer. \"\r\n\"When given a SQL query, provide a detailed and structured review focusing only on the SQL logic. \"\r\n\"Break your feedback into the following sections:\"\r\n\"1. SQL Issues Identified - List any logic errors, performance bottlenecks, poor formatting, or bad practices.\"\r\n\"2. Suggested Fixes - Provide the corrected SQL query, with improvements for readability, logic correctness, or efficiency.\"\r\n\"3. Brief Explanation - Explain why the original query needed changes and what the new version improves.\"\r\n\"Be concise, clear, and assume the reviewer has working SQL knowledge.\"<\/pre>\n<p>And here is the review comment generated by OpenAI in response:<\/p>\n<div id=\"attachment_71814\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-71814\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-71814 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-11.46.05\u202fAM-1024x626.png\" alt=\"SQL Issues | TO THE NEW Blog\" width=\"625\" height=\"382\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-11.46.05\u202fAM-1024x626.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-11.46.05\u202fAM-300x183.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-11.46.05\u202fAM-768x470.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-11.46.05\u202fAM-1536x939.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-11.46.05\u202fAM-624x381.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/05\/Screenshot-2025-05-05-at-11.46.05\u202fAM.png 1832w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-71814\" class=\"wp-caption-text\">SQL Issues<\/p><\/div>\n<p>This demonstrates how easily we can tailor prompts to fit specific review requirements like SQL, Python, or even documentation.<\/p>\n<h2>Wrapping Up<\/h2>\n<p>This setup doesn\u2019t replace human reviewers, it helps them. It ensures that every PR, no matter how small or rushed, gets a consistent, automated review that adds real value. Reviewers can focus on what matters most. Developers get feedback instantly. Teams reduce errors and build confidence in their codebase.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The State of Code Reviews in Today\u2019s Development Landscape: In today\u2019s fast-moving world of software development, AI has made remarkable progress. It can write code, debug errors, and even help design architectures. But let\u2019s be honest, we\u2019re not quite at a point where AI can take over the entire development process. Human developers are still [&hellip;]<\/p>\n","protected":false},"author":1552,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":349},"categories":[6194],"tags":[4782,1853,7161,7290,503,5919],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/71821"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1552"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=71821"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/71821\/revisions"}],"predecessor-version":[{"id":71843,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/71821\/revisions\/71843"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=71821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=71821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=71821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}