Skip to content

Commit

Permalink
ci: ai code review (#60)
Browse files Browse the repository at this point in the history
* ci: ai code review

* ci: fix script path

* ci: fix script ext

* ci: fix relative path

* ci: fix

* ci: fix
  • Loading branch information
jotanarciso authored Oct 23, 2024
1 parent 4a438ab commit 823ea5a
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/code-review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Azion AI Code Review

on:
pull_request:
types: [opened, synchronize]
branches:
- main
- stage

permissions:
contents: read
pull-requests: write
checks: write

jobs:
ai-code-review:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'

- name: Install dependencies
run: |
npm install
npm run compile
npm install @actions/github
- name: Run Azion AI Code Review
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
AZION_TOKEN: ${{ secrets.AZION_TOKEN }}
run: |
node ./scripts/code-review.mjs
continue-on-error: true
id: code_review

- name: Display error logs
if: failure() && steps.code_review.outcome == 'failure'
run: |
echo "Error during code review execution:"
cat ${{ github.workspace }}/*.log
- name: Update PR Status
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
name: 'Azion AI Code Review',
head_sha: context.payload.pull_request.head.sha,
status: 'completed',
conclusion: 'success',
output: {
title: 'Code review completed',
summary: 'The code review was completed successfully.'
}
})
93 changes: 93 additions & 0 deletions scripts/code-review.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import * as github from '@actions/github';
import { chat } from '../packages/ai/dist/index.mjs';

async function runCodeReview() {
const octokit = github.getOctokit(process.env.GITHUB_TOKEN);
const context = github.context;

try {
const { data: pullRequest } = await octokit.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
});

const { data: files } = await octokit.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
});

let fullContent = '';
for (const file of files) {
if (file.status !== 'removed') {
const { data: content } = await octokit.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: file.filename,
ref: pullRequest.head.sha,
});

const fileContent = Buffer.from(content.content, 'base64').toString('utf-8');
fullContent += `File: ${file.filename}\n\n${fileContent}\n\n`;
}
}

const prompt = `Analyze the following pull request and provide a summary of what it implements, including good practices, possible problems, and suggestions for improvement:
${fullContent}
Provide your analysis in Markdown format, starting with a general summary of the pull request.`;

const { data: response, error } = await chat(
{
messages: [{ role: 'user', content: prompt }],
},
{ debug: true },
);

if (response) {
console.log('AI response received');

const logoUrl = 'https://avatars.githubusercontent.com/u/6660972?s=200&v=4';
const logoSize = 14; // Logo size reduced to 14 pixels
const footer = `
<div align="right">
<span style="vertical-align: middle; font-size: 12px; line-height: ${logoSize}px;">
Powered by
<img src="${logoUrl}" alt="Azion Logo" width="${logoSize}" height="${logoSize}" style="vertical-align: middle; margin: 0 2px;">
<a href="https://github.com/aziontech/lib/tree/main/packages/ai" style="vertical-align: middle; text-decoration: none;">Azion AI</a>
</span>
</div>`;

const commentBody = response.choices[0].message.content + '\n\n---\n' + footer;

try {
await octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: commentBody,
});
console.log('Comment successfully created on the pull request');
} catch (commentError) {
console.error('Error creating comment:', commentError.message);
console.error('Error details:', JSON.stringify(commentError, null, 2));
}
} else {
console.error('Code review failed:', error);
}
} catch (error) {
console.error('Error during code review execution:', error.message);
console.error('Error details:', JSON.stringify(error, null, 2));
}
}

(async () => {
try {
await runCodeReview();
} catch (error) {
console.error(error);
process.exit(1);
}
})();

0 comments on commit 823ea5a

Please sign in to comment.