Skip to content

Commit

Permalink
Enhance CodeRabbitStats service and tests
Browse files Browse the repository at this point in the history
- Updated the CodeRabbitStats service to include OpenAPI documentation and improved error handling for repository not found scenarios.
- Changed badge label from 'CodeRabbit' to 'CodeRabbit Reviews' for clarity.
- Modified the tester file to reflect the new badge format and error messages, ensuring consistency with the service updates.
- Adjusted regex patterns for message validation in tests.

These changes improve the usability and accuracy of the CodeRabbit statistics service.
  • Loading branch information
aravindputrevu committed Dec 20, 2024
1 parent 32658fc commit 50bbe67
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 48 deletions.
61 changes: 30 additions & 31 deletions services/coderabbit/coderabbit-stats.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Joi from 'joi'
import { BaseJsonService } from '../index.js'
import { BaseJsonService, pathParams } from '../index.js'

const schema = Joi.object({
reviews: Joi.number().required(),
Expand All @@ -12,37 +12,40 @@ class CodeRabbitStats extends BaseJsonService {
pattern: 'stats/:provider/:org/:repo',
}

static examples = [
{
title: 'CodeRabbit Review Stats',
namedParams: {
provider: 'github',
org: 'coderabbitai',
repo: 'ast-grep-essentials',
static openApi = {
'/coderabbit/stats/{provider}/{org}/{repo}': {
get: {
summary: 'CodeRabbit Pull Request Reviews',
description:
'By default, this badge pulls the number of PRs reviewed by [CodeRabbit](https://coderabbit.ai), AI code review tool',
parameters: pathParams(
{
name: 'provider',
example: 'github, gitlab, bitbucket',
description: 'Version Control Provider (e.g., github)',
},
{
name: 'org',
example: 'coderabbitai',
description: 'Organization or User name',
},
{
name: 'repo',
example: 'ast-grep-essentials',
description: 'Repository name',
},
),
},
staticPreview: this.render({
reviews: 101,
}),
documentation: 'Shows the number of CodeRabbit reviews for a repository',
},
]
}

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

static render({ reviews }) {
return {
message: `${reviews} Reviews`,
color: 'ff570a',
}
}

static renderError({ message }) {
return {
message,
color: '9f9f9f',
message: `${reviews}`,
}
}

Expand All @@ -51,18 +54,14 @@ class CodeRabbitStats extends BaseJsonService {
schema,
url: `https://api.coderabbit.ai/stats/${provider}/${org}/${repo}`,
httpErrors: {
404: 'invalid',
404: 'repo not found',
},
})
}

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 })
}
const data = await this.fetch({ provider, org, repo })
return this.constructor.render(data)
}
}

Expand Down
26 changes: 9 additions & 17 deletions services/coderabbit/coderabbit-stats.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ 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',
label: 'CodeRabbit Reviews',
message: /^\d+$/,
})

t.create('CodeRabbitStats valid repo')
Expand All @@ -19,24 +17,20 @@ t.create('CodeRabbitStats valid repo')
.reply(200, { reviews: 101 }),
)
.expectBadge({
label: 'CodeRabbit',
message: '101 Reviews',
color: '#ff570a',
labelColor: '#171717',
label: 'CodeRabbit Reviews',
message: '101',
})

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, 'invalid'),
.reply(404, 'repo not found'),
)
.expectBadge({
label: 'CodeRabbit',
message: 'Not Found: invalid',
color: '#9f9f9f', // Note: without # prefix
labelColor: '#171717', // Note: without # prefix
label: 'CodeRabbit Reviews',
message: 'repo not found',
})

t.create('CodeRabbitStats server error')
Expand All @@ -47,8 +41,6 @@ t.create('CodeRabbitStats server error')
.reply(500, 'Internal Server Error'),
)
.expectBadge({
label: 'CodeRabbit',
message: 'Inaccessible: Got status code 500 (expected 200)', // Match exact error message
color: '#9f9f9f',
labelColor: '#171717',
label: 'CodeRabbit Reviews',
message: 'inaccessible',
})

0 comments on commit 50bbe67

Please sign in to comment.