summaryrefslogtreecommitdiffstats
path: root/extensions/PhabBugz/lib/Util.pm
blob: f2876366f45a57e1bfc15195a366e1edd5ac7c70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::Extension::PhabBugz::Util;

use 5.10.1;
use strict;
use warnings;

use Bugzilla::Bug;
use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::User;
use Bugzilla::Types qw(:types);
use Bugzilla::Util qw(trim);
use Bugzilla::Extension::PhabBugz::Constants;
use Bugzilla::Extension::PhabBugz::Types qw(:types);

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 Type::Params qw( compile );
use Type::Utils;
use Types::Standard qw( :types );

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
);

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;
}

sub intersect {
    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 !~ /^nobody\@mozilla\.org$/;
    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;
}

sub get_attachment_revisions {
    state $check = compile(Bug);
    my ($bug) = $check->(@_);

    my @attachments =
      grep { is_attachment_phab_revision($_) } @{ $bug->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) );
    }

    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;
    }

    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} = LWP::UserAgent->new(timeout => 10);
        if ($params->{proxy_url}) {
            $ua->proxy('https', $params->{proxy_url});
        }
        $ua->default_header('Content-Type' => 'application/x-www-form-urlencoded');
    }

    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;

    $data->{__conduit__} = { token => $phab_api_key };

    my $response = $ua->post($full_uri, { params => encode_json($data) });

    ThrowCodeError('phabricator_api_error', { reason => $response->message })
      if $response->is_error;

    my $result;
    my $result_ok = eval {
        my $content = $response->content;
        untaint($content);
        $result = decode_json( $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};
    }

    return $result;
}

sub set_phab_user {
    my $user = Bugzilla::User->new( { name => PHAB_AUTOMATION_USER } );
    $user->{groups} = [ Bugzilla::Group->get_all ];

    return Bugzilla->set_user($user, scope_guard => 1);
}

1;