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

total github downloads for all releases + all tags #535

Merged
merged 5 commits into from
Nov 4, 2015
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 8 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ <h3 id="build"> Build </h3>
</tbody></table>
<h3 id="downloads"> Downloads </h3>
<table class='badge'><tbody>
<tr><th data-keywords='github'> Github All Releases: </th>
<td><img src='https://img.shields.io/github/downloads/atom/atom/total.svg' alt=''/></td>
<td><code>https://img.shields.io/github/downloads/atom/atom/total.svg</code></td>
</tr>
<tr><th data-keywords='github'> Github Releases: </th>
<td><img src='https://img.shields.io/github/downloads/atom/atom/latest/total.svg' alt=''/></td>
<td><code>https://img.shields.io/github/downloads/atom/atom/latest/total.svg</code></td>
Expand Down Expand Up @@ -913,6 +917,10 @@ <h2 id="contributors"> Contributors </h2>
<a class='photo' href='https://github.com/PeterDaveHello'>
<img alt='PeterDaveHello' src='https://avatars3.githubusercontent.com/u/3691490?s=80'>
</a>
<br>
<a class='photo' href='https://github.com/sagiegurari'>
<img alt='sagiegurari' src='https://avatars.githubusercontent.com/u/8112599?s=80'>
</a>
<p><small>:wq</small></p>
</main>

Expand Down
49 changes: 38 additions & 11 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2600,15 +2600,24 @@ cache(function(data, match, sendBadge, request) {
}));

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

var tag = match[3]; //null for all releases
var asset_name = match[4].toLowerCase(); // eg. total, atom-amd64.deb, atom.x86_64.rpm
var format = match[5];
var release_path = tag !== 'latest' ? 'tags/' + match[3] : 'latest';
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '/releases/' + release_path;
var total = true;
if (tag) {
total = false;
}

var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '/releases';
if (!total) {
var release_path = tag !== 'latest' ? 'tags/' + tag : 'latest';
Copy link
Member

Choose a reason for hiding this comment

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

I logged the values of tag and release_path, and tag was "/latest", and so release_path was "tags//latest" (which still works, as most servers convert double slashes into single slashes), but not "latest", as the code presumably intends to do.

Either we must tag = tag.slice(1) somewhere, or we must compare tag to things that have a leading slash (ie, tag !== '/latest', and 'tags' + tag).

apiUrl = apiUrl + '/' + release_path;
}
// Using our OAuth App secret grants us 5000 req/hour
// instead of the standard 60 req/hour.
if (serverSecrets) {
Expand All @@ -2632,14 +2641,32 @@ cache(function(data, match, sendBadge, request) {
}
var data = JSON.parse(buffer);
var downloads = 0;
data.assets.forEach(function (asset) {
if (asset_name === 'total' || asset_name === asset.name.toLowerCase()) {
downloads += asset.download_count;

var label;
if (total) {
data.forEach(function (tagData) {
tagData.assets.forEach(function (asset) {
if (asset_name === 'total' || asset_name === asset.name.toLowerCase()) {
downloads += asset.download_count;
}
});
});

label = 'total';
if (asset_name !== 'total') {
label += ' ' + '[' + asset_name + ']';
}
} else {
data.assets.forEach(function (asset) {
if (asset_name === 'total' || asset_name === asset.name.toLowerCase()) {
downloads += asset.download_count;
}
});

label = tag !== 'latest' ? tag : '';
if (asset_name !== 'total') {
label += ' ' + '[' + asset_name + ']';
}
});
var label = tag !== 'latest' ? tag : '';
if (asset_name !== 'total') {
label += ' ' + '[' + asset_name + ']';
}
badgeData.text[1] = metric(downloads) + ' ' + label;
badgeData.colorscheme = 'brightgreen';
Expand Down
8 changes: 8 additions & 0 deletions try.html
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ <h3 id="build"> Build </h3>
</tbody></table>
<h3 id="downloads"> Downloads </h3>
<table class='badge'><tbody>
<tr><th data-keywords='github'> Github All Releases: </th>
<td><img src='https://img.shields.io/github/downloads/atom/atom/total.svg' alt=''/></td>
<td><code>https://img.shields.io/github/downloads/atom/atom/total.svg</code></td>
</tr>
<tr><th data-keywords='github'> Github Releases: </th>
<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>
Expand Down Expand Up @@ -920,6 +924,10 @@ <h2 id="contributors"> Contributors </h2>
<a class='photo' href='https://github.com/PeterDaveHello'>
<img alt='PeterDaveHello' src='https://avatars3.githubusercontent.com/u/3691490?s=80'>
</a>
<br>
<a class='photo' href='https://github.com/sagiegurari'>
<img alt='sagiegurari' src='https://avatars.githubusercontent.com/u/8112599?s=80'>
</a>
<p><small>:wq</small></p>
</main>

Expand Down