diff options
Diffstat (limited to 'extensions/PhabBugz/lib/Util.pm')
-rw-r--r-- | extensions/PhabBugz/lib/Util.pm | 140 |
1 files changed, 40 insertions, 100 deletions
diff --git a/extensions/PhabBugz/lib/Util.pm b/extensions/PhabBugz/lib/Util.pm index a00e20551..95b2b1598 100644 --- a/extensions/PhabBugz/lib/Util.pm +++ b/extensions/PhabBugz/lib/Util.pm @@ -34,38 +34,26 @@ our @EXPORT = qw( get_attachment_revisions get_bug_role_phids get_members_by_bmo_id - get_members_by_phid - get_phab_bmo_ids get_project_phid get_revisions_by_ids - get_revisions_by_phids get_security_sync_groups intersect is_attachment_phab_revision make_revision_private make_revision_public request - set_phab_user set_project_members set_revision_subscribers ); sub get_revisions_by_ids { my ($ids) = @_; - return _get_revisions({ ids => $ids }); -} - -sub get_revisions_by_phids { - my ($phids) = @_; - return _get_revisions({ phids => $phids }); -} - -sub _get_revisions { - my ($constraints) = @_; my $data = { - queryKey => 'all', - constraints => $constraints + queryKey => 'all', + constraints => { + ids => $ids + } }; my $result = request('differential.revision.search', $data); @@ -73,11 +61,11 @@ sub _get_revisions { ThrowUserError('invalid_phabricator_revision_id') unless (exists $result->{result}{data} && @{ $result->{result}{data} }); - return $result->{result}{data}; + return @{$result->{result}{data}}; } sub create_revision_attachment { - my ( $bug, $revision_id, $revision_title, $timestamp ) = @_; + my ( $bug, $revision_id, $revision_title ) = @_; my $phab_base_uri = Bugzilla->params->{phabricator_base_uri}; ThrowUserError('invalid_phabricator_uri') unless $phab_base_uri; @@ -92,10 +80,16 @@ sub create_revision_attachment { return $review_attachment if defined $review_attachment; # No attachment is present, so we can now create new one + my $is_shadow_db = Bugzilla->is_shadow_db; + Bugzilla->switch_to_main_db if $is_shadow_db; - if (!$timestamp) { - ($timestamp) = Bugzilla->dbh->selectrow_array("SELECT NOW()"); - } + my $old_user = Bugzilla->user; + _set_phab_user(); + + my $dbh = Bugzilla->dbh; + $dbh->bz_start_transaction; + + my ($timestamp) = $dbh->selectrow_array("SELECT NOW()"); my $attachment = Bugzilla::Attachment->create( { @@ -110,9 +104,13 @@ sub create_revision_attachment { } ); - # Insert a comment about the new attachment into the database. - $bug->add_comment('', { type => CMT_ATTACHMENT_CREATED, - extra_data => $attachment->id }); + $bug->update($timestamp); + $attachment->update($timestamp); + + $dbh->bz_commit_transaction; + Bugzilla->switch_to_shadow_db if $is_shadow_db; + + Bugzilla->set_user($old_user); return $attachment; } @@ -324,7 +322,12 @@ sub set_project_members { sub get_members_by_bmo_id { my $users = shift; - my $result = get_phab_bmo_ids({ ids => [ map { $_->id } @$users ] }); + my $data = { + accountids => [ map { $_->id } @$users ] + }; + + my $result = request('bmoexternalaccount.search', $data); + return [] if (!$result->{result}); my @phab_ids; foreach my $user (@{ $result->{result} }) { @@ -335,73 +338,10 @@ sub get_members_by_bmo_id { return \@phab_ids; } -sub get_members_by_phid { - my $phids = shift; - - my $result = get_phab_bmo_ids({ phids => $phids }); - - my @bmo_ids; - foreach my $user (@{ $result->{result} }) { - push(@bmo_ids, $user->{id}) - if ($user->{phid} && $user->{phid} =~ /^PHID-USER/); - } - - return \@bmo_ids; -} - -sub get_phab_bmo_ids { - my ($params) = @_; - my $memcache = Bugzilla->memcached; - - # Try to find the values in memcache first - my @results; - if ($params->{ids}) { - my @bmo_ids = @{ $params->{ids} }; - for (my $i = 0; $i < @bmo_ids; $i++) { - my $phid = $memcache->get({ key => "phab_user_bmo_id_" . $bmo_ids[$i] }); - if ($phid) { - push(@results, { - id => $bmo_ids[$i], - phid => $phid - }); - splice(@bmo_ids, $i, 1); - } - } - $params->{ids} = \@bmo_ids; - } - - if ($params->{phids}) { - my @phids = @{ $params->{phids} }; - for (my $i = 0; $i < @phids; $i++) { - my $bmo_id = $memcache->get({ key => "phab_user_phid_" . $phids[$i] }); - if ($bmo_id) { - push(@results, { - id => $bmo_id, - phid => $phids[$i] - }); - splice(@phids, $i, 1); - } - } - $params->{phids} = \@phids; - } - - my $result = request('bugzilla.account.search', $params); - - # Store new values in memcache for later retrieval - foreach my $user (@{ $result->{result} }) { - $memcache->set({ key => "phab_user_bmo_id_" . $user->{id}, - value => $user->{phid} }); - $memcache->set({ key => "phab_user_phid_" . $user->{phid}, - value => $user->{id} }); - push(@results, $user); - } - - return \@results; -} - sub is_attachment_phab_revision { - my ($attachment) = @_; + my ($attachment, $include_obsolete) = @_; return ($attachment->contenttype eq PHAB_CONTENT_TYPE + && ($include_obsolete || !$attachment->isobsolete) && $attachment->attacher->login eq PHAB_AUTOMATION_USER) ? 1 : 0; } @@ -460,12 +400,10 @@ sub request { my $result; my $result_ok = eval { $result = decode_json( $response->content); 1 }; - if (!$result_ok || $result->{error_code}) { - ThrowCodeError('phabricator_api_error', - { reason => 'JSON decode failure' }) if !$result_ok; - ThrowCodeError('phabricator_api_error', - { code => $result->{error_code}, - reason => $result->{error_info} }) if $result->{error_code}; + if ( !$result_ok ) { + ThrowCodeError( + 'phabricator_api_error', + { reason => 'JSON decode failure' } ); } return $result; @@ -486,12 +424,10 @@ sub get_security_sync_groups { return @set_groups; } -sub set_phab_user { - my $old_user = Bugzilla->user; +sub _set_phab_user { my $user = Bugzilla::User->new( { name => PHAB_AUTOMATION_USER } ); $user->{groups} = [ Bugzilla::Group->get_all ]; Bugzilla->set_user($user); - return $old_user; } sub add_security_sync_comments { @@ -510,10 +446,14 @@ sub add_security_sync_comments { : 'One revision was' ) . ' made private due to unknown Bugzilla groups.'; - my $old_user = set_phab_user(); + my $old_user = Bugzilla->user; + _set_phab_user(); $bug->add_comment( $bmo_error_message, { isprivate => 0 } ); + my $bug_changes = $bug->update(); + $bug->send_changes($bug_changes); + Bugzilla->set_user($old_user); } |