From ca58d84447db73c52d3a67f82979e539d29ed2e0 Mon Sep 17 00:00:00 2001 From: Grey Baker Date: Sun, 17 Jun 2018 15:32:57 -0400 Subject: [PATCH] Add [dependabot] SemVer compatibility badge (#1734) * Add [dependabot] SemVer compatibility badge * Add dependabot logo * Clean up and additional tests --- lib/all-badge-examples.js | 4 ++ logo/dependabot.svg | 1 + server.js | 32 +++++++++++++ services/dependabot/dependabot.tester.js | 57 ++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 logo/dependabot.svg create mode 100644 services/dependabot/dependabot.tester.js diff --git a/lib/all-badge-examples.js b/lib/all-badge-examples.js index 6ace79631fb38..997b1ae9f3e24 100644 --- a/lib/all-badge-examples.js +++ b/lib/all-badge-examples.js @@ -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' } ] } diff --git a/logo/dependabot.svg b/logo/dependabot.svg new file mode 100644 index 0000000000000..0d61577cbc8c3 --- /dev/null +++ b/logo/dependabot.svg @@ -0,0 +1 @@ + diff --git a/server.js b/server.js index fa88066453e73..882c1c24ce81c 100644 --- a/server.js +++ b/server.js @@ -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) { diff --git a/services/dependabot/dependabot.tester.js b/services/dependabot/dependabot.tester.js new file mode 100644 index 0000000000000..756fd471bc24f --- /dev/null +++ b/services/dependabot/dependabot.tester.js @@ -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 + }));