-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent_script.js
331 lines (268 loc) · 8.86 KB
/
content_script.js
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
var reviewbuddy = {};
/*****************************************************************************
* Configuration
*****************************************************************************/
reviewbuddy.getCurrentPageBaseUrl = function () {
var parts = window.location.href.split("/");
if(parts.length >= 3) {
return "http://" + parts[2];
}
return "";
}
reviewbuddy.config = {};
reviewbuddy.config.crucible = {};
reviewbuddy.config.crucible.baseUrl = reviewbuddy.getCurrentPageBaseUrl();
reviewbuddy.config.crucible.reviewBaseUrl = reviewbuddy.config.crucible.baseUrl + "/cru";
reviewbuddy.config.crucible.jsonApi = reviewbuddy.config.crucible.baseUrl + "/json/cru";
reviewbuddy.config.crucible.createReviewUrl = reviewbuddy.config.crucible.jsonApi + "/createReviewDialog";
reviewbuddy.config.crucible.changesetAdd = "/editRevisionsAjax/";
/*****************************************************************************
* Global values. ugh
*****************************************************************************/
reviewbuddy.global = {};
/**
* The number of changesets added to the review.
*/
reviewbuddy.global.changesetsAdded = 0;
/**
* The number of changesets being added to the review.
*/
reviewbuddy.global.changesetsNum = 0;
/**
* Reset the global to no changesets added, with the given number
* of changesets still to be added.
*/
reviewbuddy.global.resetState = function(changesetsNum) {
reviewbuddy.global.changesetsAdded = 0;
reviewbuddy.global.changesetsNum = changesetsNum;
}
/*****************************************************************************
* Options
*****************************************************************************/
reviewbuddy.options = {};
/**
* Returns the option value. If the option doesn't exist or has no value,
* returns an empty string.
*/
reviewbuddy.options.fetchSingleOption = function(optionId, callback) {
chrome.extension.sendRequest({optionId: "projectKey"}, function(response) {
callback(optionId, response[optionId]);
});
}
/**
* Loads all our options into variables.
*/
reviewbuddy.options.fetchOptions = function() {
reviewbuddy.options.fetchSingleOption("projectKey", reviewbuddy.options.storeOption);
}
reviewbuddy.options.storeOption = function(optionId, optionValue) {
if(optionValue) {
localStorage[optionId] = optionValue;
}
}
reviewbuddy.options.getOptionValue = function(optionId) {
return localStorage[optionId];
}
/*****************************************************************************
* Implementation
*****************************************************************************/
reviewbuddy.startReviewCreation = function() {
var changesets = reviewbuddy.getAllChangesets();
if(0 == changesets) {
alert("Cannot create review. No changesets on page.");
return;
}
reviewbuddy.options.fetchOptions();
reviewbuddy.createReview(function(reviewId) { reviewbuddy.addChangesetsToReview(changesets, reviewId); });
}
/**
* Given a link, return the changeset id to which it corresponds.
*/
reviewbuddy.changesetFromLink = function(link) {
if(null != link) {
return link.innerText;
}
// Return null instead of an empty string to make it easier to use with $.map
return null;
}
reviewbuddy.changesetFromInput = function(input) {
if(null != input) {
return input.value;
}
return null;
}
/**
* Get changesets identified by a link's CSS class.
*
* changesetSelector - A string representing a CSS selector
* changesetIdExtrator - A function that take an element selected
* by the selector and returns the changeset id.
*/
reviewbuddy.getChangesets = function(changesetSelector, changesetIdExtrator) {
var changesetLinks = $(changesetSelector);
if((null == changesetLinks) || (0 == changesetLinks.length)) {
return [];
}
return $.map(changesetLinks, function(elt, idx) {
return changesetIdExtrator(elt);
});
}
/**
* Returns all the changesets in the page.
*/
reviewbuddy.getAllChangesets = function() {
// We need to try different approaches depending on the version of Fisheye.
var changesets = reviewbuddy.getChangesets("a.changeset-link", reviewbuddy.changesetFromLink);
if(0 == changesets.length) {
changesets = reviewbuddy.getChangesets("input.csid", reviewbuddy.changesetFromInput);
}
return changesets;
}
/**
* Creates a review in Crucible.
*
* Calls the callback, passing in the review id (if successful), or an empty string (on failure).
*/
reviewbuddy.createReview = function(callback) {
var message = reviewbuddy.appendOptionValueToMessage("", "projectKey");
$.post(reviewbuddy.config.crucible.createReviewUrl, message, function(data) { reviewbuddy.onReviewCreated(data, callback); });
}
/**
* Given the data returned by Crucible, returns true if the review was created
* successfully, false otherwise.
*/
reviewbuddy.wasReviewCreated = function(data) {
if(!data) {
return false;
}
try {
var json = $.parseJSON(data);
} catch(ex) {
return false;
}
return true;
}
/**
* Parse the review creation response from Crucible for the review id.
*/
reviewbuddy.parseReviewId = function(data) {
// The expected format for payload is /cru/REVIEWID
var parts = data.payload.split("/");
return parts[parts.length - 1];
}
/**
* Handle review creation response from Crucible.
*/
reviewbuddy.onReviewCreated = function(data, callback) {
if(!reviewbuddy.wasReviewCreated(data)) {
callback(""); // pass empty string on failure
}
callback(reviewbuddy.parseReviewId(data));
}
/**
* Given a review id, returns the URL that should be used to edit
* the changesets that are part of that review.
*
* Returns an empty string if review id is null or empty.
*/
reviewbuddy.createChangesetEditUrl = function(reviewId) {
if(!reviewId) {
return "";
}
return reviewbuddy.config.crucible.jsonApi + "/" + reviewId + reviewbuddy.config.crucible.changesetAdd;
}
/**
* Creates the data that we can send to Crucible in order to add a changeset
* to a review.
*/
reviewbuddy.createChangesetAddData = function(changesetId, sourceName) {
return "csid=" + changesetId + "&command=add&attachMethod=ITERATION&sourceName=" + sourceName;
}
/**
* Adds the value of the specified option to the given message and returns it.
* If the option doesn't exist or doesn't have a value, the message
* is returned unchanged.
*/
reviewbuddy.appendOptionValueToMessage = function(message, optionId) {
var optionValue = reviewbuddy.options.getOptionValue(optionId);
if(optionValue && (undefined != optionValue)) {
var newMessage = message;
if(message) {
newMessage = newMessage + "&";
}
return newMessage + optionId + "=" + optionValue;
}
return message;
}
/**
* Adds a single changeset to a review.
*/
reviewbuddy.addChangesetToReview = function(changesetEditUrl, changesetId, reviewId, sourceName) {
var data = reviewbuddy.createChangesetAddData(changesetId, sourceName);
var callback = function(data) { reviewbuddy.onChangesetAdded(data, reviewId); };
var xhr = $.post(changesetEditUrl, data, callback);
xhr.error(callback);
}
reviewbuddy.onChangesetAdded = function(data, reviewId) {
reviewbuddy.global.changesetsAdded++;
if(reviewbuddy.global.changesetsAdded >= reviewbuddy.global.changesetsNum) {
reviewbuddy.onAllChangesetsAdded(reviewId);
}
}
reviewbuddy.createReviewUrl = function(reviewId) {
return reviewbuddy.config.crucible.reviewBaseUrl + "/" + reviewId;
}
reviewbuddy.redirectToReviewUrl = function(reviewId) {
window.location.replace(reviewbuddy.createReviewUrl(reviewId));
}
/**
* Called after all changesets have been added to a review.
*/
reviewbuddy.onAllChangesetsAdded = function(reviewId) {
reviewbuddy.closePopup();
reviewbuddy.redirectToReviewUrl(reviewId);
}
reviewbuddy.closePopup = function() {
chrome.extension.sendRequest({popupClose: true}, function(response){});
}
/**
* Try to get the source name from the URL.
*/
reviewbuddy.getSourceNameFromUrl = function() {
var parts = window.location.href.split("/");
if(null == parts || 0 == parts.length) {
return "";
}
// Handle case that URL ends with a slash
var page = parts[parts.length - 1] || parts[parts.length - 2];
if(!page) {
return "";
}
// Handle case that URL doesn't have a question mark
return page.substring(0, page.indexOf("?")) || page;
}
/**
* Get the source name from the page. I think this is like a project name.
*/
reviewbuddy.getSourceName = function() {
return reviewbuddy.getSourceNameFromUrl();
}
/**
* Adds an array of changesets to a review.
*/
reviewbuddy.addChangesetsToReview = function(changesets, reviewId) {
if(!reviewId || !$.isArray(changesets)) {
return;
}
var sourceName = reviewbuddy.getSourceName();
if(!sourceName) {
alert("Couldn't find project name.");
return;
}
var changesetEditUrl = reviewbuddy.createChangesetEditUrl(reviewId);
reviewbuddy.global.resetState(changesets.length);
for(ii in changesets) {
reviewbuddy.addChangesetToReview(changesetEditUrl, changesets[ii], reviewId, sourceName);
}
}
reviewbuddy.startReviewCreation();