style(circleci): use `encodeURIComponent` instead of `qs.escape` (#501)

pull/509/head^2
Dario Vladović 2021-02-20 15:53:54 +01:00 zatwierdzone przez GitHub
rodzic 0f5618b424
commit ba87643072
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 17 dodań i 21 usunięć

Wyświetl plik

@ -1,7 +1,10 @@
import qs from 'querystring'
import got from '../libs/got'
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
const CIRCLECI_API_URL = 'https://circleci.com/api/v1.1/'
const client = got.extend({ prefixUrl: CIRCLECI_API_URL })
export default createBadgenHandler({
title: 'CircleCI',
examples: {
@ -9,32 +12,25 @@ export default createBadgenHandler({
'/circleci/github/nuxt/nuxt.js/master': 'build (branch)',
},
handlers: {
'/circleci/:vcs<github|gitlab>/:user/:project/:branch?': handler
'/circleci/:vcs<github|gitlab>/:owner/:repo/:branch?': handler
}
})
async function handler ({ vcs, user, project, branch }: PathArgs) {
// https://circleci.com/docs/api/v1-reference/
branch = branch ? `/tree/${qs.escape(branch)}` : ''
const endpoint = `https://circleci.com/api/v1.1/project/${vcs}/${user}/${project}${branch}?filter=completed&limit=1`
const [latest] = await got(endpoint).json<any>()
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>()
const color = {
failed: 'red',
success: 'green'
}[latest.status] || 'grey'
return {
subject: 'circleci',
status: latest.status.replace(/_/g, ' '),
color: getStatusColor(latest.status)
}
}
const getStatusColor = status => {
switch (status) {
case 'failed':
return 'red'
case 'success':
return 'green'
default:
return 'grey'
color
}
}