forked from okfn/githubActivity.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubActivity.js
More file actions
244 lines (244 loc) · 8.09 KB
/
Copy pathgithubActivity.js
File metadata and controls
244 lines (244 loc) · 8.09 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
(function() {
var $, GITHUB, GITHUB_API, GithubActivity, PFX, eventHelpers, getRepoEventsData, makeLastManInCheck, util;
if (!(typeof jQuery !== "undefined" && jQuery !== null)) {
console.warn("GithubActivity requires jQuery!");
}
if (!(typeof _ !== "undefined" && _ !== null)) {
console.warn("GithubActivity requires Underscore.js!");
}
$ = jQuery;
PFX = "githubActivity";
GITHUB = 'https://github.com';
GITHUB_API = 'https://api.github.com';
getRepoEventsData = function(repo, cb) {
if (cb == null) {
cb = (function() {});
}
return $.getJSON("" + GITHUB_API + "/repos/" + repo + "/events?callback=?", null, cb);
};
makeLastManInCheck = function(gaObj, repo, kind) {
var lmic;
lmic = function(data, textStatus, jqXHR) {
gaObj.registerData(repo, kind, data.data);
if (gaObj.ready()) {
return gaObj.go();
}
};
return lmic;
};
util = {
truncate: function(s, l) {
var o;
if (l == null) {
l = 50;
}
o = s.slice(0, l);
if (s.length > l) {
o += "…";
}
return o;
},
humanTime: function(date) {
var days, del, hours, mins, now, secs;
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: function(string) {
var d, date, offset, out, regexp, time;
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);
if (d[3]) {
date.setMonth(d[3] - 1);
}
if (d[5]) {
date.setDate(d[5]);
}
if (d[7]) {
date.setHours(d[7]);
}
if (d[8]) {
date.setMinutes(d[8]);
}
if (d[10]) {
date.setSeconds(d[10]);
}
if (d[12]) {
date.setMilliseconds(Number("0." + d[12]) * 1000);
}
if (d[14]) {
offset = (Number(d[16]) * 60) + Number(d[17]);
offset *= (d[15] === "-" ? 1 : -1);
}
offset -= date.getTimezoneOffset();
time = Number(date) + (offset * 60 * 1000);
out = new Date();
out.setTime(Number(time));
return out;
}
};
eventHelpers = {
who: function(ev) {
return "<img src='http://www.gravatar.com/avatar/" + ev.actor.gravatar_id + "?s=20'>\n<a href='" + GITHUB + "/" + ev.actor.login + "'>" + ev.actor.login + "</a>";
},
repoUrl: function(ev) {
return "" + GITHUB + "/" + ev.repo.name;
},
repo: function(ev) {
return "<a href='" + (eventHelpers.repoUrl(ev)) + "'>" + ev.repo.name + "</a>";
},
titleForDefault: function(ev) {
return "did something";
},
titleForIssuesEvent: function(ev) {
return "" + ev.payload.action + " an issue on " + (eventHelpers.repo(ev));
},
titleForWatchEvent: function(ev) {
return "" + ev.payload.action + " watching " + (eventHelpers.repo(ev));
},
titleForForkEvent: function(ev) {
return "forked " + (eventHelpers.repo(ev));
},
titleForPushEvent: function(ev) {
var branch;
branch = ev.payload.ref.split('/')[2];
if (branch) {
return "pushed to <strong>" + branch + "</strong> on " + (eventHelpers.repo(ev));
} else {
return "pushed to " + (eventHelpers.repo(ev));
}
},
titleForIssueCommentEvent: function(ev) {
return "commented on\n<a href='" + ev.payload.issue.html_url + "\#issuecomment-" + ev.payload.comment.id + "'>\n issue " + ev.payload.issue.number + "</a>\non " + (eventHelpers.repo(ev));
},
title: function(ev) {
var eventWhen, t;
t = eventHelpers["titleFor" + ev.type] || eventHelpers.titleForDefault;
eventWhen = util.humanTime(util.parseISO8601(ev.created_at));
return "<span class='when'>" + eventWhen + "</span>\n<span class='what'>\n " + (eventHelpers.who(ev)) + "\n " + (t(ev)) + "\n</span>";
},
detailsForDefault: function(ev) {
return "More " + ev.type + " details here …";
},
detailsForForkEvent: function(ev) {
return "→ <a href='" + ev.payload.forkee.html_url + "'>" + ev.payload.forkee.html_url + "</a>";
},
detailsForPushEvent: function(ev) {
var c, maxCommits, o, _i, _len, _ref;
maxCommits = 3;
o = [];
_ref = ev.payload.commits.slice(0, maxCommits);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
c = _ref[_i];
o.push("<a href='" + (eventHelpers.repoUrl(ev)) + "/commit/" + c.sha + "'>" + c.sha.slice(0, 7) + "</a>: " + (util.truncate(c.message)));
}
if (ev.payload.commits.length > maxCommits) {
o.push("and " + (ev.payload.commits.length - maxCommits) + " more…");
}
return o.join("<br>");
},
detailsForIssuesEvent: function(ev) {
return "<a href='" + ev.payload.issue.html_url + "'>Issue " + ev.payload.issue.number + "</a>:\n" + (util.truncate(ev.payload.issue.title));
},
detailsForIssueCommentEvent: function(ev) {
return util.truncate(ev.payload.comment.body);
},
details: function(ev) {
var d;
d = eventHelpers["detailsFor" + ev.type] || eventHelpers.detailsForDefault;
return "" + (d(ev));
},
render: function(ev) {
return "<div class='event'>\n <p class='title'>" + (eventHelpers.title(ev)) + "</p>\n <p class='details'>" + (eventHelpers.details(ev)) + "</p>\n</div>";
}
};
GithubActivity = (function() {
function GithubActivity(element, repos, options) {
var r, _i, _len, _ref;
this.element = element;
this.repos = repos;
this.options = options;
this.data = {
repoEvents: {}
};
$(this.element).addClass('github-activity').addClass('loading');
this.wrapper = $("<div class='events-wrapper'></div>").appendTo(this.element)[0];
_ref = this.repos;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
r = _ref[_i];
getRepoEventsData(r, makeLastManInCheck(this, r, 'repoEvents'));
}
}
GithubActivity.prototype.registerData = function(repo, kind, data) {
return this.data[kind][repo] = data;
};
GithubActivity.prototype.ready = function() {
var exp, got, k, v;
got = ((function() {
var _ref, _results;
_ref = this.data['repoEvents'];
_results = [];
for (k in _ref) {
v = _ref[k];
_results.push(k);
}
return _results;
}).call(this)).sort();
exp = this.repos.sort();
if (got > exp || exp > got) {
return false;
}
return true;
};
GithubActivity.prototype.go = function() {
var r, _i, _len, _ref;
this.allEvents = [];
_ref = this.repos;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
r = _ref[_i];
this.allEvents = this.allEvents.concat(this.data.repoEvents[r]);
}
this.allEvents = _.sortBy(this.allEvents, function(e) {
return e.created_at;
}).reverse();
$(this.element).removeClass('loading');
return this.drawEvents(this.allEvents.slice(0, this.options.events || 10));
};
GithubActivity.prototype.drawEvents = function(events) {
var e, _i, _len, _results;
_results = [];
for (_i = 0, _len = events.length; _i < _len; _i++) {
e = events[_i];
_results.push($(eventHelpers.render(e)).appendTo(this.wrapper));
}
return _results;
};
return GithubActivity;
})();
jQuery.fn.githubActivity = function(options) {
if (!options['repos']) {
console.error("GithubActivity plugin needs 'repos' key!");
return this;
}
new GithubActivity(this, options['repos'], options);
return this;
};
}).call(this);