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] download count for latest pre-release #1140

Merged
merged 8 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -271,6 +271,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 @@ -285,6 +293,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