Skip to content

Commit

Permalink
Add [Coderabbit] PR Stats service and tests (#10749)
Browse files Browse the repository at this point in the history
* Add Coderabbit PR Stats service and tests

- Implemented a new service `CoderabbitStats` to fetch and display pull request statistics from the CodeRabbit API.
- Created a corresponding tester file to validate the service's functionality, including tests for valid repositories, repository not found, and server errors.
- The service returns a badge with the number of PRs and appropriate error messages based on the API response.

This addition enhances the analysis capabilities of the application by integrating CodeRabbit statistics.

* Refactor CoderabbitStats service to track reviews instead of PRs

- Updated the service to fetch and display the number of reviews from the CodeRabbit API, changing the schema and badge labels accordingly.
- Modified the tester file to reflect the new endpoint and expected responses, including regex for message validation.
- Enhanced error handling in the service to return more descriptive error messages for invalid repositories and server errors.

This change improves the accuracy of the statistics provided by the service, aligning it with the intended functionality of tracking reviews.

* Enhance CodeRabbitStats service and tests

- 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.

* Update services/coderabbit/coderabbit-stats.tester.js

Co-authored-by: jNullj <15849761+jNullj@users.noreply.github.com>

* Addressed codereview comments, changed tests.

* Fix label casing in CodeRabbitStats service and tests

* Update services/coderabbit/coderabbit-stats.service.js

Co-authored-by: chris48s <chris48s@users.noreply.github.com>

* Update CodeRabbitStats service and tests to improve error messaging

- Changed example section in CodeRabbitStats service from 'github, gitlab, bitbucket' to 'github' as per review comment.
- Updated error message for 404 response to 'provider or repo not found', to reflect the right code.

* Added github, gitlab, bitbucket to provider.

* Changing names to match the service name, removing unneeded test.

---------

Co-authored-by: jNullj <15849761+jNullj@users.noreply.github.com>
Co-authored-by: chris48s <chris48s@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 29, 2024
1 parent 7174c5a commit cb30902
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
70 changes: 70 additions & 0 deletions services/coderabbit/coderabbit-pull-request.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import Joi from 'joi'
import { BaseJsonService, pathParams } from '../index.js'

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

class CodeRabbitPullRequest extends BaseJsonService {
static category = 'analysis'
static route = {
base: 'coderabbit',
pattern: 'prs/:provider(github|bitbucket|gitlab)/:org/:repo',
}

static openApi = {
'/coderabbit/prs/{provider}/{org}/{repo}': {
get: {
summary: 'CodeRabbit Pull Request Reviews',
description:
'This badge pulls the number of PRs reviewed by [CodeRabbit](https://coderabbit.ai), AI code review tool',
parameters: pathParams(
{
name: 'provider',
example: 'github',
description: 'Version Control Provider',
schema: { type: 'string', enum: this.getEnum('provider') },
},
{
name: 'org',
example: 'coderabbitai',
description: 'Organization or User name',
},
{
name: 'repo',
example: 'ast-grep-essentials',
description: 'Repository name',
},
),
},
},
}

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

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

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

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

export default CodeRabbitPullRequest
25 changes: 25 additions & 0 deletions services/coderabbit/coderabbit-pull-request.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Joi from 'joi'
import { createServiceTester } from '../tester.js'

export const t = await createServiceTester()

t.create('live CodeRabbitPullRequest')
.get('/prs/github/coderabbitai/ast-grep-essentials.json')
.expectBadge({
label: 'coderabbit reviews',
message: Joi.number().min(0),
})

t.create('live CodeRabbitPullRequest nonexistent org')
.get('/prs/github/not-valid/not-found.json')
.expectBadge({
label: 'coderabbit reviews',
message: 'provider or repo not found',
})

t.create('live CodeRabbitPullRequest invalid repo')
.get('/prs/github/coderabbitai/invalid-repo-name.json')
.expectBadge({
label: 'coderabbit reviews',
message: 'provider or repo not found',
})

0 comments on commit cb30902

Please sign in to comment.