Skip to content
Snippets Groups Projects
Commit ea833a83 authored by Pierre-Yves B's avatar Pierre-Yves B Committed by Paul Melnikow
Browse files

[GitHubCommitActivity] improvements and examples (#2920)

parent 566a8cb4
No related branches found
No related tags found
No related merge requests found
......@@ -10,7 +10,7 @@
<a href="#sponsors" alt="Sponsors on Open Collective">
<img src="https://img.shields.io/opencollective/sponsors/shields.svg" /></a>
<a href="https://github.com/badges/shields/pulse" alt="Activity">
<img src="https://img.shields.io/github/commit-activity/4w/badges/shields.svg" /></a>
<img src="https://img.shields.io/github/commit-activity/m/badges/shields.svg" /></a>
<a href="https://circleci.com/gh/badges/shields/tree/master">
<img src="https://img.shields.io/circleci/project/github/badges/shields/master.svg" alt="build status"></a>
<a href="https://circleci.com/gh/badges/daily-tests">
......
......@@ -30,7 +30,7 @@ module.exports = class GithubCommitActivity extends LegacyService {
static get examples() {
return [
{
title: 'GitHub commit activity the past week, 4 weeks, year',
title: 'GitHub commit activity the past year',
pattern: 'y/:user/:repo',
namedParams: { user: 'eslint', repo: 'eslint' },
staticPreview: {
......@@ -41,12 +41,36 @@ module.exports = class GithubCommitActivity extends LegacyService {
keywords: ['GitHub', 'commit', 'commits', 'activity'],
documentation,
},
{
title: 'GitHub commit activity the past month',
pattern: 'm/:user/:repo',
namedParams: { user: 'eslint', repo: 'eslint' },
staticPreview: {
label: 'commit activity',
message: '38/month',
color: 'blue',
},
keywords: ['GitHub', 'commit', 'commits', 'activity'],
documentation,
},
{
title: 'GitHub commit activity the past week',
pattern: 'w/:user/:repo',
namedParams: { user: 'eslint', repo: 'eslint' },
staticPreview: {
label: 'commit activity',
message: '9/week',
color: 'blue',
},
keywords: ['GitHub', 'commit', 'commits', 'activity'],
documentation,
},
]
}
static registerLegacyRouteHandler({ camp, cache, githubApiProvider }) {
camp.route(
/^\/github\/commit-activity\/(y|4w|w)\/([^/]+)\/([^/]+)\.(svg|png|gif|jpg|json)$/,
/^\/github\/commit-activity\/(y|m|4w|w)\/([^/]+)\/([^/]+)\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const interval = match[1]
const user = match[2]
......@@ -75,11 +99,24 @@ module.exports = class GithubCommitActivity extends LegacyService {
)
intervalLabel = '/year'
break
case 'm':
// To approximate the value for the past month, get the sum for the last
// four weeks and add a weighted value for the fifth week.
const fourWeeksValue = parsedData
.slice(-4)
.reduce((sum, weekInfo) => sum + weekInfo.total, 0)
const fifthWeekValue = parsedData.slice(-5)[0].total
const averageWeeksPerMonth = 365 / 12 / 7
value =
fourWeeksValue +
Math.round((averageWeeksPerMonth - 4) * fifthWeekValue)
intervalLabel = '/month'
break
case '4w':
value = parsedData
.slice(-4)
.reduce((sum, weekInfo) => sum + weekInfo.total, 0)
intervalLabel = '/4 weeks'
intervalLabel = '/four weeks'
break
case 'w':
value = parsedData.slice(-2)[0].total
......
......@@ -14,6 +14,15 @@ t.create('commit activity (1 year)')
})
)
t.create('commit activity (1 month)')
.get('/m/eslint/eslint.json')
.expectJSONTypes(
Joi.object().keys({
name: 'commit activity',
value: isMetricOverTimePeriod,
})
)
t.create('commit activity (4 weeks)')
.get('/4w/eslint/eslint.json')
.expectJSONTypes(
......
......@@ -61,7 +61,7 @@ const isMetric = withRegex(/^[1-9][0-9]*[kMGTPEZY]?$/)
const isMetricOpenIssues = withRegex(/^[1-9][0-9]*[kMGTPEZY]? open$/)
const isMetricOverTimePeriod = withRegex(
/^[1-9][0-9]*[kMGTPEZY]?\/(year|month|4 weeks|week|day)$/
/^[1-9][0-9]*[kMGTPEZY]?\/(year|month|four weeks|week|day)$/
)
const isIntegerPercentage = withRegex(/^[1-9][0-9]?%|^100%|^0%$/)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment