-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
78 lines (67 loc) · 1.78 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
const fetch = require('node-fetch')
const ms = require('ms')
let data = []
module.exports = async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET')
return data
}
// cache data now and every X ms
cacheData()
setInterval(cacheData, ms('15m'))
function log(text) {
return slack(text, process.env.TOKEN_EVENTS)
}
function logError(text) {
return slack(text, process.env.TOKEN_ALERTS)
}
function slack(text, id) {
fetch(`https://hooks.slack.com/services/${id}`, {
method: 'POST',
body: JSON.stringify({text})
})
}
function cacheData() {
const start = Date.now()
fetch('https://api.github.com/orgs/zeit/repos', {
headers: {
Accept: 'application/vnd.github.preview'
}
})
.then(res => {
if (res.status !== 200) {
return logError('Non-200 response code from GitHub: ' + res.status)
}
return res.json()
})
.then(data_ => {
if (!data_) {
return
}
// ugly hack because github sometimes doesn't return
// all the right search results :|
let featured = 0
data_.forEach(({name}) => {
if (name === 'hyper' || name === 'next.js' || name === 'micro') {
featured++
}
})
if (featured !== 3) {
return logError(`Error: GitHub did not include all projects (${featured})`)
}
data = data_.map(({name, description, stargazers_count, html_url}) => ({
name,
description,
url: html_url,
stars: stargazers_count
})).sort((p1, p2) =>
p2.stars - p1.stars
)
log(`Re-built projects cache. ` +
`Total: ${data.length} public ▲ZEIT projects. ` +
`Elapsed: ${(new Date() - start)}ms`)
})
.catch(err => {
logError('Error parsing response from GitHub: ' + err.stack)
})
}