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] latest release badge #1122

Merged
merged 12 commits into from
Oct 4, 2017
51 changes: 49 additions & 2 deletions lib/github-provider.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
'use strict';
const moment = require('moment');

const { handleRequest: cache } = require('./request-handler');
const {
makeBadgeData: getBadgeData,
makeLabel: getLabel,
getLogo
makeLogo: getLogo,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry, I made a mistake on previous pull request. I didn't noticed that getLogo is not defined in badge-data. It is alias of makeLogo

} = require('./badge-data');
const {
formatDate
} = require('./text-formatters');

const {
age
} = require('./color-formatters');

// GitHub commits since integration.
function mapGithubCommitsSince(camp, githubApiUrl, githubAuth) {
Expand Down Expand Up @@ -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
};
6 changes: 5 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ const {
} = require('./lib/github-helpers');

const {
mapGithubCommitsSince
mapGithubCommitsSince,
mapGithubReleaseDate
} = require("./lib/github-provider");

var semver = require('semver');
Expand Down Expand Up @@ -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);

Expand Down
19 changes: 19 additions & 0 deletions service-tests/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
4 changes: 4 additions & 0 deletions try.html
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,10 @@ <h3 id="miscellaneous"> Miscellaneous </h3>
<td><img src='/github/issues/detail/last-update/badges/shields/979.svg' alt=''/></td>
<td><code>https://img.shields.io/github/issues/detail/last-update/badges/shields/979.svg</code></td>
</tr>
<tr><th data-doc='githubDoc'data-keywords='GitHub release date'> GitHub Release Date: </th>
<td><img src='/github/release-date/SubtitleEdit/subtitleedit.svg' alt=''/></td>
<td><code>https://img.shields.io/github/release-date/SubtitleEdit/subtitleedit.svg</code></td>
</tr>
<tr><th data-keywords='GitHub pullrequest detail check' data-doc='githubDoc'> GitHub pull request check state: </th>
<td><img src='/github/status/s/pulls/badges/shields/1110.svg' alt=''/></td>
<td><code>https://img.shields.io/github/status/s/pulls/badges/shields/1110.svg</code></td>
Expand Down