Gozala (owner)

Revisions

  • f5568f Gozala Tue Feb 17 07:23:09 -0800 2009
  • dea1af Irakli ... Mon Feb 16 18:47:45 -0800 2009
  • 06d3a0 Irakli ... Mon Feb 16 13:19:57 -0800 2009
  • 2fc755 Gozala Mon Feb 16 13:00:24 -0800 2009
  • b78473 Gozala Mon Feb 16 12:51:37 -0800 2009
gist: 65354 Download_button fork
public
Description:
Ubiquity Commands - bugzilla
Public Clone URL: git://gist.github.com/65354.git
Embed All Files: show embed
bugzilla.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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
var CC = Components.Constructor;
var LoginManager = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
var LoginInfo = new CC("@mozilla.org/login-manager/loginInfo;1",Ci.nsILoginInfo,"init");
 
/**
* Static Object containig metadata of the creator and of the command
*/
var MetaData = {
    icon : 'http://www.bugzilla.org/img/bugzilla_icon.png',
    homepage : 'http://rfobic.blogspot.com/2008/10/ubiquity-command-for-bugzilla.html',
    author : { name: "Irakli Gozalishvili", email: "Irakli.Gozalishvili@tomtom.com"}
};
 
var Session = function(nick, url, username, password) {
    this.nick = nick;
    
    // creating new session
    if (url && username && password) {
        // remove login if exists
        this.remove();
        // creating instance of nsILoginInfo
        var login = new LoginInfo(url, null, nick, username, password, '', '');
        // adding / changeing Bugzilla xml-rpc url for session
        Bugzilla._prefs.setValue(Bugzilla._prefNames.url + nick, url);
        // adding session to bookmarks
        Bugzilla._prefs.setValue(Bugzilla._prefNames.sessions, Bugzilla._prefs.getValue(Bugzilla._prefNames.sessions,'') + Bugzilla._prefNames.splitter + nick);
        // storing user and password
        LoginManager.addLogin(login);
    }
};
 
Session.prototype = {
    /**
* Session name
* @type {string}
*/
    nick : null,
    
    /**
* Url of the Bugzilla XML-RPC service for this session
* @type {String}
*/
    get url() {
        return Bugzilla._prefs.getValue(Bugzilla._prefNames.url + this.nick, '');
    },
    
    /**
* getter for a user and password
* @type {Object}
* { username : 'MyUeser', password : 'myPassword'}
*/
    get login() {
        var logins = LoginManager.findLogins({}, this.url, null, this.nick);
        return logins.length > 0 ? {username : logins[0].username, password : logins[0].password} : null;
    },
    
    /**
* Removes current login from memory
*/
    remove : function() {
        // removeing from stored sessions
        Bugzilla._prefs.setValue(Bugzilla._prefNames.sessions, Bugzilla._prefs.getValue(Bugzilla._prefNames.sessions,'').replace(Bugzilla._prefNames.splitter + this.nick, ''));
        // removeing login info from LoginManager
        var logins = LoginManager.findLogins({}, this.url, null, this.nick);
        for (var i = 0; i < logins.length; i++) {
            LoginManager.removeLogin(logins[i]);
        }
    }
};
 
 
 
var Bugzilla = {
    /**
* Last accessed session
* @type {Object}
* {
* url : 'https://bugzilla.mozilla.org/xmlrpc.cgi',
* user : 'myUser',
* password : 'myPassword'
* }
*/
    _lastSession : null,
    
    get lastSession() {
        if (!this._lastSession) {
            var lastSessionName = this._prefs.getValue(this._prefNames.lastSession, null);
            this._lastSession = lastSessionName ? new Session(lastSessionName) : null;
        }
        return this._lastSession;
    },
    
    set lastSession(session) {
        this._prefs.setValue(this._prefNames.lastSession, session.nick);
        this._lastSession = session;
    },
    
    _prefs : Application.prefs,
    
    /**
* Map of preference names
*/
    _prefNames : {
        splitter : '|',
        sessions : 'ubiquity.commands.bugzilla.sessions',
        url : 'ubiquity.commands.bugzilla.url.',
        lastSession : 'ubiquity.commands.bugzilla.lastSession'
    },
    
    /**
* Makes rpc calls to the JIRA XML-RPC Service and returns results as XML
* Example:
* rpc('http://jira.atlassian.com/rpc/xmlrpc','jira1.login',
* <params>
* <param>
* <value>{user}</value>
* </param>
* <param>
* <value>{password}</value>
* </param>
* </params>);
*
*
* @param url {String} XML-RPC Service url
* @param method {String} Name of the method to be called
* @param data {XML} Data to be passed to the Remote Procedure
*
* @throws Error {Error} Throw exceprion if method responce contains fault
*/
    rpc : function(url, method, data) {
        var methodResponce = this.rpc2json(new XML(jQuery.ajax({
                    url : url,
                    type : 'POST',
                    data : <methodCall>
                                <methodName>{method}</methodName>
                                {this.json2rpc(data)}
                            </methodCall>.toXMLString(),
                    async : false,
                    contentType : 'text/xml',
                    dataType : 'xml'
                }).responseText.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/,"")));
        
        if (methodResponce.fault) {
            throw methodResponce;
        } else {
            return methodResponce;
        }
    },
    
    /**
* Recursive function converts responce of the RPC
* call to the JSON Object
* @returns {Object} JSON type object
*/
    rpc2json : function(xml) {
        var self = arguments.callee;
        var data = {};
        for each (var member in xml.*) {
            if (member.nodeKind() == 'text')
                return new String(member);
            
            switch (member.name().toString()) {
                case 'i4' :
                case 'int' :
                case 'double' :
                    return new Number(member.text());
                    break;
                case 'string' :
                    return new String(member.text());
                    break;
                case 'dateTime.iso8601' :
                    return new Date(member.text());
                    break;
                case 'base64' :
                    return member.text();
                    break;
                case 'value' :
                case 'struct' :
                case 'methodResponse' :
                    return self(member);
                    break;
                case 'member' :
                    data[member.name.text()] = self(member.value);
                    break;
                case 'fault' :
                case 'params' :
                case 'param' :
                    data[member.name().toString()] = self(member);
                    break;
                case 'array' :
                    var tempArray = [];
                    for each (value in member.data.value) {
                        tempArray.push(self(value));
                    }
                    return tempArray;
                    break;
                default :
                    return null;
            }
        }
        return data;
    },
    
    /**
* Recursive function converts JSON type object to RPC method call like xml
* @returns {XML} E4X object
*/
    json2rpc : function(json) {
        var self = arguments.callee;
        if (!json) return <params/>
        
        switch (typeof json) {
            case 'boolean':
                return <boolean>{((json) ? '1' : '0')}</boolean>;
            case 'number':
                return (parseInt(json) == json) ? <int>{json}</int> : <double>{json}</double>;
            case 'string':
                return <string>{json}</string>;
            case 'object':
                if (json instanceof String)
                    return <string>{json}</string>;
                else if (json instanceof Date)
                    return <dateTime.iso8601>{json.getFullYear() + json.getMonth() + json.getDate() + 'T' + json.getHours() + ':' + json.getMinutes() + ':' + json.getSeconds()}</dateTime.iso8601>;
                else if (json instanceof Array) {
                    if (self == self.caller) {
                        var tempArray = <array><data/></array>;
                        for each(member in json)
                            tempArray.data.appendChild(<value>{self(member)}</value>);
                        return tempArray;
                    } else {
                        result = <params/>
                        for each(member in json)
                            result.appendChild(<param><value>{self(member)}</value></param>);
                        return result;
                    }
                } else {
                    var struct = <struct/>;
                    for (key in json)
                        struct.appendChild(
                            <member>
                                <name>{key}</name>
                                <value>{self(json[key])}</value>
                            </member>
                        );
                    return struct;
                }
            break;
        }
    },
    
    /**
* Makes login to the JIRA XML-RPC Service and stores session id in the session memebr.
* @private
* @param user {String} Username
* @param password {String}
* @returns {void}
*/
    _login : function(user, password) {
        try {
            this._session = this.rpc2json(this.rpc(this.url, 'jira1.login',
                <params>
                    <param>
                        <value>{user}</value>
                    </param>
                    <param>
                        <value>{password}</value>
                    </param>
                </params>).params.param);
        } catch (e) {
            if (e instanceof XML)
                displayMessage(e.fault.value.struct.member.value[0]);
            else
                displayMessage(e.toString());
        }
    },
    
    getIssue : function(key) {
        try {
            return this.rpc2json(this.rpc(this.url, 'jira1.getIssue',
                <params>
                    <param>
                        <value>{this.session}</value>
                    </param>
                    <param>
                        <value>{key}</value>
                    </param>
                </params>
                ).params.param);
        } catch (e) {
            if (e instanceof XML)
                displayMessage(e.fault.value.struct.member.value[0]);
            else
                displayMessage(e.toString());
        }
    },
    
    getInfo : function() {
        try {
            return this.rpc(Bugzilla.lastSession.url, 'Bugzilla.version').params.param.version;
        } catch (e if e.fault && e.fault.faultString) {
            displayMessage(e.fault.faultString);
        } catch (e) {
            displayMessage(e);
        }
    },
    
    getUsers : function(params) {
        try {
            dump('\n\n');
            dump(this.rpc(Bugzilla.lastSession.url, 'User.get', params).toSource());
            dump('\n\n');
        } catch (e if e.fault && e.fault.faultString) {
            displayMessage(e.fault.faultString.toString());
            dump('\nError> ' + e.fault.faultString);
        } catch (e) {
            displayMessage(e);
        }
    },
    
    getBugs : function(params) {
        try {
            return this.rpc(Bugzilla.lastSession.url, 'Bug.get_bugs', params).params.param.bugs;
        } catch (e if e.fault && e.fault.faultString) {
            displayMessage(e.fault.faultString.toString());
            dump('\nError> ' + e.fault.faultString);
            dump('\nError> ' + e.toSource());
        } catch (e) {
            displayMessage(e);
            dump('\nError> ' + e.stack);
        }
    }
};
 
Bugzilla.nouns = {
    session : {
        _name : 'Session',
        
        suggest : function(text, html) {
            var sessions = Bugzilla._prefs.getValue(Bugzilla._prefNames.sessions, '').split(Bugzilla._prefNames.splitter);
            var filter = new RegExp('[\s\S]*' + text + '[\s\S]*','i');
            var matchedsuggestions = [];
            var unmatchedSuggestions = [];
            
            for each (var session in sessions) {
                var suggestions = filter.test(session) ? matchedsuggestions : unmatchedSuggestions;
                suggestions.push({
                    text : session,
                    summary : session,
                    html : session,
                    data : new Session(session)
                });
                
            }
            
            return matchedsuggestions.length ? matchedsuggestions : unmatchedSuggestions;
        },
        
        default : function() {
            if (Bugzilla.lastSession)
                return {
                    text : Bugzilla.lastSession.nick,
                    summary : Bugzilla.lastSession.nick,
                    html : Bugzilla.lastSession.nick,
                    data : Bugzilla.lastSession
                };
        }
    },
    
    bugByID : {
        _name : 'Bug',
        
        suggest : function(text, html) {
            var suggestions = [];
            var bugs = Bugzilla.getBugs([{ids : text.split(/\s+/), permissive : true}]);
            
            for each (var bug in bugs) {
                suggestions.push({
                    text : bug.internals.bug_id,
                    summary : bug.internals.bug_id,
                    html : bug.internals.bug_id,
                    data : bug
                });
            }
            return suggestions;
        },
        
        default : function() {
            return {
                text : 'No bugs being found',
                summary : 'No bugs being found',
                html : '',
                data : {}
            };
        }
    }
};
 
 
CmdUtils.CreateCommand({
    name : 'bugzilla-session-add',
    
    icon : MetaData.icon,
    
    description : 'Creates and stores new session',
    
    author : MetaData.author,
    
    homepage : MetaData.homepage,
    
    help : 'type bugzilla-add-session name url user password',
    
    modifiers : {
        'name' : noun_arb_text,
        'url' : noun_arb_text,
        'user' : noun_arb_text,
        'password' : noun_arb_text
    },
    
    preview : function(pblock, takes, modifiers) {
        [name, url, username, password] = [modifiers.name.text, modifiers.url.text, modifiers.user.text, modifiers.password.text];
        var error = 'color: red';
        var input = 'background: none; border: none; color: white; font-size: 12px;';
        var link = 'text-decoration: underline;'
        pblock.innerHTML =
            <div>
                <b>{'Creates and saves new session Bugzilla:'}</b>
                <br/>
                <br/>
                <div>
                    <b>{ 'Session name : '}</b>
                    { name ? <b>{name}</b> : <b style={error}>{'Needs a name'}</b> }
                </div>
                <div>
                    <b>{ 'Bugzilla URL : '}</b>
                    { url ? <code style={link}><a href={url}>{url}</a></code> : <b style={error}>{'Needs url'}</b>}
                </div>
                <div>
                    <b>{ 'User : '}</b>
                    { username ? <input type={'text'} value={username} style={input}/> : <b style={error}>{'Needs user name'}</b>}
                </div>
                <div>
                    <b>{ 'password : '}</b>
                    { password ? <input type={'password'} value={password} style={input}/> : <b style={error}>{'Needs password'}</b>}
                </div>
            </div>.toXMLString();
    },
    
    execute : function(takes, modifiers) {
        [name, url, username, password] = [modifiers.name.text, modifiers.url.text || takes.text, modifiers.user.text, modifiers.password.text];
        try {
            if (name && url && username && password)
                var session = new Session(name, url, username, password);
                if (!Bugzilla.lastSession) {
                    Bugzilla.lastSession = session;
                }
        } catch(e) {
            displayMessage(e.toSource());
        }
    }
});
 
CmdUtils.CreateCommand({
    name : 'bugzilla-session-remove',
    
    icon : MetaData.icon,
    
    description : 'Removes selected session from memory',
    
    author : MetaData.author,
    
    homepage : MetaData.homepage,
    
    help : 'type bugzilla-session-remove name',
    
    takes : {
        'session' : Bugzilla.nouns.session,
    },
    
    preview : function(pblock, takes) {
        
        var error = 'color: red';
        var input = 'background: none; border: none; color: white; font-size: 12px;';
        var link = 'text-decoration: underline;'
        pblock.innerHTML =
            <div>
                <b>{'Removes Bugzilla session :'}</b>
                <br/>
                <br/>
                <div>
                    <b>{ takes.text }</b>
                </div>
            </div>.toXMLString();
    },
    
    execute : function(takes) {
        try {
            (new Session(takes.text)).remove();
        } catch(e) {
            displayMessage(e.toSource());
        }
    }
});
CmdUtils.CreateCommand({
    name : 'bugzilla-info-version',
    
    icon : MetaData.icon,
    
    description : 'Gets bugzilla version',
    
    author : MetaData.author,
    
    homepage : MetaData.homepage,
    
    help : 'type bugzilla-info-version',
    
    modifiers : {
        'session' : Bugzilla.nouns.session,
    },
    
    previewDelay : 200,
    
    preview : function(pblock, takes, modifiers) {
        Bugzilla.lastSession = modifiers.session.data;
        pblock.innerHTML =
            <div>
                <b>{'Bugzilla information : '+ modifiers.session.text}</b>
                <br/>
                <br/>
                <div>
                    <b>{ 'Version : '}</b>
                    { Bugzilla.getInfo() }
                </div>
            </div>.toXMLString();
    },
    
    execute : function(takes, modifiers) {}
});
 
CmdUtils.CreateCommand({
    name : 'bugzilla-user',
    
    icon : MetaData.icon,
    
    description : 'Gets information about user accounts in Bugzilla',
    
    author : MetaData.author,
    
    homepage : MetaData.homepage,
    
    //help : 'type bugzilla-info-version',
    
    modifiers : {
        'id' : noun_arb_text,
        'name' : noun_arb_text,
        'match' : noun_arb_text,
        'session' : Bugzilla.nouns.session,
    },
    
    previewDelay : 200,
    
    preview : function(pblock, takes, modifiers) {
        Bugzilla.lastSession = modifiers.session.data;
        pblock.innerHTML =
            <div>
                <b>{'User information : '+ modifiers.id.text}</b>
                <br/>
                <br/>
                <div>
                    <b>{ 'Version : '}</b>
                    {Bugzilla.getUsers({
                        params : {
                            param : {
                                ids : modifiers.id.text.split('|')
                            }
                        }
                    })}
                </div>
            </div>.toXMLString();
    },
    
    execute : function(takes, modifiers) {}
});
 
CmdUtils.CreateCommand({
    name : 'bugzilla-get',
    
    icon : MetaData.icon,
    
    description : 'Gets information about particular bugs in the database.',
    
    author : MetaData.author,
    
    homepage : MetaData.homepage,
    
    //help : 'type bugzilla-info-version',
    takes : {
        'id' : Bugzilla.nouns.bugByID
    },
    
    modifiers : {
        'session' : Bugzilla.nouns.session,
    },
    
    previewDelay : 300,
    
    preview : function(pblock, takes, modifiers) {
        Bugzilla.lastSession = modifiers.session.data;
        var bug = takes.data.internals;
        pblock.innerHTML =
            <div>
                <b><u>{'Bug '+ bug.bug_id}</u></b>
                <br/>
                <br/>
                <b>{bug.short_desc}</b>
                <br/><br/>
                <div>
                    {bug.resolution ? <div><span>{'Resolution : '}</span><span>{bug.resolution}</span></div> : ''}
                    {bug.version ? <div><span>{'Version : '}</span><span>{bug.version}</span></div> : ''}
                    {bug.version ? <div><span>{'Target Milestone : '}</span><span>{bug.target_milestone}</span></div> : ''}
                    <br/>
                    {bug.version ? <div><span>{'Status : '}</span><span>{bug.bug_status}</span></div> : ''}
                    {bug.version ? <div><span>{'Priority : '}</span><span>{bug.priority}</span></div> : ''}
                    {bug.version ? <div><span>{'Severity : '}</span><span>{bug.bug_severity}</span></div> : ''}
                    <br/>
                    {bug.version ? <div><span>{'Platform : '}</span><span>{bug.rep_platform}</span></div> : ''}
                    {bug.version ? <div><span>{'OS : '}</span><span>{bug.op_sys}</span></div> : ''}
                    <br/>
                    {bug.bug_file_loc != '' ? <div><span>{'Location : '}</span><u><a href={bug.bug_file_loc}>{bug.bug_file_loc}</a></u></div> : ''}
                </div>
            </div>.toXMLString();
    },
    
    execute : function(takes, modifiers) {}
});