Skip to content

Commit

Permalink
Add [dependabot] SemVer compatibility badge (#1734)
Browse files Browse the repository at this point in the history
* Add [dependabot] SemVer compatibility badge

* Add dependabot logo

* Clean up and additional tests
  • Loading branch information
greysteil authored and platan committed Jun 17, 2018
1 parent 028d8d7 commit ca58d84
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/all-badge-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,10 @@ const allBadgeExamples = [
{
title: 'Sourcegraph for Repo Reference Count',
previewUri: '/sourcegraph/rrc/github.com/gorilla/mux.svg'
},
{
title: 'SemVer Compatibility',
previewUri: '/dependabot/semver/bundler/puma.svg'
}
]
}
Expand Down
1 change: 1 addition & 0 deletions logo/dependabot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7889,6 +7889,38 @@ cache(function (data, match, sendBadge, request) {
});
}));

// Dependabot SemVer compatibility integration
camp.route(/^\/dependabot\/semver\/([^/]+)\/(.+)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
const packageManager = match[1];
const dependencyName = match[2];
const format = match[3];
const options = {
method: 'GET',
headers: { 'Accept': 'application/json' },
uri: `https://api.dependabot.com/badges/compatibility_score?package-manager=${packageManager}&dependency-name=${dependencyName}&version-scheme=semver`
};
const badgeData = getBadgeData('semver compatibility', data);
badgeData.links = [`https://dependabot.com/compatibility-score.html?package-manager=${packageManager}&dependency-name=${dependencyName}&version-scheme=semver`];
badgeData.logo = getLogo('dependabot', data);
request(options, function(err, res) {
if (checkErrorResponse(badgeData, err, res)) {
sendBadge(format, badgeData);
return;
}
try {
const dependabotData = JSON.parse(res['body']);
badgeData.text[1] = dependabotData.status;
badgeData.colorscheme = dependabotData.colour;
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
badgeData.colorscheme = 'red';
sendBadge(format, badgeData);
}
});
}));

// Any badge.
camp.route(/^\/(:|badge\/)(([^-]|--)*?)-(([^-]|--)*)-(([^-]|--)+)\.(svg|png|gif|jpg)$/,
function(data, match, end, ask) {
Expand Down
57 changes: 57 additions & 0 deletions services/dependabot/dependabot.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

const Joi = require('joi');
const ServiceTester = require('../service-tester');
const { isIntegerPercentage } = require('../test-validators');
const { invalidJSON } = require('../response-fixtures');
const colorscheme = require('../../lib/colorscheme.json');
const mapValues = require('lodash.mapvalues');
const colorsB = mapValues(colorscheme, 'colorB');

const t = new ServiceTester({ id: 'dependabot', title: 'Dependabot' });
module.exports = t;

t.create('semver compatibility (valid)')
.get('/semver/bundler/puma.json?style=_shields_test')
.expectJSONTypes(Joi.object().keys({
name: 'semver compatibility',
value: isIntegerPercentage,
link: 'https://dependabot.com/compatibility-score.html?dependency-name=puma&package-manager=bundler&version-scheme=semver',
colorB: Joi.equal(colorsB.brightgreen, colorsB.orange).required()
}));

t.create('semver compatibility (connection error)')
.get('/semver/bundler/puma.json?style=_shields_test')
.networkOff()
.expectJSONTypes(Joi.object().keys({
name: 'semver compatibility',
value: 'inaccessible',
colorB: colorsB.red
}));

t.create('semver compatibility (invalid error)')
.get('/semver/invalid-manager/puma.json?style=_shields_test')
.expectJSONTypes(Joi.object().keys({
name: 'semver compatibility',
value: 'invalid',
colorB: colorsB.lightgrey
}));

t.create('semver compatibility (invalid JSON response)')
.get('/semver/bundler/puma.json')
.intercept(nock => nock('https://api.dependabot.com')
.get('/badges/compatibility_score?package-manager=bundler&dependency-name=puma&version-scheme=semver')
.reply(invalidJSON)
)
.expectJSONTypes(Joi.object().keys({
name: 'semver compatibility',
value: 'invalid'
}));

t.create('semver compatibility (missing dependency)')
.get('/semver/bundler/some-random-missing-dependency.json?style=_shields_test')
.expectJSONTypes(Joi.object().keys({
name: 'semver compatibility',
value: 'unknown',
colorB: colorsB.lightgrey
}));

0 comments on commit ca58d84

Please sign in to comment.