-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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 Releases Download counter #404
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1966,6 +1966,50 @@ cache(function(data, match, sendBadge, request) { | |
}); | ||
})); | ||
|
||
// GitHub release-download-count integration. | ||
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 id = match[3]; | ||
var asset_name = match[4].toLowerCase(); | ||
var format = match[5]; | ||
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '/releases/' + id; | ||
// Using our OAuth App secret grants us 5000 req/hour | ||
// instead of the standard 60 req/hour. | ||
if (serverSecrets) { | ||
apiUrl += '?client_id=' + serverSecrets.gh_client_id | ||
+ '&client_secret=' + serverSecrets.gh_client_secret; | ||
} | ||
var badgeData = getBadgeData('downloads', data); | ||
// A special User-Agent is required: | ||
// http://developer.github.com/v3/#user-agent-required | ||
request(apiUrl, { headers: githubHeaders }, function(err, res, buffer) { | ||
if (err != null) { | ||
badgeData.text[1] = 'inaccessible'; | ||
sendBadge(format, badgeData); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you return there? |
||
} | ||
try { | ||
if ((+res.headers['x-ratelimit-remaining']) === 0) { | ||
return; // Hope for the best in the cache. | ||
} | ||
var data = JSON.parse(buffer); | ||
var downloads = 0; | ||
data.assets.map(function (asset) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I usually default to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That microbenchmark describes If you'd like to make a change for performance reasons, I would suggest using a for loop with a cached length, as that is known to be the easiest to optimize for JS engines. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, you are right, clearly further reading is required :) |
||
if (asset_name === 'total' || asset_name === asset.name.toLowerCase()) { | ||
downloads += asset.download_count | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a semicolon |
||
} | ||
}); | ||
badgeData.text[1] = metric(downloads) + (asset_name? ' ' + asset_name: ' total'); | ||
badgeData.colorscheme = 'brightgreen'; | ||
sendBadge(format, badgeData); | ||
} catch(e) { | ||
badgeData.text[1] = 'none'; | ||
sendBadge(format, badgeData); | ||
} | ||
}); | ||
})); | ||
|
||
// GitHub issues integration. | ||
camp.route(/^\/github\/issues(-raw)?\/([^\/]+)\/([^\/]+)\.(svg|png|gif|jpg|json)$/, | ||
cache(function(data, match, sendBadge, request) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give an example of asset name apart from
total
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm adding the comment inline with examples, any other suggestions for surfacing the examples of this value to visitors on the web page?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding an example badge with a specific asset would be great!