1
0
mirror of https://github.com/bigchaindb/github-projects.git synced 2024-12-28 23:57:50 +01:00
github-projects/index.js

136 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-05-08 20:46:26 +02:00
const fetch = require('node-fetch')
const ms = require('ms')
const chalk = require('chalk')
let data = []
let dataRepos = []
let dataReleases = []
2017-06-28 19:53:52 +02:00
const orgname = 'bigchaindb'
2017-05-08 20:46:26 +02:00
const reponame = 'bigchaindb' // Used for fetching specific release
const log = text => console.log(text)
const logError = text => console.log(chalk.bold.red(text))
// Response handling for all fetch calls
const handleResponse = res => {
if (res.status !== 200) {
return logError('Non-200 response code from GitHub: ' + res.status)
}
return res.json()
}
2017-06-28 20:14:43 +02:00
// Request options for all fetch calls
const options = {
2017-05-08 20:46:26 +02:00
headers: {
2017-06-28 20:15:45 +02:00
// For getting topics, see note on https://developer.github.com/v3/search/
Accept: 'application/vnd.github.mercy-preview+json'
// Accept: 'application/vnd.github.preview'
2017-05-08 20:46:26 +02:00
}
2017-06-28 20:14:43 +02:00
}
2017-05-08 20:46:26 +02:00
//
// Fetch all public GitHub repos
//
const fetchRepos = () => {
const start = Date.now()
2017-06-28 19:53:52 +02:00
const url = 'https://api.github.com/orgs/' + orgname + '/repos'
2017-05-08 20:46:26 +02:00
2017-06-28 20:14:43 +02:00
fetch(url, options)
2017-05-08 20:46:26 +02:00
.then(res => {
return handleResponse(res)
})
.then(data_ => {
if (!data_) {
return
}
dataRepos = data_.map(({
name,
description,
html_url,
stargazers_count,
forks_count,
2017-06-28 20:15:45 +02:00
fork,
topics
2017-05-08 20:46:26 +02:00
}) => ({
name,
description,
url: html_url,
stars: stargazers_count,
forks: forks_count,
2017-06-28 20:15:45 +02:00
is_fork: fork,
topics
2018-02-15 18:07:33 +01:00
})).sort((p1, p2) => p2.stars - p1.stars)
2017-05-08 20:46:26 +02:00
2018-09-04 13:40:36 +02:00
log('Re-built projects cache. ' +
2017-05-08 20:46:26 +02:00
`Total: ${data_.length} public BigchainDB projects. ` +
`Elapsed: ${(new Date() - start)}ms`)
})
2018-09-04 13:40:36 +02:00
.catch(error => {
logError('Error parsing response from GitHub: ' + error.stack)
2017-05-08 20:46:26 +02:00
})
}
//
// Fetch GitHub releases
//
// @TODO: make this fetch all releases of all repos
//
const fetchReleases = () => {
const start = Date.now()
const url = 'https://api.github.com/repos/bigchaindb/' + reponame + '/releases/latest'
2017-06-28 20:14:43 +02:00
fetch(url, options)
2017-05-08 20:46:26 +02:00
.then(res => {
return handleResponse(res)
})
.then(data_ => {
if (!data_) {
return
}
dataReleases = ({
name: reponame,
2018-04-10 14:46:51 +02:00
release: data_.tag_name,
release_url: data_.html_url
2017-05-08 20:46:26 +02:00
})
2018-09-04 13:40:36 +02:00
log('Re-built releases cache. ' +
2017-05-08 20:46:26 +02:00
`Latest release: ${data_.tag_name}. ` +
`Elapsed: ${(new Date() - start)}ms`)
})
2018-09-04 13:40:36 +02:00
.catch(error => {
logError('Error parsing response from GitHub: ' + error.stack)
2017-05-08 20:46:26 +02:00
})
}
const engage = () => {
fetchRepos()
fetchReleases()
}
//
// Let's roll, and roll again every X ms
//
engage()
setInterval(engage, ms('15m'))
//
// Create the response
//
module.exports = async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET')
// Merge the responses together
// kinda hacky, needs rewrite for adding release info to all objects in dataRepos
2018-09-04 13:40:36 +02:00
data = await Object.assign(dataReleases, dataRepos[0])
2017-05-08 20:46:26 +02:00
data = Object.assign(dataRepos, {0: data})
// Make json pretty again.
data = JSON.stringify(data, null, 2)
return data
}