| 1 | package Smolder::Control::Projects; | ||
| 2 | use base 'Smolder::Control'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use CGI::Application::Plugin::Stream qw(stream_file); | ||
| 6 | use DateTime; | ||
| 7 | use Smolder::DB::Project; | ||
| 8 | use Smolder::DB::SmokeReport; | ||
| 9 | use Smolder::Conf qw(HostName); | ||
| 10 | use Smolder::DB::TestFile; | ||
| 11 | use Smolder::DB::TestFileResult; | ||
| 12 | use Smolder::Util; | ||
| 13 | use Smolder::Control::Public::Auth; | ||
| 14 | use Smolder::Constraints qw( | ||
| 15 | enum_value | ||
| 16 | length_max | ||
| 17 | unsigned_int | ||
| 18 | bool | ||
| 19 | file_mtype | ||
| 20 | smoke_report_tags | ||
| 21 | ); | ||
| 22 | use Exception::Class; | ||
| 23 | use HTML::TagCloud; | ||
| 24 | use URI::Escape qw(uri_escape); | ||
| 25 | use XML::Atom::SimpleFeed; | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | Smolder::Control::Projects | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | Controller module that deals with actions associated with projects and test reports | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | sub setup { | ||
| 38 | my $self = shift; | ||
| 39 | $self->start_mode('show_all'); | ||
| 40 | $self->run_modes( | ||
| 41 | [ | ||
| 42 | qw( | ||
| 43 | show_all | ||
| 44 | public | ||
| 45 | add_report | ||
| 46 | process_add_report | ||
| 47 | smoke_reports | ||
| 48 | smoke_report | ||
| 49 | report_details | ||
| 50 | test_file_report_details | ||
| 51 | platform_options | ||
| 52 | architecture_options | ||
| 53 | smoke_test_validity | ||
| 54 | admin_settings | ||
| 55 | process_admin_settings | ||
| 56 | delete_tag | ||
| 57 | details | ||
| 58 | tap_archive | ||
| 59 | tap_stream | ||
| 60 | mute_testfiles | ||
| 61 | comment_testfiles | ||
| 62 | test_file_history | ||
| 63 | feed | ||
| 64 | ) | ||
| 65 | ] | ||
| 66 | ); | ||
| 67 | } | ||
| 68 | |||
| 69 | =head1 RUN MODES | ||
| 70 | |||
| 71 | =head2 smoke_test_validity | ||
| 72 | |||
| 73 | Set or unset the C<invalid> flag on a given smoke report. Uses the | ||
| 74 | F<Projects/smoke_report_details.tmpl> template. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub smoke_test_validity { | ||
| 79 | my $self = shift; | ||
| 80 | my $report = Smolder::DB::SmokeReport->retrieve($self->param('id')); | ||
| 81 | return $self->error_message("Smoke Report does not exist!") | ||
| 82 | unless $report; | ||
| 83 | |||
| 84 | # only project admins can do this | ||
| 85 | return $self->error_message("Not an admin of this project!") | ||
| 86 | unless $report->project->is_admin($self->developer); | ||
| 87 | |||
| 88 | # make sure it's not too long or malicious | ||
| 89 | my $form = { | ||
| 90 | required => [qw(invalid)], | ||
| 91 | optional => [qw(invalid_reason)], | ||
| 92 | constraint_methods => { | ||
| 93 | invalid => bool(), | ||
| 94 | invalid_reason => length_max(255), | ||
| 95 | } | ||
| 96 | }; | ||
| 97 | my $results = Data::FormValidator->check($self->query, $form) | ||
| 98 | || return $self->error_message('Invalid data!'); | ||
| 99 | my $valid = $results->valid(); | ||
| 100 | |||
| 101 | # now update the DB | ||
| 102 | $report->invalid($valid->{invalid}); | ||
| 103 | $report->invalid_reason($valid->{invalid_reason}); | ||
| 104 | $report->update(); | ||
| 105 | |||
| 106 | # notify the user | ||
| 107 | $self->add_message(msg => "Report #$report has been marked as " | ||
| 108 | . ($valid->{invalid} ? 'invalid' : 'valid') | ||
| 109 | . "."); | ||
| 110 | return $self->tt_process( | ||
| 111 | 'Projects/smoke_report_details.tmpl', | ||
| 112 | {report => $report, project => $report->project}, | ||
| 113 | ); | ||
| 114 | } | ||
| 115 | |||
| 116 | =head2 platform_options | ||
| 117 | |||
| 118 | Returns an HTML list sutiable for a Prototype based "Autocomplete" form | ||
| 119 | of exising platform otpions | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub platform_options { | ||
| 124 | my $self = shift; | ||
| 125 | return $self->auto_complete_results( | ||
| 126 | Smolder::DB::SmokeReport->column_values('platform', $self->query->param('platform'),)); | ||
| 127 | } | ||
| 128 | |||
| 129 | =head2 architecture_options | ||
| 130 | |||
| 131 | Returns an HTML list sutiable for a Prototype based "Autocomplete" form | ||
| 132 | of exising architecture otpions | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub architecture_options { | ||
| 137 | my $self = shift; | ||
| 138 | return $self->auto_complete_results( | ||
| 139 | Smolder::DB::SmokeReport->column_values( | ||
| 140 | 'architecture', $self->query->param('architecture'), | ||
| 141 | ) | ||
| 142 | ); | ||
| 143 | } | ||
| 144 | |||
| 145 | =head2 add_report | ||
| 146 | |||
| 147 | Shows the form to allow the developer to add a new smoke report to a project. | ||
| 148 | Uses the C<Projects/add_report.tmpl> template. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | sub add_report { | ||
| 153 | my ($self, $tt_params) = @_; | ||
| 154 | $tt_params ||= {}; | ||
| 155 | |||
| 156 | my $project = Smolder::DB::Project->retrieve($self->param('id')); | ||
| 157 | return $self->error_message('Project does not exist') | ||
| 158 | unless $project; | ||
| 159 | |||
| 160 | # make sure ths developer is a member of this project, or it's a public project | ||
| 161 | # that allows anonymous uploads | ||
| 162 | if (!$project->has_developer($self->developer)) { | ||
| 163 | if ($project->public && !$project->allow_anon) { | ||
| 164 | return $self->error_message('Project does not allow anonymous reports'); | ||
| 165 | } elsif (!$project->public) { | ||
| 166 | return $self->error_message('Unauthorized for this project'); | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | $tt_params->{project} = $project; | ||
| 171 | return $self->tt_process($tt_params); | ||
| 172 | } | ||
| 173 | |||
| 174 | =head2 process_add_report | ||
| 175 | |||
| 176 | Process the incoming information from the C<add_report> mode. If validation passes | ||
| 177 | we upload the file and update the database. The report is stored for permanent storage, | ||
| 178 | and summary information is extracted and inserted into the database. | ||
| 179 | If successful, redirects the user to the "Recent Smoke Tests" screen for the same | ||
| 180 | project. | ||
| 181 | |||
| 182 | If the newly uploaded report causes the C<ProjectFullReportsMax> configuration limit | ||
| 183 | to be reached then any full reports over that limit will be purged. Their summary data | ||
| 184 | will still be retained in the database, but the compressed XML file which contains the | ||
| 185 | details will be removed, and the reports details will no longer be accessible. | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | sub _goto_login { | ||
| 190 | my $self = shift; | ||
| 191 | my $q = $self->query; | ||
| 192 | $self->header_type('redirect'); | ||
| 193 | my $url = "/app/public_auth/login"; | ||
| 194 | $self->header_props(-url => $url, -status => '401'); | ||
| 195 | return "Redirecting to $url"; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub process_add_report { | ||
| 199 | my $self = shift; | ||
| 200 | my $q = $self->query; | ||
| 201 | my $project = Smolder::DB::Project->retrieve($self->param('id')); | ||
| 202 | return $self->error_message('Project does not exist') | ||
| 203 | unless $project; | ||
| 204 | |||
| 205 | if ($q->param('username') && $q->param('password')) { | ||
| 206 | Smolder::Control::Public::Auth::do_login($self, $q->param('username'), | ||
| 207 | $q->param('password')); | ||
| 208 | } | ||
| 209 | |||
| 210 | # make sure ths developer is a member of this project, or it's a public project | ||
| 211 | # that allows anonymous uploads | ||
| 212 | if (!$project->has_developer($self->developer)) { | ||
| 213 | if ($project->public && !$project->allow_anon) { | ||
| 214 | return $self->error_message('Project does not allow anonymous reports'); | ||
| 215 | } elsif (!$project->public) { | ||
| 216 | return $self->error_message('Unauthorized for this project'); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | my $form = { | ||
| 221 | required => [qw(report_file)], | ||
| 222 | optional => [qw(architecture platform comments tags revision)], | ||
| 223 | constraint_methods => { | ||
| 224 | architecture => length_max(255), | ||
| 225 | platform => length_max(255), | ||
| 226 | revision => length_max(255), | ||
| 227 | comments => length_max(1000), | ||
| 228 | report_file => file_mtype( | ||
| 229 | qw( | ||
| 230 | application/x-gzip | ||
| 231 | application/x-gtar | ||
| 232 | application/x-tar | ||
| 233 | application/x-zip | ||
| 234 | multipart/x-gzip | ||
| 235 | ) | ||
| 236 | ), | ||
| 237 | tags => smoke_report_tags(), | ||
| 238 | }, | ||
| 239 | }; | ||
| 240 | |||
| 241 | my $results = $self->check_rm('add_report', $form) | ||
| 242 | || return $self->check_rm_error_page; | ||
| 243 | my $valid = $results->valid(); | ||
| 244 | |||
| 245 | my $report; | ||
| 246 | eval { | ||
| 247 | $report = Smolder::DB::SmokeReport->upload_report( | ||
| 248 | file => $valid->{report_file}, | ||
| 249 | developer => $self->developer, | ||
| 250 | project => $project, | ||
| 251 | architecture => $valid->{architecture}, | ||
| 252 | platform => $valid->{platform}, | ||
| 253 | comments => $valid->{comments}, | ||
| 254 | revision => $valid->{revision}, | ||
| 255 | ); | ||
| 256 | }; | ||
| 257 | |||
| 258 | # is this an exception we can deal with? | ||
| 259 | my $e; | ||
| 260 | if ($e = Exception::Class->caught('Smolder::Exception::InvalidTAP')) { | ||
| 261 | $self->log->warning($e->error . "\n" . $e->trace->as_string); | ||
| 262 | return $self->add_report({invalid_report_file => 1}); | ||
| 263 | } elsif ($e = Exception::Class->caught('Smolder::Exception::InvalidArchive')) { | ||
| 264 | $self->log->warning($e->error . "\n" . $e->trace->as_string); | ||
| 265 | return $self->add_report({invalid_report_file => 1}); | ||
| 266 | } else { | ||
| 267 | $e = Exception::Class->caught(); | ||
| 268 | if ($e) { | ||
| 269 | ref $e && $e->isa('Exception::Class') ? $e->rethrow : die $e; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | |||
| 273 | # add the tags if present | ||
| 274 | if ($valid->{tags}) { | ||
| 275 | $report->add_tag($_) foreach @{$valid->{tags}}; | ||
| 276 | } | ||
| 277 | |||
| 278 | # redirect to our recent reports | ||
| 279 | $self->header_type('redirect'); | ||
| 280 | my $url = "/app/projects/smoke_reports/$project"; | ||
| 281 | $self->header_add(-uri => $url); | ||
| 282 | return "Reported #$report added.\nRedirecting to $url"; | ||
| 283 | } | ||
| 284 | |||
| 285 | =head2 smoke_report | ||
| 286 | |||
| 287 | Shows the summary info for a given smoke_report. Uses the F<Projects/smoke_report.tmpl> | ||
| 288 | template. | ||
| 289 | |||
| 290 | =cut | ||
| 291 | |||
| 292 | sub smoke_report { | ||
| 293 | my $self = shift; | ||
| 294 | my $query = $self->query(); | ||
| 295 | |||
| 296 | my $smoke = Smolder::DB::SmokeReport->retrieve($self->param('id')); | ||
| 297 | return $self->error_message('Smoke report does not exist') | ||
| 298 | unless $smoke; | ||
| 299 | my $project = $smoke->project; | ||
| 300 | |||
| 301 | # make sure ths developer is a member of this project | ||
| 302 | return $self->error_message('Unauthorized for this project') | ||
| 303 | unless $self->can_see_project($project); | ||
| 304 | |||
| 305 | return $self->tt_process({report => $smoke, project => $project}); | ||
| 306 | } | ||
| 307 | |||
| 308 | =head2 smoke_reports | ||
| 309 | |||
| 310 | Shows a list of smoke reports for a given project based on the limit, offset | ||
| 311 | and tag parameters. Uses the F<Projects/smoke_reports.tmpl> | ||
| 312 | template. | ||
| 313 | |||
| 314 | =cut | ||
| 315 | |||
| 316 | sub smoke_reports { | ||
| 317 | my ($self, $tt_params) = @_; | ||
| 318 | $tt_params ||= {}; | ||
| 319 | my $query = $self->query(); | ||
| 320 | |||
| 321 | my $project = Smolder::DB::Project->retrieve($self->param('id')); | ||
| 322 | return $self->error_message('Project does not exist') | ||
| 323 | unless $project; | ||
| 324 | |||
| 325 | # make sure ths developer is a member of this project | ||
| 326 | return $self->error_message('Unauthorized for this project') | ||
| 327 | unless $self->can_see_project($project); | ||
| 328 | |||
| 329 | # prevent malicious limit and offset values | ||
| 330 | my $form = { | ||
| 331 | optional => [qw(limit offset tag)], | ||
| 332 | constraint_methods => { | ||
| 333 | limit => unsigned_int(), | ||
| 334 | offset => unsigned_int(), | ||
| 335 | }, | ||
| 336 | }; | ||
| 337 | return $self->error_message('Something fishy') | ||
| 338 | unless Data::FormValidator->check($query, $form); | ||
| 339 | |||
| 340 | $tt_params->{project} = $project; | ||
| 341 | $tt_params->{limit} = | ||
| 342 | defined $query->param('limit') | ||
| 343 | ? $query->param('limit') | ||
| 344 | : (Smolder::Conf->get('ReportsPerPage') || 5); | ||
| 345 | $tt_params->{offset} = $query->param('offset') || 0; | ||
| 346 | $tt_params->{tag} = $query->param('tag') || undef; | ||
| 347 | |||
| 348 | return $self->tt_process($tt_params); | ||
| 349 | } | ||
| 350 | |||
| 351 | =head2 report_details | ||
| 352 | |||
| 353 | Show the full report for download. Does not use a template but simply | ||
| 354 | outputs the pregenerated file. | ||
| 355 | |||
| 356 | =cut | ||
| 357 | |||
| 358 | sub report_details { | ||
| 359 | my $self = shift; | ||
| 360 | my $report = Smolder::DB::SmokeReport->retrieve($self->param('id')); | ||
| 361 | |||
| 362 | if (Smolder::Conf->get('AutoRefreshReports')) { | ||
| 363 | $report->update_from_tap_archive(); | ||
| 364 | } | ||
| 365 | |||
| 366 | return $self->error_message('Test Report does not exist') | ||
| 367 | unless $report; | ||
| 368 | |||
| 369 | # make sure ths developer is a member of this project | ||
| 370 | return $self->error_message('Unauthorized for this project') | ||
| 371 | unless $self->can_see_project($report->project); | ||
| 372 | |||
| 373 | my $tt_params = { | ||
| 374 | tap => ${$report->html}, | ||
| 375 | project => $report->project, | ||
| 376 | report => $report, | ||
| 377 | }; | ||
| 378 | |||
| 379 | return $self->tt_process('Projects/tap.tmpl', $tt_params),; | ||
| 380 | } | ||
| 381 | |||
| 382 | =head2 test_file_report_details | ||
| 383 | |||
| 384 | Show the details of an individual test file. This is called from | ||
| 385 | the HTML page generated by L<report_details> by an AJAX call. | ||
| 386 | |||
| 387 | =cut | ||
| 388 | |||
| 389 | sub test_file_report_details { | ||
| 390 | my $self = shift; | ||
| 391 | my $report = Smolder::DB::SmokeReport->retrieve($self->param('id')); | ||
| 392 | return $self->error_message('Test Report does not exist') | ||
| 393 | unless $report; | ||
| 394 | my $num = $self->param('type') || 0; | ||
| 395 | |||
| 396 | # make sure ths developer is a member of this project | ||
| 397 | return $self->error_message('Unauthorized for this project') | ||
| 398 | unless $self->can_see_project($report->project); | ||
| 399 | return $report->html_test_detail($num); | ||
| 400 | } | ||
| 401 | |||
| 402 | =head2 tap_archive | ||
| 403 | |||
| 404 | Return the TAP archive for a given report to the browser | ||
| 405 | |||
| 406 | =cut | ||
| 407 | |||
| 408 | sub tap_archive { | ||
| 409 | my $self = shift; | ||
| 410 | my $report = Smolder::DB::SmokeReport->retrieve($self->param('id')); | ||
| 411 | return $self->error_message('Test Report does not exist') | ||
| 412 | unless $report; | ||
| 413 | |||
| 414 | # make sure ths developer is a member of this project | ||
| 415 | return $self->error_message('Unauthorized for this project') | ||
| 416 | unless $self->can_see_project($report->project); | ||
| 417 | |||
| 418 | return $self->stream_file($report->file); | ||
| 419 | } | ||
| 420 | |||
| 421 | =head2 tap_stream | ||
| 422 | |||
| 423 | Return the TAP stream for a given report and given stream index number to the browser. | ||
| 424 | |||
| 425 | =cut | ||
| 426 | |||
| 427 | sub tap_stream { | ||
| 428 | my $self = shift; | ||
| 429 | my $report_id = $self->param('id'); | ||
| 430 | my $tap_index = $self->param('stream_index'); | ||
| 431 | my $report = Smolder::DB::SmokeReport->retrieve($report_id); | ||
| 432 | return $self->error_message("Test Report $report_id does not exist") | ||
| 433 | unless $report; | ||
| 434 | |||
| 435 | # make sure ths developer is a member of this project | ||
| 436 | return $self->error_message('Unauthorized for this project') | ||
| 437 | unless $self->can_see_project($report->project); | ||
| 438 | my $output = $report->tap_stream($tap_index); | ||
| 439 | $$output = '<pre>' . $$output . '</pre>'; | ||
| 440 | return $output; | ||
| 441 | } | ||
| 442 | |||
| 443 | =head2 show_all | ||
| 444 | |||
| 445 | Show all of the projects this developer is associated with and a menu for | ||
| 446 | each one. | ||
| 447 | |||
| 448 | =cut | ||
| 449 | |||
| 450 | sub show_all { | ||
| 451 | my $self = shift; | ||
| 452 | my @projects = $self->developer->projects; | ||
| 453 | my $public = $self->public_projects; | ||
| 454 | |||
| 455 | # only keep the stuff in public that's not in our project list | ||
| 456 | my @non_overlap_public; | ||
| 457 | foreach my $pub_proj (@$public) { | ||
| 458 | my $found = 0; | ||
| 459 | foreach my $proj (@projects) { | ||
| 460 | if( $proj->id == $pub_proj->id ) { | ||
| 461 | $found = 1; | ||
| 462 | next; | ||
| 463 | } | ||
| 464 | } | ||
| 465 | push(@non_overlap_public, $pub_proj) if !$found; | ||
| 466 | } | ||
| 467 | return $self->tt_process({my_projects => \@projects, public_projects => \@non_overlap_public}); | ||
| 468 | } | ||
| 469 | |||
| 470 | =head2 public | ||
| 471 | |||
| 472 | Show all of the public projects with and a menu for each one. | ||
| 473 | |||
| 474 | =cut | ||
| 475 | |||
| 476 | sub public { | ||
| 477 | my $self = shift; | ||
| 478 | return $self->tt_process({public_projects => $self->public_projects}); | ||
| 479 | } | ||
| 480 | |||
| 481 | =head2 admin_settings | ||
| 482 | |||
| 483 | If this developer is the admin of a project then show them a form to update some | ||
| 484 | project specific settings. Uses the F<Projects/admin_settings_form.tmpl> | ||
| 485 | and the F<Projects/admin_settings.tmpl> templates. | ||
| 486 | |||
| 487 | =cut | ||
| 488 | |||
| 489 | sub admin_settings { | ||
| 490 | my ($self, $tt_params) = @_; | ||
| 491 | |||
| 492 | my $project = Smolder::DB::Project->retrieve($self->param('id')); | ||
| 493 | return $self->error_message('Project does not exist') | ||
| 494 | unless $project; | ||
| 495 | |||
| 496 | # only show if this developer is an admin of this project | ||
| 497 | return $self->error_message('You are not an admin of this Project!') | ||
| 498 | unless $project->is_admin($self->developer); | ||
| 499 | |||
| 500 | # if we have something then we're coming from another sub so fill the | ||
| 501 | # form from the CGI params | ||
| 502 | my $out; | ||
| 503 | if ($tt_params) { | ||
| 504 | $tt_params->{project} = $project; | ||
| 505 | $tt_params->{tag_cloud} = $self->_project_tag_cloud($project); | ||
| 506 | $out = HTML::FillInForm->new()->fill( | ||
| 507 | scalarref => | ||
| 508 | $self->tt_process('Projects/admin_settings_form.tmpl', $tt_params), | ||
| 509 | fobject => $self->query(), | ||
| 510 | ); | ||
| 511 | |||
| 512 | } else { | ||
| 513 | |||
| 514 | # else we weren't passed anything, then we need to fill in the form | ||
| 515 | # from the DB | ||
| 516 | $tt_params = {}; | ||
| 517 | $tt_params->{project} = $project; | ||
| 518 | $tt_params->{tag_cloud} = $self->_project_tag_cloud($project); | ||
| 519 | my $fill_data = { | ||
| 520 | default_platform => $project->default_platform, | ||
| 521 | default_arch => $project->default_arch, | ||
| 522 | allow_anon => $project->allow_anon, | ||
| 523 | graph_start => $project->graph_start, | ||
| 524 | }; | ||
| 525 | $out = HTML::FillInForm->new()->fill( | ||
| 526 | scalarref => $self->tt_process($tt_params), | ||
| 527 | fdat => $fill_data, | ||
| 528 | ); | ||
| 529 | } | ||
| 530 | return $out; | ||
| 531 | } | ||
| 532 | |||
| 533 | sub _project_tag_cloud { | ||
| 534 | my ($self, $project, $url) = @_; | ||
| 535 | my @tags = $project->tags(with_counts => 1); | ||
| 536 | if (@tags) { | ||
| 537 | my $cloud = HTML::TagCloud->new(); | ||
| 538 | foreach (@tags) { | ||
| 539 | my $tag_url = $url ? "$url?tag=" . uri_escape($_->{tag}) : 'javascript:void(0)'; | ||
| 540 | $cloud->add($_->{tag}, $tag_url, $_->{count}); | ||
| 541 | } | ||
| 542 | return $cloud->html_and_css(100); | ||
| 543 | } else { | ||
| 544 | return ''; | ||
| 545 | } | ||
| 546 | } | ||
| 547 | |||
| 548 | =head2 process_admin_settings | ||
| 549 | |||
| 550 | Process the incoming information from the C<admin_settings> mode. If | ||
| 551 | it passes validation then update the database. If successful, returns | ||
| 552 | to the C<admin_settings> mode. | ||
| 553 | |||
| 554 | =cut | ||
| 555 | |||
| 556 | sub process_admin_settings { | ||
| 557 | my $self = shift; | ||
| 558 | my $project = Smolder::DB::Project->retrieve($self->param('id')); | ||
| 559 | return $self->error_message('Project does not exist') | ||
| 560 | unless $project; | ||
| 561 | |||
| 562 | # only process if this developer is an admin of this project | ||
| 563 | return $self->error_message('You are not an admin of this Project!') | ||
| 564 | unless $project->is_admin($self->developer); | ||
| 565 | |||
| 566 | # validate the incoming data | ||
| 567 | my $form = { | ||
| 568 | required => [qw(allow_anon graph_start)], | ||
| 569 | optional => [qw(default_arch default_platform)], | ||
| 570 | constraint_methods => { | ||
| 571 | allow_anon => bool(), | ||
| 572 | default_arch => length_max(255), | ||
| 573 | default_platform => length_max(255), | ||
| 574 | graph_start => enum_value('project', 'graph_start'), | ||
| 575 | }, | ||
| 576 | }; | ||
| 577 | my $results = $self->check_rm('admin_settings', $form) | ||
| 578 | || return $self->check_rm_error_page; | ||
| 579 | my $valid = $results->valid(); | ||
| 580 | |||
| 581 | # set and save | ||
| 582 | foreach my $field qw(allow_anon default_arch default_platform graph_start) { | ||
| 583 | $project->$field($valid->{$field}); | ||
| 584 | } | ||
| 585 | $project->update(); | ||
| 586 | |||
| 587 | $self->add_message(msg => "Project settings successfully updated.",); | ||
| 588 | return $self->admin_settings({success => 1}); | ||
| 589 | } | ||
| 590 | |||
| 591 | =head2 delete_tag | ||
| 592 | |||
| 593 | Deletes a tag that is associated with a given Project. If validation | ||
| 594 | passes the database is updated and all smoke reports that were associated | ||
| 595 | with this tag are either re-assigned or simply not associated with a | ||
| 596 | tag (depending on what the project admin chooses). Returns to the | ||
| 597 | C<admin_settings> mode if successful. | ||
| 598 | |||
| 599 | =cut | ||
| 600 | |||
| 601 | sub delete_tag { | ||
| 602 | my $self = shift; | ||
| 603 | my $project = Smolder::DB::Project->retrieve($self->param('id')); | ||
| 604 | return $self->error_message('Project does not exist') | ||
| 605 | unless $project; | ||
| 606 | |||
| 607 | # only process if this developer is an admin of this project | ||
| 608 | return $self->error_message('You are not an admin of this Project!') | ||
| 609 | unless $project->is_admin($self->developer); | ||
| 610 | |||
| 611 | my $query = $self->query(); | ||
| 612 | my ($tag, $replacement) = map { $query->param($_) } qw(tag replacement); | ||
| 613 | |||
| 614 | if ($replacement) { | ||
| 615 | # change the tag | ||
| 616 | $project->change_tag($tag, $replacement); | ||
| 617 | $self->add_message(msg => "Tag '$tag' was successfully replaced by '$replacement'."); | ||
| 618 | } else { | ||
| 619 | # delete the old tag | ||
| 620 | $project->delete_tag($tag); | ||
| 621 | $self->add_message( | ||
| 622 | msg => "Tag '$tag' successfully deleted from project '" . $project->name . "'."); | ||
| 623 | } | ||
| 624 | |||
| 625 | return $self->admin_settings(); | ||
| 626 | } | ||
| 627 | |||
| 628 | =head2 details | ||
| 629 | |||
| 630 | Shows the details of a project. | ||
| 631 | |||
| 632 | =cut | ||
| 633 | |||
| 634 | sub details { | ||
| 635 | my $self = shift; | ||
| 636 | my $id = $self->param('id'); | ||
| 637 | my $proj = Smolder::DB::Project->retrieve($id); | ||
| 638 | |||
| 639 | if ($proj) { | ||
| 640 | return $self->error_message('Unauthorized for this project') | ||
| 641 | unless $self->can_see_project($proj); | ||
| 642 | my $tag_cloud = $self->_project_tag_cloud($proj, "/app/projects/smoke_reports/$proj"); | ||
| 643 | return $self->tt_process({project => $proj, tag_cloud => $tag_cloud}); | ||
| 644 | } else { | ||
| 645 | return $self->error_message('That project does not exist!'); | ||
| 646 | } | ||
| 647 | } | ||
| 648 | |||
| 649 | =head2 mute_testfiles | ||
| 650 | |||
| 651 | Mute a group of test files. | ||
| 652 | |||
| 653 | =cut | ||
| 654 | |||
| 655 | sub mute_testfiles { | ||
| 656 | my $self = shift; | ||
| 657 | my $q = $self->query; | ||
| 658 | my $report = Smolder::DB::SmokeReport->retrieve($self->param('id')); | ||
| 659 | |||
| 660 | # Determine which test files were checked | ||
| 661 | my @testfile_ids = $q->param('testfiles'); | ||
| 662 | my @testfiles = map { Smolder::DB::TestFile->retrieve($_) } @testfile_ids; | ||
| 663 | |||
| 664 | # mute those files | ||
| 665 | my $days = $q->param('days'); | ||
| 666 | my $mute_until_time = DateTime->now(time_zone => 'local')->add(days => $days)->truncate(to => 'day')->epoch; | ||
| 667 | foreach my $test_file (@testfiles) { | ||
| 668 | return $self->error_message('Unauthorized for this project') | ||
| 669 | unless $test_file->project->has_developer($self->developer); | ||
| 670 | $test_file->mute_until($mute_until_time); | ||
| 671 | $test_file->update; | ||
| 672 | } | ||
| 673 | |||
| 674 | # Recompute page after any bulk action | ||
| 675 | $report->update_from_tap_archive(); | ||
| 676 | $self->add_message(msg => $days ? "Test files successfully muted." : "Test files successfully un-muted."); | ||
| 677 | my $return_to = $q->param('return_to') || 'report_details'; | ||
| 678 | return $self->$return_to; | ||
| 679 | } | ||
| 680 | |||
| 681 | =head2 comment_testfiles | ||
| 682 | |||
| 683 | Create a comment on a group of test files. | ||
| 684 | |||
| 685 | =cut | ||
| 686 | |||
| 687 | sub comment_testfiles { | ||
| 688 | my $self = shift; | ||
| 689 | my $q = $self->query; | ||
| 690 | |||
| 691 | # attach the comment to the given testfiles | ||
| 692 | my @testfiles = map { Smolder::DB::TestFile->retrieve($_) } ($q->param('testfiles')); | ||
| 693 | my $comment = $q->param('comment'); | ||
| 694 | foreach my $test_file (@testfiles) { | ||
| 695 | return $self->error_message('Unauthorized for this project') | ||
| 696 | unless $test_file->project->has_developer($self->developer); | ||
| 697 | Smolder::DB::TestFileComment->create( | ||
| 698 | { | ||
| 699 | project => $test_file->project, | ||
| 700 | developer => $self->developer, | ||
| 701 | comment => $comment, | ||
| 702 | test_file => $test_file, | ||
| 703 | } | ||
| 704 | ); | ||
| 705 | } | ||
| 706 | |||
| 707 | $self->add_message(msg => 'Comment successfully added'); | ||
| 708 | $self->param(id => $testfiles[0]->project->id); | ||
| 709 | $self->param(test_file_id => $testfiles[0]->id); | ||
| 710 | return $self->test_file_history; | ||
| 711 | } | ||
| 712 | |||
| 713 | =head2 test_file_history | ||
| 714 | |||
| 715 | Show history of a particular test file in this project | ||
| 716 | |||
| 717 | =cut | ||
| 718 | |||
| 719 | sub test_file_history { | ||
| 720 | my $self = shift; | ||
| 721 | my $q = $self->query; | ||
| 722 | my $project = Smolder::DB::Project->retrieve($self->param('id')); | ||
| 723 | my $test_file = Smolder::DB::TestFile->retrieve($self->param('test_file_id')); | ||
| 724 | |||
| 725 | return $self->error_message('Unauthorized for this project') | ||
| 726 | unless $self->can_see_project($project); | ||
| 727 | |||
| 728 | # prevent malicious limit and offset values | ||
| 729 | my $form = { | ||
| 730 | optional => [qw(limit offset)], | ||
| 731 | constraint_methods => { | ||
| 732 | limit => unsigned_int(), | ||
| 733 | offset => unsigned_int(), | ||
| 734 | }, | ||
| 735 | }; | ||
| 736 | return $self->error_message('Something fishy') | ||
| 737 | unless Data::FormValidator->check($q, $form); | ||
| 738 | |||
| 739 | my $limit = $q->param('limit') || 20; | ||
| 740 | my $offset = $q->param('offset') || 0; | ||
| 741 | my @test_file_results = Smolder::DB::TestFileResult->search_where( | ||
| 742 | { | ||
| 743 | project => $project, | ||
| 744 | test_file => $test_file | ||
| 745 | }, | ||
| 746 | { | ||
| 747 | limit_dialect => 'LimitOffset', | ||
| 748 | offset => $offset, | ||
| 749 | limit => $limit, | ||
| 750 | order_by => 'added DESC' | ||
| 751 | } | ||
| 752 | ); | ||
| 753 | my $tt_params = { | ||
| 754 | project => $project, | ||
| 755 | test_file => $test_file, | ||
| 756 | results => \@test_file_results | ||
| 757 | }; | ||
| 758 | return $self->tt_process('Projects/test_file_history.tmpl', $tt_params); | ||
| 759 | } | ||
| 760 | |||
| 761 | =head2 feed | ||
| 762 | |||
| 763 | Will return an XML data feed (Atom) to the browser. The 5 most recent smoke | ||
| 764 | reports for a project are included in this feed. An optional C<type> | ||
| 765 | can also be specified which is can either be C<all> or C<failures>.Only | ||
| 766 | projects that have been marked as C<enable_feed> will appear in any feed. | ||
| 767 | |||
| 768 | =cut | ||
| 769 | |||
| 770 | sub feed { | ||
| 771 | my $self = shift; | ||
| 772 | my @binds; | ||
| 773 | my $sql = qq/ | ||
| 774 | SELECT sr.* FROM smoke_report sr | ||
| 775 | JOIN project p ON (sr.project = p.id) | ||
| 776 | WHERE p.enable_feed = 1 AND p.id = ? | ||
| 777 | /; | ||
| 778 | my $id = $self->param('id'); | ||
| 779 | my $project = Smolder::DB::Project->retrieve($id); | ||
| 780 | |||
| 781 | return $self->error_message('Unauthorized for this project') | ||
| 782 | unless $self->can_see_project($project); | ||
| 783 | |||
| 784 | my $type = $self->param('type'); | ||
| 785 | push(@binds, $id); | ||
| 786 | |||
| 787 | if ($type and $type eq 'failed') { | ||
| 788 | $sql .= ' AND sr.failed = 1 '; | ||
| 789 | } | ||
| 790 | |||
| 791 | $sql .= ' ORDER BY sr.added DESC LIMIT 5'; | ||
| 792 | |||
| 793 | my $sth = Smolder::DB::SmokeReport->db_Main->prepare_cached($sql); | ||
| 794 | $sth->execute(@binds); | ||
| 795 | my @reports = Smolder::DB::SmokeReport->sth_to_objects($sth); | ||
| 796 | |||
| 797 | $self->header_props(-type => 'text/xml'); | ||
| 798 | |||
| 799 | my $updated; | ||
| 800 | if (@reports) { | ||
| 801 | $updated = $reports[0]->added; | ||
| 802 | } else { | ||
| 803 | $updated = DateTime->now(); | ||
| 804 | } | ||
| 805 | |||
| 806 | my $feed = XML::Atom::SimpleFeed->new( | ||
| 807 | title => '[' . $project->name . '] Smolder - ' . HostName, | ||
| 808 | link => Smolder::Util::url_base, | ||
| 809 | id => Smolder::Util::url_base, | ||
| 810 | updated => $updated->strftime('%FT%TZ'), | ||
| 811 | ); | ||
| 812 | |||
| 813 | foreach my $report (@reports) { | ||
| 814 | my $link = | ||
| 815 | Smolder::Util::url_base() . '/app/' | ||
| 816 | . ($project->public ? 'public' : 'developer') | ||
| 817 | . '_projects/smoke_report/' | ||
| 818 | . $report->id; | ||
| 819 | $feed->add_entry( | ||
| 820 | title => '#' | ||
| 821 | . $report->id . ' - ' | ||
| 822 | . ($report->failed ? 'Failed' : 'New') | ||
| 823 | . ' Smoke Report', | ||
| 824 | author => $report->developer->username, | ||
| 825 | link => $link, | ||
| 826 | id => $link, | ||
| 827 | summary => $report->summary, | ||
| 828 | updated => $report->added->strftime('%FT%TZ'), | ||
| 829 | ); | ||
| 830 | } | ||
| 831 | return $feed->as_string(); | ||
| 832 | } | ||
| 833 | |||
| 834 | 1; |