-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtr_quicksearch.cgi
More file actions
executable file
·511 lines (464 loc) · 19.5 KB
/
tr_quicksearch.cgi
File metadata and controls
executable file
·511 lines (464 loc) · 19.5 KB
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
#!/usr/bin/perl -wT
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Testopia System.
#
# The Initial Developer of the Original Code is Greg Hendricks.
# Portions created by Greg Hendricks are Copyright (C) 2006
# Novell. All Rights Reserved.
#
# Contributor(s): Greg Hendricks <ghendricks@novell.com>
use strict;
use lib qw(. lib);
use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Config;
use Bugzilla::Error;
use Bugzilla::Util;
BEGIN { Bugzilla->extensions }
use Bugzilla::Extension::Testopia::Util;
use Bugzilla::Extension::Testopia::Search;
use Bugzilla::Extension::Testopia::Table;
use Bugzilla::Extension::Testopia::TestRun;
use Bugzilla::Extension::Testopia::Classification;
use Bugzilla::Extension::Testopia::Constants;
use JSON;
use vars qw($vars);
Bugzilla->login(LOGIN_REQUIRED);
my $template = Bugzilla->template;
my $cgi = Bugzilla->cgi;
my $action = $cgi->param('action') || '';
my $term = trim($cgi->param('searchstr')) || '';
push @{$::vars->{'style_urls'}}, 'testopia/css/default.css';
# Quicksearch allows a user to look up any of the major objects in Testopia
# using a simple prefix. For instance, e:linux will search for all environments
# with linux in the name or tr 33 will bring up Test Run 33.
# If only one is returned, we jump to the appropriate page, otherwise
# we display the list.
# If we have a term we are using this quicksearch
if ($term){
SWITCH: for ($term){
/^(tag)?[\s:-]+(.*)$/i && do{
my $text = trim($2);
print "Location: " . Bugzilla->params->{'urlbase'} . "tr_tags.cgi?tag=" . $text . "\n\n";
last SWITCH;
};
/^(plan|TP|p)?[\s:-]+(.*)$/i && do{
my $text = trim($2);
if ($text =~ /^\d+$/){
print "Location: " . Bugzilla->params->{'urlbase'} . "tr_show_plan.cgi?plan_id=" . $text . "\n\n";
}
else{
$cgi->param('current_tab', 'plan');
$cgi->param('name_type', 'anywordssubstr');
$cgi->param('name', $text);
my $search = Bugzilla::Extension::Testopia::Search->new($cgi);
my $table = Bugzilla::Extension::Testopia::Table->new('plan', 'tr_list_plans.cgi', $cgi, undef, $search->query);
if ($table->list_count == 1){
print "Location: " . Bugzilla->params->{'urlbase'} . "tr_show_plan.cgi?plan_id=" . ${$table->list}[0]->id . "\n\n";
}
else{
print "Location: " . Bugzilla->params->{'urlbase'} . "tr_list_plans.cgi?" . $table->get_query_part . "\n\n";
}
}
last SWITCH;
};
/^(run|TR|r)?[\s:-]+(.*)$/i && do{
my $text = trim($2);
if ($text =~ /^\d+$/){
print "Location: " . Bugzilla->params->{'urlbase'} . "tr_show_run.cgi?run_id=" . $text . "\n\n";
}
else{
$cgi->param('current_tab', 'run');
$cgi->param('summary_type', 'anywordssubstr');
$cgi->param('summary', $text);
my $search = Bugzilla::Extension::Testopia::Search->new($cgi);
my $table = Bugzilla::Extension::Testopia::Table->new('run', 'tr_list_runs.cgi', $cgi, undef, $search->query);
if ($table->list_count == 1){
print "Location: " . Bugzilla->params->{'urlbase'} . "tr_show_run.cgi?run_id=" . ${$table->list}[0]->id . "\n\n";
}
else{
print "Location: " . Bugzilla->params->{'urlbase'} . "tr_list_runs.cgi?" . $table->get_query_part . "\n\n";
}
}
last SWITCH;
};
/^(environment|TE|e|env)?[\s:-]+(.*)$/i && do{
my $text = trim($2);
if ($text =~ /^\d+$/){
print "Location: " . Bugzilla->params->{'urlbase'} . "tr_environments.cgi?env_id=" . $text . "\n\n";
}
else{
$cgi->param('current_tab', 'environment');
$cgi->param('name_type', 'anywordssubstr');
$cgi->param('name', $text);
my $search = Bugzilla::Extension::Testopia::Search->new($cgi);
my $table = Bugzilla::Extension::Testopia::Table->new('environment', 'tr_list_runs.cgi', $cgi, undef, $search->query);
if ($table->list_count == 1){
print "Location: " . Bugzilla->params->{'urlbase'} . "tr_environments.cgi?env_id=" . ${$table->list}[0]->id . "\n\n";
}
else{
print "Location: " . Bugzilla->params->{'urlbase'} . "tr_list_environments.cgi?" . $table->get_query_part . "\n\n";
}
}
last SWITCH;
};
do{
$term =~ s/^(case|TC|c)?[\s:-]+(.*)$/$2/gi;
if ($term =~ /^\d+$/){
print "Location: " . Bugzilla->params->{'urlbase'} . "tr_show_case.cgi?case_id=" . $term . "\n\n";
}
else{
$cgi->param('current_tab', 'case');
$cgi->param('summary_type', 'anywordssubstr');
$cgi->param('summary', $term);
my $search = Bugzilla::Extension::Testopia::Search->new($cgi);
my $table = Bugzilla::Extension::Testopia::Table->new('case', 'tr_list_cases.cgi', $cgi, undef, $search->query);
if ($table->list_count == 1){
print "Location: " . Bugzilla->params->{'urlbase'} . "tr_show_case.cgi?case_id=" . ${$table->list}[0]->id . "\n\n";
}
else{
print "Location: " . Bugzilla->params->{'urlbase'} . "tr_list_cases.cgi?" . $table->get_query_part . "\n\n";
}
}
};
}
}
############
### Ajax ###
############
# This is where we lookup items typed into Dojo combo boxes
else{
print $cgi->header;
# Environment Lookup
if ($action eq 'getenvironments'){
my $search = $cgi->param('search');
my $prod_ids = $cgi->param('prod_id');
trick_taint($search);
my @ids;
foreach my $id (split(',', $prod_ids)){
detaint_natural($id);
my $product = new Bugzilla::Product($id);
next unless $product;
push @ids, $id if Bugzilla->user->can_see_product($product->name);
}
unless (scalar @ids > 0){
print "{}";
exit;
}
$prod_ids = join(',', @ids);
$search = "%$search%";
my $dbh = Bugzilla->dbh;
# The order of name and environment are important in the select statment.
# JSON will convert this to an array of arrays which Dojo will interpret
# as a select list in the ComboBox widget.
my $ref;
if ($prod_ids){
$ref = $dbh->selectall_arrayref(
"SELECT test_environments.name AS name, test_environments.environment_id
FROM test_environments
WHERE name like ? AND product_id IN($prod_ids) AND isactive = 1
ORDER BY name",
undef, ($search));
}
else{
$ref = $dbh->selectall_arrayref(
"SELECT name, environment_id
FROM test_environments
WHERE name like ? AND isactive = 1
ORDER BY name
LIMIT 20",
undef, ($search));
}
print"{environments:";
print to_json($ref);
print "}";
}
# user lookup
elsif ($action eq 'getuser'){
my $search = $cgi->param('search');
my $start = $cgi->param('start');
my $limit = 20;
detaint_natural($start) || exit;
exit if ($search eq '');
$search = "%$search%";
trick_taint($search);
my $dbh = Bugzilla->dbh;
my $countquery = "SELECT COUNT(DISTINCT login_name) ";
my $query = "SELECT login_name, realname,";
my $qbody = '';
if (Bugzilla->params->{'usevisibilitygroups'}) {
$query .= " COUNT(group_id) ";
} else {
$query .= " 1 ";
}
$qbody .= "FROM profiles ";
if (Bugzilla->params->{'usevisibilitygroups'}) {
$qbody .= "LEFT JOIN user_group_map " .
"ON user_group_map.user_id = userid AND isbless = 0 " .
"AND group_id IN(" .
join(', ', (-1, @{Bugzilla->user->visible_groups_inherited})) . ")";
}
$qbody .= " WHERE disabledtext = '' AND (login_name LIKE ? OR realname LIKE ?) ";
$countquery .= $qbody;
$query .= $qbody;
$query .= $dbh->sql_group_by('userid', 'login_name, realname');
$query .= " ORDER BY login_name LIMIT $limit OFFSET $start";
my ($total) = $dbh->selectrow_array($countquery,undef,($search,$search));
my $sth = $dbh->prepare($query);
$sth->execute($search,$search);
my @userlist;
while (my($login, $name, $visible) = $sth->fetchrow_array) {
if ($visible){
push @userlist, {
'id' => $login, 'name' => $name
};
}
}
print "{'total':$total,'users':";
print to_json(\@userlist);
print "}"
}
# Tag lookup
elsif ($action eq 'gettag'){
my $search = $cgi->param('search');
my @product_ids;
foreach my $id (split(",", $cgi->param('product_id'))){
detaint_natural($id);
my $product = new Bugzilla::Product($id);
next unless $product;
push @product_ids, $id if Bugzilla->user->can_see_product($product->name);
}
my $product_ids = join(",". @product_ids);
trick_taint($search);
$search = "%$search%";
my $dbh = Bugzilla->dbh;
my $ref = {};
my $run_id = $cgi->param('run_id');
if ($product_ids){
$ref = $dbh->selectall_arrayref(
"SELECT tag_name, test_tags.tag_id
FROM test_tags
INNER JOIN test_case_tags ON test_tags.tag_id = test_case_tags.tag_id
INNER JOIN test_cases on test_cases.case_id = test_case_tags.case_id
INNER JOIN test_case_plans on test_case_plans.case_id = test_cases.case_id
INNER JOIN test_plans ON test_plans.plan_id = test_case_plans.plan_id
WHERE tag_name like ? AND test_plans.product_id IN ($product_ids)
UNION SELECT tag_name, test_tags.tag_id
FROM test_tags
INNER JOIN test_plan_tags ON test_plan_tags.tag_id = test_tags.tag_id
INNER JOIN test_plans ON test_plan_tags.plan_id = test_plans.plan_id
WHERE tag_name like ? AND test_plans.product_id IN ($product_ids)
UNION SELECT tag_name, test_tags.tag_id
FROM test_tags
INNER JOIN test_run_tags ON test_run_tags.tag_id = test_tags.tag_id
INNER JOIN test_runs ON test_runs.run_id = test_run_tags.run_id
INNER JOIN test_plans ON test_plans.plan_id = test_runs.plan_id
WHERE tag_name like ? AND test_plans.product_id IN ($product_ids)
ORDER BY tag_name",
{'Slice' =>{}}, ($search,$search,$search));
}
else {
if (Bugzilla->user->in_group('Testers')){
$ref = $dbh->selectall_arrayref(
"SELECT tag_name, tag_id
FROM test_tags
WHERE tag_name like ?
ORDER BY tag_name
LIMIT 20",
{'Slice' =>{}}, $search);
}
}
print "{'tags':" . to_json($ref) . "}";
}
elsif ($action eq 'getversions'){
my $plan = Bugzilla::Extension::Testopia::TestPlan->new({});
my $prod_id = $cgi->param("product_id");
my @versions;
if ($prod_id && $prod_id == -1){
# For update multiple from tr_list_plans
push @versions, {'id' => "--Do Not Change--", 'name' => "--Do Not Change--"};
}
else{
detaint_natural($prod_id);
my $prod = $plan->lookup_product($prod_id);
if ($prod && !Bugzilla->user->can_see_product($prod)){
print '{ERROR:"You do not have permission to view this product"}';
exit;
}
if ($prod){
my $product = Bugzilla::Extension::Testopia::Product->new($prod_id);
@versions = @{$product->versions};
}
else {
@versions = [];
}
}
my $json = new JSON;
print "{versions:";
print $json->encode(\@versions);
print "}";
}
elsif ($action eq 'getmilestones'){
my $product = Bugzilla::Extension::Testopia::Product->new($cgi->param("product_id"));
exit unless $product->canedit;
my $json = new JSON;
print "{milestones:";
print $json->encode($product->milestones);
print "}";
}
elsif ($action eq 'getplantypes'){
my $plan = Bugzilla::Extension::Testopia::TestPlan->new({});
my $json = new JSON;
print "{types:";
print $json->encode($plan->get_plan_types());
print "}";
}
elsif ($action eq 'getpriorities'){
my $plan = Bugzilla::Extension::Testopia::TestCase->new({});
my $json = new JSON;
print "{priorities:";
print $json->encode($plan->get_priority_list());
print "}";
}
elsif ($action eq 'getcasestatus'){
my $plan = Bugzilla::Extension::Testopia::TestCase->new({});
my $json = new JSON;
print "{statuses:";
print $json->encode($plan->get_status_list());
print "}";
}
elsif ($action eq 'getcaserunstatus'){
my $plan = Bugzilla::Extension::Testopia::TestCaseRun->new({});
my $json = new JSON;
print "{statuses:";
print $json->encode($plan->get_status_list());
print "}";
}
elsif ($action eq 'getproducts'){
my $products;
my $q = $cgi->param('query');
my $json = new JSON;
if ($cgi->param('class_id')){
my $class = Bugzilla::Extension::Testopia::Classification->new($cgi->param('class_id'));
$products = $class->user_visible_products;
}
else{
$products = Bugzilla->user->get_selectable_products;
}
my @prods;
foreach my $p (@$products){
if ($cgi->param('query')){
push @prods, {name => $p->name, id => $p->id} if ($p->name =~ m/$q/i);
}
else{
push @prods, {name => $p->name, id => $p->id};
}
}
print "{products:" . $json->encode(\@prods) . "}";
}
elsif ($action eq 'getclassificationstree'){
my $node = $cgi->param('node');
if ($node && $node ne 'classes'){
$node =~ s/\D*//;
my @products;
my $classification = Bugzilla::Extension::Testopia::Classification->new($node);
foreach my $p (@{$classification->user_visible_products}){
push @products, {
id => $p->id,
text => $p->name,
leaf => JSON::true,
attributes =>{
defaultmilestone => $p->default_milestone,
canedit => $p->canedit ? JSON::true : JSON::false
}};
}
my $json = new JSON;
print $json->encode(\@products);
exit;
}
my @classifications;
foreach my $c (@{Bugzilla->user->get_selectable_classifications}){
push @classifications, {id => "c" . $c->id, text => $c->name, leaf => scalar @{$c->products} > 0 ? JSON::false : JSON::true};
}
my $json = new JSON;
print $json->encode(\@classifications);
}
# For use in new_case and show_case since new_plan does not require an id
elsif ($action eq 'getcomponents'){
my $plan = Bugzilla::Extension::Testopia::TestPlan->new({});
my $product_id = $cgi->param('product_id');
my $q = $cgi->param('query');
detaint_natural($product_id);
my $prod = $plan->lookup_product($product_id);
unless (Bugzilla->user->can_see_product($prod)){
print '{ERROR:"You do not have permission to view this product"}';
exit;
}
my $product = Bugzilla::Extension::Testopia::Product->new($product_id);
my @comps;
foreach my $c (@{$product->components}) {
if (!$cgi->param('query') || $c->name =~ m/$q/i) {
push @comps, {
'id' => $c->id,
'name' => $c->name,
'qa_contact' => $c->default_qa_contact ? $c->default_qa_contact->login : '',
'product' => $c->product->name,
};
}
}
my $json = new JSON;
print "{'components':". $json->encode(\@comps) ."}";
exit;
}
elsif ($action eq 'get_action'){
if( $cgi->param('bug_id')){
my $bug = Bugzilla::Bug->new($cgi->param('bug_id'),Bugzilla->user->id);
my $tcaction = Bugzilla->params->{"bug-to-test-case-action"};
my $bug_id = $bug->bug_id;
my $description = '<br><pre>' . wrap_comment(@{$bug->comments({order => 'oldest_to_newest'})}[0]->{'thetext'}) . '</pre>';
$tcaction =~ s/%id%/<a href="show_bug.cgi?id=$bug_id">$bug_id<\/a>/g;
$tcaction =~ s/%description%/$description/g;
print $tcaction;
}
else {
print Bugzilla->params->{'new-case-action-template'};
}
}
elsif ($action eq 'get_effect'){
if( $cgi->param('bug_id')){
my $effect = Bugzilla->params->{"bug-to-test-case-results"};
my $bug_id = $cgi->param('bug_id');
$effect =~ s/%id%/<a href="show_bug.cgi?id=$bug_id">$bug_id<\/a>/g;
print $effect;
}
else{
print Bugzilla->params->{'new-case-results-template'};
}
}
elsif ($action eq 'check_plan_rights'){
Bugzilla->error_mode(ERROR_MODE_AJAX);
my @plan_id = split(',', $cgi->param('plan_id'));
foreach my $id (@plan_id){
my $plan = Bugzilla::Extension::Testopia::TestPlan->new($id);
if ($plan){
ThrowUserError("testopia-create-denied", {'object' => 'Test Case', 'plan' => $plan}) unless $plan->canedit;
}
}
}
# If neither is true above, display the quicksearch form and explanation.
else{
$template->process("testopia/quicksearch.html.tmpl", $vars) ||
ThrowTemplateError($template->error());
}
}