AI‑Driven Code Review Automation with .NET 8, Azure OpenAI, and GitHub Actions

Mahmut Sarıkaya 5 dk okuma 5 Görüntülenme 0
AI‑Driven Code Review Automation with .NET 8, Azure OpenAI, and GitHub Actions

Why automate code reviews for .NET 8 projects?

Imagine a pull request that contains 500 lines of C# code, a mix of async streams, minimal APIs, and nullable reference types. A human reviewer can spend 30‑45 minutes just to spot style inconsistencies, potential null‑reference bugs, or misuse of new .NET 8 features. According to a 2023 internal Microsoft study, teams that introduced AI‑assisted review cut average review time by 42% while maintaining defect density under 0.8 per 1,000 lines of code.

Automation does not replace expertise; it surfaces the low‑hanging technical debt early, letting senior engineers focus on architectural decisions. The combination of .NET 8, Azure OpenAI, and GitHub Actions provides a scalable, cloud‑native pipeline that runs on every pull request.

Preparing Azure OpenAI for C# analysis

The first step is to provision an Azure OpenAI resource that supports the gpt‑4o‑mini model (or any ChatGPT‑compatible deployment). In the Azure portal, select "Create a resource" → "AI Services" → "Azure OpenAI". Choose a region where compliance is required and note the endpoint URL and key.

Next, add a small C# console application that calls the OpenAI Chat Completion API. The app receives source code via standard input, sends a prompt that asks the model to act as a .NET 8 code reviewer, and prints the feedback. Below is a minimal implementation.

using System; using Azure.AI.OpenAI; var client = new OpenAIClient(new Uri("https://my-openai-resource.openai.azure.com/"), new AzureKeyCredential("YOUR_KEY")); var request = new ChatCompletionsOptions() { DeploymentName = "gpt-4o-mini", Messages = { new ChatMessage(ChatRole.System, "You are a code reviewer for C# .NET 8 projects.") } }; string sourceCode = Console.In.ReadToEnd(); request.Messages.Add(new ChatMessage(ChatRole.User, sourceCode)); var response = await client.GetChatCompletionsAsync(request); Console.WriteLine(response.Value.Choices[0].Message.Content);

Remember to replace the placeholder endpoint and key with the values stored in GitHub secrets. Build the project with dotnet publish -c Release -o ./out so the workflow can invoke the binary quickly.

Connecting the reviewer to GitHub Actions

GitHub Actions provides a native trigger for pull requests. By defining a workflow that checks out the repository, sets up .NET 8, and runs the reviewer binary, you get instant feedback in the PR comments. The following YAML file demonstrates a complete pipeline.

name: Code Review Automation on PR on: pull_request: branches: [ main ] jobs: review: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up .NET uses: actions/setup-dotnet@v3 with: dotnet-version: '8.0.x' - name: Build reviewer project run: dotnet publish ./tools/CodeReviewer/CodeReviewer.csproj - name: Run OpenAI reviewer env: AZURE_OPENAI_ENDPOINT: ${{ secrets.AZURE_OPENAI_ENDPOINT }} AZURE_OPENAI_KEY: ${{ secrets.AZURE_OPENAI_KEY }} run: | dotnet ./tools/CodeReviewer/out/CodeReviewer.dll < $(git diff --cached) 

The workflow uses git diff --cached to capture the staged changes, pipes them to the reviewer, and the console output appears in the Actions log. To surface the feedback directly in the PR, you can add a step that posts a comment via the github-script action, using the output captured from the previous step.

Sample step that posts a comment

Capture the reviewer output into a GitHub Actions variable and then invoke github-script to create a comment on the pull request.

- name: Capture review output id: capture run: | REVIEW=$(dotnet ./tools/CodeReviewer/out/CodeReviewer.dll < $(git diff --cached)) echo "::set-output name=review::$REVIEW" outputs: review: "" - name: Post comment uses: actions/github-script@v6 with: script: | const review = '${{ steps.capture.outputs.review }}'; github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, body: `**AI Code Review**

${review}` });

This pattern keeps the pipeline lightweight while delivering actionable suggestions such as "Consider using the new `await foreach` syntax introduced in .NET 8" or "Nullable reference warnings detected on line 42".

Best practices and tuning tips

1. **Prompt engineering** – The quality of the review depends heavily on the system prompt. Include explicit rules like "Only comment on style, performance, and nullability; ignore naming conventions that follow your team's guidelines." 2. **Rate limiting** – Azure OpenAI enforces token quotas. Cache the model response for identical diffs using a hash of the diff content; this reduces cost for trivial changes. 3. **Security** – Never expose the OpenAI key in logs. Use GitHub secret masking and set maskSecrets: true in the workflow. 4. **Version pinning** – Specify the exact .NET SDK version (e.g., 8.0.200) to avoid breaking changes when the SDK updates. 5. **Feedback loop** – Store reviewer comments in a separate issue label (e.g., "ai‑reviewed") so the team can track adoption metrics over time.

Conclusion

Integrating Azure OpenAI with .NET 8 and GitHub Actions transforms a traditionally manual code‑review bottleneck into an automated, repeatable process. By provisioning a lightweight reviewer service, wiring it into a PR‑triggered workflow, and following the tuning guidelines above, development teams can shave minutes off every review, catch subtle .NET 8 pitfalls early, and keep the focus on high‑level design. The result is faster delivery, higher code quality, and a measurable reduction in review latency.

Sources

Microsoft Docs – Azure OpenAI Service; GitHub Docs – GitHub Actions Workflow Syntax; .NET Blog – New features in .NET 8 (2023).

Author: Mahmut Sarıkaya — sarikayadev.com

Etiketler: #.NET 8 #C# #Azure OpenAI #GitHub Actions #code review automation
Paylaş:
M

Yazar

Mahmut Sarıkaya

yazılım Geliştirici

Yorumlar

Henüz yorum yok. İlk yorumu siz yapın!

Yorum Bırakın

0 + 7 =