Skip to content

Commit

Permalink
[Github] download count for latest pre-release (#1140)
Browse files Browse the repository at this point in the history
Implements #1113.

Removed extra space in 2 cases (failing tests without this change):

  1) Github downloads for latest release 
	[ GET http://localhost:1111/github/downloads/photonstorm/phaser/latest/total.json ]:
     ValidationError: child "value" fails because ["value" with value "63k " fails to match the required pattern: /^[0-9]+[kMGTPEZY]?$/]
--
  2) Github downloads-pre for latest release 
	[ GET http://localhost:1111/github/downloads-pre/photonstorm/phaser/latest/total.json ]:
     ValidationError: child "value" fails because ["value" with value "34 " fails to match the required pattern: /^[0-9]+[kMGTPEZY]?$/]
--
  3) Github downloads for specific asset from latest release 
	[ GET http://localhost:1111/github/downloads/atom/atom/latest/atom-amd64.deb.json ]:
     ValidationError: child "value" fails because ["value" with value "3k  [atom-amd64.deb]" fails to match the required pattern: /^[0-9]+[kMGTPEZY]? \[atom-amd64\.deb\]$/]
--
  4) Github downloads-pre for specific asset from latest release 
	[ GET http://localhost:1111/github/downloads-pre/atom/atom/latest/atom-amd64.deb.json ]:
     ValidationError: child "value" fails because ["value" with value "372  [atom-amd64.deb]" fails to match the required pattern: /^[0-9]+[kMGTPEZY]? \[atom-amd64\.deb\]$/]
  • Loading branch information
platan authored and paulmelnikow committed Oct 9, 2017
1 parent c579ed7 commit 848422c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
38 changes: 23 additions & 15 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3330,15 +3330,16 @@ mapGithubReleaseDate(camp, githubApiUrl, githubAuth);
// GitHub commits since integration.
mapGithubCommitsSince(camp, githubApiUrl ,githubAuth);

// GitHub release-download-count integration.
camp.route(/^\/github\/downloads\/([^\/]+)\/([^\/]+)(\/.+)?\/([^\/]+)\.(svg|png|gif|jpg|json)$/,
// GitHub release-download-count and pre-release-download-count integration.
camp.route(/^\/github\/(downloads|downloads-pre)\/([^\/]+)\/([^\/]+)(\/.+)?\/([^\/]+)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var user = match[1]; // eg, qubyte/rubidium
var repo = match[2];
const type = match[1]; // downloads or downloads-pre
var user = match[2]; // eg, qubyte/rubidium
var repo = match[3];

var tag = match[3]; // eg, v0.190.0, latest, null if querying all releases
var asset_name = match[4].toLowerCase(); // eg. total, atom-amd64.deb, atom.x86_64.rpm
var format = match[5];
var tag = match[4]; // eg, v0.190.0, latest, null if querying all releases
var asset_name = match[5].toLowerCase(); // eg. total, atom-amd64.deb, atom.x86_64.rpm
var format = match[6];

if (tag) { tag = tag.slice(1); }

Expand All @@ -3349,8 +3350,10 @@ cache(function(data, match, sendBadge, request) {

var apiUrl = githubApiUrl + '/repos/' + user + '/' + repo + '/releases';
if (!total) {
var release_path = tag !== 'latest' ? 'tags/' + tag : 'latest';
apiUrl = apiUrl + '/' + release_path;
var release_path = tag === 'latest' ? (type === 'downloads' ? 'latest' : '') : 'tags/' + tag;
if (release_path) {
apiUrl = apiUrl + '/' + release_path;
}
}
var badgeData = getBadgeData('downloads', data);
if (badgeData.template === 'social') {
Expand All @@ -3363,9 +3366,12 @@ cache(function(data, match, sendBadge, request) {
}
try {
var data = JSON.parse(buffer);
if (type === 'downloads-pre' && tag === 'latest') {
data = data[0];
}
var downloads = 0;

var label;
const labelWords = [metric(downloads)];
if (total) {
data.forEach(function (tagData) {
tagData.assets.forEach(function (asset) {
Expand All @@ -3375,9 +3381,9 @@ cache(function(data, match, sendBadge, request) {
});
});

label = 'total';
labelWords.push('total');
if (asset_name !== 'total') {
label += ' ' + '[' + asset_name + ']';
labelWords.push(`[${asset_name}]`);
}
} else {
data.assets.forEach(function (asset) {
Expand All @@ -3386,12 +3392,14 @@ cache(function(data, match, sendBadge, request) {
}
});

label = tag !== 'latest' ? tag : '';
if (tag !== 'latest') {
labelWords.push(tag);
}
if (asset_name !== 'total') {
label += ' ' + '[' + asset_name + ']';
labelWords.push(`[${asset_name}]`);
}
}
badgeData.text[1] = metric(downloads) + ' ' + label;
badgeData.text[1] = labelWords.join(' ');
badgeData.colorscheme = 'brightgreen';
sendBadge(format, badgeData);
} catch(e) {
Expand Down
22 changes: 22 additions & 0 deletions service-tests/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,14 @@ t.create('Downloads all releases')
value: Joi.string().regex(/^\w+\s+total$/)
}));

t.create('downloads for latest release')
.get('/downloads/photonstorm/phaser/latest/total.json')
.expectJSONTypes(Joi.object().keys({ name: 'downloads', value: isMetric }));

t.create('downloads-pre for latest release')
.get('/downloads-pre/photonstorm/phaser/latest/total.json')
.expectJSONTypes(Joi.object().keys({ name: 'downloads', value: isMetric }));

t.create('downloads for release without slash')
.get('/downloads/atom/atom/v0.190.0/total.json')
.expectJSONTypes(Joi.object().keys({
Expand All @@ -293,6 +301,20 @@ t.create('downloads for specific asset without slash')
value: Joi.string().regex(/^[0-9]+[kMGTPEZY]? v0\.190\.0 \[atom-amd64\.deb\]$/)
}));

t.create('downloads for specific asset from latest release')
.get('/downloads/atom/atom/latest/atom-amd64.deb.json')
.expectJSONTypes(Joi.object().keys({
name: 'downloads',
value: Joi.string().regex(/^[0-9]+[kMGTPEZY]? \[atom-amd64\.deb\]$/)
}));

t.create('downloads-pre for specific asset from latest release')
.get('/downloads-pre/atom/atom/latest/atom-amd64.deb.json')
.expectJSONTypes(Joi.object().keys({
name: 'downloads',
value: Joi.string().regex(/^[0-9]+[kMGTPEZY]? \[atom-amd64\.deb\]$/)
}));

t.create('downloads for release with slash')
.get('/downloads/NHellFire/dban/stable/v2.2.8/total.json')
.expectJSONTypes(Joi.object().keys({
Expand Down
10 changes: 10 additions & 0 deletions try.html
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ <h3 id="downloads"> Downloads </h3>
<td><img src='/github/downloads/atom/atom/latest/total.svg' alt=''/></td>
<td><code>https://img.shields.io/github/downloads/atom/atom/latest/total.svg</code></td>
</tr>
<tr>
<th data-keywords='github' data-doc='githubDoc'> Github Pre-Releases: </th>
<td><img src='/github/downloads-pre/atom/atom/latest/total.svg' alt=''/></td>
<td><code>https://img.shields.io/github/downloads-pre/atom/atom/latest/total.svg</code></td>
</tr>
<tr><th data-keywords='github' data-doc='githubDoc'> Github Releases (by Release): </th>
<td><img src='/github/downloads/atom/atom/v0.190.0/total.svg' alt=''/></td>
<td><code>https://img.shields.io/github/downloads/atom/atom/v0.190.0/total.svg</code></td>
Expand All @@ -267,6 +272,11 @@ <h3 id="downloads"> Downloads </h3>
<td><img src='/github/downloads/atom/atom/latest/atom-amd64.deb.svg' alt=''/></td>
<td><code>https://img.shields.io/github/downloads/atom/atom/latest/atom-amd64.deb.svg</code></td>
</tr>
<tr>
<th data-keywords='github' data-doc='githubDoc'> Github Pre-Releases (by Asset): </th>
<td><img src='/github/downloads-pre/atom/atom/latest/atom-amd64.deb.svg' alt=''/></td>
<td><code>https://img.shields.io/github/downloads-pre/atom/atom/latest/atom-amd64.deb.svg</code></td>
</tr>
<tr><th data-keywords='node'> npm: </th>
<td><img src='/npm/dw/localeval.svg' alt=''/></td>
<td><code>https://img.shields.io/npm/dw/localeval.svg</code></td>
Expand Down

0 comments on commit 848422c

Please sign in to comment.