Skip to content

Commit

Permalink
feat: autorun reviewbot on each PR (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
nherment authored Nov 8, 2023
1 parent 3d9934c commit 4479865
Show file tree
Hide file tree
Showing 13 changed files with 472 additions and 4,609 deletions.
56 changes: 34 additions & 22 deletions functions/createReview/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import * as dotenv from 'dotenv'
import { createLLMPRComments } from './llm/index.js'
import { createASTPRComments } from './astParsing/index.js'
import { createRegexComments } from './regex/index.js'
import { REVIEW_TYPE, filterOutInvalidComments } from './utils.js'
import getOctokit from './oktokit/index.js'
import { filterOutInvalidComments } from './utils.js'
import pino from 'pino'

const logger = pino({ name: 'reviewbot' })

dotenv.config()
/**
Expand All @@ -12,36 +15,45 @@ dotenv.config()
* @return {void}
*/
export default async function app(message) {
console.log('[reviewbot] - createReview')
logger.info('createReview')

const messageContext = JSON.parse(
Buffer.from(message.data, 'base64').toString()
)

console.log('[reviewbot] - creating suggestions')
let comments = []

const llmComments = await createLLMPRComments(
messageContext.files,
messageContext.diff
)
if (messageContext.reviewType === REVIEW_TYPE.LLM) {
logger.info('Creating LLM review comments')
comments = await createLLMPRComments(
messageContext.files,
messageContext.diff
)
} else if (messageContext.reviewType === REVIEW_TYPE.RuleBased) {
logger.info('Creating rule based review comments')
const astComments = createASTPRComments(
messageContext.files,
messageContext.fullFiles,
messageContext.diff
)

const astComments = createASTPRComments(
messageContext.files,
messageContext.fullFiles,
messageContext.diff
)
const regexpComments = createRegexComments(
messageContext.files,
messageContext.diff
)

const regexpComments = createRegexComments(
messageContext.files,
messageContext.diff
)
comments = astComments.concat(regexpComments)
} else {
logger.warn(
'Received a message but could not determine the type of review requested. Ignoring the message.'
)
return
}

const comments = filterOutInvalidComments(
llmComments.concat(astComments, regexpComments)
)
comments = filterOutInvalidComments(comments)

console.log(
`[reviewbot] - creating review with ${comments.length} comments for commit ${messageContext.latestCommit}`
logger.info(
`creating review with ${comments.length} comments for commit ${messageContext.latestCommit}`
)

const octokit = await getOctokit(messageContext.installationId)
Expand All @@ -56,5 +68,5 @@ export default async function app(message) {
comments: comments
})

console.log('[reviewbot] - review finished')
logger.info('review finished')
}
24 changes: 18 additions & 6 deletions functions/createReview/astParsing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import jsxPlugin from 'acorn-jsx'
import validators from './rules/index.js'
import { parseAndAggregate } from './parser.js'
import { mapLineToDiff } from 'map-line-to-diff'
import pino from 'pino'
import { filterAcceptedFiles, filterOnlyModified } from '../utils.js'

const logger = pino({ name: 'reviewbot' })

function getParser(fileName) {
const fileExtension = path.extname(fileName)
Expand Down Expand Up @@ -38,8 +42,8 @@ export function generateAST(fileContent, fileDiff) {
locations: true
})
} catch (err) {
console.log(
`[reviewbot] - Failed to parse the file content for [${fileDiff.afterName}] into AST. Falling back to the loose parser. Error: ${err.message}`
logger.info(
`Failed to parse the file content for [${fileDiff.afterName}] into AST. Falling back to the loose parser. Error: ${err.message}`
)
ast = LooseParser.parse(fileContent, { locations: true })
}
Expand Down Expand Up @@ -70,20 +74,28 @@ export function parseForIssues(astDocument, gitDiff) {

export function createASTPRComments(gitDiff, filesContents, rawDiff) {
if (!filesContents) {
console.log(
'[reviewbot] - Skipping AST comment generation because raw files contents are missing'
logger.info(
'Skipping AST comment generation because raw files contents are missing'
)
return []
}
const acceptedFiles = filterAcceptedFiles(gitDiff)
const filesWithModifiedLines = filterOnlyModified(acceptedFiles)

let comments = []
for (let fileDiff of gitDiff) {
logger.info(
`Evaluating ${filesWithModifiedLines.length} files for AST rules violations`
)
for (let fileDiff of filesWithModifiedLines) {
const fileContent = filesContents.find(
fileContent => fileContent.filename === fileDiff.afterName
)
if (fileContent) {
const ast = generateAST(fileContent.content, fileDiff)
const fileIssues = parseForIssues(ast, fileDiff)

logger.info(
`Found ${fileIssues.length} violations in file ${fileContent.filename}`
)
for (let issue of fileIssues) {
comments.push({
path: fileContent.filename,
Expand Down
Loading

0 comments on commit 4479865

Please sign in to comment.