This repository has been archived by the owner on May 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (93 loc) · 2.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// some of this code is adapted from here: https://github.com/zeit/github-repos/blob/master/index.js
const serverless = require('serverless-http')
const express = require('express')
const app = express()
const fetch = require('node-fetch')
const ms = require('ms')
const cors = require('cors')
app.use(cors())
function log (text) {
return console.log(text)
}
function logError (res, text, status) {
console.log(text)
return res.status(status).send(text)
}
const token = process.env.TOKEN
app.get('/', function (req, res) {
fetch('https://api.github.com/orgs/eventOneHQ/repos?per_page=100', {
headers: {
Accept: 'application/vnd.github.preview',
Authorization: `token ${token}`
}
})
.then(resp => {
if (resp.status !== 200) {
return logError(
res,
'Non-200 response code from GitHub: ' + resp.status,
resp.status
)
}
return resp.json()
})
.then(data_ => {
if (!data_) {
return logError(res, 'Missing data', 500)
}
// Ugly hack because github sometimes doesn't return
// all the right search results :|
let featured = 0
data_.forEach(({ name }) => {
if (
name === 'filiosoft.github.io' ||
name === 'gitlab-issue-bot' ||
name === 'rva-cli'
) {
featured++
}
})
if (featured !== 3) {
return logError(
res,
`Error: GitHub did not include all projects (${featured})`,
500
)
}
const blacklist = []
const data = data_
.map(
({
name,
description,
stargazers_count,
fork,
forks,
html_url,
language,
archived
}) => ({
name,
description,
url: html_url,
stars: stargazers_count,
forks,
fork,
language,
archived
})
)
.sort((p1, p2) => p2.stars - p1.stars)
.filter(p => p.name !== blacklist.find(e => e === p.name))
.filter(p => p.archived !== true)
return res.json(data)
})
.catch(err => {
return logError(
res,
'Error parsing response from GitHub: ' + err.stack,
500
)
})
})
module.exports.handler = serverless(app)