-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
chris48s
merged 8 commits into
badges:master
from
PaulaBarszcz:number-of-commits-between-tags
Sep 16, 2022
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
764e015
1st version of github commit difference is done - but it doesnt work…
PaulaBarszcz 8294058
github commit difference works for branches (also with slashes), tags…
PaulaBarszcz 0311eb4
Merge branch 'master' into number-of-commits-between-tags
PaulaBarszcz a097482
Merge branch 'master' into number-of-commits-between-tags
PaulaBarszcz 45c777b
move branchA and branchB to queryParams
PaulaBarszcz eea9e06
Merge branch 'master' into number-of-commits-between-tags
PaulaBarszcz a8fa356
Merge branch 'master' into number-of-commits-between-tags
PaulaBarszcz 730fd1e
change error msg; rename branchA to base and branchB to head
PaulaBarszcz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
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 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
andbranchB
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 likeHEAD~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
andhead
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"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we use underscores in query params (and not hyphens or camelCase)?
Sure, I'll rename
branchA
tobase
, andbranchB
tohead
.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 thatref
is a great term to encompass all of the possibilities. Thanks;)