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', + })