From c9081aa8b03c66bb8f484c047758a2dd386f71f7 Mon Sep 17 00:00:00 2001 From: Amio Date: Mon, 13 Aug 2018 15:28:01 +0800 Subject: [PATCH] live-fns: (github) use got & refactor - Replace axios with got - Separate assets downloads count --- libs/index.md | 2 +- libs/live-fetcher.js | 3 +- libs/live-fns/github.js | 68 ++++++++++++++++++++++++++--------------- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/libs/index.md b/libs/index.md index b167e6e..e5e08e7 100644 --- a/libs/index.md +++ b/libs/index.md @@ -109,7 +109,7 @@ Available query params: ['tags', '/github/tags/micromatch/micromatch'], ['license', '/github/license/micromatch/micromatch'], ['last commit', '/github/last-commit/micromatch/micromatch'], - ['total downloads', '/github/dt/electron/electron'], + ['latest assets downloads', '/github/dl/electron/electron'], ['repository dependents', '/github/dependents-repo/micromatch/micromatch'], ['package dependents', '/github/dependents-pkg/micromatch/micromatch'] ], diff --git a/libs/live-fetcher.js b/libs/live-fetcher.js index 8a5f2ca..2e47df4 100644 --- a/libs/live-fetcher.js +++ b/libs/live-fetcher.js @@ -14,7 +14,8 @@ module.exports = async (scope, fn, paramsPath) => { status = 'timeout' } - console.error(fetchKey, `LIVEFN_ERR<${status}>`, e.message) + const info = status === 'unknown' ? e.stack : e.message + console.error(fetchKey, `LIVEFN_ERR<${status}>`, info) return { status, failed: true } }).then(result => { console.timeEnd(fetchKey) diff --git a/libs/live-fns/github.js b/libs/live-fns/github.js index 5f7796e..8a4a55b 100644 --- a/libs/live-fns/github.js +++ b/libs/live-fns/github.js @@ -2,8 +2,11 @@ const cheerio = require('cheerio') const distanceInWordsToNow = require('date-fns/distance_in_words_to_now') const millify = require('millify') const axios = require('../axios.js') +const got = require('../got.js') const v = require('../utils/version-formatter.js') + const token = process.env.GH_TOKEN +const tokenHeader = token ? { Authorization: `token ${token}` } : {} // https://developer.github.com/v3/repos/ @@ -26,8 +29,10 @@ module.exports = async (topic, ...args) => { case 'tag': case 'license': case 'last-commit': - case 'dt': return stats(topic, ...args) + case 'dt': // deprecated + case 'dl': + return downloads(args[0], args[1], '/latest') case 'release': return release(...args) case 'dependents-repo': @@ -45,30 +50,27 @@ module.exports = async (topic, ...args) => { } } -const queryGithub = (query, graphql = true) => { - const headers = token ? { Authorization: `token ${token}` } : {} - - if (!graphql) { - const url = `https://api.github.com/${query}` - return axios({ - url, - headers: { - ...headers, - Accept: 'application/vnd.github.hellcat-preview+json' - } - }) +// query github api v3 (rest) +const restGithub = path => got(`https://api.github.com/${path}`, { + headers: { + ...tokenHeader, + Accept: 'application/vnd.github.hellcat-preview+json' } +}).then(res => res.body) - return axios.post('https://api.github.com/graphql', { query }, { +// query github api v4 (graphql) +const queryGithub = query => { + return got.post('https://api.github.com/graphql', { + body: { query }, headers: { - ...headers, + ...tokenHeader, Accept: 'application/vnd.github.hawkgirl-preview+json' } - }) + }).then(res => res.body) } const release = async (user, repo, channel) => { - const { data: releases } = await queryGithub(`repos/${user}/${repo}/releases`, false) + const releases = await restGithub(`repos/${user}/${repo}/releases`) const [latest] = releases const stable = releases.find(release => !release.prerelease) @@ -98,7 +100,7 @@ const release = async (user, repo, channel) => { } const contributors = async (user, repo) => { - const { data: contributors } = await queryGithub(`repos/${user}/${repo}/contributors`, false) + const contributors = await restGithub(`repos/${user}/${repo}/contributors`) return { subject: 'contributors', @@ -107,6 +109,29 @@ const contributors = async (user, repo) => { } } +const downloads = async (user, repo, scope = '') => { + const release = await restGithub(`repos/${user}/${repo}/releases${scope}`) + + if (!release || !release.assets || !release.assets.length) { + return { + subject: 'downloads', + status: 'no assets', + color: 'grey' + } + } + + /* eslint-disable camelcase */ + const downloadCount = release.assets.reduce((result, { download_count }) => { + return result + download_count + }, 0) + + return { + subject: 'downloads', + status: millify(downloadCount), + color: 'green' + } +} + const stats = async (topic, user, repo, ...args) => { let query = '' let graphqlQuery = true @@ -216,12 +241,7 @@ const stats = async (topic, user, repo, ...args) => { ` } - const { data, errors } = await queryGithub(query, graphqlQuery) - - if (errors) { - console.error(JSON.stringify(errors)) - return { subject: topic } - } + const data = await queryGithub(query, graphqlQuery) switch (topic) { case 'watchers':