From 45dc7d556d4e6bc8afa77d013bac7a6c006059f1 Mon Sep 17 00:00:00 2001 From: Amio Date: Tue, 7 Aug 2018 14:16:45 +0800 Subject: [PATCH] live-fns: add github dependents badge --- libs/live-fns/github.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/libs/live-fns/github.js b/libs/live-fns/github.js index f0676a4..318fec1 100644 --- a/libs/live-fns/github.js +++ b/libs/live-fns/github.js @@ -1,3 +1,4 @@ +const cheerio = require('cheerio') const axios = require('../axios.js') const token = process.env.GH_TOKEN @@ -19,6 +20,10 @@ module.exports = async function (topic, ...args) { return issues('open', ...args) case 'issues': return issues('all', ...args) + case 'dependents-repo': + return dependents('REPOSITORY', ...args) + case 'dependents-pkg': + return dependents('PACKAGE', ...args) default: return { subject: 'github', @@ -64,6 +69,28 @@ 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: {