Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add [Coderabbit] PR Stats service and tests #10749

Merged
merged 14 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions services/coderabbit/coderabbit-stats.service.js
jNullj marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Joi from 'joi'
import { BaseJsonService } from '../index.js'

const schema = Joi.object({
reviews: Joi.number().required(),
}).required()

class CodeRabbitStats extends BaseJsonService {
static category = 'analysis'
static route = {
base: 'coderabbit',
pattern: 'stats/:provider/:org/:repo',
chris48s marked this conversation as resolved.
Show resolved Hide resolved
}

static examples = [
chris48s marked this conversation as resolved.
Show resolved Hide resolved
{
title: 'CodeRabbit Review Stats',
namedParams: {
provider: 'github',
org: 'coderabbitai',
repo: 'ast-grep-essentials',
},
staticPreview: this.render({
reviews: 101,
}),
documentation: 'Shows the number of CodeRabbit reviews for a repository',
},
]

static defaultBadgeData = {
label: 'CodeRabbit',
labelColor: '171717',
}

static render({ reviews }) {
return {
message: `${reviews} Reviews`,
chris48s marked this conversation as resolved.
Show resolved Hide resolved
color: 'ff570a',
}
}

static renderError({ message }) {
return {
message,
color: '9f9f9f',
chris48s marked this conversation as resolved.
Show resolved Hide resolved
}
}

async fetch({ provider, org, repo }) {
return this._requestJson({
schema,
url: `https://api.coderabbit.ai/stats/${provider}/${org}/${repo}`,
httpErrors: {
404: 'invalid',
chris48s marked this conversation as resolved.
Show resolved Hide resolved
},
})
}

async handle({ provider, org, repo }) {
try {
const data = await this.fetch({ provider, org, repo })
return this.constructor.render(data)
} catch (error) {
return this.constructor.renderError({ message: error.message })
}
chris48s marked this conversation as resolved.
Show resolved Hide resolved
}
}

export default CodeRabbitStats
54 changes: 54 additions & 0 deletions services/coderabbit/coderabbit-stats.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { createServiceTester } from '../tester.js'

export const t = await createServiceTester()

t.create('live CodeRabbitStats')
.get('/stats/github/coderabbitai/ast-grep-essentials.json')
.expectBadge({
label: 'CodeRabbit',
message: /^\d+ Reviews$/, // Using regex pattern instead of isMetric
color: '#ff570a',
labelColor: '#171717',
})

t.create('CodeRabbitStats valid repo')
.get('/stats/github/coderabbitai/ast-grep-essentials.json')
.intercept(nock =>
nock('https://api.coderabbit.ai')
.get('/stats/github/coderabbitai/ast-grep-essentials')
.reply(200, { reviews: 101 }),
)
.expectBadge({
label: 'CodeRabbit',
message: '101 Reviews',
color: '#ff570a',
labelColor: '#171717',
})

t.create('CodeRabbitStats repo not found')
chris48s marked this conversation as resolved.
Show resolved Hide resolved
.get('/stats/github/not-valid/not-found.json')
.intercept(nock =>
nock('https://api.coderabbit.ai')
.get('/stats/github/not-valid/not-found')
.reply(404, 'invalid'),
)
.expectBadge({
label: 'CodeRabbit',
message: 'Not Found: invalid',
color: '#9f9f9f', // Note: without # prefix
labelColor: '#171717', // Note: without # prefix
})

t.create('CodeRabbitStats server error')
.get('/stats/github/coderabbitai/error-repo.json')
.intercept(nock =>
nock('https://api.coderabbit.ai')
.get('/stats/github/coderabbitai/error-repo')
.reply(500, 'Internal Server Error'),
)
.expectBadge({
label: 'CodeRabbit',
message: 'Inaccessible: Got status code 500 (expected 200)', // Match exact error message
color: '#9f9f9f',
labelColor: '#171717',
})
Loading