exporttickets.in 7.55 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# Copyright (c) 2008 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
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
#
use strict;
use English;
use Getopt::Std;
use Errno;
use Mail::Internet;
use Mail::Address;

sub usage()
{
    exit(-1);
}
my $optlist = "d";
my $debug   = 0;

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
my $OURDOMAIN= "@OURDOMAIN@";
my $FLYEMAIL = "flyspray\@" . $OURDOMAIN;
my $FLYCONF  = "/usr/local/etc/flyspray.conf.php";

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libtestbed;
use libtbdb;

# Locals
my $dbname;
my $dbuser;
my $dbpass;
my %tickets = ();

# Protos
sub fatal($);

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}

#
# The DB user/passwd are stored in the flyspray config file, which is
# hopefully not group readable. Open and parse that file, then open a
# connection to the DB.
#
$FLYCONF = "$TB/etc/flyspray.conf.php"
    if (! -e $FLYCONF);

open(FLY, $FLYCONF) or
    die("*** $0:\n".
	"    Could not open $FLYCONF for reading!\n");

while (<FLY>) {
    if ($_ =~ /^([-\w]*)\s*=\s*"([-\w]*)"$/) {
	if ($1 eq "dbname") {
	    $dbname = $2;
	}
	elsif ($1 eq "dbuser") {
	    $dbuser = $2;
	}
	elsif ($1 eq "dbpass") {
	    $dbpass = $2;
	}
    }
}
close(FLY);

# Make sure we have everything we need.
if (!defined($dbname) ||
    !defined($dbuser) ||
    !defined($dbpass)) {
    fatal("Could not find db parameters in $FLYCONF!");
}
if (TBDBConnect($dbname, $dbuser, $dbpass) < 0) {
    fatal("Could not connect to flyspray database!");
}

my $query_result =
    DBQueryFatal("select project_id from flyspray_projects ".
		 "where project_title='Emulab'");
fatal("Could not project ID for Emulab")
    if (!defined($query_result) || !$query_result->numrows);
my ($project_id) = $query_result->fetchrow_array();

my %types = ( "1" => "Bug Report",
	      "2" => "Feature Request",
	      "3" => "Support Request",
	      "4" => "Gripe",
	      );

my %severities = ( "1" => "Very Low",
		   "2" => "Low",
		   "3" => "Medium",
		   "4" => "High",
		   "5" => "Critical",
		   );

my %resolutions = ( "0" => '',
		    "1" => '',
		    "2" => "invalid",
		    "3" => "wontfix",
		    "4" => "wontfix",
		    "5" => "worksforme",
		    "6" => "duplicate",
		    "7" => "wontfix",
		    "8" => "fixed",
		    "9" => "fixed",
		    );

my %status_list = ( "1" => 'new',
		    "2" => 'new',
		    "3" => 'assigned',
		    "4" => 'foo',
		    );

$query_result =
    DBQueryFatal("select t.*,u.email_address as opened_by, ".
		 "      a.email_address as assigned_to ".
		 "   from flyspray_tasks as t ".
		 "left join flyspray_users as u on u.user_id=t.opened_by ".
		 "left join flyspray_users as a on a.user_id=t.assigned_to ".
		 "where t.attached_to_project=$project_id ".
		 "order by task_id");

while (my $row = $query_result->fetchrow_hashref()) {
    my $task_id      = $row->{'task_id'};
    my $task_type    = $row->{'task_type'};
    my $date_opened  = $row->{'date_opened'};
    my $opened_by    = $row->{'opened_by'};
    my $is_closed    = $row->{'is_closed'};
    my $date_closed  = $row->{'date_closed'};
    my $closed_by    = $row->{'closed_by'};
    my $summary      = $row->{'item_summary'};
    my $description  = $row->{'detailed_desc'};
    my $item_status  = $row->{'item_status'};
    my $assigned_to  = $row->{'assigned_to'};
    my $resolution_reason = $row->{'resolution_reason'};
    my $task_severity = $row->{'task_severity'};
    my $task_priority = $row->{'task_priority'};

    $date_closed = ""
	if (!defined($date_closed));
    $assigned_to = ""
	if (!defined($assigned_to));

#    print "$task_id, $task_type, $date_opened, $opened_by, $is_closed, ".
#	"$date_closed, $closed_by, $item_status, $assigned_to, ".
#	"$resolution_reason, $task_severity, $task_priority\n";

    my $ticket = {};
    $ticket->{'type'}          = $types{"$task_type"};
    $ticket->{'time'}          = $date_opened;
    $ticket->{'changetime'}    = ''; # XXX
    $ticket->{'component'}     = '';
    $ticket->{'severity'}      = $severities{"$task_severity"};
    $ticket->{'priority'}      = "normal";
    $ticket->{'owner'}         = $assigned_to;
    $ticket->{'reporter'}      = $opened_by;
    $ticket->{'cc'}            = '';
    $ticket->{'version'}       = '';
    $ticket->{'milestone'}     = '';
    $ticket->{'resolution'}    = $resolutions{$resolution_reason};
    $ticket->{'summary'}       = DBQuoteSpecial($summary);
    $ticket->{'description'}   = DBQuoteSpecial($description);
    $ticket->{'status'}        = '';

    if ($is_closed) {
	$ticket->{'status'}     = 'closed';
	$ticket->{'changetime'} = $date_closed;
    }
    else {
	if ($item_status > 3) {
	    print STDERR "Setting item_status to foo\n";
	    $item_status = 4;
	}
	$ticket->{'status'}    = $status_list{$item_status};
    }

    my $comments_query =
	DBQueryFatal("select c.*,u.email_address from flyspray_comments as c ".
		     "left join flyspray_users as u on u.user_id=c.user_id ".
		     "where task_id=$task_id order by comment_id");
    if ($comments_query->numrows) {
	my $comment_id = 1;
	while (my $comrow = $comments_query->fetchrow_hashref()) {
	    my $comment_date  = $comrow->{'date_added'};
	    my $comment_email = $comrow->{'email_address'};
	    my $comment_text  = DBQuoteSpecial($comrow->{'comment_text'});

	    print "replace into ticket_change values ".
		"($task_id, $comment_date, '$comment_email', 'comment', ".
		" '$comment_id', $comment_text);\n";
	    $comment_id++;
	    $ticket->{'changetime'} = $comment_date;
	}
    }
		     
    {
    my $idx          = $task_id;
    my $type         = $ticket->{'type'};
    my $time         = $ticket->{'time'};
    my $changetime   = $ticket->{'changetime'};
    my $component    = $ticket->{'component'};
    my $severity     = $ticket->{'severity'};
    my $priority     = $ticket->{'priority'};
    my $owner        = $ticket->{'owner'};
    my $reporter     = $ticket->{'reporter'};
    my $cc           = $ticket->{'cc'};
    my $version      = $ticket->{'version'};
    my $milestone    = $ticket->{'milestone'};
    my $status       = $ticket->{'status'};
    my $resolution   = $ticket->{'resolution'};
    my $summary      = $ticket->{'summary'};
    my $description  = $ticket->{'description'};

    print "replace into ticket values ".
	"($task_id, '$type', $time, '$changetime', '$component', ".
	" '$severity', '$priority', '$owner', '$reporter', '$cc', ".
	" '$version', '$milestone', '$status', '$resolution', ".
	" $summary, $description, '');\n";
    }
}
exit(0);


sub fatal($)
{
    my ($mesg) = @_;

    print STDERR
	"*** $0:\n".
	"    $mesg\n";
    exit(-1);
}