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

Support MyGet and pre-release packages for all nuget kind of feeds #235

Merged
merged 2 commits into from
Aug 28, 2014
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
121 changes: 109 additions & 12 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1281,26 +1281,100 @@ cache(function(data, match, sendBadge) {
});
}));

function getNugetPackage(apiUrl, id, done) {
var filter = 'Id eq \'' + id + '\' and IsLatestVersion eq true';
var reqUrl = apiUrl + '/Packages()?$filter=' + encodeURIComponent(filter);
request(reqUrl, { headers: { 'Accept': 'application/atom+json,application/json' } }, function(err, res, buffer) {
if (err != null) {
done(err);
return;
}

try {
var data = JSON.parse(buffer);
var result = data.d.results[0];
if (result == null) {
// package was not found, might be pre-release only
var filter = 'Id eq \'' + id + '\' and IsAbsoluteLatestVersion eq true';
var reqUrl = apiUrl + '/Packages()?$filter=' + encodeURIComponent(filter);
request(reqUrl, { headers: { 'Accept': 'application/atom+json,application/json' } }, function(err, res, buffer) {
if (err != null) {
done(err);
return;
}

try {
var data = JSON.parse(buffer);
var result = data.d.results[0];
if (result == null) {
done(null, null);
} else {
done (null, result);
}
} catch(e) {
done(e);
}
});
} else {
done(null, result);
}
}
catch (e) {
done(e);
}
});
}

// NuGet/chocolatey version integration.
camp.route(/^\/(nuget|chocolatey)\/v\/(.*)\.(svg|png|gif|jpg)$/,
cache(function(data, match, sendBadge) {
var site = match[1];
var repo = match[2]; // eg, `Nuget.Core`.
var format = match[3];
var filter = 'Id eq \'' + repo + '\' and IsLatestVersion eq true';
var apiUrl = 'https://www.' + site + '.org/api/v2/Packages()?$filter=' + encodeURIComponent(filter);
var apiUrl = 'https://www.' + site + '.org/api/v2';
var badgeData = getBadgeData(site, data);
request(apiUrl, { headers: { 'Accept': 'application/atom+json,application/json' } }, function(err, res, buffer) {
getNugetPackage(apiUrl, repo, function(err, data) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
}
try {
var data = JSON.parse(buffer);
var result = data.d.results[0];
var version = result.NormalizedVersion || result.Version;
var version = data.NormalizedVersion || data.Version;
badgeData.text[1] = 'v' + version;
if (version[0] === '0') {
if (version.indexOf('-') !== -1) {
badgeData.colorscheme = 'yellow';
} else if (version[0] === '0') {
badgeData.colorscheme = 'orange';
} else {
badgeData.colorscheme = 'blue';
}
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));

// MyGet version integration.
camp.route(/^\/myget\/(.*)\/v\/(.*)\.(svg|png|gif|jpg)$/,
cache(function(data, match, sendBadge) {
var feed = match[1]; // eg yolodev
var repo = match[2]; // eg FSharpSupport
var format = match[3]; // eg svg or png
var apiUrl = 'https://www.myget.org/F/' + feed + '/api/v2';
var badgeData = getBadgeData(feed, data);
getNugetPackage(apiUrl, repo, function(err, data) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
}
try {
var version = data.NormalizedVersion || data.Version;
badgeData.text[1] = 'v' + version;
if (version.indexOf('-') !== -1) {
badgeData.colorscheme = 'yellow';
} else if (version[0] === '0') {
badgeData.colorscheme = 'orange';
} else {
badgeData.colorscheme = 'blue';
Expand All @@ -1319,17 +1393,40 @@ cache(function(data, match, sendBadge) {
var site = match[1];
var repo = match[2]; // eg, `Nuget.Core`.
var format = match[3];
var filter = 'Id eq \'' + repo + '\' and IsLatestVersion eq true';
var apiUrl = 'https://www.' + site + '.org/api/v2/Packages()?$filter=' + encodeURIComponent(filter);
var apiUrl = 'https://www.' + site + '.org/api/v2';
var badgeData = getBadgeData('downloads', data);
request(apiUrl, { headers: { 'Accept': 'application/atom+json,application/json' } }, function(err, res, buffer) {
getNugetPackage(apiUrl, repo, function(err, data) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
}
try {
var data = JSON.parse(buffer);
var downloads = data.d.results[0].DownloadCount;
var downloads = data.DownloadCount;
badgeData.text[1] = metric(downloads) + ' total';
badgeData.colorscheme = downloadCountColor(downloads);
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));

// MyGet download count integration.
camp.route(/^\/myget\/(.*)\/dt\/(.*)\.(svg|png|gif|jpg)$/,
cache(function(data, match, sendBadge) {
var feed = match[1]; // eg yolodev
var repo = match[2]; // eg FSharpSupport
var format = match[3]; // eg svg or png
var apiUrl = 'https://www.myget.org/F/' + feed + '/api/v2';
var badgeData = getBadgeData('downloads', data);
getNugetPackage(apiUrl, repo, function(err, data) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
}
try {
var downloads = data.DownloadCount;
badgeData.text[1] = metric(downloads) + ' total';
badgeData.colorscheme = downloadCountColor(downloads);
sendBadge(format, badgeData);
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> Downloads </h3>
<td><img src='/nuget/dt/Microsoft.AspNet.Mvc.svg' alt=''/></td>
<td><code>http://img.shields.io/nuget/dt/Microsoft.AspNet.Mvc.svg</code></td>
</tr>
<tr><th> MyGet: </th>
<td><img src='/myget/yolodev/dt/FSharpSupport.svg' alt=''/></td>
<td><code>http://img.shields.io/myget/yolodev/dt/FSharpSupport.svg</code></td>
</tr>
<tr><th> Chocolatey: </th>
<td><img src='/chocolatey/dt/scriptcs.svg' alt=''/></td>
<td><code>http://img.shields.io/chocolatey/dt/scriptcs.svg</code></td>
Expand Down Expand Up @@ -281,6 +285,10 @@ <h3> Version </h3>
<td><img src='/nuget/v/Nuget.Core.svg' alt=''/></td>
<td><code>http://img.shields.io/nuget/v/Nuget.Core.svg</code></td>
</tr>
<tr><th> MyGet: </th>
<td><img src='/myget/yolodev/v/FSharpSupport.svg' alt=''/></td>
<td><code>http://img.shields.io/myget/yolodev/v/FSharpSupport.svg</code></td>
</tr>
<tr><th> Chocolatey: </th>
<td><img src='/chocolatey/v/git.svg' alt=''/></td>
<td><code>http://img.shields.io/chocolatey/v/git.svg</code></td>
Expand Down