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

[GitHub] Number of commits between branches/tags/commits #8394

Merged
merged 8 commits into from
Sep 16, 2022
59 changes: 59 additions & 0 deletions services/github/github-commits-difference.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { nonNegativeInteger } from '../validators.js'
import { GithubAuthV3Service } from './github-auth-service.js'
import { documentation, errorMessagesFor } from './github-helpers.js'

const schema = Joi.object({ total_commits: nonNegativeInteger }).required()

const queryParamSchema = Joi.object({
base: Joi.string().required(),
head: Joi.string().required(),
}).required()

export default class GithubCommitsDifference extends GithubAuthV3Service {
static category = 'activity'
static route = {
base: 'github/commits-difference',
pattern: ':user/:repo',
queryParamSchema,
}

static examples = [
{
title: 'GitHub commits difference between two branches/tags/commits',
namedParams: {
user: 'microsoft',
repo: 'vscode',
},
queryParams: {
base: '1.60.0',
head: '82f2db7',
},
staticPreview: this.render({
commitCount: 9227,
}),
documentation,
},
]

static defaultBadgeData = { label: 'commits difference', namedLogo: 'github' }

static render({ commitCount }) {
return {
message: metric(commitCount),
color: 'blue',
}
}

async handle({ user, repo }, { base, head }) {
const notFoundMessage = 'could not establish commit difference between refs'
const { total_commits: commitCount } = await this._requestJson({
schema,
url: `/repos/${user}/${repo}/compare/${base}...${head}`,
errorMessages: errorMessagesFor(notFoundMessage),
})

return this.constructor.render({ commitCount })
}
}
43 changes: 43 additions & 0 deletions services/github/github-commits-difference.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { isMetric } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('Commits difference - correct, between branches')
.get('/microsoft/vscode.json?base=standalone/0.1.x&head=release/1.21')
.expectBadge({
label: 'commits difference',
message: isMetric,
color: 'blue',
})

t.create('Commits difference - correct, between tags')
.get('/microsoft/vscode.json?base=1.58.0&head=1.59.0')
.expectBadge({
label: 'commits difference',
message: isMetric,
color: 'blue',
})

t.create('Commits difference - correct, between commits')
.get('/microsoft/vscode.json?base=3d82ef7&head=82f2db7')
.expectBadge({
label: 'commits difference',
message: isMetric,
color: 'blue',
})

t.create('Commits difference - incorrect, between commits')
.get('/microsoft/vscode.json?base=fffffff&head=82f2db7')
.expectBadge({
label: 'commits difference',
message: 'could not establish commit difference between refs',
color: 'red',
})

t.create('Commits difference - incorrect, missing head')
.get('/microsoft/vscode.json?base=fffffff')
.expectBadge({
label: 'commits difference',
message: 'invalid query parameter: head',
color: 'red',
})