badgen.net/pages/api/circleci.ts

46 wiersze
1.2 KiB
TypeScript

2023-08-08 15:57:11 +00:00
import got from '../../libs/got'
import { createBadgenHandler, PathArgs } from '../../libs/create-badgen-handler-next'
2019-05-31 08:21:34 +00:00
const CIRCLECI_API_URL = 'https://circleci.com/api/v1.1/'
const client = got.extend({ prefixUrl: CIRCLECI_API_URL })
export default createBadgenHandler({
2019-05-31 08:21:34 +00:00
title: 'CircleCI',
examples: {
2023-08-08 15:57:11 +00:00
'/circleci/github/circleci/circleci-docs': 'build',
'/circleci/github/circleci/circleci-docs/master': 'build (branch)',
},
handlers: {
'/circleci/:vcs<github|gitlab>/:owner/:repo/:branch?': handler
2019-05-31 08:21:34 +00:00
}
})
2019-05-31 08:21:34 +00:00
async function handler ({ vcs, owner, repo, branch }: PathArgs) {
// https://circleci.com/docs/api/#recent-builds-for-a-single-project
branch = branch ? `/tree/${encodeURIComponent(branch)}` : ''
const path = `project/${vcs}/${owner}/${repo}${branch}`
const searchParams = { filter: 'completed', limit: 1, shallow: true }
const [latest] = await client.get(path, { searchParams }).json<any>()
2023-08-08 15:57:11 +00:00
if (latest === undefined) {
return {
subject: 'circleci',
status: 'not found',
color: 'grey'
}
}
const color = {
failed: 'red',
success: 'green'
}[latest.status] || 'grey'
2019-05-31 08:21:34 +00:00
return {
subject: 'circleci',
status: latest.status.replace(/_/g, ' '),
color
2019-05-31 08:21:34 +00:00
}
}