3 =head1 NAME $RCSfile: BugzillaUtils.pm,v $
5 Some shared functions dealing with Bugzilla. Note this uses DBI to directly
6 access Bugzilla's database. This requires that your userid was granted access.
7 For this I setup adefaria with pretty much read only access.
15 Andrew DeFaria <Andrew@ClearSCM.com>
23 Fri Mar 12 10:17:44 PST 2004
27 $Date: 2013/05/30 15:48:06 $
31 The following routines are exported:
35 package BugzillaUtils;
65 sub _checkDBError ($$) {
66 my ($msg, $statement) = @_;
68 my $dberr = $bugzilla->err;
69 my $dberrmsg = $bugzilla->errstr;
72 $dberrmsg ||= 'Success';
77 my $function = (caller (1)) [3];
79 $message = "$function: $msg\nError #$dberr: $dberrmsg\n"
80 . "SQL Statement: $statement";
83 $main::log->err ($message, $dberr) if $dberr;
88 sub openBugzilla (;$$$$) {
89 my ($dbhost, $dbname, $dbuser, $dbpass) = @_;
91 $dbhost //= 'jira-dev';
92 $dbname //= 'bugzilla';
93 $dbuser //= 'adefaria';
96 $main::log->msg ("Connecting to Bugzilla ($dbuser\@$dbhost)");
98 $bugzilla = DBI->connect (
99 "DBI:mysql:$dbname:$dbhost",
107 _checkDBError 'Unable to execute statement', 'Connect';
113 my ($bugid, @fields) = @_;
115 push @fields, 'short_desc' unless @fields;
117 my $statement = 'select ' . join (',', @fields) .
118 " from bugs where bug_id = $bugid";
120 my $sth = $bugzilla->prepare ($statement);
122 _checkDBError 'Unable to prepare statement', $statement;
124 _checkDBError 'Unable to execute statement', $statement;
128 return $sth->fetchrow_hashref;
131 sub getBugComments ($) {
134 my $statement = <<"END";
138 substring_index(login_name,'\@',1) as username,
148 my $sth = $bugzilla->prepare ($statement);
150 _checkDBError 'Unable to prepare statement', $statement;
154 _checkDBError 'Unable to execute statement', $statement;
158 while (my $comment = $sth->fetchrow_hashref) {
159 my $commentText = <<"END";
160 The following comment was entered by [~$comment->{username}] on $comment->{bug_when}:
165 push @comments, $commentText;
171 sub getRelationships ($$$$@) {
172 my ($table, $returnField, $testField, $relationshipType, @bugs) = @_;
174 $main::log->msg ("Getting $relationshipType");
176 my $statement = "select $returnField from $table where $table.$testField = ?";
178 my $sth = $bugzilla->prepare ($statement);
180 _checkDBError 'Unable to prepare statement', $statement;
186 map {$bugmap{$_} = 1} @bugs unless %bugmap;
188 for my $bugid (@bugs) {
189 $sth->execute ($bugid);
191 _checkDBError 'Unable to exit statement', $statement;
193 my $result = JIRAUtils::findIssue ($bugid, %bugmap);
195 if ($result =~ /^Unable/) {
196 $main::log->warn ($result);
198 $main::total{'Missing JIRA Issues'}++;
201 } elsif ($result =~ /^Future/) {
202 $main::total{'Future JIRA Issues'}++;
207 my $jiraIssue = $result;
208 my $key = $jiraIssue || $bugid;
211 my $relations = $sth->fetchall_arrayref;
214 map {push @relations, $_->[0]} @$relations;
216 for my $relation (@relations) {
217 $jiraIssue = JIRAUtils::findIssue ($relation);
219 if ($jiraIssue =~ /^Unable/ || $jiraIssue =~ /^Future/) {
220 $main::log->warn ($jiraIssue);
222 $main::total{'Missing JIRA Issues'}++ if $jiraIssue =~ /^Unable/;
223 $main::total{'Future JIRA Issues'}++ if $jiraIssue =~ /^Future/;
225 push @relationships, $relation;
227 push @relationships, $jiraIssue;
231 push @{$relationships{$key}}, @relationships if @relationships;
234 $main::total{$relationshipType} = keys %relationships;
236 return \%relationships;
239 sub getDependencies (@) {
242 return getRelationships (
243 'dependencies', # table
244 'dependson', # returned field
245 'blocked', # test field
246 'Depends on', # relationship
251 sub getBlockers (@) {
254 return getRelationships (
263 sub getDuplicates (@) {
266 return getRelationships (
278 return getRelationships (
287 sub getWatchers ($) {
290 my $statement = <<"END";
297 cc.who = profiles.userid and
301 my $sth = $bugzilla->prepare ($statement);
303 _checkDBError 'Unable to prepare statement', $statement;
305 $sth->execute ($bugid);
307 _checkDBError 'Unable to execute statement', $statement;
309 my @rows = @{$sth->fetchall_arrayref};
314 if ($$_[0] =~ /(.*)\@/) {
318 $main::total{'Watchers Processed'}++;