-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgithubActivity.coffee
220 lines (170 loc) · 5.59 KB
/
githubActivity.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
if not jQuery?
console.warn("GithubActivity requires jQuery!")
if not _?
console.warn("GithubActivity requires Underscore.js!")
$ = jQuery
PFX = "githubActivity"
GITHUB = 'https://github.com'
GITHUB_API = 'https://api.github.com'
getRepoEventsData = (repo, cb=(->)) ->
$.getJSON "#{GITHUB_API}/repos/#{repo}/events?callback=?", null, cb
makeLastManInCheck = (gaObj, repo, kind) ->
lmic = (data, textStatus, jqXHR) ->
gaObj.registerData(repo, kind, data.data)
if gaObj.ready()
gaObj.go()
return lmic
util =
truncate: (s, l=50) ->
o = s[0...l]
if s.length > l
o += "…"
o
humanTime: (date) ->
now = new Date()
del = now - date
secs = del / 1000
if secs < 5
return "just now"
if secs < 60
return "#{Math.floor(secs)}s ago"
mins = secs / 60
if mins < 60
return "#{Math.floor(mins)}m ago"
hours = mins / 60
if hours < 24
return "#{Math.floor(hours)}h ago"
days = hours / 24
return "#{Math.floor(days)}d ago"
parseISO8601: (string) ->
# The following code adapted from Paul Sowden's ISO8601 conversion code
regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
"(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(.([0-9]+))?)?" +
"(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?"
d = string.match(new RegExp(regexp))
offset = 0
date = new Date(d[1], 0, 1)
date.setMonth d[3] - 1 if d[3]
date.setDate d[5] if d[5]
date.setHours d[7] if d[7]
date.setMinutes d[8] if d[8]
date.setSeconds d[10] if d[10]
date.setMilliseconds Number("0." + d[12]) * 1000 if d[12]
if d[14]
offset = (Number(d[16]) * 60) + Number(d[17])
offset *= (if (d[15] is "-") then 1 else -1)
offset -= date.getTimezoneOffset()
time = (Number(date) + (offset * 60 * 1000))
out = new Date()
out.setTime Number(time)
return out
eventHelpers =
who: (ev) ->
"""
<img src='#{ev.actor.avatar_url}s=20'>
<a href='#{GITHUB}/#{ev.actor.login}'>#{ev.actor.login}</a>
"""
repoUrl: (ev) ->
"#{GITHUB}/#{ev.repo.name}"
repo: (ev) ->
"<a href='#{eventHelpers.repoUrl(ev)}'>#{ev.repo.name}</a>"
titleForDefault: (ev) ->
"did something"
titleForIssuesEvent: (ev) ->
"#{ev.payload.action} an issue on #{eventHelpers.repo(ev)}"
titleForWatchEvent: (ev) ->
"#{ev.payload.action} watching #{eventHelpers.repo(ev)}"
titleForForkEvent: (ev) ->
"forked #{eventHelpers.repo(ev)}"
titleForPushEvent: (ev) ->
branch = ev.payload.ref.split('/')[2]
if branch
"pushed to <strong>#{branch}</strong> on #{eventHelpers.repo(ev)}"
else
"pushed to #{eventHelpers.repo(ev)}"
titleForIssueCommentEvent: (ev) ->
"""
commented on
<a href='#{ev.payload.issue.html_url}\#issuecomment-#{ev.payload.comment.id}'>
issue #{ev.payload.issue.number}</a>
on #{eventHelpers.repo(ev)}
"""
title: (ev) ->
t = eventHelpers["titleFor#{ev.type}"] or eventHelpers.titleForDefault
eventWhen = util.humanTime(util.parseISO8601(ev.created_at))
"""
<span class='when'>#{eventWhen}</span>
<span class='what'>
#{eventHelpers.who(ev)}
#{t(ev)}
</span>
"""
detailsForDefault: (ev) ->
"More #{ev.type} details here …"
detailsForForkEvent: (ev) ->
"→ <a href='#{ev.payload.forkee.html_url}'>#{ev.payload.forkee.html_url}</a>"
detailsForPushEvent: (ev) ->
maxCommits = 3
o = []
for c in ev.payload.commits[0...maxCommits]
o.push "<a href='#{eventHelpers.repoUrl(ev)}/commit/#{c.sha}'>#{c.sha[0...7]}</a>: #{util.truncate(c.message)}"
if ev.payload.commits.length > maxCommits
o.push "and #{ev.payload.commits.length - maxCommits} more…"
o.join("<br>")
detailsForIssuesEvent: (ev) ->
"""
<a href='#{ev.payload.issue.html_url}'>Issue #{ev.payload.issue.number}</a>:
#{util.truncate(ev.payload.issue.title)}
"""
detailsForIssueCommentEvent: (ev) ->
util.truncate(ev.payload.comment.body)
details: (ev) ->
d = eventHelpers["detailsFor#{ev.type}"] or eventHelpers.detailsForDefault
"#{d(ev)}"
render: (ev) ->
"""
<div class='event'>
<p class='title'>#{eventHelpers.title(ev)}</p>
<p class='details'>#{eventHelpers.details(ev)}</p>
</div>
"""
class GithubActivity
constructor: (@element, @repos, @options) ->
@data =
repoEvents: {}
$(@element).addClass('github-activity').addClass('loading')
# Add wrapper
@wrapper = $("<div class='events-wrapper'></div>").appendTo(@element)[0]
for r in @repos
getRepoEventsData(r, makeLastManInCheck(this, r, 'repoEvents'))
registerData: (repo, kind, data) ->
@data[kind][repo] = data
ready: () ->
got = (k for k, v of @data['repoEvents']).sort()
exp = @repos.sort()
if got > exp or exp > got
return false
return true
go: () ->
@allEvents = []
for r in @repos
re = @data.repoEvents[r]
if re.message?
if re.message == 'Not Found'
console.warn "GithubActivity: #{r} is not a valid repo!"
else
console.error re.message
else
@allEvents = @allEvents.concat(re)
@allEvents = _.sortBy(@allEvents, (e) -> e.created_at).reverse()
$(@element).removeClass('loading')
this.drawEvents @allEvents[0...(@options.events or 10)]
drawEvents: (events) ->
for e in events
$(eventHelpers.render(e)).appendTo(@wrapper)
jQuery.fn.githubActivity = (options) ->
if not options['repos']
console.error "GithubActivity plugin needs 'repos' key!"
return this
new GithubActivity(this, options['repos'], options)
return this