[Bio] / FortyEight / Mantis.pm Repository:
ViewVC logotype

View of /FortyEight/Mantis.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (download) (as text) (annotate)
Thu Oct 4 22:07:31 2007 UTC (12 years, 9 months ago) by olson
Branch: MAIN
initial mantis bug-create code


package Mantis;

use Data::Dumper;
use POSIX;
use LWP::UserAgent;

use base 'Class::Accessor';
use DBI;
use strict;

__PACKAGE__->mk_accessors(qw(info dbh ua base_url web_user web_pass logged_in));

sub new
{
    my($class, $info) = @_;

    my $db_conn = $info->{db_connect};
    my $db_user = $info->{db_user};
    my $db_pass = $info->{db_pass};
    my $base_url = $info->{base_url};

    my $dbh = DBI->connect($db_conn, $db_user, $db_pass);

    $dbh or die "cannot connect to db: conn=$db_conn user=$db_user\n";

    my $ua = LWP::UserAgent->new();
    my $cookies = {};
    $ua->cookie_jar($cookies);

    my $self = {
	dbh => $dbh,
	info => $info,
	ua => $ua,
	base_url => $base_url,
	web_user => $info->{web_user},
	web_pass => $info->{web_pass},
    };
    return bless $self, $class;
}

#
# Invoked like:
#
# 	    Mantis::report_bug(info => $FIG_Config::mantis_info,
# 			       stage => $stage,
# 			       genome => $genome,
# 			       genome_name => $genome_name,
# 			       job_id =>$ job_id,
#			       job_dir => $job_dir,
#		               meta => $meta,
# 			       user_email => $email,
# 			       user_name => $name,
# 			       msg => $msg);

sub report_bug
{
    my($self, %opts) = @_;

    my $reporter = $self->check_for_reporter($opts{user_email});

    my $project = $self->info->{project_id} or 0;

    #
    # Bug description.
    #
    
    my $descr = "Error reported in job $opts{job_id} in $opts{job_dir}\n";
    $descr .= $opts{msg};

    #
    # Extra info. Include dump of metadata here plus the metadata log.
    #
    my $extra = "";
    my $meta = $opts{meta};

    my $dbh = $self->dbh;
    $dbh->do(qq(INSERT INTO mantis_bug_text_table (description, steps_to_reproduce, additional_information)
		VALUES (?, ?, ?)), undef,
	     $descr, '', $extra);
                      
    my $text_id = $dbh->{mysql_insertid};
    print "inserted: '$text_id'\n";

    $dbh->do(qq(INSERT INTO mantis_bug_table (project_id, reporter_id, date_submitted, last_updated, bug_text_id)
		VALUES (?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?)), undef,
	     $project, $reporter, $text_id);
    my $bug_id = $dbh->{mysql_insertid};

    my $b = $meta->get_metadata('mantis.bug');
    if (ref($b))
    {
	push(@$b, $bug_id);
    }
    else
    {
	$b = [$bug_id];
    }
    $meta->set_metadata('mantis.bug', $b);

    #
    # custom field.
    #
    $dbh->do(qq(INSERT INTO mantis_custom_field_string_table (field_id, bug_id, value)
		VALUES (?, ?, ?)), undef,
	     $self->info->{field_job_number}, $bug_id, $opts{job_id});
    $dbh->do(qq(INSERT INTO mantis_custom_field_string_table (field_id, bug_id, value)
		VALUES (?, ?, ?)), undef,
	     $self->info->{field_server}, $bug_id, $self->info->{server_value});
    
    if ($meta)
    {
	my $tmp = "/tmp/mantis.$$";
	mkdir $tmp;
	open(T, ">$tmp/metadata_dump");
	
	for my $key (sort $meta->get_metadata_keys())
	{
	    my $val = $meta->get_metadata($key);
	    if (ref($val))
	    {
		$val = Dumper($val);
	    }
	    print T "$key: $val\n";
	}
	close(T);

	$self->upload_file($bug_id, "$tmp/metadata_dump");
	unlink("$tmp/metadata_dump");

	open(T, ">$tmp/log_dump");
	
	my $log = $meta->get_log();
	for my $l (@$log)
	{
	    my($type, $what, $date, $data) = @$l;
	    my $dstr = strftime("%Y-%m-%d %H:%M:%S", localtime $date);
	    if (ref($data))
	    {
		$data = Dumper($data);
	    }
	    print T "$type\t$what\t$dstr\t$data\n";
	}
	close(T);
	$self->upload_file($bug_id, "$tmp/log_dump");
	unlink("$tmp/log_dump");
	rmdir($tmp);
    }

}


sub check_for_reporter
{
    my($self, $email) = @_;

    my $res = $self->dbh->selectall_arrayref(qq(SELECT id
						FROM mantis_user_table
						WHERE email = ? AND enabled = 1
						ORDER BY access_level), undef, $email);
    if (! $res)
    {
	die "check_for_reporter: lookup error: " . DBI->errstr;
    }
    if (@$res)
    {
	return $res->[0]->[0];
    }
    else
    {
	#
	# Look up the rast user.
	#

	my $rast_user = $self->info->{default_reporter};
	my $res = $self->dbh->selectall_arrayref(qq(SELECT id
						    FROM mantis_user_table
						    WHERE username = ? AND enabled = 1
						    ORDER BY access_level), undef, $rast_user);
	if (@$res)
	{
	    return $res->[0]->[0];
	}
	else
	{
	    die "Default mantis reporter not found (rast_user='$rast_user')";
	}
    }
}

sub login
{
    my($self) = @_;

    my $resp = $self->ua->post($self->base_url . '/login.php',
		     [ username => $self->web_user,
		      password =>  $self->web_pass,
		      perm_login => '',
		      ],
			       Content_Type => 'form-data',
		      );
    $self->logged_in(1);
}

sub upload_file
{
    my($self, $bug_id, $file) = @_;

    if (!$self->logged_in)
    {
	$self->login();
    }

    my $l = -s $file;

    my $resp = $self->ua->post($self->base_url . "/bug_file_add.php",
		     [
		      bug_id => $bug_id,
		     max_file_size => 2000000,
		     file => [$file],
		      ],
		      Content_Length => $l,
		     Content_Type => 'form-data'
		    );

}


1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3