| 1 | package BugCli; | ||
| 2 | |||
| 3 | our $VERSION = '0.2'; | ||
| 4 | use base qw(Term::Shell); | ||
| 5 | use BugCli::Help; | ||
| 6 | use BugCli::Summaries; | ||
| 7 | use BugCli::Completions; | ||
| 8 | use BugCli::Config; | ||
| 9 | use strict; | ||
| 10 | use Getopt::Std; | ||
| 11 | use FileHandle; | ||
| 12 | use DBIx::SQLEngine; | ||
| 13 | use Data::Dumper; | ||
| 14 | use POSIX qw(strftime); | ||
| 15 | use Text::Table; | ||
| 16 | use Config::Tiny; | ||
| 17 | use Term::ANSIColor qw(:constants); | ||
| 18 | |||
| 19 | $Term::ANSIColor::AUTORESET = 1; | ||
| 20 | |||
| 21 | our ( $config, $dbh, %login_to_uid, %uid_to_login, $llogin, %lastbugs, | ||
| 22 | $lastquery, $hostname ); | ||
| 23 | our (%Query) = ( "AutoIncrement" => 'select LAST_INSERT_ID()', ); | ||
| 24 | |||
| 25 | sub process_bind { | ||
| 26 | my ( $self, $cmd ) = @_; | ||
| 27 | $self->cmd( $config->{bind}->{$cmd} ); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub init_mysql { | ||
| 31 | my ($self) = @_; | ||
| 32 | $self->{API}{check_idle} = | ||
| 33 | exists $config->{server}->{timeout} ? $config->{server}->{timeout} : 60; | ||
| 34 | foreach my $cmd ( keys %{ $config->{command} } ) { | ||
| 35 | my ($code) = ord eval "\"$config->{command}->{$cmd}\""; | ||
| 36 | $self->term() | ||
| 37 | ->add_defun( 'process_bind', sub { process_bind( $self, $cmd ) }, | ||
| 38 | $code ); | ||
| 39 | } | ||
| 40 | $dbh = DBIx::SQLEngine->new( | ||
| 41 | "DBI:mysql:$config->{server}->{table}:$config->{server}->{host}", | ||
| 42 | $config->{server}->{user}, | ||
| 43 | $config->{server}->{password}, | ||
| 44 | { | ||
| 45 | RaiseError => 1, | ||
| 46 | ShowErrorStatement => 1, | ||
| 47 | } | ||
| 48 | ); | ||
| 49 | my ($uth) = $dbh->fetch_select( | ||
| 50 | table => 'profiles', | ||
| 51 | columns => [ 'userid', 'login_name' ] | ||
| 52 | ); | ||
| 53 | |||
| 54 | foreach my $uid ( @{$uth} ) { | ||
| 55 | $login_to_uid{ $$uid{login_name} } = $$uid{userid}; | ||
| 56 | $uid_to_login{ $$uid{userid} } = $$uid{login_name}; | ||
| 57 | } | ||
| 58 | $llogin = $config->{server}->{login}; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub idle { | ||
| 62 | %lastbugs = query_to_bugs( $dbh->fetch_select( sql => $lastquery ) ) | ||
| 63 | if defined $lastquery; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub run_history { | ||
| 67 | my ($self) = @_; | ||
| 68 | print join( "\n", $self->term()->GetHistory() ) . "\n"; | ||
| 69 | |||
| 70 | } | ||
| 71 | |||
| 72 | sub add_bug { | ||
| 73 | my ( $dbh, $assignee, $subject, $text, $version, $milestone, $severity ) = | ||
| 74 | @_; | ||
| 75 | my ($time) = strftime "%Y-%m-%d %H:%M:%S", localtime; | ||
| 76 | my ($sth) = $dbh->do_insert( | ||
| 77 | 'table' => 'bugs', | ||
| 78 | 'columns' => [ | ||
| 79 | qw(assigned_to bug_severity bug_status creation_ts short_desc product_id reporter version target_milestone component_id) | ||
| 80 | ], | ||
| 81 | 'values' => [ | ||
| 82 | $login_to_uid{$assignee}, $severity, | ||
| 83 | $time, $subject, | ||
| 84 | $config->{product}->{product_id}, $login_to_uid{$llogin}, | ||
| 85 | $version, $milestone, | ||
| 86 | $config->{product}->{component_id} | ||
| 87 | ] | ||
| 88 | ); | ||
| 89 | my ($bugid) = $dbh->fetch_one_value( sql => $Query{AutoIncrement} ); | ||
| 90 | update_bug( $dbh, $bugid, undef, undef, $text ); | ||
| 91 | return $bugid; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub update_bug { | ||
| 95 | my ( $dbh, $bug_id, $bug_resolution, $bug_status, $text ) = @_; | ||
| 96 | if ( defined $bug_resolution ) { | ||
| 97 | $dbh->do_update( | ||
| 98 | 'table' => 'bugs', | ||
| 99 | 'values' => { resolution => "'$bug_resolution'" }, | ||
| 100 | 'criteria' => { bug_id => $bug_id } | ||
| 101 | ); | ||
| 102 | } | ||
| 103 | if ( defined $bug_status ) { | ||
| 104 | $dbh->do_update( | ||
| 105 | 'table' => 'bugs', | ||
| 106 | 'values' => { bug_status => "'$bug_status'" }, | ||
| 107 | 'criteria' => { bug_id => $bug_id } | ||
| 108 | ); | ||
| 109 | } | ||
| 110 | |||
| 111 | my ($time) = strftime "%Y-%m-%d %H:%M:%S", localtime; | ||
| 112 | $dbh->do_insert( | ||
| 113 | 'table' => 'longdescs', | ||
| 114 | 'columns' => [qw(bug_id who bug_when work_time thetext)], | ||
| 115 | 'values' => [ $bug_id, $login_to_uid{$llogin}, "'$time'", 0.00, $text ] | ||
| 116 | ); | ||
| 117 | } | ||
| 118 | |||
| 119 | #only delete a certain description | ||
| 120 | |||
| 121 | sub bugs_to_table { | ||
| 122 | my (%bugs) = @_; | ||
| 123 | my (@b); | ||
| 124 | foreach my $bid ( keys %bugs ) { | ||
| 125 | push @b, | ||
| 126 | [ | ||
| 127 | $bid, $bugs{$bid}->{bug_severity}, | ||
| 128 | $bugs{$bid}->{bug_status}, $bugs{$bid}->{short_desc}, | ||
| 129 | $bugs{$bid}->{version}, $bugs{$bid}->{resolution} | ||
| 130 | ]; | ||
| 131 | } | ||
| 132 | return @b; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub query_to_bugs { | ||
| 136 | my ($sth) = @_; | ||
| 137 | my (%bugs); | ||
| 138 | foreach my $a ( @{$sth} ) { | ||
| 139 | $bugs{ $$a{bug_id} } = $a; | ||
| 140 | $bugs{ $$a{bug_id} }{COMMENTS} = get_comments( $dbh, $$a{bug_id} ); | ||
| 141 | } | ||
| 142 | |||
| 143 | return %bugs; | ||
| 144 | |||
| 145 | } | ||
| 146 | |||
| 147 | sub print_bug { | ||
| 148 | my ( $id, $bug ) = @_; | ||
| 149 | return <<EOT; | ||
| 150 | ===================== | ||
| 151 | Bug #$id Information | ||
| 152 | Subject: $$bug{short_desc} | ||
| 153 | Severity: $$bug{bug_severity} | ||
| 154 | Status: $$bug{bug_status} | ||
| 155 | Resolution: $$bug{resolution} | ||
| 156 | Comments: | ||
| 157 | $$bug{COMMENTS} | ||
| 158 | ===================== | ||
| 159 | EOT | ||
| 160 | } | ||
| 161 | |||
| 162 | sub get_comments { | ||
| 163 | my ( $dbh, $bugid ) = @_; | ||
| 164 | my ($text); | ||
| 165 | my ($bth) = $dbh->fetch_select( | ||
| 166 | table => 'longdescs', | ||
| 167 | criteria => { bug_id => $bugid }, | ||
| 168 | columns => [qw(who bug_when thetext)] | ||
| 169 | ); | ||
| 170 | |||
| 171 | foreach my $z ( @{$bth} ) { | ||
| 172 | next if not defined $$z{thetext} or | ||
| 173 | $$z{thetext} =~ /^\n$/ or | ||
| 174 | $$z{thetext} =~ /^\s*(:?done|fixed|\.)\s*$/i; | ||
| 175 | $text .= "\n-------------------\n"; | ||
| 176 | $text .= "From: $uid_to_login{$$z{who}} When: $$z{bug_when}\n"; | ||
| 177 | $text .= $$z{thetext}; | ||
| 178 | } | ||
| 179 | return $text; | ||
| 180 | |||
| 181 | } | ||
| 182 | |||
| 183 | sub prompt_str { "$config->{server}->{login}" . BLUE . "\@" . RESET . UNDERLINE . | ||
| 184 | "$config->{server}->{host}> " } | ||
| 185 | |||
| 186 | sub alias_exit { 'q', 'quit' } | ||
| 187 | |||
| 188 | sub prepare_query { | ||
| 189 | my ( $fmt, $regexp ) = @_; | ||
| 190 | $fmt =~ s/\%uid/$login_to_uid{$llogin}/g; | ||
| 191 | $fmt =~ s/\%regexp/$regexp/g; | ||
| 192 | return $fmt; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub run_changelog { | ||
| 196 | my ($self, $date, @param) = @_; | ||
| 197 | my(@ta) = @ARGV; | ||
| 198 | @ARGV = @param; | ||
| 199 | my(%opts,$fh); | ||
| 200 | getopts('fo:',\%opts); | ||
| 201 | @ARGV = @ta; | ||
| 202 | my($sql) = "SELECT * FROM bugs WHERE (lastdiffed > '" . range_to_date($date) . "' AND assigned_to = $login_to_uid{$llogin} AND resolution = 'FIXED')"; | ||
| 203 | my(%bugz) = query_to_bugs( $dbh->fetch_select(sql=>$sql ) ); | ||
| 204 | $fh = new FileHandle; | ||
| 205 | if($opts{o}){ | ||
| 206 | $fh->open("> $opts{o}"); | ||
| 207 | }else{ | ||
| 208 | $fh->open(">&STDOUT"); | ||
| 209 | } | ||
| 210 | foreach my $bugid (sort keys %bugz) { | ||
| 211 | print $fh "[BUG #$bugid] $bugz{$bugid}{short_desc}\n"; | ||
| 212 | if($opts{f}){ | ||
| 213 | print $fh "$bugz{$bugid}{COMMENTS}\n"; | ||
| 214 | } | ||
| 215 | |||
| 216 | } | ||
| 217 | print "Output written to $opts{o} \n" if $opts{o}; | ||
| 218 | $fh->close; | ||
| 219 | } | ||
| 220 | |||
| 221 | sub range_to_date { | ||
| 222 | my($scale) = @_; | ||
| 223 | # general format: "[d<day-of-mont/day-of-week>][w<week-of-year>][m<month>][y<year>] | ||
| 224 | # default month,year - current | ||
| 225 | # month-week mutualy excluded. | ||
| 226 | # monthes - 0-11; weekdays - 0-6; monthdays 1-31 | ||
| 227 | # week #0 is week containgin 1 Jan. | ||
| 228 | # negative value - backward from this moment i.e. kw-1 - previous week | ||
| 229 | # TODO ranges - like km2-4 for spring. | ||
| 230 | if (my ($day1,$week1,$mon1,$year1) = $scale =~ /^(?:d(-?\d+))?(?:w(-?\d+))?(?:m(-?\d+))?(?:y(-?\d+))?$/) { | ||
| 231 | # Error "wrong kalendar range given: $scale", return undef unless | ||
| 232 | # defined $day1 || ( defined $week1 && !defined $mon1); | ||
| 233 | |||
| 234 | my(undef,undef,undef,$cur_day,$cur_mon,$cur_year,$cur_wday,$cur_yday) = localtime; | ||
| 235 | my($day,$mon,$dd,$dm,$week2) = (1,0,0,0); | ||
| 236 | my($year) = !defined $year1 ? $cur_year : | ||
| 237 | $year1 > 1000 ? $year1-1900 : | ||
| 238 | $year1 > 0 ? $year1 : | ||
| 239 | $cur_year + $year1; | ||
| 240 | |||
| 241 | $mon1 = $cur_mon+$mon1 if defined $mon1 && $mon1=~/^-/; | ||
| 242 | |||
| 243 | if (defined $week1) { | ||
| 244 | my $f_wday = (localtime POSIX::mktime(0,0,0,0,0,$year,0,0,-1))[6]; | ||
| 245 | if ($week1 !~ /^-/) { | ||
| 246 | $week2 = $week1; | ||
| 247 | $week1 = $week1 * 7 - $f_wday; | ||
| 248 | } else { | ||
| 249 | $week1 = $cur_yday + 1 + $week1 * 7 - $cur_wday; | ||
| 250 | $week2 = int(($week1 + $f_wday) / 7); | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | my $k; | ||
| 255 | |||
| 256 | if (defined $day1) { | ||
| 257 | $dd = 1; | ||
| 258 | $day = $day1 > 0 ? $day1 : $cur_day + $day1; | ||
| 259 | $k .= "d$day"; | ||
| 260 | if (defined $week1) { | ||
| 261 | $day += $week1; | ||
| 262 | $k .= "w$week2"; | ||
| 263 | } elsif (defined $mon1) { | ||
| 264 | $mon = $mon1; | ||
| 265 | $k .= "m$mon"; | ||
| 266 | } else { | ||
| 267 | $mon = $cur_mon; | ||
| 268 | $k .= "m$mon"; | ||
| 269 | } | ||
| 270 | } elsif (defined $week1) { | ||
| 271 | $k .= "w$week2"; | ||
| 272 | $dd = 7; | ||
| 273 | $day = $week1; | ||
| 274 | } elsif (defined $mon1) { | ||
| 275 | $dm = 1; | ||
| 276 | $mon = $mon1; | ||
| 277 | $k .= "m$mon"; | ||
| 278 | } else { | ||
| 279 | $dm = 12; | ||
| 280 | } | ||
| 281 | |||
| 282 | $k .= "y$year"; | ||
| 283 | |||
| 284 | return strftime("%Y-%m-%d %H:%M:%S", localtime (POSIX::mktime(0,0,0,$day,$mon,$year,0,0,-1))); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | # delete command declaration {{{ | ||
| 289 | sub run_delete { | ||
| 290 | my ( $self, $comm, @params ) = @_; | ||
| 291 | if ( $comm =~ /^\d+$/ ) { | ||
| 292 | print "BUG: ID#$comm TITLE:$lastbugs{$comm}->{short_desc}\n"; | ||
| 293 | my ($ans); | ||
| 294 | if ( $params[0] ne '-f' ) { | ||
| 295 | $ans = | ||
| 296 | $self->prompt( "Are you sure that you want to delete this bug?", | ||
| 297 | "Y" ); | ||
| 298 | } | ||
| 299 | else { $ans = 'y' } | ||
| 300 | if ( $ans =~ /^y/i ) { | ||
| 301 | $dbh->query("DELETE FROM bugs WHERE bug_id=$comm"); | ||
| 302 | $dbh->query("DELETE FROM longdescs WHERE bug_id=$comm"); | ||
| 303 | } | ||
| 304 | print "Bug deleted.\n"; | ||
| 305 | } | ||
| 306 | |||
| 307 | } | ||
| 308 | |||
| 309 | # }}} | ||
| 310 | |||
| 311 | # show command declarations {{{ | ||
| 312 | |||
| 313 | sub run_show { | ||
| 314 | my ( $self, $comm ) = @_; | ||
| 315 | my (@bugids); | ||
| 316 | if ( $comm =~ /^\d+$/ ) { #ok. just show by bugid | ||
| 317 | push @bugids, $comm; | ||
| 318 | } | ||
| 319 | elsif ( $comm =~ m#^/(.*)/$# ) { #ok, got regexp. let's find em! | ||
| 320 | my ($r) = $1; | ||
| 321 | my (%a) = query_to_bugs( | ||
| 322 | $dbh->fetch_select( | ||
| 323 | sql => "SELECT bug_id FROM bugs where short_desc REGEXP '$1'" | ||
| 324 | ) | ||
| 325 | ); | ||
| 326 | |||
| 327 | push @bugids, keys %a; | ||
| 328 | } | ||
| 329 | else { | ||
| 330 | print "Whoopsie! Bad syntax. See 'help show' for explainations.\n"; | ||
| 331 | return; | ||
| 332 | } | ||
| 333 | foreach my $bugid (@bugids) { | ||
| 334 | my (%b) = query_to_bugs( | ||
| 335 | $dbh->fetch_select( | ||
| 336 | table => 'bugs', | ||
| 337 | criteria => { 'bug_id' => $bugid } | ||
| 338 | ) | ||
| 339 | ); | ||
| 340 | $self->page( print_bug( $bugid, $b{$bugid} ) ); | ||
| 341 | print "Press any key to continue..."; | ||
| 342 | getc; | ||
| 343 | } | ||
| 344 | |||
| 345 | } | ||
| 346 | |||
| 347 | sub run_take { | ||
| 348 | push @_, undef if scalar(@_) < 3; | ||
| 349 | do_fix( @_, undef, undef, undef, 'take' ); | ||
| 350 | } | ||
| 351 | |||
| 352 | sub run_comment { | ||
| 353 | push @_, undef if scalar(@_) < 3; | ||
| 354 | do_fix( @_, undef, undef, undef, 'comment' ); | ||
| 355 | } | ||
| 356 | |||
| 357 | sub run_fix { | ||
| 358 | push @_, undef if scalar(@_) < 3; | ||
| 359 | do_fix( @_, undef, undef, undef, 'fix' ); | ||
| 360 | } | ||
| 361 | |||
| 362 | |||
| 363 | sub do_fix { | ||
| 364 | my ( $self, $bugid, $text, undef, undef, undef, $cmd ) = @_; | ||
| 365 | if ( defined $bugid && $bugid =~ /^\d+/ ) { | ||
| 366 | my (%b) = query_to_bugs( | ||
| 367 | $dbh->fetch_select( | ||
| 368 | table => 'bugs', | ||
| 369 | criteria => { 'bug_id' => $bugid } | ||
| 370 | ) | ||
| 371 | ); | ||
| 372 | print "No such bug!\n", return if not keys %b; | ||
| 373 | if ( not defined $text ) { | ||
| 374 | print "Please enter a comment: (Ctrl-D to finish) \n"; | ||
| 375 | $text = join '', <STDIN>; | ||
| 376 | } | ||
| 377 | print "You should enter some comments!", return if $text eq ''; | ||
| 378 | if ( $cmd eq 'fix') { | ||
| 379 | update_bug( $dbh, $bugid, 'FIXED', 'RESOLVED', $text ); | ||
| 380 | } | ||
| 381 | elsif ( $cmd eq 'comment' ) { | ||
| 382 | update_bug( $dbh, $bugid, undef, undef, $text ); | ||
| 383 | } | ||
| 384 | elsif ( $cmd eq 'take' ) { | ||
| 385 | update_bug( $dbh, $bugid, undef, 'ASSIGNED', $text ); | ||
| 386 | } | ||
| 387 | } | ||
| 388 | else { | ||
| 389 | print "Incorrect syntax! Read 'help fix'. \n"; | ||
| 390 | } | ||
| 391 | |||
| 392 | } | ||
| 393 | |||
| 394 | # }}} | ||
| 395 | |||
| 396 | # bugs command declarations {{{ | ||
| 397 | sub run_bugs { | ||
| 398 | my ( $self, $query, $param ) = @_; | ||
| 399 | my $tb = | ||
| 400 | Text::Table->new(qw|ID SEVERITY STATUS SUBJECT VERSION RESOLUTION|); | ||
| 401 | if ($query) { | ||
| 402 | my ($q); | ||
| 403 | if ( $query =~ m#^/(.*)/$# ) { #ok user wants a regexp | ||
| 404 | $q = | ||
| 405 | "SELECT * from $config->{server}->{table} where short_desc regexp '$1'"; | ||
| 406 | } | ||
| 407 | if ( exists $config->{query}->{$query} ) { | ||
| 408 | $q = prepare_query( $config->{query}->{$query}, $param ); | ||
| 409 | } | ||
| 410 | if ($q) { | ||
| 411 | %lastbugs = query_to_bugs( $dbh->fetch_select( sql => $q ) ); | ||
| 412 | $lastquery = $q; | ||
| 413 | my (@b) = bugs_to_table(%lastbugs); | ||
| 414 | $tb->load(@b); | ||
| 415 | } | ||
| 416 | else { | ||
| 417 | print "Such query is not defined!\n"; | ||
| 418 | } | ||
| 419 | |||
| 420 | print "\n$tb"; | ||
| 421 | } | ||
| 422 | else { | ||
| 423 | $self->config_show_query(1); | ||
| 424 | } | ||
| 425 | |||
| 426 | } | ||
| 427 | |||
| 428 | 1; | ||
| 429 |