From ea833a83c4f4ac82619f58f186a315e12a87d67c Mon Sep 17 00:00:00 2001 From: Pierre-Yves B <PyvesDev@gmail.com> Date: Tue, 5 Feb 2019 01:27:52 +0000 Subject: [PATCH] [GitHubCommitActivity] improvements and examples (#2920) --- README.md | 2 +- .../github/github-commit-activity.service.js | 43 +++++++++++++++++-- .../github/github-commit-activity.tester.js | 9 ++++ services/test-validators.js | 2 +- 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fb5e7189bf..dd4c97e2e3 100644 --- a/README.md +++ b/README.md @@ -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"> diff --git a/services/github/github-commit-activity.service.js b/services/github/github-commit-activity.service.js index 6eab06e51a..36a2c150a5 100644 --- a/services/github/github-commit-activity.service.js +++ b/services/github/github-commit-activity.service.js @@ -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 diff --git a/services/github/github-commit-activity.tester.js b/services/github/github-commit-activity.tester.js index 68e3543e65..105912d7b7 100644 --- a/services/github/github-commit-activity.tester.js +++ b/services/github/github-commit-activity.tester.js @@ -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( diff --git a/services/test-validators.js b/services/test-validators.js index 394920c497..590eb9cf20 100644 --- a/services/test-validators.js +++ b/services/test-validators.js @@ -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%$/) -- GitLab