From 7fcd46600174e66dfaa69f5fb4f9b9c0d4b2b21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20M=C3=BCller?= Date: Wed, 8 Aug 2018 05:25:02 +0300 Subject: [PATCH 1/2] live-fns: (github) add "closed-issues" and "license" badges (#88) --- libs/index.md | 10 ++- libs/live-fns/github.js | 156 +++++++++++++++++++++++----------------- 2 files changed, 98 insertions(+), 68 deletions(-) diff --git a/libs/index.md b/libs/index.md index 91f8a84..050507f 100644 --- a/libs/index.md +++ b/libs/index.md @@ -1,6 +1,6 @@ # Badgen -Fast badge generating service. +Fast badge generating service. [![classic](https://badgen.net/badge/style/classic/pink)](https://badgen.net) [![flat](https://flat.badgen.net/badge/style/flat/pink)](https://flat.badgen.net) @@ -82,11 +82,15 @@ Available query params: ['latest release', '/github/release/babel/babel'], ['latest stable release', '/github/release/babel/babel/stable'], ['latest tag', '/github/tag/micromatch/micromatch'], + ['watchers', '/github/watchers/micromatch/micromatch'], ['stars', '/github/stars/micromatch/micromatch'], ['forks', '/github/forks/micromatch/micromatch'], - ['watchers', '/github/watchers/micromatch/micromatch'], ['issues', '/github/issues/micromatch/micromatch'], - ['open issues', '/github/open-issues/micromatch/micromatch'] + ['open issues', '/github/open-issues/micromatch/micromatch'], + ['closed issues', '/github/closed-issues/micromatch/micromatch'], + ['license', '/github/license/micromatch/micromatch'], + ['repository dependents', '/github/dependents-repo/micromatch/micromatch'], + ['package dependents', '/github/dependents-pkg/micromatch/micromatch'] ], /* release registries */ npm: [ diff --git a/libs/live-fns/github.js b/libs/live-fns/github.js index 318fec1..0d7bba0 100644 --- a/libs/live-fns/github.js +++ b/libs/live-fns/github.js @@ -10,16 +10,14 @@ module.exports = async function (topic, ...args) { return release(...args) case 'tag': return tag(...args) + case 'watchers': case 'stars': - return stats('stargazers', ...args) case 'forks': - return stats('forks', ...args) - case 'watchers': - return stats('watchers', ...args) - case 'open-issues': - return issues('open', ...args) case 'issues': - return issues('all', ...args) + case 'open-issues': + case 'closed-issues': + case 'license': + return stats(topic, ...args) case 'dependents-repo': return dependents('REPOSITORY', ...args) case 'dependents-pkg': @@ -69,28 +67,6 @@ async function tag (user, repo) { } } -async function dependents (type, user, repo) { - const html = await axios({ - url: `https://github.com/${user}/${repo}/network/dependents`, - headers: { - 'Accept': 'text/html,application/xhtml+xml,application/xml' - } - }).then(res => res.data) - - return { - subject: type === 'PACKAGE' ? 'pkg dependents' : 'repo dependents', - status: parseDependents(html, type), - color: 'blue' - } -} - -function parseDependents (html, type) { - const $ = cheerio.load(html) - const depLink = $(`a[href$="?dependent_type=${type}"]`) - if (depLink.length !== 1) return -1 - return depLink.text().replace(/[^0-9,]/g, '') -} - function queryGithub (query) { return axios.post('https://api.github.com/graphql', { query }, { headers: { @@ -100,55 +76,105 @@ function queryGithub (query) { }).then(res => res.data) } -async function issues (filter, user, repo) { - const queryFilter = filter === 'open' ? '(states:[OPEN])' : '' +async function stats (topic, user, repo) { + let query = '' + switch (topic) { + case 'watchers': + query = `watchers { totalCount }` + break + case 'stars': + query = `stargazers { totalCount }` + break + case 'forks': + query = `forks { totalCount }` + break + case 'issues': + query = `issues { totalCount }` + break + case 'open-issues': + query = `issues(states:[OPEN]) { totalCount }` + break + case 'closed-issues': + query = `issues(states:[CLOSED]) { totalCount }` + break + case 'license': + query = `licenseInfo { spdxId }` + break + } + const { data, errors } = await queryGithub(` query { repository(owner:"${user}", name:"${repo}") { - issues${queryFilter} { - totalCount - } + ${query} } } `) if (errors) { console.error(JSON.stringify(errors)) - return { subject: 'issues' } - } else { - return { - subject: filter === 'open' ? 'open issues' : 'issues', - status: data.repository.issues.totalCount, - color: filter === 'open' ? 'orange' : 'blue' - } + return { subject: topic } } -} -async function stats (topic, user, repo) { - const { data, errors } = await queryGithub(` - query { - repository(owner:"${user}", name:"${repo}") { - forks { - totalCount - } - stargazers { - totalCount - } - watchers { - totalCount - } + switch (topic) { + case 'watchers': + case 'forks': + case 'issues': + return { + subject: topic, + status: data.repository[topic].totalCount, + color: 'blue' } - } - `) + case 'stars': + return { + subject: topic, + status: data.repository.stargazers.totalCount, + color: 'blue' + } + case 'open-issues': + return { + subject: 'open issues', + status: data.repository.issues.totalCount, + color: 'orange' + } + case 'closed-issues': + return { + subject: 'closed issues', + status: data.repository.issues.totalCount, + color: 'blue' + } + case 'license': + return { + subject: topic, + status: data.repository.licenseInfo.spdxId, + color: 'blue' + } + default: + return { + subject: 'github', + status: 'unknown topic', + color: 'grey' + } + } +} - if (errors) { - console.error(JSON.stringify(errors)) - return { subject: topic } - } else { - return { - subject: topic.replace('stargazers', 'stars'), - status: data.repository[topic].totalCount, - color: 'blue' +function parseDependents (html, type) { + const $ = cheerio.load(html) + const depLink = $(`a[href$="?dependent_type=${type}"]`) + if (depLink.length !== 1) return -1 + return depLink.text().replace(/[^0-9,]/g, '') +} + +async function dependents (type, user, repo) { + const html = await axios({ + url: `https://github.com/${user}/${repo}/network/dependents`, + headers: { + 'Accept': 'text/html,application/xhtml+xml,application/xml' } + }).then(res => res.data) + + return { + subject: type === 'PACKAGE' ? 'pkg dependents' : 'repo dependents', + status: parseDependents(html, type), + color: 'blue' } } From 293fe8ccf06f0b16239173f09abbb0048d260465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20M=C3=BCller?= Date: Wed, 8 Aug 2018 05:48:25 +0300 Subject: [PATCH 2/2] chore: add license and improve readme (#89) --- LICENSE.md | 5 +++++ README.md | 32 +++++++++++++++++--------------- 2 files changed, 22 insertions(+), 15 deletions(-) create mode 100644 LICENSE.md diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..13e5e80 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,5 @@ +Copyright 2018 Amio + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md index ac79ca5..3bb8031 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ # Badgen Service -![License ISC](https://badgen.net/badge/license/ISC) +[![Uptime last week][uptime-src]][uptime-href] +[![Response time][response-src]][uptime-href] +[![Dependencies][dependencies-src]][dependencies-href] +[![License][license-src]][license-href] [![StandardJS][standard-src]][standard-href] -[![dependencies][dep-src]][dep-href] -[![uptime past week][uptime-src]][uptime-href] -[![response time][response-src]][uptime-href] -[![deploy to now][deploy-to-now]](#deploy-to-now-cloud) +[![Deploy to now][deploy-to-now-src]](#deploy-to-now) Home of [badgen.net](https://badgen.net), fast badge generating service. @@ -13,7 +13,7 @@ Home of [badgen.net](https://badgen.net), fast badge generating service. - Written in latest, vanilla JavaScript => no build process - Using [badgen](https://github.com/amio/badgen) library to generate svg on the fly => fast & stateless -- Hosted on [Now Cloud][now-href], serving behind Now CDN => faster & reliable +- Hosted on [Now][now-href], serving behind Now CDN => faster & reliable - Cache less than 4 minutes => fresh & hot - Three endpoints in one server - https://badgen.net - classic style badges @@ -43,9 +43,9 @@ To ensure that your addition is working correctly start the development server w Badgen Server will auto load all svg files in [libs/icons](libs/icons/). Please make sure new icon is optimized using [svgomg](https://jakearchibald.github.io/svgomg/). -### Deploy to Now Cloud +### Deploy to Now -Badgen generate badges on the fly, which means it's stateless (not rely on any db service). Deploy your own Badgen Service to [Now Cloud](https://zeit.co/now) with one single command: +Badgen generate badges on the fly, which means it's stateless (not rely on any db service). Deploy your own Badgen Service to [Now][now-href] with one single command: ``` now amio/badgen-service -e GH_TOKEN='' ``` @@ -57,12 +57,14 @@ now amio/badgen-service -e GH_TOKEN='' Made with ❤️ by [Amio](https://github.com/amio), built with ⚡️ from [badgen](https://github.com/amio/badgen). -[now-href]: https://zeit.co/now -[standard-src]: https://badgen.net/badge/code%20style/standard/F2A -[standard-href]: https://standardjs.com/ -[dep-src]: https://badgen.net/david/dep/amio/badgen-service?label=deps -[dep-href]: https://david-dm.org/amio/badgen-service [uptime-src]: https://badgen.net/uptime-robot/day/m780731617-a9e038618dc1aee36a44c4af -[response-src]: https://badgen.net/uptime-robot/response/m780731617-a9e038618dc1aee36a44c4af [uptime-href]: https://stats.uptimerobot.com/z6nqBfYGB -[deploy-to-now]: https://badgen.net/badge/▲/$%20now%20amio%2Fbadgen-service/222 +[response-src]: https://badgen.net/uptime-robot/response/m780731617-a9e038618dc1aee36a44c4af +[dependencies-src]: https://badgen.net/david/dep/amio/badgen-service?label=dependencies +[dependencies-href]: https://david-dm.org/amio/badgen-service/ +[license-src]: https://badgen.net/github/license/amio/badgen-service +[license-href]: LICENSE.md +[standard-src]: https://badgen.net/badge/code%20style/standard/pink +[standard-href]: https://standardjs.com/ +[deploy-to-now-src]: https://badgen.net/badge/▲/$%20now%20amio%2Fbadgen-service/222 +[now-href]: https://zeit.co/now/