-
Notifications
You must be signed in to change notification settings - Fork 1
/
github-events.js
64 lines (55 loc) · 1.94 KB
/
github-events.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
import { T, always, both, complement, cond, last, map, propEq, reduceWhile } from 'ramda'
import axios from 'axios'
import { format } from 'date-fns'
const user = process.env.GITHUB_USER || 'cuchi'
const many = Number(process.env.GITHUB_MANY_EVENTS || 7)
const interval = Number(process.env.GITHUB_INTERVAL || 1800)
let events = []
const url = `https://api.github.com/users/${user}/events`
const typeEq = propEq('type')
const formatEventDate = e =>
format(new Date(e.created_at), 'MMM do, yyyy - HH:mm')
const eventTypes = [{
trigger: typeEq('WatchEvent'),
icon: 'star',
getMessage: e => `starred ${e.repo.name}`
}, {
trigger: typeEq('PushEvent'),
icon: 'file_upload',
getMessage: e => `pushed updates to ${e.repo.name}`
}, {
trigger: both(typeEq('IssuesEvent'), propEq('action', 'opened')),
icon: 'info',
getMessage: e => `opened an issue on ${e.repo.name}`
}]
const getEventInfo = cond(
[...map(
({ trigger, icon, getMessage }) =>
[trigger, e =>
({ icon, message: getMessage(e), date: formatEventDate(e) })],
eventTypes),
[T, always(null)]])
async function updateLastEvents() {
try {
events = reduceWhile(
complement(propEq('length', many)),
(eventsAcc, rawEvent) => {
const event = getEventInfo(rawEvent)
const lastEvent = last(eventsAcc) || {}
return event && event.message !== lastEvent.message
? [...eventsAcc, event]
: eventsAcc
},
[],
(await axios.get(url)).data)
} catch (err) {
console.error(`Error retrieving GitHub data: ${err.message}`)
}
}
setInterval(updateLastEvents, interval * 1000)
const firstRequest = updateLastEvents()
export default async function (req, res) {
await firstRequest
res.writeHead(200, { 'Content-Type': 'application/json' })
.end(JSON.stringify(events))
}