From f8a6f476946b994f7f555da0ebbb14fc57a4f9c4 Mon Sep 17 00:00:00 2001 From: dklawren Date: Mon, 25 Jun 2018 15:37:53 -0400 Subject: Bug 1469881 - Patches posted by Phabricator to Bugzilla don't list the patch author --- extensions/PhabBugz/lib/Feed.pm | 12 +++++---- extensions/PhabBugz/lib/Revision.pm | 2 +- extensions/PhabBugz/lib/Util.pm | 50 ++++++++++++++++++++++++------------- 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/extensions/PhabBugz/lib/Feed.pm b/extensions/PhabBugz/lib/Feed.pm index 981c95fb3..31dd8bca0 100644 --- a/extensions/PhabBugz/lib/Feed.pm +++ b/extensions/PhabBugz/lib/Feed.pm @@ -445,8 +445,10 @@ sub process_revision_change { my ($timestamp) = Bugzilla->dbh->selectrow_array("SELECT NOW()"); - my $attachment = create_revision_attachment($bug, $revision, $timestamp); - + INFO('Checking for revision attachment'); + my $attachment = create_revision_attachment($bug, $revision, $timestamp, $revision->author->bugzilla_user); + INFO('Attachment ' . $attachment->id . ' created or already exists.'); + # ATTACHMENT OBSOLETES # fixup attachments on current bug @@ -641,9 +643,9 @@ sub process_new_user { date => $timestamp, phab_user_login => $phab_user->name, phab_user_realname => $phab_user->realname, - bugzilla_userid => $phab_user->bugzilla_user->id, - bugzilla_login => $phab_user->bugzilla_user->login, - bugzilla_realname => $phab_user->bugzilla_user->name, + bugzilla_userid => $bug_user->id, + bugzilla_login => $bug_user->login, + bugzilla_realname => $bug_user->name, squat_userid => $row->{userid}, squat_login => $row->{login_name}, squat_realname => $row->{realname} diff --git a/extensions/PhabBugz/lib/Revision.pm b/extensions/PhabBugz/lib/Revision.pm index d87ca8bd2..854cc48d4 100644 --- a/extensions/PhabBugz/lib/Revision.pm +++ b/extensions/PhabBugz/lib/Revision.pm @@ -288,7 +288,7 @@ sub _build_author { } ); if ($phab_user) { - return $self->{author} = $phab_user->bugzilla_user; + return $self->{author} = $phab_user; } } diff --git a/extensions/PhabBugz/lib/Util.pm b/extensions/PhabBugz/lib/Util.pm index b24124ede..c4c9c7992 100644 --- a/extensions/PhabBugz/lib/Util.pm +++ b/extensions/PhabBugz/lib/Util.pm @@ -22,6 +22,7 @@ use JSON::XS qw(encode_json decode_json); use List::Util qw(first); use LWP::UserAgent; use Taint::Util qw(untaint); +use Try::Tiny; use base qw(Exporter); @@ -39,7 +40,7 @@ our @EXPORT = qw( ); sub create_revision_attachment { - my ( $bug, $revision, $timestamp ) = @_; + my ( $bug, $revision, $timestamp, $submitter ) = @_; my $phab_base_uri = Bugzilla->params->{phabricator_base_uri}; ThrowUserError('invalid_phabricator_uri') unless $phab_base_uri; @@ -59,22 +60,38 @@ sub create_revision_attachment { ($timestamp) = Bugzilla->dbh->selectrow_array("SELECT NOW()"); } - my $attachment = Bugzilla::Attachment->create( - { - bug => $bug, - creation_ts => $timestamp, - data => $revision_uri, - description => $revision->title, - filename => 'phabricator-D' . $revision->id . '-url.txt', - ispatch => 0, - isprivate => 0, - mimetype => PHAB_CONTENT_TYPE, + # If submitter, then switch to that user when creating attachment + my ($old_user, $attachment); + try { + if ($submitter) { + $old_user = Bugzilla->user; + $submitter->{groups} = [ Bugzilla::Group->get_all ]; # We need to always be able to add attachment + Bugzilla->set_user($submitter); } - ); - # Insert a comment about the new attachment into the database. - $bug->add_comment($revision->summary, { type => CMT_ATTACHMENT_CREATED, - extra_data => $attachment->id }); + $attachment = Bugzilla::Attachment->create( + { + bug => $bug, + creation_ts => $timestamp, + data => $revision_uri, + description => $revision->title, + filename => 'phabricator-D' . $revision->id . '-url.txt', + ispatch => 0, + isprivate => 0, + mimetype => PHAB_CONTENT_TYPE, + } + ); + + # Insert a comment about the new attachment into the database. + $bug->add_comment($revision->summary, { type => CMT_ATTACHMENT_CREATED, + extra_data => $attachment->id }); + } + catch { + die $_; + } + finally { + Bugzilla->set_user($old_user) if $old_user; + }; return $attachment; } @@ -106,8 +123,7 @@ sub get_bug_role_phids { sub is_attachment_phab_revision { my ($attachment) = @_; - return ($attachment->contenttype eq PHAB_CONTENT_TYPE - && $attachment->attacher->login eq PHAB_AUTOMATION_USER) ? 1 : 0; + return $attachment->contenttype eq PHAB_CONTENT_TYPE; } sub get_attachment_revisions { -- cgit v1.2.3-24-g4f1b