summaryrefslogtreecommitdiffstats
path: root/extensions/Review/web/js/review_history.js
blob: 8d016b00993815ead80f8af2d68e7a1b120760f3 (plain)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, v. 2.0. */

(function () {
    'use strict';

    YUI.add('bz-review-history', function (Y) {
        function format_duration(o) {
            if (o.value) {
                if (o.value < 0) {
                    return "???";
                } else {
                    return moment.duration(o.value).humanize();
                }
            }
            else {
                return "---";
            }
        }

        function format_attachment(o) {
            if (o.value) {
                return o.value.description;
            }
        }

        function format_action(o) {
            return o.value;
        }

        function format_setter(o) {
            return o.value.real_name ? o.value.real_name + " <" + o.value.name + ">" : o.value.name;
        }

        function format_date(o) {
            return o.value && Y.DataType.Date.format(o.value, {
                format: "%Y-%m-%d"
            });
        }

        function parse_date(str) {
            var parts = str.split(/\D/);
            return new Date(parts[0], parts[1] - 1, parts[2], parts[3], parts[4], parts[5]);
        }

        var flagDS, bugDS, attachmentDS, historyTable;
        flagDS = new Y.DataSource.IO({ source: 'jsonrpc.cgi' });
        flagDS.plug(Y.Plugin.DataSourceJSONSchema, {
            schema: {
                resultListLocator: 'result',
                resultFields: [
                    { key: 'id' },
                    { key: 'requestee' },
                    { key: 'setter' },
                    { key: 'flag_id' },
                    { key: 'creation_time' },
                    { key: 'status' },
                    { key: 'bug_id' },
                    { key: 'type' },
                    { key: 'attachment_id' }
                ]
            }
        });

        bugDS = new Y.DataSource.IO({ source: 'jsonrpc.cgi' });
        bugDS.plug(Y.Plugin.DataSourceJSONSchema, {
            schema: {
                resultListLocator: 'result.bugs',
                resultFields: [
                    { key: 'id' },
                    { key: 'summary' }
                ]
            }
        });

        attachmentDS = new Y.DataSource.IO({ source: 'jsonrpc.cgi' });
        attachmentDS.plug(Y.Plugin.DataSourceJSONSchema, {
            schema: {
                metaFields: { 'attachments': 'result.attachments' }
            }
        });

        historyTable = new Y.DataTable({
            columns: [
                { key: 'creation_time', label: 'Created', sortable: true, formatter: format_date },
                { key: 'attachment', label: 'Attachment', formatter: format_attachment },
                { key: 'setter', label: 'Requester', formatter: format_setter },
                { key: "action", label: "Action", sortable: true,  formatter: format_action },
                { key: "duration", label: "Duration", sortable: true, formatter: format_duration },
                { key: "bug_id", label: "Bug", sortable: true, allowHTML: true,
                  formatter: '<a href="show_bug.cgi?id={value}" target="_blank">{value}</a>' },
                { key: 'bug_summary', label: 'Summary' }
            ]
        });

        function fetch_flag_ids(user) {
            return new Y.Promise(function (resolve, reject) {
                var flagIdCallback = {
                    success: function (e) {
                        var flags = e.response.results;
                        var flag_ids = flags.filter(function (flag) {
                            return flag.status == '?';
                        })
                        .map(function (flag) {
                            return flag.flag_id;
                        });

                        if (flag_ids.length > 0) {
                            resolve(flag_ids);
                        } else {
                            reject("No reviews found");
                        }
                    },
                    failure: function (e) {
                        reject(e.error.message);
                    }
                };

                flagDS.sendRequest({
                    request: Y.JSON.stringify({
                        version: '1.1',
                        method: 'Review.flag_activity',
                        params: {
                            type_name: 'review',
                            requestee: user,
                            include_fields: ['flag_id', 'status'],
                            Bugzilla_api_token : (BUGZILLA.api_token ? BUGZILLA.api_token : '')
                        }
                    }),
                    cfg: {
                        method: "POST",
                        headers: { 'Content-Type': 'application/json' }
                    },
                    callback: flagIdCallback
                });
            });
        }

        function fetch_flags(flag_ids) {
            return new Y.Promise(function (resolve, reject) {
                flagDS.sendRequest({
                    request: Y.JSON.stringify({
                        version: '1.1',
                        method: 'Review.flag_activity',
                        params: {
                            flag_ids: flag_ids,
                            Bugzilla_api_token: (BUGZILLA.api_token ? BUGZILLA.api_token : '')
                        }
                    }),
                    cfg: {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/json' }
                    },
                    callback: {
                        success: function (e) {
                            var flags = e.response.results;
                            flags.forEach(function(flag) {
                                flag.creation_time = parse_date(flag.creation_time);
                            });
                            resolve(flags.sort(function (a, b) {
                                if (a.id > b.id) return 1;
                                if (a.id < b.id) return -1;
                                return 0;
                            }));
                        },
                        failure: function (e) {
                            reject(e.error.message);
                        }
                    }
                });
            });
        }

        function fetch_bug_summaries(flags) {
            return new Y.Promise(function (resolve, reject) {
                var bug_ids = Y.Array.dedupe(flags.map(function (f) {
                    return f.bug_id;
                }));

                bugDS.sendRequest({
                    request: Y.JSON.stringify({
                        version: '1.1',
                        method: 'Bug.get',
                        params: {
                            ids: bug_ids,
                            include_fields: ['summary', 'id'],
                            Bugzilla_api_token: (BUGZILLA.api_token ? BUGZILLA.api_token : '')
                        }
                    }),
                    cfg: {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/json' }
                    },
                    callback: {
                        success: function (e) {
                            var bugs    = e.response.results,
                                summary = {};

                            bugs.forEach(function (bug) {
                                summary[bug.id] = bug.summary;
                            });
                            flags.forEach(function (flag) {
                                flag.bug_summary = summary[flag.bug_id];
                            });
                            resolve(flags);
                        },
                        failure: function (e) {
                            reject(e.error.message);
                        }
                    }
                });
            });
        }

        function fetch_attachment_descriptions(flags) {
            return new Y.Promise(function (resolve, reject) {
                var attachment_ids = Y.Array.dedupe(flags.map(function (f) {
                    return f.attachment_id;
                }));

                attachmentDS.sendRequest({
                    request: Y.JSON.stringify({
                        version: '1.1',
                        method: 'Bug.attachments',
                        params: {
                            attachment_ids: attachment_ids,
                            include_fields: ['id', 'description'],
                            Bugzilla_api_token : (BUGZILLA.api_token ? BUGZILLA.api_token : '')
                        }
                    }),
                    cfg: {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/json' }
                    },
                    callback: {
                        success: function (e) {
                            var attachments = e.response.meta.attachments;
                            flags.forEach(function (flag) {
                                flag.attachment = attachments[flag.attachment_id];
                            });
                            resolve(flags);
                        },
                        failure: function (e) {
                            reject(e.error.message);
                        }
                    }
                });
            });
        }

        function add_historical_action(history, flag, stash, action) {
            history.push({
                attachment:    flag.attachment,
                bug_id:        flag.bug_id,
                bug_summary:   flag.bug_summary,
                creation_time: stash.creation_time,
                duration:      flag.creation_time - stash.creation_time,
                setter:        stash.setter,
                action:        action
            });
        }

        function generate_history(flags, user) {
            var history = [],
                stash   = {},
                flag, stash_key ;

            flags.forEach(function (flag) {
                var flag_id = flag.flag_id;

                switch (flag.status) {
                    case '?':
                        // If we get a ? after a + or -, we get a fresh start.
                        if (stash[flag_id] && stash[flag_id].is_complete)
                            delete stash[flag_id];

                        // handle untargeted review requests.
                        if (!flag.requestee)
                            flag.requestee = { id: 'the wind', name: 'the wind' };

                        if (stash[flag_id]) {
                            // flag was reassigned
                            if (flag.requestee.id != stash[flag_id].requestee.id) {
                                // if ? started out mine, but went to someone else.
                                if (stash[flag_id].requestee.name == user) {
                                    add_historical_action(history, flag, stash[flag_id], 'reassigned to ' + flag.requestee.name);
                                    stash[flag_id] = flag;
                                }
                                else {
                                    // flag changed hands. Reset the creation_time and requestee
                                    stash[flag_id].creation_time = flag.creation_time;
                                    stash[flag_id].requestee     = flag.requestee;
                                }
                            }
                        } else {
                            stash[flag_id] = flag;
                        }
                        break;

                    case 'X':
                        if (stash[flag_id]) {
                            // Only process if we did not get a + or a - since
                            if (!stash[flag_id].is_complete) {
                                add_historical_action(history, flag, stash[flag_id], 'cancelled');
                            }
                            delete stash[flag_id];
                        }
                        break;


                    case '+':
                    case '-':
                        // if we get a + or -, we only accept it if the requestee is the user we're interested in.
                        // we set is_complete to handle cancelations.
                        if (stash[flag_id] && stash[flag_id].requestee.name == user) {
                            add_historical_action(history, flag, stash[flag_id], "review" + flag.status);
                            stash[flag_id].is_complete = true;
                        }
                        break;
                }
            });

            for (stash_key in stash) {
                flag = stash[stash_key];
                if (flag.is_complete) continue;
                if (flag.requestee.name != user) continue;
                history.push({
                    attachment:    flag.attachment,
                    bug_id:        flag.bug_id,
                    bug_summary:   flag.bug_summary,
                    creation_time: flag.creation_time,
                    duration:      new Date() - flag.creation_time,
                    setter:        flag.setter,
                    action:        'review?'
                });
            }

            return history;
        }

        Y.ReviewHistory = {};

        Y.ReviewHistory.render = function (sel) {
            Y.one('#history-loading').hide();
            historyTable.render(sel);
            historyTable.setAttrs({
                width: "100%"
            }, true);
        };

        Y.ReviewHistory.refresh = function (user, real_name) {
            var caption = "Review History for " + (real_name ? real_name + ' &lt;' + user + '&gt;' : user);
            historyTable.setAttrs({
                caption: caption
            });
            historyTable.set('data', null);
            historyTable.showMessage('Loading...');
            fetch_flag_ids(user)
            .then(fetch_flags)
            .then(fetch_bug_summaries)
            .then(fetch_attachment_descriptions)
            .then(function (flags) {
                return new Y.Promise(function (resolve, reject) {
                    try {
                        resolve(generate_history(flags, user));
                    }
                    catch (e) {
                        reject(e.message);
                    }
                });
            })
            .then(function (history) {
                historyTable.set('data', history);
                historyTable.sort({
                    creation_time: 'desc'
                });
            }, function (message) {
                historyTable.showMessage(message);
            });
        };

    }, '0.0.1', {
        requires: [
            "node", "datatype-date", "datatable", "datatable-sort", "datatable-message", "json-stringify",
            "datatable-datasource", "datasource-io", "datasource-jsonschema", "cookie",
            "gallery-datatable-row-expansion-bmo", "handlebars", "escape", "promise"
        ]
    });
}());