~launchpad-pqm/launchpad/devel

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
/* Copyright 2009 Canonical Ltd.  This software is licensed under the
 * GNU Affero General Public License version 3 (see the file LICENSE).
 *
 * Code for handling links to branches from bugs and specs.
 *
 * @module BranchLinks
 * @requires base, lazr.anim, lazr.formoverlay
 */

YUI.add('lp.code.branch.bugspeclinks', function(Y) {

var namespace = Y.namespace('lp.code.branch.bugspeclinks');

var lp_client;          // The LP client

var link_bug_overlay;

var error_handler;

/*
 * Extract the best candidate for a bug number from the branch name.
 */
function extract_candidate_bug_id(branch_name) {
    // Extract all the runs of numbers in the branch name and sort by
    // descending length.
    var chunks = branch_name.split(/\D/g).sort(function (a, b) {
        return b.length - a.length;
    });
    var chunk, i;
    for (i=0; i<chunks.length; i++) {
        chunk = chunks[i];
        // Bugs with fewer than six digits aren't being created any more (by
        // Canonical's LP at least), but there are lots of open five digit
        // bugs so ignore runs of fewer than five digits in the branch name.
        if (chunk.length < 5) {
            break;
        }
        // Bug IDs don't start with a zero.
        if (chunk[0] !== '0') {
            return chunk;
        }
    }
    return null;
}
// Expose in the namespace so we can test it.
namespace._extract_candidate_bug_id = extract_candidate_bug_id;

/*
 * Connect the links to the javascript events.
 */
namespace.connect_branchlinks = function() {

    error_handler = new Y.lp.client.ErrorHandler();
    error_handler.clearProgressUI = function() {
        destroy_temporary_spinner();
    };
    error_handler.showError = function(error_message) {
        alert('An unexpected error has occurred.');
        Y.log(error_message);
    };

    link_bug_overlay = new Y.lazr.FormOverlay({
        headerContent: '<h2>Link to a bug</h2>',
        form_submit_button: Y.Node.create(
            '<button type="submit" name="buglink.actions.change" ' +
            'value="Change" class="lazr-pos lazr-btn">Ok</button>'),
        form_cancel_button: Y.Node.create(
            '<button type="button" name="buglink.actions.cancel" ' +
            'class="lazr-neg lazr-btn">Cancel</button>'),
        centered: true,
        form_submit_callback: link_bug_to_branch,
        visible: false
    });
    link_bug_overlay.render();
    link_bug_overlay.loadFormContentAndRender('+linkbug/++form++');
    var linkbug_handle = Y.one('#linkbug');
    linkbug_handle.addClass('js-action');
    linkbug_handle.on('click', function(e) {
        e.preventDefault();
        link_bug_overlay.show();
        var field = Y.DOM.byId('field.bug');
        field.focus();
        var guessed_bug_id = extract_candidate_bug_id(LP.cache.context.name);
        if (Y.Lang.isValue(guessed_bug_id)) {
            field.value = guessed_bug_id;
            // Select the pre-filled bug number (if any) so that it will be
            // replaced by anything the user types (getting the guessed bug
            // number out of the way quickly if we guessed incorrectly).
            field.selectionStart = 0;
            field.selectionEnd = 999;
        }
    });
    connect_remove_links();
};

/*
 * Connect the remove links of each bug link to the javascript functions to
 * remove the links.
 */
function connect_remove_links() {
    Y.on('click', function(e) {
        e.preventDefault();
        var bugnumber = get_bugnumber_from_id(e.currentTarget.get('id'));
        unlink_bug_from_branch(bugnumber);
    }, '.delete-buglink');
}

/*
 * Link a specified bug to the branch.
 */
function link_bug_to_branch(data) {
    link_bug_overlay.hide();

    create_temporary_spinner();

    var bugnumber = data['field.bug'];
    var existing = Y.one('#buglink-' + bugnumber);
    if (Y.Lang.isValue(existing)) {
        // Bug is already linked, don't do unneccessary requests.
        Y.lazr.anim.green_flash({node: existing}).run();
        return;
    }

    get_bug_from_bugnumber(bugnumber, function(bug) {

        config = {
            on: {
                success: function(entry) {
                    // XXX: rockstar - linkBug still is returning BugBranches.
                    // This means that I'll need to change this once I fix
                    // that.
                    var config = {
                        on: {
                            success: function(bugtasks) {
                                update_bug_links(bug);
                            }
                        }
                    };
                    bug.follow_link('bug_tasks', config);
                },
                failure: error_handler.getFailureHandler()
            },
            parameters: {
                bug: bug.get('self_link')
            }
        };
        set_up_lp_client();
        lp_client.named_post(
            LP.cache.context.self_link, 'linkBug', config);
    });
}

/*
 * Update the list of bug links.
 */
function update_bug_links(bug) {

    BUG_LINK_SNIPPET = '++bug-links';
    Y.io(BUG_LINK_SNIPPET, {
        on: {
            success: function(id, response) {
                destroy_temporary_spinner();
                Y.one('#linkbug')
                    .set('innerHTML', 'Link to another bug report');
                Y.one('#buglink-list')
                    .set('innerHTML', response.responseText);
                var new_buglink = Y.one('#buglink-' + bug.get('id'));
                var anim = Y.lazr.anim.green_flash({node: new_buglink});
                anim.on('end', connect_remove_links);
                anim.run();
            },
            failure: function(id, response) {
                // At least remove the "Linking..." text
                destroy_temporary_spinner();

                alert('Unable to update bug links.');
                Y.log(response);
            }
        }
    });

}

/*
 * Unlink a bug from the branch.
 */
function unlink_bug_from_branch(bugnumber) {
    link_bug_overlay.hide();

    Y.one('#delete-buglink-' + bugnumber).get('children').set(
        'src', '/@@/spinner');
    get_bug_from_bugnumber(bugnumber, function(bug) {

        config = {
            on: {
                success: function(updated_entry) {
                    var element = Y.one('#buglink-' + bugnumber);
                    var parent_element = element.get('parentNode');
                    anim = Y.lazr.anim.red_flash({node: element});
                    anim.on('end', function() {
                        parent_element.removeChild(element);

                        // Check to see if that was the only bug linked.
                        var buglinks = Y.all(".bug-branch-summary");
                        if (!buglinks.size()) {
                            Y.one('#linkbug').set('innerHTML',
                                'Link to a bug report');
                        }
                    });
                    anim.run();
                },
                failure: function(id, response) {
                    alert('An unexpected error has occurred.');
                    Y.one('#delete-buglink-' + bugnumber).get('children').set(
                        'src', '/@@/remove');
                    Y.log(response.responseText);
                }
            },
            parameters: {
                bug: bug.get('self_link')
            }
        };
        set_up_lp_client();
        lp_client.named_post(
            LP.cache.context.self_link, 'unlinkBug', config);
    });
}


/*
 * Get the bugnumber for the element id.
 *
 * Since we control the element id, we don't have to use crazy reqexes or
 * something.
 */
function get_bugnumber_from_id(id) {
    return id.substr('remove-buglink-'.length, id.length);
}

/*
 * Get the bug representation from the bugnumber.
 *
 * XXX: rockstar - There is a better way to do this, I'm sure.  I just need to
 * figure it out after everything else is done.
 */
function get_bug_from_bugnumber(bugnumber, callback) {
    var bug_uri = '/bugs/' + bugnumber;
    config = {
        on: {
            success: callback
        }
    };
    set_up_lp_client();
    lp_client.get(bug_uri, config);
}

/*
 * Set up the lp_client.
 *
 * This would probably be better served in a place where everyone could get to
 * it, or at least so everything in code could get to it.
 */
function set_up_lp_client() {
    if (lp_client === undefined) {
        lp_client = new Y.lp.client.Launchpad();
    }
}

/*
 * Show the temporary "Linking..." text
 */
function create_temporary_spinner() {
    var temp_spinner = Y.Node.create([
        '<div id="temp-spinner">',
        '<img src="/@@/spinner"/>Linking...',
        '</div>'].join(''));
    var buglinks = Y.one('#buglinks');
    var last = Y.one('#linkbug').get('parentNode');
    if (last) {
        buglinks.insertBefore(temp_spinner, last);
    }
}

/*
 * Destroy the temporary "Linking..." text
 */
function destroy_temporary_spinner() {

    var temp_spinner = Y.one('#temp-spinner');
    var spinner_parent = temp_spinner.get('parentNode');
    spinner_parent.removeChild(temp_spinner);

}

}, "0.1", {"requires": ["base", "lazr.anim", "lazr.formoverlay",
                        "lp.client", "lp.client.plugins"]});