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
60 changes: 60 additions & 0 deletions services/github/github-commits-difference.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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({
branchA: Joi.string().required(),
branchB: 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: {
branchA: '1.60.0',
branchB: '82f2db7',
Copy link
Member

@chris48s chris48s Sep 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As is often the case, the hardest job is giving things good names :)

As demonstrated by the fact that the 2 examples you've used here are a commit hash and a tag, branchA and branchB don't actually have to be branches. Fortunately in git there is a word for this identifier that could be a branch name, commit hash or tag name, which is "ref" (you'll also notice you can use the "special" ref names just fine here like HEAD~1, etc if you try them).

The other thing here is that A and B aren't very descriptive either. It isn't really clear to me which one of this is expected to be the ref that is further ahead/behind. The Github API calls these params {base}...{head} which isn't going to be completely obvious to everyone, but I think those terms are more descriptive than A and B and at least have the advantage of maintaining consistency with the terms GitHub uses. Another option is GitLab uses {from}...{to} for these concepts..

So we could go with

  • base / head
  • base_ref / head_ref
  • from / to
  • from_ref / to_ref

(we use underscores in query params)

Personally, I have a weak preference for base and head just because they're the same words github uses in the API docs, but if you have a strong preference that one of the other 3 options there is more meaningful we can go with any of those. And lets make the error message "could not establish commit difference between refs"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I have a weak preference for base and head just because they're the same words github uses in the API docs, but if you have a strong preference that one of the other 3 options there is more meaningful we can go with any of those. And lets make the error message "could not establish commit difference between refs"

Why do we use underscores in query params (and not hyphens or camelCase)?

Sure, I'll rename branchA to base, and branchB to head.

The error message will read "could not establish commit difference between refs".

Yeah, I was also not fond of putting branch in the URL (since users can use a couple of other options) - you're right that ref is a great term to encompass all of the possibilities. Thanks;)

},
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 }, { branchA, branchB }) {
const notFoundMessage =
'could not establish commit difference between branches/tags/commits'
const { total_commits: commitCount } = await this._requestJson({
schema,
url: `/repos/${user}/${repo}/compare/${branchA}...${branchB}`,
errorMessages: errorMessagesFor(notFoundMessage),
})

return this.constructor.render({ commitCount })
}
}
44 changes: 44 additions & 0 deletions services/github/github-commits-difference.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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?branchA=standalone/0.1.x&branchB=release/1.21')
.expectBadge({
label: 'commits difference',
message: isMetric,
color: 'blue',
})

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

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

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

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