badgen.net/pages/api/coveralls.ts

49 wiersze
1.6 KiB
TypeScript

2023-08-01 14:22:56 +00:00
import got from '../../libs/got'
import { coverage as cov, coverageColor } from '../../libs/utils'
import { createBadgenHandler, PathArgs } from '../../libs/create-badgen-handler-next'
2019-05-31 09:26:28 +00:00
const COVERALLS_BADGE_URL = 'https://coveralls.io/repos/'
const client = got.extend({ prefixUrl: COVERALLS_BADGE_URL })
export default createBadgenHandler({
2019-05-31 09:26:28 +00:00
title: 'Coveralls',
examples: {
'/coveralls/c/github/jekyll/jekyll': 'coverage (github)',
'/coveralls/c/github/jekyll/jekyll/master': 'coverage (github, branch)',
'/coveralls/c/bitbucket/pyKLIP/pyklip': 'coverage (bitbucket)',
'/coveralls/c/bitbucket/pyKLIP/pyklip/master': 'coverage (bitbucket, branch)',
},
handlers: {
'/coveralls/c/:vcs<github|bitbucket>/:owner/:repo/:branch?': handler
2019-05-31 09:26:28 +00:00
}
})
2019-05-31 09:26:28 +00:00
// Detect coveralls.io's badge redirection instead of using it's api
// See https://github.com/badgen/badgen.net/issues/96
async function handler ({ vcs, owner, repo, branch }: PathArgs) {
const endpoint = `${vcs}/${owner}/${repo}/badge.svg`
const searchParams = new URLSearchParams()
if (branch) searchParams.set('branch', branch)
const badgeURL = await client.head(endpoint, {
searchParams,
2019-05-31 09:26:28 +00:00
followRedirect: false // Expecting 302 redirection to "coveralls_xxx.svg"
}).then(res => res.headers.location) || ''
2019-05-31 09:26:28 +00:00
const percentage = Number(badgeURL.match(/_(\d+)\.svg/)?.[1])
if (Number.isNaN(percentage)) {
2019-05-31 09:26:28 +00:00
return {
subject: 'coverage',
status: 'invalid',
color: 'grey'
}
}
return {
subject: 'coverage',
status: cov(percentage),
color: coverageColor(percentage)
}
2019-05-31 09:26:28 +00:00
}