refactor & cleanup

This commit is contained in:
Matthias Kretschmann 2020-05-30 03:41:32 +02:00
parent 3aec9bc56f
commit 0e6644c2db
Signed by: m
GPG Key ID: 606EEEF3C479A91F
7 changed files with 6259 additions and 121 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
node_modules node_modules
npm-debug.log npm-debug.log
yarn.lock yarn.lock
package-lock.json
.vercel

144
index.js
View File

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

6169
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,10 @@
{ {
"name": "bigchaindb-github-projects", "name": "@bigchaindb/github-projects",
"private": true,
"version": "1.0.0", "version": "1.0.0",
"main": "index.js",
"scripts": { "scripts": {
"start": "micro", "start": "vercel dev",
"test": "xo && ava" "test": "xo"
}, },
"xo": { "xo": {
"esnext": true, "esnext": true,
@ -15,22 +15,12 @@
} }
}, },
"dependencies": { "dependencies": {
"chalk": "2.4.2", "chalk": "^4.0.0",
"micro": "9.3.4", "micro": "^9.3.4",
"ms": "2.1.1", "node-fetch": "^2.6.0",
"node-fetch": "2.6.0",
"request": "^2.88.0" "request": "^2.88.0"
}, },
"devDependencies": { "devDependencies": {
"ava": "^1.0.1", "xo": "^0.30.0"
"request-promise": "^4.2.1",
"test-listen": "^1.1.0",
"xo": "^0.24.0"
},
"now": {
"alias": "bigchaindb-github.now.sh",
"env": {
"NODE_ENV": "production"
}
} }
} }

View File

@ -61,7 +61,13 @@ npm test
## Deployment ## Deployment
Deploy to [now](https://zeit.co/now), make sure to switch to BigchainDB org before deploying: Every branch is automatically deployed to [Vercel](https://vercel.com) with their GitHub integration. A link to a deployment will appear under each Pull Request.
The latest deployment of the `master` branch is automatically aliased to `bigchaindb-github.now.sh`, configured as `alias` in [`vercel.json`](vercel.json).
### Manual Deployment
If needed, app can be deployed manually. Make sure to switch to Ocean Protocol org before deploying:
```bash ```bash
# first run # first run

View File

@ -1,17 +0,0 @@
import micro from 'micro'
import test from 'ava'
import listen from 'test-listen'
import request from 'request-promise'
test('it works', async t => {
const service = micro(async (req, res) => {
await micro.send(res, 200, {
test: 'woot'
})
})
const url = await listen(service)
const body = await request(url)
t.deepEqual(JSON.parse(body).test, 'woot')
})

13
vercel.json Normal file
View File

@ -0,0 +1,13 @@
{
"version": 2,
"alias": "bigchaindb-github.now.sh",
"env": {
"NODE_ENV": "production"
},
"builds": [
{
"src": "index.js",
"use": "@now/node"
}
]
}