@ -1,11 +1,6 @@
const fetch = require ( 'node-fetch' )
const ms = require ( 'ms' )
const chalk = require ( 'chalk' )
let data = [ ]
let dataRepos = [ ]
let dataReleases = [ ]
const orgname = 'bigchaindb'
const reponame = 'bigchaindb' // Used for fetching specific release
@ -13,12 +8,13 @@ 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 )
const handleResponse = async res ponse => {
if ( res ponse . status !== 200 ) {
return logError ( 'Non-200 response code from GitHub: ' + res ponse . status )
}
return res . json ( )
const json = await response . json ( )
return json
}
// Request options for all fetch calls
@ -33,44 +29,35 @@ const options = {
//
// Fetch all public GitHub repos
//
const fetchRepos = ( ) => {
const fetchRepos = async ( ) => {
const start = Date . now ( )
const url = 'https://api.github.com/orgs/' + orgname + '/repos'
fetch ( url , options )
. then ( res => {
return handleResponse ( res )
} )
. then ( data _ => {
if ( ! data _ ) {
return
}
dataRepos = data _ . map ( ( {
name ,
description ,
html _url ,
stargazers _count ,
forks _count ,
fork ,
topics
} ) => ( {
name ,
description ,
url : html _url ,
stars : stargazers _count ,
forks : forks _count ,
is _fork : fork ,
topics
} ) ) . sort ( ( p1 , p2 ) => p2 . stars - p1 . stars )
log ( 'Re-built projects cache. ' +
` Total: ${ data _ . length } public BigchainDB projects. ` +
` Elapsed: ${ ( new Date ( ) - start ) } ms ` )
} )
. catch ( error => {
logError ( 'Error parsing response from GitHub: ' + error . stack )
} )
const response = await fetch ( url , options )
const json = await handleResponse ( response )
const repos = json . map ( ( {
name ,
description ,
html _url ,
stargazers _count ,
forks _count ,
fork ,
topics
} ) => ( {
name ,
description ,
url : html _url ,
stars : stargazers _count ,
forks : forks _count ,
is _fork : fork ,
topics
} ) ) . sort ( ( p1 , p2 ) => p2 . stars - p1 . stars )
log ( ` Total: ${ repos . length } public BigchainDB projects. ` +
` Elapsed: ${ ( new Date ( ) - start ) } ms ` )
return repos
}
//
@ -78,59 +65,48 @@ const fetchRepos = () => {
//
// @TODO: make this fetch all releases of all repos
//
const fetchReleases = ( ) => {
const fetchReleases = async ( ) => {
const start = Date . now ( )
const url = 'https://api.github.com/repos/bigchaindb/' + reponame + '/releases/latest'
fetch ( url , options )
. then ( res => {
return handleResponse ( res )
} )
. then ( data _ => {
if ( ! data _ ) {
return
}
dataReleases = ( {
name : reponame ,
release : data _ . tag _name ,
release _url : data _ . html _url
} )
log ( 'Re-built releases cache. ' +
` Latest release: ${ data _ . tag _name } . ` +
` Elapsed: ${ ( new Date ( ) - start ) } ms ` )
} )
. catch ( error => {
logError ( 'Error parsing response from GitHub: ' + error . stack )
} )
}
const response = await fetch ( url , options )
const json = await handleResponse ( response )
const engage = ( ) => {
fetchRepos ( )
fetchReleases ( )
}
const releases = ( {
name : reponame ,
release : json . tag _name ,
release _url : json . html _url
} )
//
// Let's roll, and roll again every X ms
//
engage ( )
setInterval ( engage , ms ( '15m' ) )
log ( ` Latest release: ${ json . tag _name } . ` +
` Elapsed: ${ ( new Date ( ) - start ) } ms ` )
return releases
}
//
// 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
data = await Object . assign ( dataReleases , dataRepos [ 0 ] )
data = Object . assign ( dataRepos , { 0 : data } )
// Make json pretty again.
data = JSON . stringify ( data , null , 2 )
return data
module . exports = async ( request , response ) => {
response . setHeader ( 'Access-Control-Allow-Origin' , '*' )
response . setHeader ( 'Access-Control-Allow-Methods' , 'GET' )
//
// Let's roll
//
try {
const repos = await fetchRepos ( )
const releases = await fetchReleases ( )
// Merge the responses together
// kinda hacky, needs rewrite for adding release info to all objects in dataRepos
let data
data = await Object . assign ( releases , repos [ 0 ] )
data = Object . assign ( repos , { 0 : data } )
const dataPretty = JSON . stringify ( data , null , 2 )
response . end ( dataPretty )
} catch ( error ) {
logError ( 'Error parsing response from GitHub: ' + error . message )
}
}