summaryrefslogtreecommitdiffstats
path: root/extensions/PhabBugz/lib/Feed.pm
blob: 8b6801e9159bdfae47f5609a0c91d3948efc7796 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# 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::Feed;

use 5.10.1;

use List::Util qw(first);
use List::MoreUtils qw(any);
use Moo;

use Bugzilla::Constants;
use Bugzilla::Extension::PhabBugz::Constants;
use Bugzilla::Extension::PhabBugz::Revision;
use Bugzilla::Extension::PhabBugz::Util qw(
    add_security_sync_comments
    create_private_revision_policy
    create_revision_attachment
    edit_revision_policy
    get_bug_role_phids
    get_phab_bmo_ids
    get_security_sync_groups
    is_attachment_phab_revision
    make_revision_public
    request
    set_phab_user
);

has 'is_daemon' => ( is => 'rw', default => 0 );
has 'logger'    => ( is => 'rw' );

sub start {
    my ($self) = @_;
    while (1) {
        my $ok = eval {
            if (Bugzilla->params->{phabricator_enabled}) {
                $self->feed_query();
                Bugzilla->_cleanup();
            }
            1;
        };
        $self->logger->error( $@ // "unknown exception" ) unless $ok;
        sleep(PHAB_POLL_SECONDS);
    }
}

sub feed_query {
    my ($self) = @_;
    my $dbh = Bugzilla->dbh;

    # Ensure Phabricator syncing is enabled
    if (!Bugzilla->params->{phabricator_enabled}) {
        $self->logger->info("PHABRICATOR SYNC DISABLED");
        return;
    }

    $self->logger->info("FEED: Fetching new transactions");

    my $last_id = $dbh->selectrow_array("
        SELECT value FROM phabbugz WHERE name = 'feed_last_id'");
    $last_id ||= 0;
    $self->logger->debug("QUERY LAST_ID: $last_id");

    # Check for new transctions (stories)
    my $transactions = $self->feed_transactions($last_id);
    if (!@$transactions) {
        $self->logger->info("FEED: No new transactions");
        return;
    }

    # Process each story
    foreach my $story_data (@$transactions) {
        my $story_id    = $story_data->{id};
        my $story_phid  = $story_data->{phid};
        my $author_phid = $story_data->{authorPHID};
        my $object_phid = $story_data->{objectPHID};
        my $story_text  = $story_data->{text};

        $self->logger->debug("STORY ID: $story_id");
        $self->logger->debug("STORY PHID: $story_phid");
        $self->logger->debug("AUTHOR PHID: $author_phid");
        $self->logger->debug("OBJECT PHID: $object_phid");
        $self->logger->info("STORY TEXT: $story_text");

        # Only interested in changes to revisions for now.
        if ($object_phid !~ /^PHID-DREV/) {
            $self->logger->debug("SKIPPING: Not a revision change");
            $self->save_feed_last_id($story_id);
            next;
        }

        # Skip changes done by phab-bot user
        my $phab_users = get_phab_bmo_ids({ phids => [$author_phid] });
        if (@$phab_users) {
            my $user = Bugzilla::User->new({ id => $phab_users->[0]->{id}, cache => 1 });
            if ($user->login eq PHAB_AUTOMATION_USER) {
                $self->logger->debug("SKIPPING: Change made by phabricator user");
                $self->save_feed_last_id($story_id);
                next;
            }
        }

        my $revision = Bugzilla::Extension::PhabBugz::Revision->new({ phids => [$object_phid] });

        if (!$revision->bug_id) {
            if ($story_text =~ /\s+created\s+D\d+/) {
                # If new revision and bug id was omitted, make revision public
                $self->logger->debug("No bug associated with new revision. Marking public.");
                $revision->set_policy('view', 'public');
                $revision->set_policy('edit', 'users');
                $revision->update();
                $self->logger->info("SUCCESS");
            }
            else {
                $self->logger->debug("SKIPPING: No bug associated with revision change");
            }
            $self->save_feed_last_id($story_id);
            next;
        }

        $self->process_revision_change($revision, $story_text);
        $self->save_feed_last_id($story_id);
    }
}

sub save_feed_last_id {
    my ($self, $story_id) = @_;
    # Store the largest last key so we can start from there in the next session
    $self->logger->debug("UPDATING FEED_LAST_ID: $story_id");
    Bugzilla->dbh->do("REPLACE INTO phabbugz (name, value) VALUES ('feed_last_id', ?)",
                      undef, $story_id);
}

sub process_revision_change {
    my ($self, $revision, $story_text) = @_;

    # Pre setup before making changes
    my $old_user = set_phab_user();

    my $is_shadow_db = Bugzilla->is_shadow_db;
    Bugzilla->switch_to_main_db if $is_shadow_db;

    my $dbh = Bugzilla->dbh;
    $dbh->bz_start_transaction;

    my ($timestamp) = Bugzilla->dbh->selectrow_array("SELECT NOW()");

    my $log_message = sprintf(
        "REVISION CHANGE FOUND: D%d: %s | bug: %d | %s",
        $revision->id,
        $revision->title,
        $revision->bug_id,
        $story_text);
    $self->logger->info($log_message);

    my $bug = Bugzilla::Bug->new({ id => $revision->bug_id, cache => 1 });

    # REVISION SECURITY POLICY

    # Do not set policy if a custom policy has already been set
    # This keeps from setting new custom policy everytime a change
    # is made.
    unless ($revision->view_policy =~ /^PHID-PLCY/) {

        # If bug is public then remove privacy policy
        if (!@{ $bug->groups_in }) {
            $revision->set_policy('view', 'public');
            $revision->set_policy('edit', 'users');
        }
        # else bug is private
        else {
            my @set_groups = get_security_sync_groups($bug);

            # If bug privacy groups do not have any matching synchronized groups,
            # then leave revision private and it will have be dealt with manually.
            if (!@set_groups) {
                add_security_sync_comments([$revision], $bug);
            }

            my $policy_phid = create_private_revision_policy($bug, \@set_groups);
            my $subscribers = get_bug_role_phids($bug);

            $revision->set_policy('view', $policy_phid);
            $revision->set_policy('edit', $policy_phid);
            $revision->set_subscribers($subscribers);
        }
    }

    my $attachment = create_revision_attachment($bug, $revision->id, $revision->title, $timestamp);

    # ATTACHMENT OBSOLETES

    # fixup attachments on current bug
    my @attachments =
      grep { is_attachment_phab_revision($_) } @{ $bug->attachments() };

    foreach my $attachment (@attachments) {
        my ($attach_revision_id) = ($attachment->filename =~ PHAB_ATTACHMENT_PATTERN);
        next if $attach_revision_id != $revision->id;

        my $make_obsolete = $revision->status eq 'abandoned' ? 1 : 0;
        $attachment->set_is_obsolete($make_obsolete);

        if ($revision->id == $attach_revision_id
            && $revision->title ne $attachment->description) {
            $attachment->set_description($revision->title);
        }

        $attachment->update($timestamp);
        last;
    }

    # fixup attachments with same revision id but on different bugs
    my $other_attachments = Bugzilla::Attachment->match({
        mimetype => PHAB_CONTENT_TYPE,
        filename => 'phabricator-D' . $revision->id . '-url.txt',
        WHERE    => { 'bug_id != ? AND NOT isobsolete' => $bug->id }
    });
    foreach my $attachment (@$other_attachments) {
        $attachment->set_is_obsolete(1);
        $attachment->update($timestamp);
    }

    # REVIEWER STATUSES

    my (@accepted_phids, @denied_phids, @accepted_user_ids, @denied_user_ids);
    foreach my $reviewer (@{ $revision->reviewers }) {
        push(@accepted_phids, $reviewer->phab_phid) if $reviewer->phab_review_status eq 'accepted';
        push(@denied_phids, $reviewer->phab_phid) if $reviewer->phab_review_status eq 'rejected';
    }

    my $phab_users = get_phab_bmo_ids({ phids => \@accepted_phids });
    @accepted_user_ids = map { $_->{id} } @$phab_users;
    $phab_users = get_phab_bmo_ids({ phids => \@denied_phids });
    @denied_user_ids = map { $_->{id} } @$phab_users;

    foreach my $attachment (@attachments) {
        my ($attach_revision_id) = ($attachment->filename =~ PHAB_ATTACHMENT_PATTERN);
        next if $revision->id != $attach_revision_id;

        # Clear old flags if no longer accepted
        my (@denied_flags, @new_flags, @removed_flags, %accepted_done, $flag_type);
        foreach my $flag (@{ $attachment->flags }) {
            next if $flag->type->name ne 'review';
            $flag_type = $flag->type if $flag->type->is_active;
            if (any { $flag->setter->id == $_ } @denied_user_ids) {
                push(@denied_flags, { id => $flag->id, setter => $flag->setter, status => 'X' });
            }
            if (any { $flag->setter->id == $_ } @accepted_user_ids) {
                $accepted_done{$flag->setter->id}++;
            }
            if ($flag->status eq '+'
                && !any { $flag->setter->id == $_ } (@accepted_user_ids, @denied_user_ids)) {
                push(@removed_flags, { id => $flag->id, setter => $flag->setter, status => 'X' });
            }
        }

        $flag_type ||= first { $_->name eq 'review' && $_->is_active } @{ $attachment->flag_types };

        # Create new flags
        foreach my $user_id (@accepted_user_ids) {
            next if $accepted_done{$user_id};
            my $user = Bugzilla::User->check({ id => $user_id, cache => 1 });
            push(@new_flags, { type_id => $flag_type->id, setter => $user, status => '+' });
        }

        # Also add comment to for attachment update showing the user's name
        # that changed the revision.
        my $comment;
        foreach my $flag_data (@new_flags) {
            $comment .= $flag_data->{setter}->name . " has approved the revision.\n";
        }
        foreach my $flag_data (@denied_flags) {
            $comment .= $flag_data->{setter}->name . " has requested changes to the revision.\n";
        }
        foreach my $flag_data (@removed_flags) {
            $comment .= $flag_data->{setter}->name . " has been removed from the revision.\n";
        }

        if ($comment) {
            $comment .= "\n" . Bugzilla->params->{phabricator_base_uri} . "D" . $revision->id;
            # Add transaction_id as anchor if one present
            # $comment .= "#" . $params->{transaction_id} if $params->{transaction_id};
            $bug->add_comment($comment, {
                isprivate  => $attachment->isprivate,
                type       => CMT_ATTACHMENT_UPDATED,
                extra_data => $attachment->id
            });
        }

        $attachment->set_flags([ @denied_flags, @removed_flags ], \@new_flags);
        $attachment->update($timestamp);
    }

    # FINISH UP

    $bug->update($timestamp);
    $revision->update();

    Bugzilla::BugMail::Send($revision->bug_id, { changer => Bugzilla->user });

    $dbh->bz_commit_transaction;
    Bugzilla->switch_to_shadow_db if $is_shadow_db;

    Bugzilla->set_user($old_user);

    $self->logger->info("SUCCESS");
}

sub feed_transactions {
    my ($self, $after) = @_;
    my $data = { view => 'text' };
    $data->{after} = $after if $after;
    my $result = request('feed.query_id', $data);
    unless (ref $result->{result}{data} eq 'ARRAY'
            && @{ $result->{result}{data} })
    {
        return [];
    }
    # Guarantee that the data is in ascending ID order
    return [ sort { $a->{id} <=> $b->{id} } @{ $result->{result}{data} } ];
}

1;