From 24828bea175bf91ae13d5230a3099e6d2a629b12 Mon Sep 17 00:00:00 2001 From: Amio / Date: Tue, 18 Sep 2018 17:28:22 +0800 Subject: [PATCH] Continue #158 (#161) * feat(github): add statuses badge Signed-off-by: Charlike Mike Reagent * chore: update index Signed-off-by: Charlike Mike Reagent * docs: update examples for github status --- libs/index.md | 4 ++++ libs/live-fns/github.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/libs/index.md b/libs/index.md index 9c53bf1..90b6aa0 100644 --- a/libs/index.md +++ b/libs/index.md @@ -70,6 +70,10 @@ Advanced usage (for badge makers): ['latest stable release', '/github/release/babel/babel/stable'], ['latest tag', '/github/tag/micromatch/micromatch'], ['watchers', '/github/watchers/micromatch/micromatch'], + ['status', '/github/status/micromatch/micromatch'], + ['status (branch)', '/github/status/tunnckoCore/badgen-service/github-status'], + ['status (branch)', '/github/status/micromatch/micromatch/gh-pages'], + ['status (commit)', '/github/status/micromatch/micromatch/f4809eb6df80b'], ['stars', '/github/stars/micromatch/micromatch'], ['forks', '/github/forks/micromatch/micromatch'], ['issues', '/github/issues/micromatch/micromatch'], diff --git a/libs/live-fns/github.js b/libs/live-fns/github.js index 821270c..3c56032 100644 --- a/libs/live-fns/github.js +++ b/libs/live-fns/github.js @@ -37,6 +37,8 @@ module.exports = async (topic, ...args) => { return downloads(args[0], args[1], '/latest') case 'release': return release(...args) + case 'status': + return singleStatus(...args) case 'dependents-repo': return dependents('REPOSITORY', ...args) case 'dependents-pkg': @@ -67,6 +69,43 @@ const queryGithub = query => { }).then(res => res.body) } +const singleStatus = async (user, repo, ref = 'master') => { + const statuses = await restGithub(`repos/${user}/${repo}/commits/${ref}/status`) + + switch (statuses.state) { + case 'success': + return { + subject: 'status', + status: 'success', + color: 'green' + } + case 'error': + return { + subject: 'status', + status: 'error', + color: 'red' + } + case 'failure': + return { + subject: 'status', + status: 'failure', + color: 'red' + } + case 'pending': + return { + subject: 'status', + status: 'pending', + color: 'orange' + } + default: + return { + subject: 'status', + status: 'unknown', + color: 'grey' + } + } +} + const release = async (user, repo, channel) => { const releases = await restGithub(`repos/${user}/${repo}/releases`)