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 1 commit
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
57 changes: 57 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,57 @@
import Joi from 'joi'
import { BaseJsonService } from '../index.js'

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

// Note: Service name must match file name without '.service.js'
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 PR Stats',
namedParams: {
provider: 'github',
org: 'coderabbit-ai',
repo: 'demo-repository',
},
staticPreview: this.render({
prCount: 100,
}),
},
]

static defaultBadgeData = {
label: 'coderabbit reviews',
}

static render({ prCount }) {
return {
message: `${prCount} PRs`,
color: 'blue',
}
}

async fetch({ provider, org, repo }) {
return this._requestJson({
schema,
url: `https://api.coderabbit.ai/stats/${provider}/${org}/${repo}`,
httpErrors: {
404: 'repository not found',
},
})
}

async handle({ provider, org, repo }) {
const data = await this.fetch({ provider, org, repo })
return this.constructor.render(data)
}
}

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

export const t = await createServiceTester()

t.create('CoderabbitStats valid repo')
.get('/stats/github/coderabbit-ai/demo-repository.json')
.intercept(nock =>
nock('https://api.coderabbit.ai')
.get('/stats/github/coderabbit-ai/demo-repository')
.reply(200, { prCount: 42 }),
)
.expectBadge({
label: 'coderabbit reviews',
message: '42 PRs',
color: 'blue',
})

t.create('CoderabbitStats repo not found')
.get('/stats/github/not-valid/not-found.json')
.intercept(nock =>
nock('https://api.coderabbit.ai')
.get('/stats/github/not-valid/not-found')
.reply(404),
)
.expectBadge({
label: 'coderabbit reviews',
message: 'repository not found',
color: 'red',
})

t.create('CoderabbitStats server error')
.get('/stats/github/coderabbit-ai/error-repo.json')
.intercept(nock =>
nock('https://api.coderabbit.ai')
.get('/stats/github/coderabbit-ai/error-repo')
.reply(500),
)
.expectBadge({
label: 'coderabbit reviews',
message: 'inaccessible',
color: 'lightgrey', // Changed from 'red' to 'lightgrey'
})
Loading