Files
shields/services/dependabot/dependabot.service.js
T
Paul MelnikowandGitHub 02ec19fd22 BaseService terminology: Rename url to route (#2278)
The term “url” is overloaded in services, to refer to the Shields route and also the API URL. Calling the Shields URL a “route” is on the whole more descriptive, and makes it clearer and more obvious which one of these we’re talking about. It’s a small thing, though seems like an improvement.

We have a few functions called `buildUrl`. I’ve renamed them to `buildRoute` when they refer to routes, and left them as `buildUrl` when they refer to API URLs.

I included a minor style tweak and some formatting cleanup in `TUTORIAL.md`.
2018-11-09 15:11:03 -05:00

67 lines
2.0 KiB
JavaScript

'use strict'
const LegacyService = require('../legacy-service')
const {
makeBadgeData: getBadgeData,
makeLogo: getLogo,
} = require('../../lib/badge-data')
const { checkErrorResponse } = require('../../lib/error-helper')
module.exports = class DependabotSemverCompatibility extends LegacyService {
static get category() {
return 'other'
}
static get route() {
return {
base: 'dependabot/semver',
}
}
static get examples() {
return [
{
title: 'SemVer Compatibility',
previewUrl: 'bundler/puma',
},
]
}
static registerLegacyRouteHandler({ camp, cache }) {
camp.route(
/^\/dependabot\/semver\/([^/]+)\/(.+)\.(svg|png|gif|jpg|json)$/,
cache((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 stability', 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, (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)
}
})
})
)
}
}