-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.coffee
More file actions
executable file
·96 lines (81 loc) · 2.89 KB
/
server.coffee
File metadata and controls
executable file
·96 lines (81 loc) · 2.89 KB
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
exports.onHttp = (request) !->
if !request.data?
log 'HTTP: no data'
return
parsed = JSON.parse request.data
if !parsed?
log 'HTTP: could not parse data'
return
Db.shared.set 'received', parsed
if parsed.push?.changes?
general = {}
general.repositoryName = parsed.repository?.name
general.repositoryFullName = parsed.repository?.full_name
general.received = new Date()/1000
general.source = "BitBucket"
for change in parsed.push.changes
if !change.commits?
log 'no commits in change'
continue
if change.new?.type is "branch" and change.new?.name isnt "master"
log 'not on master branch'
return
# If the push had more than 5 commits truncated will be true and only the last 5 commits are in here
# First commit is newest and we want the oldest commit to have the lowest id, so reverse
for commit in change.commits.reverse()
commitData = {}
user = commit.author?.user
commitData.byName = user?.display_name
commitData.byUsername = user?.username
commitData.byUUID = user?.uuid
commitData.avatar = user?.links?.avatar?.href
commitData.message = commit.message
commitData.date = commit.date
commitData.hash = commit.hash
commitData.url = commit.links?.html?.href
applyKeysToObject general, commitData
# Get next id
commitMaxId = Db.shared.get('commitMaxId') || 0
commitMaxId++
Db.shared.set 'commitMaxId', commitMaxId
# Set commit data
Db.shared.set 'commits', commitMaxId, commitData
log 'BitBucket Commit: '+commitData.message
# Send notification
Event.create
unit: 'commit'
text: 'Commit: '+general.repositoryName+': '+commit.message
else if parsed.commits and parsed.commits.length > 0
general = {}
general.repositoryName = parsed.repository?.name
general.repositoryFullName = parsed.repository?.full_name
general.repositoryUrl = parsed.repository?.url
general.received = new Date()/1000
general.source = "GitHub"
for commit in parsed.commits
commitData = {}
commitData.message = commit.message
commitData.date = commit.timestamp
commitData.hash = commit.hash
commitData.byName = commit.author?.name
commitData.byUsername = commit.author?.username
commitData.byEmail = commit.author?.email
commitData.avatar = "https://avatars.githubusercontent.com/"+(commit.author?.username)
commitData.commitUrl = commit.url
applyKeysToObject general, commitData
# Get next id
commitMaxId = Db.shared.get('commitMaxId') || 0
commitMaxId++
Db.shared.set 'commitMaxId', commitMaxId
# Set commit data
Db.shared.set 'commits', commitMaxId, commitData
log 'Github Commit: '+commitData.message
# Send notification
Event.create
unit: 'commit'
text: 'Commit: '+general.repositoryName+': '+commit.message
else
log 'no changes listed in the data'
applyKeysToObject = (source, target) !->
for key, value of source
target[key] = value