summaryrefslogtreecommitdiffstats
path: root/extensions/MozReview/web/js/mozreview.js
blob: 22ef4c5f1c91cbca9697b2f109d098f95f286274 (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
/* 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.
 */

var MozReview = {};

MozReview.getReviewRequest = function() {
    var hostUrl = $('.mozreview-requests').data('mozreviewUrl');
    var tr = $('<tr/>');
    var td = $('<td/>');
    var link = $('<a/>');

    var rrSummaryApiUrl = hostUrl +
        'api/extensions/mozreview.extension.MozReviewExtension/summary/?bug=' +
        BUGZILLA.bug_id;
    var rrUiBaseUrl = hostUrl + 'r/';

    function rrUrl(rrId) {
        return rrUiBaseUrl + rrId + '/';
    }

    function rrDiffUrl(rrId) {
        return rrUrl(rrId) + 'diff/#index_header';
    }

    function rrCommitRow(rr, firstCommit) {
        var trCommit = tr.clone();
        var tdSubmitter = td.clone();
        var tdRev = td.clone();
        var tdSummary = td.clone();
        var diffLink = link.clone();
        var reviewLink = link.clone();

        if (firstCommit) {
            tdSubmitter.text(rr.submitter);
        }

        diffLink.attr('href', rrDiffUrl(rr.id));
        diffLink.text(rr.commit.substr(0, 12));
        diffLink.addClass('mozreview-diff-link');
        tdRev.append(diffLink);

        tdSummary.addClass('mozreview-summary');
        reviewLink.attr('href', rrUrl(rr.id));
        reviewLink.text(rr.summary);
        tdSummary.append(reviewLink);

        trCommit.append(
            tdSubmitter,
            tdRev,
            tdSummary,
            td.clone().text(rr.issue_open_count)
                      .addClass('mozreview-open-issues'),
            td.clone().text(timeAgo(new Date(rr.last_updated)))
        );

        if (rr.status == "discarded") {
            $('.mozreview-hide-discarded-row').removeClass('bz_default_hidden');
            trCommit.addClass('bz_default_hidden mozreview-discarded-request');
        }

        return trCommit;
    }

    $('.mozreview-hide-discarded-link').click(function(event) {
        event.preventDefault();
        if ($('.bz_default_hidden.mozreview-discarded-request').length) {
            $('.mozreview-discarded-request').removeClass('bz_default_hidden');
            $('.mozreview-discarded-action').text('Hide');
        } else {
            $('.mozreview-discarded-request').addClass('bz_default_hidden');
            $('.mozreview-discarded-action').text('Show');
        }
    });

    var tbody = $('tbody.mozreview-request');

    function displayLoadError(errStr) {
        var errRow = tbody.find('.mozreview-loading-error-row');
        errRow.find('.mozreview-load-error-string').text(errStr);
        errRow.removeClass('bz_default_hidden');
    }

    $.getJSON(rrSummaryApiUrl, function(data) {
        var family, parent, i, j;

        if (data.review_request_summaries.length === 0) {
            displayLoadError('none returned from server');
        } else {
            for (i = 0; i < data.review_request_summaries.length; i++) {
                family = data.review_request_summaries[i];
                for (j = 0; j < family.children.length; j++) {
                    tbody.append(rrCommitRow(family.children[j], j == 0));
                }
            }
        }

        tbody.find('.mozreview-loading-row').addClass('bz_default_hidden');
    }).fail(function(jqXHR, textStatus, errorThrown) {
        var errStr;
        if (jqXHR.responseJSON && jqXHR.responseJSON.err &&
            jqXHR.responseJSON.err.msg) {
            errStr = jqXHR.responseJSON.err.msg;
        } else if (errorThrown) {
            errStr = errorThrown;
        } else {
            errStr = 'unknown';
        }
        displayLoadError(errStr);
        tbody.find('.mozreview-loading-row').addClass('bz_default_hidden');
    });
};

$().ready(function() {
    MozReview.getReviewRequest();
});