-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.coffee
82 lines (70 loc) · 1.96 KB
/
index.coffee
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
_ = require 'highland'
Redis = require 'redis'
Github = require './lib/github'
Trello = require './lib/trello'
utility = require './lib/utility'
{
TRELLO_KEY
TRELLO_SECRET
TRELLO_BOARD
GITHUB_USERNAME
GITHUB_TOKEN
GITHUB_ORG
REDIS_HOST
REDIS_PORT
REDIS_PASSWORD
} = process.env
# Set up connection to Redis
redis = Redis.createClient REDIS_PORT, REDIS_HOST
redis.auth REDIS_PASSWORD
# The main loop, which runs immediately
runLoop = ->
## Get a stream of events
events = github.listOrgEvents GITHUB_USERNAME, GITHUB_ORG
# Initial content is the resource stream. Need to lift it
.flatten()
# Convert buffer to string
.invoke 'toString'
# Take all strings together
.collect()
# Join them into one
.invoke 'join', ['']
# Ignore empty strings when there is no valid JSON responses
.compact()
# Parse it as JSON
.map JSON.parse
# Take each event as its own data packet
.flatten()
# Filter all events already in the database. We must do this filter step
# explicitly rather than just allowing Redis to save to database regardless
# record existence because we don't want to process records already in the
# database
.flatFilter (evt) ->
utility.once (done) ->
redis.get evt.id, (err, res) ->
done err, not res?
# Save the record by event IDs
.flatMap (evt) ->
utility.once (done) ->
redis.set evt.id, JSON.stringify(evt), (err, res) ->
done err, evt
## Process issue events
events.fork()
# We only care about issue events
.filter (evt) ->
evt.type is 'IssuesEvent'
# Should get them all in series
.toArray (x) ->
console.log JSON.stringify x
redis.end()
## TODO: Process other events by forking
trello = Trello.create
username: TRELLO_KEY
password: TRELLO_SECRET
runLoop: runLoop
github = Github.create
username: GITHUB_USERNAME
password: GITHUB_TOKEN
runLoop: runLoop
# Start the loop
runLoop()