Browse Source

Merge branch 'master' of https://github.com/amio/badgen-service

refactor-github
Amio 6 years ago
parent
commit
5f877bd77c
  1. 5
      LICENSE.md
  2. 32
      README.md
  3. 8
      libs/index.md
  4. 140
      libs/live-fns/github.js

5
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.

32
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/

8
libs/index.md

@ -83,11 +83,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: [

140
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: topic }
}
switch (topic) {
case 'watchers':
case 'forks':
case 'issues':
return {
subject: topic,
status: data.repository[topic].totalCount,
color: 'blue'
}
case 'stars':
return {
subject: filter === 'open' ? 'open issues' : 'issues',
subject: topic,
status: data.repository.stargazers.totalCount,
color: 'blue'
}
case 'open-issues':
return {
subject: 'open issues',
status: data.repository.issues.totalCount,
color: filter === 'open' ? 'orange' : 'blue'
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'
}
async function stats (topic, user, repo) {
const { data, errors } = await queryGithub(`
query {
repository(owner:"${user}", name:"${repo}") {
forks {
totalCount
default:
return {
subject: 'github',
status: 'unknown topic',
color: 'grey'
}
stargazers {
totalCount
}
watchers {
totalCount
}
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)
if (errors) {
console.error(JSON.stringify(errors))
return { subject: topic }
} else {
return {
subject: topic.replace('stargazers', 'stars'),
status: data.repository[topic].totalCount,
subject: type === 'PACKAGE' ? 'pkg dependents' : 'repo dependents',
status: parseDependents(html, type),
color: 'blue'
}
}
}

Loading…
Cancel
Save