From 5cd697c78080765e1194b5b1ae3ee0f24ee6b853 Mon Sep 17 00:00:00 2001
From: Don Armstrong <don@donarmstrong.com>
Date: Mon, 12 Mar 2018 16:43:16 -0700
Subject: [PATCH] get_bug_status can now accept a schema to pull from DB

---
 Debbugs/CGI/Pkgreport.pm | 11 ++++---
 Debbugs/Status.pm        | 65 ++++++++++++++++++++++++++++++++++++++--
 2 files changed, 70 insertions(+), 6 deletions(-)

diff --git a/Debbugs/CGI/Pkgreport.pm b/Debbugs/CGI/Pkgreport.pm
index 074ae9fe..078fecaa 100644
--- a/Debbugs/CGI/Pkgreport.pm
+++ b/Debbugs/CGI/Pkgreport.pm
@@ -301,6 +301,9 @@ sub pkg_htmlizebugs {
 					  dist     => {type => SCALAR,
 						       optional => 1,
 						      },
+					  schema   => {type => OBJECT,
+						       optional => 1,
+						      },
 					 }
 			      );
      my @bugs = @{$param{bugs}};
@@ -351,10 +354,10 @@ sub pkg_htmlizebugs {
      }
 
      foreach my $bug (@bugs) {
-	  my %status = %{get_bug_status(bug=>$bug,
-					(exists $param{dist}?(dist => $param{dist}):()),
-					bugusertags => $param{bugusertags},
-					(exists $param{version}?(version => $param{version}):()),
+	 my %status = %{get_bug_status(bug=>$bug,
+				       (map {exists $param{$_}?($_,$param{$_}):()}
+					qw(dist version schema bugusertags)
+				       ),
 					(exists $param{arch}?(arch => $param{arch}):(arch => $config{default_architectures})),
 				       )};
 	  next unless %status;
diff --git a/Debbugs/Status.pm b/Debbugs/Status.pm
index 62cba343..7af7fcf1 100644
--- a/Debbugs/Status.pm
+++ b/Debbugs/Status.pm
@@ -49,7 +49,8 @@ use File::Copy qw(copy);
 use Encode qw(decode encode is_utf8);
 
 use Storable qw(dclone);
-use List::AllUtils qw(min max);
+use List::AllUtils qw(min max uniq);
+use DateTime::Format::Pg;
 
 use Carp qw(croak);
 
@@ -1248,6 +1249,9 @@ sub get_bug_status {
 					  indicatesource => {type => BOOLEAN,
 							     default => 1,
 							    },
+					  schema => {type => OBJECT,
+                                                     optional => 1,
+						    },
 					 },
 			      );
      my %status;
@@ -1260,7 +1264,64 @@ sub get_bug_status {
 	  return \%status;
      }
      if (defined $param{status}) {
-	  %status = %{$param{status}};
+	 %status = %{$param{status}};
+     }
+     elsif (defined $param{schema}) {
+	 my $b = $param{schema}->resultset('Bug')->
+	     search_rs({'me.id' => $param{bug}},
+		      {prefetch => [{'bug_tags'=>'tag'},
+				    'severity',
+				   {'bug_binpackages'=> 'bin_pkg'},
+				   {'bug_srcpackages'=> 'src_pkg'},
+				   {'bug_user_tags'=>{'user_tag'=>'correspondent'}},
+				   {owner => 'correspondent_full_names'},
+				   {submitter => 'correspondent_full_names'},
+                                    'bug_merged_bugs',
+                                    'bug_mergeds_merged',
+                                    'bug_blocks_blocks',
+                                    'bug_blocks_bugs',
+                                   {'bug_vers' => ['src_pkg','src_ver']},
+				   ],
+		       '+columns' => [qw(subject log_modified creation last_modified)],
+		       collapse => 1,
+		       result_class => 'DBIx::Class::ResultClass::HashRefInflator',
+		      })->first();
+	 $status{keywords} =
+	     join(' ',map {$_->{tag}{tag}} @{$b->{bug_tags}});
+	 $status{tags} = $status{keywords};
+	 $status{subject} = $b->{subject};
+	 $status{bug_num} = $b->{id};
+	 $status{severity} = $b->{severity}{severity};
+	 $status{package} =
+	     join(' ',
+		  (map {$_->{bin_pkg}{pkg}} @{$b->{bug_binpackages}//[]}),
+		  (map {$_->{src_pkg}{pkg}} @{$b->{bug_srcpackages}//[]}));
+         $status{originator} = $b->{submitter_full};
+	 $status{log_modified} =
+	     DateTime::Format::Pg->parse_datetime($b->{log_modified})->epoch;
+	 $status{date} =
+	     DateTime::Format::Pg->parse_datetime($b->{creation})->epoch;
+	 $status{last_modified} =
+	     DateTime::Format::Pg->parse_datetime($b->{last_modified})->epoch;
+         $status{blocks} =
+             join(' ',
+                  uniq(sort(map {$_->{block}}
+                            @{$b->{bug_blocks_block}},
+                           )));
+         $status{blockedby} =
+             join(' ',
+                  uniq(sort(map {$_->{bug}}
+                            @{$b->{bug_blocks_bug}},
+                           )));
+         $status{mergedwith} =
+             join(' ',uniq(sort(map {$_->{bug},$_->{merged}}
+                                @{$b->{bug_merged_bugs}},
+                                @{$b->{bug_mergeds_merged}},
+                               )));
+         $status{fixed_versions} =
+             [map {$_->{found}?():$_->{ver_string}} @{$b->{bug_vers}}];
+         $status{found_versions} =
+             [map {$_->{found}?$_->{ver_string}:()} @{$b->{bug_vers}}];
      }
      else {
 	  my $location = getbuglocation($param{bug}, 'summary');
-- 
2.39.5