#!/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 Test Runner System. # # The Initial Developer of the Original Code is Maciej Maczynski. # Portions created by Maciej Maczynski are Copyright (C) 2001 # Maciej Maczynski. All Rights Reserved. # # Contributor(s): Greg Hendricks use strict; use lib qw(. lib); use Bugzilla::Constants; use lib (bz_locations()->{extensionsdir} . '/testopia/lib'); use Bugzilla; use Bugzilla::Bug; use Bugzilla::Error; use Bugzilla::Util; use Bugzilla::User; use Testopia::Util; use Testopia::Search; use Testopia::Table; use Testopia::TestRun; use Testopia::TestCase; use Testopia::TestCaseRun; use Testopia::Constants; use JSON; my $vars = {}; my $template = Bugzilla->template; Bugzilla->error_mode(ERROR_MODE_AJAX); Bugzilla->login(LOGIN_REQUIRED); my $cgi = Bugzilla->cgi; my $caserun; my $action = $cgi->param('action') || ''; if ($cgi->param('caserun_id')){ $caserun = Testopia::TestCaseRun->new($cgi->param('caserun_id')); } elsif ($cgi->param('run_id')){ $caserun = Testopia::TestCaseRun->new($cgi->param('run_id'), $cgi->param('case_id'), $cgi->param('build_id'), $cgi->param('env_id')); } else{ $cgi->send_header; ThrowUserError('testopia-missing-parameter', {'param' => 'caserun_id or case_id and run_id'}); } if ($action eq 'update_build'){ $cgi->send_header; ThrowUserError("testopia-read-only", {'object' => $caserun}) unless $caserun->canedit; my $build_id = $cgi->param('build_id'); detaint_natural($build_id); validate_test_id($build_id, 'build'); $caserun = $caserun->switch($build_id, $caserun->environment->id); print "{'success': true, caserun:" . $caserun->TO_JSON ."}"; } elsif ($action eq 'update_environment'){ $cgi->send_header; ThrowUserError("testopia-read-only", {'object' => $caserun}) unless $caserun->canedit; my $environment_id = $cgi->param('caserun_env'); detaint_natural($environment_id); validate_test_id($environment_id, 'environment'); $caserun = $caserun->switch($caserun->build->id, $environment_id); print "{'success': true, caserun:" . $caserun->TO_JSON ."}"; } elsif ($action eq 'update_status'){ $cgi->send_header; ThrowUserError("testopia-read-only", {'object' => $caserun}) unless $caserun->canedit; my $status_id = $cgi->param('status_id'); detaint_natural($status_id); $caserun->set_status($status_id, $cgi->param('update_bug')); print "{'success': true}"; } elsif ($action eq 'update_note'){ $cgi->send_header; ThrowUserError("testopia-read-only", {'object' => $caserun}) unless $caserun->canedit; my $note = $cgi->param('note'); trick_taint($note); $caserun->append_note($note); print "{'success': true}"; } elsif ($action eq 'update_assignee'){ $cgi->send_header; ThrowUserError("testopia-read-only", {'object' => $caserun}) unless $caserun->canedit; my $assignee_id; if ($cgi->param('assignee')){ $assignee_id = login_to_id(trim($cgi->param('assignee')),'THROW_ERROR'); } $caserun->set_assignee($assignee_id); print "{'success': true}"; } elsif ($action eq 'update_sortkey'){ $cgi->send_header; ThrowUserError("testopia-read-only", {'object' => $caserun}) unless $caserun->canedit; my $sortkey = $cgi->param('sortkey'); ThrowUserError("number_not_numeric", {'num' => $sortkey, field => 'index', field_descs =>{'index' => 'index'}}) unless $caserun->canedit; $caserun->set_sortkey($sortkey); print "{'success': true}"; } elsif ($action eq 'update_priority'){ $cgi->send_header; ThrowUserError("testopia-read-only", {'object' => $caserun}) unless $caserun->canedit; $caserun->set_priority($cgi->param('priority')); $caserun->update(); print "{'success': true, caserun:" . $caserun->TO_JSON ."}"; } elsif ($action eq 'update_category'){ $cgi->send_header; ThrowUserError("testopia-read-only", {'object' => $caserun}) unless $caserun->canedit; $caserun->case->set_category($cgi->param('category')); $caserun->case->update(); print "{'success': true, caserun:" . $caserun->TO_JSON ."}"; } elsif ($action eq 'getbugs'){ $cgi->send_header; ThrowUserError("testopia-read-only", {'object' => $caserun}) unless $caserun->canedit; my $bugs; foreach my $bug (@{$caserun->bugs}){ $bugs->{'summary'} = $bug->short_desc; $bugs->{'bug_id'} = $bug->bug_id; } print '{"bugs":'; print to_json($bugs); print '}'; } elsif ($action eq 'gettext'){ unless ($caserun->canview){ $cgi->send_header; ThrowUserError("testopia-permission-denied", {'object' => $caserun}); } my $text = $caserun->case->text; $text->{'notes'} = $caserun->notes; $text->{'case_id'} = $caserun->case->id; $text->{'summary'} = $caserun->case->summary; $vars->{'text'} = $text; $cgi->send_header(-type => 'text/xml'); Bugzilla->template->process("testopia/case/text.xml.tmpl", $vars) || ThrowTemplateError(Bugzilla->template->error()); } elsif ($action eq 'gethistory'){ $cgi->send_header; ThrowUserError("testopia-permission-denied", {'object' => $caserun}) unless $caserun->canview; print '{"records":'; print to_json($caserun->get_history); print '}'; } else { print "Location: tr_show_run.cgi\n\n"; }