diff --git a/lib/github-provider.js b/lib/github-provider.js index b1a190621dfa7..678a1becb8ce8 100644 --- a/lib/github-provider.js +++ b/lib/github-provider.js @@ -1,11 +1,19 @@ 'use strict'; +const moment = require('moment'); const { handleRequest: cache } = require('./request-handler'); const { makeBadgeData: getBadgeData, makeLabel: getLabel, - getLogo + makeLogo: getLogo, } = require('./badge-data'); +const { + formatDate +} = require('./text-formatters'); + +const { + age +} = require('./color-formatters'); // GitHub commits since integration. function mapGithubCommitsSince(camp, githubApiUrl, githubAuth) { @@ -68,9 +76,48 @@ function mapGithubCommitsSince(camp, githubApiUrl, githubAuth) { })); } +//Github Release Date Integration +function mapGithubReleaseDate(camp, githubApiUrl, githubAuth) { + camp.route(/^\/github\/release-date\/([^\/]+)\/([^\/]+)\.(svg|png|gif|jpg|json)$/, + cache(function (data, match, sendBadge, request) { + const user = match[1]; // eg, microsoft + const repo = match[2]; // eg, vscode + const format = match[3]; + const apiUrl = `${githubApiUrl}/repos/${user}/${repo}/releases/latest`; + const badgeData = getBadgeData('release date', data); + if (badgeData.template === 'social') { + badgeData.logo = getLogo('github', data); + } + githubAuth.request(request, apiUrl, {}, function (err, res, buffer) { + if (err != null) { + badgeData.text[1] = 'inaccessible'; + sendBadge(format, badgeData); + return; + } + //github return 404 if repo not found or no release + if(res.statusCode === 404) { + badgeData.text[1] = 'no releases or repo not found'; + sendBadge(format, badgeData); + return; + } + + try { + const data = JSON.parse(buffer); + const releaseDate = moment(data.created_at); + badgeData.text[1] = formatDate(releaseDate); + badgeData.colorscheme = age(releaseDate); + sendBadge(format, badgeData); + } catch (e) { + badgeData.text[1] = 'invalid'; + sendBadge(format, badgeData); + } + }); + })); +} module.exports = { - mapGithubCommitsSince + mapGithubCommitsSince, + mapGithubReleaseDate }; diff --git a/server.js b/server.js index 8da99a9b18657..e76863f58f4ad 100644 --- a/server.js +++ b/server.js @@ -115,7 +115,8 @@ const { } = require('./lib/github-helpers'); const { - mapGithubCommitsSince + mapGithubCommitsSince, + mapGithubReleaseDate } = require("./lib/github-provider"); var semver = require('semver'); @@ -3323,6 +3324,9 @@ cache(function(data, match, sendBadge, request) { }); })); +// GitHub release date integration. +mapGithubReleaseDate(camp, githubApiUrl, githubAuth); + // GitHub commits since integration. mapGithubCommitsSince(camp, githubApiUrl ,githubAuth); diff --git a/service-tests/github.js b/service-tests/github.js index 1005985b64e49..95100e445eaa4 100644 --- a/service-tests/github.js +++ b/service-tests/github.js @@ -165,6 +165,25 @@ t.create('(pre-)Release') value: Joi.string() })); +t.create('Release Date. e.g release date|today') +.get('/release-date/microsoft/vscode.json') +.expectJSONTypes(Joi.object().keys({ + name: 'release date', + value: validDateString +})); + +t.create('Release Date - Custom Label. e.g myRelease|today') +.get('/release-date/microsoft/vscode.json?label=myRelease') +.expectJSONTypes(Joi.object().keys({ + name: 'myRelease', + value: validDateString +})); + +t.create('Release Date - Should return `no releases or repo not found` for invalid repo') +.get('/release-date/not-valid-name/not-valid-repo.json') +.expectJSON({ name: 'release date', value: 'no releases or repo not found' }); + + t.create('Tag') .get('/tag/photonstorm/phaser.json') .expectJSONTypes(Joi.object().keys({ diff --git a/try.html b/try.html index 92b07a8c06cf3..df1b7db6206ef 100644 --- a/try.html +++ b/try.html @@ -912,6 +912,10 @@

Miscellaneous

https://img.shields.io/github/issues/detail/last-update/badges/shields/979.svg + GitHub Release Date: + + https://img.shields.io/github/release-date/SubtitleEdit/subtitleedit.svg + GitHub pull request check state: https://img.shields.io/github/status/s/pulls/badges/shields/1110.svg