diff options
author | dklawren <dklawren@users.noreply.github.com> | 2017-11-29 19:54:22 +0100 |
---|---|---|
committer | Dylan William Hardison <dylan@hardison.net> | 2017-11-29 19:54:22 +0100 |
commit | 7da8e374e0c96e10077690935e829b0c04fc82f4 (patch) | |
tree | 44a92ccdee16c45badd35e9c986294fd40beec12 /extensions/PhabBugz/lib/Util.pm | |
parent | 64ce7cb12deee5f394a869d9ec6b5e46cd2679f0 (diff) | |
download | bugzilla-7da8e374e0c96e10077690935e829b0c04fc82f4.tar.gz bugzilla-7da8e374e0c96e10077690935e829b0c04fc82f4.tar.xz |
Bug 1409957 - Create polling daemon to query Phabricator for recent transcations and update bug data according to revision changes
Diffstat (limited to 'extensions/PhabBugz/lib/Util.pm')
-rw-r--r-- | extensions/PhabBugz/lib/Util.pm | 140 |
1 files changed, 100 insertions, 40 deletions
diff --git a/extensions/PhabBugz/lib/Util.pm b/extensions/PhabBugz/lib/Util.pm index 95b2b1598..a00e20551 100644 --- a/extensions/PhabBugz/lib/Util.pm +++ b/extensions/PhabBugz/lib/Util.pm @@ -34,26 +34,38 @@ 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 => { - ids => $ids - } + queryKey => 'all', + constraints => $constraints }; my $result = request('differential.revision.search', $data); @@ -61,11 +73,11 @@ sub get_revisions_by_ids { 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 ) = @_; + my ( $bug, $revision_id, $revision_title, $timestamp ) = @_; my $phab_base_uri = Bugzilla->params->{phabricator_base_uri}; ThrowUserError('invalid_phabricator_uri') unless $phab_base_uri; @@ -80,16 +92,10 @@ 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; - - my $old_user = Bugzilla->user; - _set_phab_user(); - my $dbh = Bugzilla->dbh; - $dbh->bz_start_transaction; - - my ($timestamp) = $dbh->selectrow_array("SELECT NOW()"); + if (!$timestamp) { + ($timestamp) = Bugzilla->dbh->selectrow_array("SELECT NOW()"); + } my $attachment = Bugzilla::Attachment->create( { @@ -104,13 +110,9 @@ sub create_revision_attachment { } ); - $bug->update($timestamp); - $attachment->update($timestamp); - - $dbh->bz_commit_transaction; - Bugzilla->switch_to_shadow_db if $is_shadow_db; - - Bugzilla->set_user($old_user); + # Insert a comment about the new attachment into the database. + $bug->add_comment('', { type => CMT_ATTACHMENT_CREATED, + extra_data => $attachment->id }); return $attachment; } @@ -322,12 +324,7 @@ sub set_project_members { sub get_members_by_bmo_id { my $users = shift; - my $data = { - accountids => [ map { $_->id } @$users ] - }; - - my $result = request('bmoexternalaccount.search', $data); - return [] if (!$result->{result}); + my $result = get_phab_bmo_ids({ ids => [ map { $_->id } @$users ] }); my @phab_ids; foreach my $user (@{ $result->{result} }) { @@ -338,10 +335,73 @@ 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, $include_obsolete) = @_; + my ($attachment) = @_; return ($attachment->contenttype eq PHAB_CONTENT_TYPE - && ($include_obsolete || !$attachment->isobsolete) && $attachment->attacher->login eq PHAB_AUTOMATION_USER) ? 1 : 0; } @@ -400,10 +460,12 @@ sub request { my $result; my $result_ok = eval { $result = decode_json( $response->content); 1 }; - if ( !$result_ok ) { - ThrowCodeError( - 'phabricator_api_error', - { reason => 'JSON decode failure' } ); + 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}; } return $result; @@ -424,10 +486,12 @@ sub get_security_sync_groups { return @set_groups; } -sub _set_phab_user { +sub set_phab_user { + my $old_user = Bugzilla->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 { @@ -446,14 +510,10 @@ sub add_security_sync_comments { : 'One revision was' ) . ' made private due to unknown Bugzilla groups.'; - my $old_user = Bugzilla->user; - _set_phab_user(); + my $old_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); } |