Browse Source

live-fns: (github) use got & refactor

- Replace axios with got
- Separate assets downloads count
github-label
Amio 6 years ago
committed by Amio /
parent
commit
c9081aa8b0
  1. 2
      libs/index.md
  2. 3
      libs/live-fetcher.js
  3. 64
      libs/live-fns/github.js

2
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']
],

3
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)

64
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,
// query github api v3 (rest)
const restGithub = path => got(`https://api.github.com/${path}`, {
headers: {
...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':

Loading…
Cancel
Save