From 2345af75c44863833a7883b4dae0db53e8377017 Mon Sep 17 00:00:00 2001 From: Amio Date: Fri, 31 May 2019 16:11:24 +0800 Subject: [PATCH] Migrate travis badge to v2 --- endpoints/travis.ts | 63 +++++++++++++++++++++++++++++++++++++++++++++ libs/examples.ts | 1 + 2 files changed, 64 insertions(+) create mode 100644 endpoints/travis.ts diff --git a/endpoints/travis.ts b/endpoints/travis.ts new file mode 100644 index 0000000..25d644c --- /dev/null +++ b/endpoints/travis.ts @@ -0,0 +1,63 @@ +import got from '../libs/got' +import { + badgenServe, + BadgenServeMeta as Meta, + BadgenServeHandlers as Handlers, + BadgenServeHandlerArgs as Args +} from '../libs/badgen-serve' + +export const meta: Meta = { + title: 'Travis CI', + examples: { + '/travis/babel/babel': 'build', + '/travis/babel/babel/6.x': 'build (branch)', + } +} + +export const handlers: Handlers = { + '/travis/:user/:repo/:branch?': handler +} + +export default badgenServe(handlers) + +async function handler ({ user, repo, branch = 'master' }: Args) { + const com = `https://api.travis-ci.com/${user}/${repo}.svg?branch=${branch}` + const org = `https://api.travis-ci.org/${user}/${repo}.svg?branch=${branch}` + const [svg1, svg2] = await Promise.all([ + // @ts-ignore + got(com, { json: false }).then(({ body }) => body), + // @ts-ignore + got(org, { json: false }).then(({ body }) => body) + ]) + + const result = statuses.find(st => { + return (svg1 && svg1.includes(st[0])) || (svg2 && svg2.includes(st[0])) + }) + + if (result) { + return { + subject: 'travis', + status: result[0], + color: result[1] + } + } else { + return { + subject: 'travis', + status: 'unknown', + color: 'grey' + } + } +} + +const statuses = [ + ['passed', 'green'], + ['passing', 'green'], + ['failed', 'orange'], + ['failing', 'orange'], + ['error', 'red'], + ['errored', 'red'], + ['pending', 'yellow'], + ['fixed', 'yellow'], + ['broken', 'red'], + ['canceled', 'grey'] +] diff --git a/libs/examples.ts b/libs/examples.ts index b5121f6..d8e0c14 100644 --- a/libs/examples.ts +++ b/libs/examples.ts @@ -25,6 +25,7 @@ const liveBadgeExampleList = [ 'hackage', 'vs-marketplace', // CI + 'travis', 'appveyor', // quality & metrics // utilities