🎭 it's a thing

This commit is contained in:
Matthias Kretschmann 2017-05-08 20:46:26 +02:00
commit 3d7dac5ac4
Signed by: m
GPG Key ID: BD3C1F3EDD7831FC
10 changed files with 287 additions and 0 deletions

13
.editorconfig Normal file
View File

@ -0,0 +1,13 @@
# EditorConfig is awesome: http://EditorConfig.org
[*]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
[*.json]
indent_size = 2

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules
npm-debug.log
yarn.lock

2
.travis.yml Normal file
View File

@ -0,0 +1,2 @@
language: node_js
node_js: node

132
index.js Executable file
View File

@ -0,0 +1,132 @@
const fetch = require('node-fetch')
const ms = require('ms')
const chalk = require('chalk')
let data = []
let dataRepos = []
let dataReleases = []
const username = 'bigchaindb'
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()
}
// Request headers for all fetch calls
const headers = [{
headers: {
Accept: 'application/vnd.github.preview'
}
}]
//
// Fetch all public GitHub repos
//
const fetchRepos = () => {
const start = Date.now()
const url = 'https://api.github.com/users/' + username + '/repos'
fetch(url, headers)
.then(res => {
return handleResponse(res)
})
.then(data_ => {
if (!data_) {
return
}
dataRepos = data_.map(({
name,
description,
html_url,
stargazers_count,
forks_count,
fork
}) => ({
name,
description,
url: html_url,
stars: stargazers_count,
forks: forks_count,
is_fork: fork
})).sort((p1, p2) =>
p2.stars - p1.stars
)
log(`Re-built projects cache. ` +
`Total: ${data_.length} public BigchainDB projects. ` +
`Elapsed: ${(new Date() - start)}ms`)
})
.catch(err => {
logError('Error parsing response from GitHub: ' + err.stack)
})
}
//
// 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'
fetch(url, headers)
.then(res => {
return handleResponse(res)
})
.then(data_ => {
if (!data_) {
return
}
dataReleases = ({
name: reponame,
release: data_.tag_name
})
log(`Re-built releases cache. ` +
`Latest release: ${data_.tag_name}. ` +
`Elapsed: ${(new Date() - start)}ms`)
})
.catch(err => {
logError('Error parsing response from GitHub: ' + err.stack)
})
}
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
data = Object.assign(dataReleases, dataRepos[0])
data = Object.assign(dataRepos, {0: data})
// Make json pretty again.
data = JSON.stringify(data, null, 2)
return data
}

21
license.md Executable file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 BigchainDB GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

BIN
media/repo-banner.sketch Normal file

Binary file not shown.

BIN
media/repo-banner@2x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

33
package.json Executable file
View File

@ -0,0 +1,33 @@
{
"name": "bigchaindb-github-projects",
"private": true,
"version": "1.0.0",
"scripts": {
"start": "micro",
"test": "xo && ava"
},
"xo": {
"esnext": true,
"space": 4,
"semicolon": false,
"rules": {
"camelcase": 0
}
},
"dependencies": {
"chalk": "1.1.3",
"micro": "7.3.3",
"ms": "1.0.0",
"node-fetch": "1.6.3"
},
"devDependencies": {
"ava": "^0.19.1",
"request-promise": "^4.2.1",
"test-listen": "^1.0.2",
"xo": "^0.18.2"
},
"alias": "bigchaindb-github.now.sh",
"env": {
"NODE_ENV": "production"
}
}

66
readme.md Executable file
View File

@ -0,0 +1,66 @@
# [![github-projects](media/repo-banner@2x.png)](https://www.bigchaindb.com)
> Microservice to cache and expose GitHub projects for use throughout [www.bigchaindb.com](https://www.bigchaindb.com).
[![Build Status](https://travis-ci.org/bigchaindb/github-projects.svg?branch=master)](https://travis-ci.org/bigchaindb/github-projects)
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
<img src="http://forthebadge.com/images/badges/powered-by-electricity.svg" height="20"/>
<img src="http://forthebadge.com/images/badges/as-seen-on-tv.svg" height="20"/>
<img src="http://forthebadge.com/images/badges/uses-badges.svg" height="20"/>
## API
Endpoint: [`https://bigchaindb-github.now.sh`](https://bigchaindb-github.now.sh)
### GET /
**200**: Returns a list of all public projects as follows
```json
[
{
"name": "project-name",
"description": "The description",
"stars": 3040,
"forks": 293,
"is_fork": false,
"release": "v0.10.0",
"url": "https://github.com/bigchaindb/project"
}
]
```
## Development
Install dependencies:
```bash
npm install
```
And run the server:
```bash
npm start
```
## Deployment
Deploy to [now](https://zeit.co/now), make sure to switch to BigchainDB org before deploying:
```bash
# first run
now login
now switch
# deploy
now
# switch alias to new deployment, e.g.
now alias bigchaindb-github-projects-wxkyissxos bigchaindb-github
```
## Authors
- Matthias Kretschmann ([@kremalicious](https://github.com/kremalicious)) - [BigchainDB](https://www.bigchaindb.com)
Blatantly ~~copied from~~ inspired by [zeit/github-projects](https://github.com/zeit/github-projects)

17
test/index.js Normal file
View File

@ -0,0 +1,17 @@
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) => {
micro.send(res, 200, {
test: 'woot'
})
})
const url = await listen(service)
const body = await request(url)
t.deepEqual(JSON.parse(body).test, 'woot')
})