badge: add snyk support (#394)

* chore: pass search params to badge handler

* badge: ass synk support

Extract data from Synk's badge.

* refactor: correct query type

* refactor: convert `targetFile` to path segment
pull/402/head
Dario Vladović 2020-05-25 15:34:13 +02:00 zatwierdzone przez GitHub
rodzic 0b2daa5ce5
commit d0b32d6ab6
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 50 dodań i 0 usunięć

49
api/snyk.ts 100644
Wyświetl plik

@ -0,0 +1,49 @@
import cheerio from 'cheerio'
import got from '../libs/got'
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
export default createBadgenHandler({
title: 'Snyk',
examples: {
'/snyk/badgen/badgen.net': 'vulnerability scan',
'/snyk/babel/babel/6.x': 'vulnerability scan (branch)',
'/snyk/tunnckoCore/opensource/master/@tunnckocore%2Futils%2Fpackage.json': 'vulnerability scan (custom path)'
},
handlers: {
'/snyk/:user/:repo/:branch?/:targetFile?': handler
}
})
async function handler ({ user, repo, branch = 'master', targetFile }: PathArgs) {
const badgeUrl = `https://snyk.io/test/github/${user}/${repo}/${branch}/badge.svg`
const searchParams = new URLSearchParams()
if (targetFile) searchParams.set('targetFile', targetFile)
const svg = await got(badgeUrl, { searchParams }).text()
const $ = cheerio.load(svg, { xmlMode: true })
const $color = $('g[mask] path')
.filter((_, el) => el.attribs.d?.startsWith('M90'))
.first()
const $subject = $('g text')
.filter((_, el) => parseInt(el.attribs.x, 10) === 45)
.first()
const $status = $('g text')
.filter((_, el) => parseInt(el.attribs.x, 10) === 100)
.first()
const subject = $subject.text().trim() || 'vulnerabilities'
const status = $status.text().trim()
const color = ($color.attr('fill')?.trim() || '').replace(/^#/, '')
if (!status || !color) {
const context = [
`${user}/${repo}/${branch}`,
targetFile && `targetFile=${targetFile}`
].filter(Boolean).join(' ')
throw new Error(`Unknown Synk status: ${context}`)
}
return { subject, status, color }
}

Wyświetl plik

@ -42,6 +42,7 @@ export const liveBadgeList = [
'codeclimate',
'azure-pipelines',
// quality & metrics
'snyk',
'lgtm',
'uptime-robot',
'xo',