-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtracker.js
More file actions
273 lines (231 loc) · 9.09 KB
/
tracker.js
File metadata and controls
273 lines (231 loc) · 9.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// ==UserScript==
// This script works on the PR page. Uses Parse to store data.
// @match https://github.com/*
// ==/UserScript==
// Other file forwarders
var Parse;
var waitForKeyElements;
var unresolvedCommentsMap = {};
var findAllThreads = function () {
var threads = [];
$('.timeline-comment-wrapper .js-comments-holder').each(function () {
var childComments = $(this).find('.review-comment[id]');
if (childComments.length > 0) {
var firstCommentChild = childComments.first()[0];
threads.push({
id: firstCommentChild.id,
comments: childComments,
lastCommentId: childComments.last()[0].id,
});
}
});
$('.timeline-comment-group').each(function () {
if (this.id && this.id.match(/^issuecomment/)) {
threads.push({
id: this.id,
comments: $(this),
lastCommentId: this.id,
});
}
});
$('#files .review-comment').each(function () {
threads.push({
id: this.id,
comments: $(this),
lastCommentId: this.id,
});
});
return threads;
};
var checkThreads = function () {
var newThreads = findAllThreads();
if (_.isEqual(_.pluck(newThreads, 'id'), _.pluck(allThreads, 'id'))) {
if (_.isEqual(_.pluck(newThreads, 'lastCommentId'), _.pluck(allThreads, 'lastCommentId'))) {
return;
}
}
resetManipulations();
};
var resetManipulations = function () {
allThreads = findAllThreads();
unresolvedCommentsMap = {};
annotateWithParseInfo(allThreads).then(function () {
_.each(allThreads, function (info) { updateThread(info, {suppressMergeUpdate: true}); });
}).then(function () {
expandUnresolvedThreads();
updateMergeButton();
});
};
var CommentTracker;
var Settings;
var appSettings;
var main = function () {
/* global chrome */
chrome.storage.sync.get({
polling: true
}, function (items) {
Parse.initialize("ghct");
Parse.serverURL = 'https://ghct.herokuapp.com/1';
CommentTracker = Parse.Object.extend('CommentTracker');
Settings = Parse.Object.extend('Settings');
resetManipulations();
// waitForKeyElements will trigger for *each* changed/added element.
// Debounce both to only call checkThreads once, and to call with a slight
// delay for better compatiblity with the WideGithub extension:
// https://chrome.google.com/webstore/detail/wide-github/kaalofacklcidaampbokdplbklpeldpj
var debouncedCheckThreads = _.debounce(checkThreads, 100);
waitForKeyElements('.comment', debouncedCheckThreads);
if (items.polling) {
new Parse.Query(Settings).get("bdWmF0aC6c").then(function (settings) {
appSettings = settings;
setInterval(resetManipulations, appSettings.get('pollInterval'));
});
}
});
};
var expandUnresolvedThreads = function () {
_.each(allThreads, function (info) {
if (!info.resolved) {
var id = info.id;
var elem = $('#' + id).first();
var container = elem.parents('.outdated-comment');
if (container.length > 0) {
container.attr('open', '1')
}
}
});
};
var allThreads;
var initalCanBeMerged = false;
var allThreadsResolved = function () {
return _.all(allThreads, function (info) {
return info.resolved;
});
};
var updateMergeButton = function () {
if (!initalCanBeMerged) {
initalCanBeMerged = $('.js-merge-branch-action').hasClass('btn-primary');
}
$('.comment-track-status').remove();
if (initalCanBeMerged) {
if (allThreadsResolved()) {
// Make button green
$('.js-merge-branch-action').addClass('btn-primary');
$('.branch-action').addClass('branch-action-state-clean').removeClass('branch-action-state-dirty');
$('.status-heading').text('This pull request can be automatically merged.');
$('.status-meta').text('Merging can be performed automatically.');
$('.branch-action-item-icon').removeClass('completeness-indicator-problem').addClass('completeness-indicator-success').html('<svg aria-hidden="true" class="octicon octicon-alert" height="16" role="img" version="1.1" viewBox="0 0 12 16" width="12"><path d="M12 5L4 13 0 9l1.5-1.5 2.5 2.5 6.5-6.5 1.5 1.5z"></path></svg>');
} else {
// Make button grey
$('.js-merge-branch-action').removeClass('btn-primary');
$('.branch-action').removeClass('branch-action-state-clean').addClass('branch-action-state-dirty');
$('.status-heading').text('Merge with caution!');
$('.status-meta').text('You have unresolved comments!');
$('.branch-action-item-icon').removeClass('completeness-indicator-success').addClass('completeness-indicator-problem').html('<svg aria-hidden="true" class="octicon octicon-alert" height="16" role="img" version="1.1" viewBox="0 0 16 16" width="16"><path d="M15.72 12.5l-6.85-11.98C8.69 0.21 8.36 0.02 8 0.02s-0.69 0.19-0.87 0.5l-6.85 11.98c-0.18 0.31-0.18 0.69 0 1C0.47 13.81 0.8 14 1.15 14h13.7c0.36 0 0.69-0.19 0.86-0.5S15.89 12.81 15.72 12.5zM9 12H7V10h2V12zM9 9H7V5h2V9z"></path></svg>');
}
} else {
if (!allThreadsResolved()) {
//map to array of values
var unresolvedCommentsArray = []
for (var key in unresolvedCommentsMap) {
if (unresolvedCommentsMap.hasOwnProperty(key)) {
unresolvedCommentsArray.push(unresolvedCommentsMap[key])
}
}
var commentStatus =
'<div class="branch-action-item comment-track-status">' +
' <div class="branch-action-item-icon completeness-indicator completeness-indicator-problem">' +
' <svg aria-hidden="true" class="octicon octicon-alert" height="16" role="img" version="1.1" viewBox="0 0 16 16" width="16"><path d="M15.72 12.5l-6.85-11.98C8.69 0.21 8.36 0.02 8 0.02s-0.69 0.19-0.87 0.5l-6.85 11.98c-0.18 0.31-0.18 0.69 0 1C0.47 13.81 0.8 14 1.15 14h13.7c0.36 0 0.69-0.19 0.86-0.5S15.89 12.81 15.72 12.5zM9 12H7V10h2V12zM9 9H7V5h2V9z"></path></svg>' +
' </div>' +
' <h4 class="status-heading">This branch has unresolved comments</h4>' +
' <span class="status-meta">' +
' See above for red unresolved comments' +
' </span>' +
' <lu><li>' + unresolvedCommentsArray.join('</li><li>') + "</li></lu>" +
' </div>';
//discussions tab
$('.merge-message').before(commentStatus);
//files tab
$('#files').after(commentStatus);
}
}
};
var annotateWithParseInfo = function (allThreads) {
var ids = _.pluck(allThreads, 'id');
var query = new Parse.Query(CommentTracker);
query.containedIn('commentId', ids);
return query.find().then(function (results) {
_.each(results, function (result) {
var id = result.get('commentId');
var info = _.findWhere(allThreads, {id: id});
if (info) {
info.resolved = result.get('resolved') && result.get('lastCommentSeen') === info.lastCommentId;
info.lastCommentSeen = result.get('lastCommentSeen');
info.tracker = result;
}
});
});
};
var makeButton = function (elem, threadInfo) {
var $elem = $(elem);
$elem.find('.comment-track-action').remove();
var actionSelector = '.unminimized-comment .review-comment-contents';
if ($elem.find(actionSelector).length === 0) {
actionSelector = '.unminimized-comment .timeline-comment-actions';
}
delete unresolvedCommentsMap[threadInfo.id];
var string;
if (threadInfo.resolved) {
string = '<span class="octicon comment-track-action comment-track-unresolve"></span>';
$elem.find(actionSelector).prepend(string);
$elem.find('.comment-track-unresolve').on('click', function (event) {
event.preventDefault();
var tracker = threadInfo.tracker;
tracker.set('resolved', false);
tracker.set('lastCommentSeen', null);
tracker.save();
threadInfo.resolved = false;
updateThread(threadInfo);
});
} else {
string = '<span class="octicon comment-track-action comment-track-resolve"></span>';
$elem.find(actionSelector).prepend(string);
var content = $elem.find(actionSelector)[0].innerText.trim();
if (content === '') {
// If we didn't find any content on the actionSelect, its probably a
// timeline-comment and the content is in the comment-body
content = $elem.find('.comment-body')[0].innerText.trim();
}
if (content != '') {
unresolvedCommentsMap[threadInfo.id] = content;
}
$elem.find('.comment-track-resolve').on('click', function (event) {
event.preventDefault();
var tracker = threadInfo.tracker || new CommentTracker();
tracker.set('commentId', threadInfo.id);
tracker.set('resolved', true);
tracker.set('lastCommentSeen', threadInfo.lastCommentId);
tracker.save();
threadInfo.resolved = true;
threadInfo.tracker = tracker;
updateThread(threadInfo);
});
}
};
var updateThread = function (info, options) {
options = options || {};
var id = info.id;
var elem = $('#' + id).first();
if (id.match(/^issuecomment/)) {
makeButton(elem, info);
} else {
var threadComments = $(elem).parents('.js-comments-holder').find('.review-comment[id]');
threadComments.each(function () {
makeButton(this, info);
});
}
if (!options.suppressMergeUpdate) {
updateMergeButton();
}
};
main();