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

Badge For Visual Studio Marketplace added. #1074

Merged
merged 17 commits into from
Sep 26, 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
86 changes: 86 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4865,6 +4865,92 @@ cache(function(data, match, sendBadge, request) {
});
}));


//To generate API request Options for VS Code marketplace
function getVscodeApiReqOptions(package) {
return {
method: 'POST',
url: 'https://marketplace.visualstudio.com/_apis/public/gallery/extensionquery/',
headers:
{
'accept': 'application/json;api-version=3.0-preview.1',
'content-type': 'application/json'
},
body:
{
filters: [{
criteria: [
{ filterType: 7, value: package }]
}],
flags: 914
},
json: true
};
}

//To extract Statistics (Install/Rating/RatingCount) from respose object for vscode marketplace
function getVscodeStatistic(data, statisticName) {
let statistics = data.results[0].extensions[0].statistics;
try {
let statistic = statistics.find(x => x.statisticName.toLowerCase() === statisticName.toLowerCase());
return statistic.value;
} catch (err) {
return 0; //In case required statistic is not found means ZERO.
}
}

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for refactoring this!

//vscode-marketplace download/version/rating integration
camp.route(/^\/vscode-marketplace\/(d|v|r)\/(.*)\.(svg|png|gif|jpg|json)$/,
cache(function (data, match, sendBadge, request) {
let reqType = match[1]; // eg, d/v/r
let repo = match[2]; // eg, `ritwickdey.LiveServer`.
let format = match[3];

let badgeData = getBadgeData('vscode-marketplace', data); //temporary name
let options = getVscodeApiReqOptions(repo);

request(options, function (err, res, buffer) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}

try {
switch (reqType) {
case 'd': {
badgeData.text[0] = getLabel('downloads', data);
let count = getVscodeStatistic(buffer, 'install');
badgeData.text[1] = metric(count);
break;
}
case 'r': {
badgeData.text[0] = getLabel('rating', data);
let rate = getVscodeStatistic(buffer, 'averagerating').toFixed(2);
let totalrate = getVscodeStatistic(buffer, 'ratingcount');
badgeData.text[1] = rate + '/5 (' + totalrate + ')';
break;
}
case 'v': {
badgeData.text[0] = getLabel('visual studio marketplace', data);
badgeData.text[1] = 'v' + buffer.results[0].extensions[0].versions[0].version;
break;
}
}

} catch (e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
return;
}

badgeData.colorscheme = 'brightgreen';
sendBadge(format, badgeData);
});
})
);


camp.route(/^\/dockbit\/([A-Za-z0-9-_]+)\/([A-Za-z0-9-_]+)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
const org = match[1];
Expand Down
49 changes: 49 additions & 0 deletions service-tests/vscode-marketplace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');

const t = new ServiceTester({ id: 'vscode-marketplace', title: 'VS Code Marketplace' });
module.exports = t;

t.create('downloads should be formatted. eg. downloads|72M')
.get('/d/ritwickdey.LiveServer.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('downloads'),
value: Joi.string().regex(/^[0-9]+[K|M]?$/i)
}));

t.create('downloads label should be changed to custom label. eg. Total Installs|72M')
.get('/d/ritwickdey.LiveServer.json?label="Total Installs"')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('Total Installs'),
value: Joi.string().regex(/^[0-9]+[K|M]?$/i)
}));

t.create('rating should be formatted. eg. rating|4.25/5(30)')
.get('/r/ritwickdey.LiveServer.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('rating'),
value: Joi.string().regex(/[0-5].[0-9]{2}\/5?\s*\([0-9]*\)$/)
}));

t.create('rating label should be changed to custom label. eg. My custom rating label|4.25/5(30)')
.get('/r/ritwickdey.LiveServer.json?label="My custom rating label"')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('My custom rating label'),
value: Joi.string().regex(/[0-5].[0-9]{2}\/5?\s*\([0-9]*\)$/)
}));

t.create('version should be formatted. eg. Visual Studio Marketplace|v7.2.5')
.get('/v/ritwickdey.LiveServer.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('visual studio marketplace'),
value: Joi.string().regex(/^v[0-9]*.[0-9]*.[0-9]*$/)
}));

t.create('version label should be changed to custom label. eg. VSM|v7.2.5')
.get('/v/ritwickdey.LiveServer.json?label=VSM')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('VSM'),
value: Joi.string().regex(/^v[0-9]*.[0-9]*.[0-9]*$/)
}));
12 changes: 12 additions & 0 deletions try.html
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ <h3 id="downloads"> Downloads </h3>
<td><img src='/amo/d/dustman.svg' alt=''/></td>
<td><code>https://img.shields.io/amo/d/dustman.svg</code></td>
</tr>
<tr><th data-keywords='vscode-marketplace'>Visual Studio Marketplace: </th>
<td><img src='/vscode-marketplace/d/ritwickdey.LiveServer.svg' alt=''/></td>
<td><code>https://img.shields.io/vscode-marketplace/d/ritwickdey.LiveServer.svg</code></td>
</tr>
</tbody></table>
<h3 id="version"> Version </h3>
<table class='badge'><tbody>
Expand Down Expand Up @@ -630,6 +634,10 @@ <h3 id="version"> Version </h3>
<td><img src='/amo/v/dustman.svg' alt=''/></td>
<td><code>https://img.shields.io/amo/v/dustman.svg</code></td>
</tr>
<tr><th data-keywords='vscode-marketplace'>Visual Studio Marketplace: </th>
<td><img src='/vscode-marketplace/v/ritwickdey.LiveServer.svg' alt=''/></td>
<td><code>https://img.shields.io/vscode-marketplace/v/ritwickdey.LiveServer.svg</code></td>
</tr>
<tr><th> Itunes App Store: </th>
<td><img src='/itunes/v/803453959.svg' alt=''/></td>
<td><code>https://img.shields.io/itunes/v/BUNDLE_ID.svg</code></td>
Expand Down Expand Up @@ -997,6 +1005,10 @@ <h3 id="miscellaneous"> Miscellaneous </h3>
<td><img src='/discord/102860784329052160.svg' alt=''/></td>
<td><code>https://img.shields.io/discord/102860784329052160.svg</code></td>
</tr>
<tr><th data-keywords='vscode-marketplace'>Visual Studio Marketplace: </th>
<td><img src='/vscode-marketplace/r/ritwickdey.LiveServer.svg' alt=''/></td>
<td><code>https://img.shields.io/vscode-marketplace/r/ritwickdey.LiveServer.svg</code></td>
</tr>
</tbody></table>

<h3 id="miscellaneous"> Longer Miscellaneous </h3>
Expand Down