From cb309028db610bae35b97c368c66b1125975d036 Mon Sep 17 00:00:00 2001 From: Aravind Putrevu Date: Sun, 29 Dec 2024 17:35:31 +0100 Subject: [PATCH] Add [Coderabbit] PR Stats service and tests (#10749) * 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 * 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 --- .../coderabbit-pull-request.service.js | 70 +++++++++++++++++++ .../coderabbit-pull-request.tester.js | 25 +++++++ 2 files changed, 95 insertions(+) create mode 100644 services/coderabbit/coderabbit-pull-request.service.js create mode 100644 services/coderabbit/coderabbit-pull-request.tester.js diff --git a/services/coderabbit/coderabbit-pull-request.service.js b/services/coderabbit/coderabbit-pull-request.service.js new file mode 100644 index 0000000000000..1ccea952b90ca --- /dev/null +++ b/services/coderabbit/coderabbit-pull-request.service.js @@ -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 diff --git a/services/coderabbit/coderabbit-pull-request.tester.js b/services/coderabbit/coderabbit-pull-request.tester.js new file mode 100644 index 0000000000000..a7da6f70ce099 --- /dev/null +++ b/services/coderabbit/coderabbit-pull-request.tester.js @@ -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', + })