summaryrefslogtreecommitdiffstats
path: root/extensions/PhabBugz/lib/Util.pm
diff options
context:
space:
mode:
Diffstat (limited to 'extensions/PhabBugz/lib/Util.pm')
-rw-r--r--extensions/PhabBugz/lib/Util.pm251
1 files changed, 125 insertions, 126 deletions
diff --git a/extensions/PhabBugz/lib/Util.pm b/extensions/PhabBugz/lib/Util.pm
index 32f860413..613fd3466 100644
--- a/extensions/PhabBugz/lib/Util.pm
+++ b/extensions/PhabBugz/lib/Util.pm
@@ -32,167 +32,166 @@ use Mojo::JSON qw(encode_json);
use base qw(Exporter);
our @EXPORT = qw(
- create_revision_attachment
- get_attachment_revisions
- get_bug_role_phids
- intersect
- is_attachment_phab_revision
- request
- set_phab_user
+ create_revision_attachment
+ get_attachment_revisions
+ get_bug_role_phids
+ intersect
+ is_attachment_phab_revision
+ request
+ set_phab_user
);
sub create_revision_attachment {
- state $check = compile(Bug, Revision, Str, User);
- my ( $bug, $revision, $timestamp, $submitter ) = $check->(@_);
-
- my $phab_base_uri = Bugzilla->params->{phabricator_base_uri};
- ThrowUserError('invalid_phabricator_uri') unless $phab_base_uri;
-
- my $revision_uri = $phab_base_uri . "D" . $revision->id;
-
- # Check for previous attachment with same revision id.
- # If one matches then return it instead. This is fine as
- # BMO does not contain actual diff content.
- my @review_attachments = grep { is_attachment_phab_revision($_) } @{ $bug->attachments };
- my $review_attachment = first { trim($_->data) eq $revision_uri } @review_attachments;
- return $review_attachment if defined $review_attachment;
-
- # No attachment is present, so we can now create new one
-
- if (!$timestamp) {
- ($timestamp) = Bugzilla->dbh->selectrow_array("SELECT NOW()");
- }
-
- # If submitter, then switch to that user when creating attachment
- local $submitter->{groups} = [ Bugzilla::Group->get_all ]; # We need to always be able to add attachment
- my $restore_prev_user = Bugzilla->set_user($submitter, scope_guard => 1);
-
- 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,
- }
- );
-
- # Insert a comment about the new attachment into the database.
- $bug->add_comment($revision->summary, { type => CMT_ATTACHMENT_CREATED,
- extra_data => $attachment->id });
-
- delete $bug->{attachments};
-
- return $attachment;
+ state $check = compile(Bug, Revision, Str, User);
+ my ($bug, $revision, $timestamp, $submitter) = $check->(@_);
+
+ my $phab_base_uri = Bugzilla->params->{phabricator_base_uri};
+ ThrowUserError('invalid_phabricator_uri') unless $phab_base_uri;
+
+ my $revision_uri = $phab_base_uri . "D" . $revision->id;
+
+ # Check for previous attachment with same revision id.
+ # If one matches then return it instead. This is fine as
+ # BMO does not contain actual diff content.
+ my @review_attachments
+ = grep { is_attachment_phab_revision($_) } @{$bug->attachments};
+ my $review_attachment
+ = first { trim($_->data) eq $revision_uri } @review_attachments;
+ return $review_attachment if defined $review_attachment;
+
+ # No attachment is present, so we can now create new one
+
+ if (!$timestamp) {
+ ($timestamp) = Bugzilla->dbh->selectrow_array("SELECT NOW()");
+ }
+
+ # If submitter, then switch to that user when creating attachment
+ local $submitter->{groups} = [Bugzilla::Group->get_all]; # We need to always be able to add attachment
+ my $restore_prev_user = Bugzilla->set_user($submitter, scope_guard => 1);
+
+ 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,
+ });
+
+ # Insert a comment about the new attachment into the database.
+ $bug->add_comment($revision->summary,
+ {type => CMT_ATTACHMENT_CREATED, extra_data => $attachment->id});
+
+ delete $bug->{attachments};
+
+ return $attachment;
}
sub intersect {
- my ($list1, $list2) = @_;
- my %e = map { $_ => undef } @{$list1};
- return grep { exists( $e{$_} ) } @{$list2};
+ my ($list1, $list2) = @_;
+ my %e = map { $_ => undef } @{$list1};
+ return grep { exists($e{$_}) } @{$list2};
}
sub get_bug_role_phids {
- state $check = compile(Bug);
- my ($bug) = $check->(@_);
-
- my @bug_users = ( $bug->reporter );
- push(@bug_users, $bug->assigned_to)
- if $bug->assigned_to->email != Bugzilla->params->{'nobody_user'};
- push(@bug_users, $bug->qa_contact) if $bug->qa_contact;
- push(@bug_users, @{ $bug->cc_users }) if @{ $bug->cc_users };
-
- my $phab_users =
- Bugzilla::Extension::PhabBugz::User->match(
- {
- ids => [ map { $_->id } @bug_users ]
- }
- );
-
- return [ map { $_->phid } @{ $phab_users } ];
+ state $check = compile(Bug);
+ my ($bug) = $check->(@_);
+
+ my @bug_users = ($bug->reporter);
+ push(@bug_users, $bug->assigned_to)
+ if $bug->assigned_to->email != Bugzilla->params->{'nobody_user'};
+ push(@bug_users, $bug->qa_contact) if $bug->qa_contact;
+ push(@bug_users, @{$bug->cc_users}) if @{$bug->cc_users};
+
+ my $phab_users
+ = Bugzilla::Extension::PhabBugz::User->match({
+ ids => [map { $_->id } @bug_users]
+ });
+
+ return [map { $_->phid } @{$phab_users}];
}
sub is_attachment_phab_revision {
- state $check = compile(Attachment);
- my ($attachment) = $check->(@_);
- return $attachment->contenttype eq PHAB_CONTENT_TYPE;
+ state $check = compile(Attachment);
+ my ($attachment) = $check->(@_);
+ return $attachment->contenttype eq PHAB_CONTENT_TYPE;
}
sub get_attachment_revisions {
- state $check = compile(Bug);
- my ($bug) = $check->(@_);
+ state $check = compile(Bug);
+ my ($bug) = $check->(@_);
- my @attachments =
- grep { is_attachment_phab_revision($_) } @{ $bug->attachments() };
+ my @attachments
+ = grep { is_attachment_phab_revision($_) } @{$bug->attachments()};
- return unless @attachments;
+ return unless @attachments;
- my @revision_ids;
- foreach my $attachment (@attachments) {
- my ($revision_id) =
- ( $attachment->filename =~ PHAB_ATTACHMENT_PATTERN );
- next if !$revision_id;
- push( @revision_ids, int($revision_id) );
- }
+ my @revision_ids;
+ foreach my $attachment (@attachments) {
+ my ($revision_id) = ($attachment->filename =~ PHAB_ATTACHMENT_PATTERN);
+ next if !$revision_id;
+ push(@revision_ids, int($revision_id));
+ }
- return unless @revision_ids;
+ return unless @revision_ids;
- my @revisions;
- foreach my $revision_id (@revision_ids) {
- my $revision = Bugzilla::Extension::PhabBugz::Revision->new_from_query({
- ids => [ $revision_id ]
- });
- push @revisions, $revision if $revision;
- }
+ my @revisions;
+ foreach my $revision_id (@revision_ids) {
+ my $revision
+ = Bugzilla::Extension::PhabBugz::Revision->new_from_query({
+ ids => [$revision_id]
+ });
+ push @revisions, $revision if $revision;
+ }
- return \@revisions;
+ return \@revisions;
}
sub request {
- state $check = compile(Str, HashRef);
- my ($method, $data) = $check->(@_);
- my $request_cache = Bugzilla->request_cache;
- my $params = Bugzilla->params;
-
- my $ua = $request_cache->{phabricator_ua};
- unless ($ua) {
- $ua = $request_cache->{phabricator_ua} = Mojo::UserAgent->new;
- if ($params->{proxy_url}) {
- $ua->proxy($params->{proxy_url});
- }
+ state $check = compile(Str, HashRef);
+ my ($method, $data) = $check->(@_);
+ my $request_cache = Bugzilla->request_cache;
+ my $params = Bugzilla->params;
+
+ my $ua = $request_cache->{phabricator_ua};
+ unless ($ua) {
+ $ua = $request_cache->{phabricator_ua} = Mojo::UserAgent->new;
+ if ($params->{proxy_url}) {
+ $ua->proxy($params->{proxy_url});
}
+ }
- my $phab_api_key = $params->{phabricator_api_key};
- ThrowUserError('invalid_phabricator_api_key') unless $phab_api_key;
- my $phab_base_uri = $params->{phabricator_base_uri};
- ThrowUserError('invalid_phabricator_uri') unless $phab_base_uri;
+ my $phab_api_key = $params->{phabricator_api_key};
+ ThrowUserError('invalid_phabricator_api_key') unless $phab_api_key;
+ my $phab_base_uri = $params->{phabricator_base_uri};
+ ThrowUserError('invalid_phabricator_uri') unless $phab_base_uri;
- my $full_uri = $phab_base_uri . '/api/' . $method;
+ my $full_uri = $phab_base_uri . '/api/' . $method;
- $data->{__conduit__} = { token => $phab_api_key };
+ $data->{__conduit__} = {token => $phab_api_key};
- my $response = $ua->post($full_uri => form => { params => encode_json($data) })->result;
- ThrowCodeError('phabricator_api_error', { reason => $response->message })
- if $response->is_error;
+ my $response
+ = $ua->post($full_uri => form => {params => encode_json($data)})->result;
+ ThrowCodeError('phabricator_api_error', {reason => $response->message})
+ if $response->is_error;
- my $result = $response->json;
- ThrowCodeError('phabricator_api_error',
- { reason => 'JSON decode failure' }) if !defined($result);
- ThrowCodeError('phabricator_api_error',
- { code => $result->{error_code},
- reason => $result->{error_info} }) if $result->{error_code};
+ my $result = $response->json;
+ ThrowCodeError('phabricator_api_error', {reason => 'JSON decode failure'})
+ if !defined($result);
+ ThrowCodeError('phabricator_api_error',
+ {code => $result->{error_code}, reason => $result->{error_info}})
+ if $result->{error_code};
- return $result;
+ return $result;
}
sub set_phab_user {
- my $user = Bugzilla::User->new( { name => PHAB_AUTOMATION_USER } );
- $user->{groups} = [ Bugzilla::Group->get_all ];
+ my $user = Bugzilla::User->new({name => PHAB_AUTOMATION_USER});
+ $user->{groups} = [Bugzilla::Group->get_all];
- return Bugzilla->set_user($user, scope_guard => 1);
+ return Bugzilla->set_user($user, scope_guard => 1);
}
1;