1
0
mirror of https://github.com/bigchaindb/github-projects.git synced 2024-11-23 10:27:21 +01:00

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
npm-debug.log
yarn.lock
package-lock.json
.vercel

144
index.js
View File

@ -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 response => {
if (response.status !== 200) {
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
@ -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
}
const response = await fetch(url, options)
const json = await handleResponse(response)
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)
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('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)
})
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
}
const response = await fetch(url, options)
const json = await handleResponse(response)
dataReleases = ({
name: reponame,
release: data_.tag_name,
release_url: data_.html_url
})
const releases = ({
name: reponame,
release: json.tag_name,
release_url: json.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)
})
log(`Latest release: ${json.tag_name}. ` +
`Elapsed: ${(new Date() - start)}ms`)
return releases
}
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')
module.exports = async (request, response) => {
response.setHeader('Access-Control-Allow-Origin', '*')
response.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})
//
// Let's roll
//
try {
const repos = await fetchRepos()
const releases = await fetchReleases()
// Make json pretty again.
data = JSON.stringify(data, null, 2)
// 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)
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",
"private": true,
"name": "@bigchaindb/github-projects",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "micro",
"test": "xo && ava"
"start": "vercel dev",
"test": "xo"
},
"xo": {
"esnext": true,
@ -15,22 +15,12 @@
}
},
"dependencies": {
"chalk": "2.4.2",
"micro": "9.3.4",
"ms": "2.1.1",
"node-fetch": "2.6.0",
"chalk": "^4.0.0",
"micro": "^9.3.4",
"node-fetch": "^2.6.0",
"request": "^2.88.0"
},
"devDependencies": {
"ava": "^1.0.1",
"request-promise": "^4.2.1",
"test-listen": "^1.1.0",
"xo": "^0.24.0"
},
"now": {
"alias": "bigchaindb-github.now.sh",
"env": {
"NODE_ENV": "production"
}
"xo": "^0.30.0"
}
}

View File

@ -61,7 +61,13 @@ npm test
## 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
# 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"
}
]
}