aboutsummaryrefslogtreecommitdiffstats
path: root/extensions/Mageia/Extension.pm
blob: 982a3bdb7ca016340aaaceee985ead5297402b8c (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
# 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::Mageia;

use 5.10.1;
use strict;
use warnings;

use parent qw(Bugzilla::Extension);

use Bugzilla::Constants qw(EVT_CC REL_GLOBAL_WATCHER);
use Bugzilla::Bug qw(LogActivityEntry);
use Bugzilla::Field qw(get_field_id);
use Bugzilla::User qw();
use Bugzilla::User::Setting qw(add_setting);
use Bugzilla::Extension::Mageia::Util qw(compare_datetimes);

use Email::Address;
use Encode qw(encode);

# Let's match the Bugzilla version it's written for.
our $VERSION = '5.0';

# sysadmin-bugs@ml.mageia.org user ID = 175.
use constant SYSADMIN_USER_ID => 175;

use constant MGA_SETTINGS => {
    mga_inline_history => { options => ['on', 'off'], default => 'on' },
};

sub bug_end_of_create_validators {
    my ($self, $args) = @_;

    # If a user enters 'validated_update' as keyword,
    # automatically CC sysadmin-bugs@ml.mageia.org.
    my $keywords = $args->{params}->{keywords};

    if (grep { $_->name eq 'validated_update' } @$keywords) {
        my $cc_list = $args->{params}->{cc};
        if (!grep { $_ == SYSADMIN_USER_ID } @$cc_list) {
            push(@$cc_list, SYSADMIN_USER_ID);
        }
    }
}

sub bug_end_of_update {
    my ($self, $args) = @_;
    my $bug = $args->{bug};
    my $dbh = Bugzilla->dbh;
    my $user = Bugzilla->user;

    my $new_keywords_str;
    # Call exists to avoid autovivification of $args->{changes} if it does not exist.
    # Else $bug->update() always sees the bug as being edited.
    if (exists $args->{changes} && exists $args->{changes}->{keywords}) {
        $new_keywords_str = $args->{changes}->{keywords}->[1];
    }

    # If a user enters 'validated_update' as keyword, automatically
    # CC sysadmin-bugs@ml.mageia.org.
    if ($new_keywords_str) {
        my @new_keywords = split(/[,\s]+/, $new_keywords_str);
        if (grep { $_ eq 'validated_update' } @new_keywords
            and !grep { $_->id == SYSADMIN_USER_ID } @{$bug->cc_users})
        {
            # Safer to clear the cache and let Bugzilla regenerate them if needed.
            delete $bug->{cc_users};
            delete $bug->{cc};
            $dbh->do('INSERT INTO cc (bug_id, who) VALUES (?, ?)',
                     undef, ($bug->id, SYSADMIN_USER_ID));

            # We also have to update the bug history to reflect this change.
            my $changed_cc = $args->{changes}->{cc};
            my $sysadmin_login = Bugzilla::User->new(SYSADMIN_USER_ID)->login;

            if ($changed_cc->[1]) {
                $changed_cc->[1] .= ", $sysadmin_login";
            }
            else {
                $changed_cc->[1] = $sysadmin_login;
            }

            my $field_id = get_field_id('cc');
            $dbh->do('DELETE FROM bugs_activity
                      WHERE bug_id = ? AND bug_when = ? AND fieldid = ?',
                      undef, ($bug->id, $args->{timestamp}, $field_id));

            LogActivityEntry($bug->id, 'cc', $changed_cc->[0] // '', $changed_cc->[1],
                             $user->id, $args->{timestamp});
        }
    }
}

sub bug_format_comment {
    my ($self, $args) = @_;
    my $regexes = $args->{'regexes'};
    # The Mageia DB was initially using SQL_ASCII as encoding (which means "no encoding").
    # The move to UTF8 caused some characters to be badly decoded, which we fix here.
    # Convert "é" into "é".
    push(@$regexes, { match => qr/\xc3\xa9/, replace => "\xe9" });
}

sub enter_bug_entrydefaultvars {
    my ($self, $vars) = @_;
    my $cgi = Bugzilla->cgi;

    # By default, users should get the guided form when reporting a new bug.
    # Pass &normal=1 to the URL to get the official bug form.
    my $format = $cgi->param('normal') ? '' : 'guided';
    $cgi->param('format', $format);
}

sub install_before_final_checks {
    # A hook in Bugzilla::Install::SETTINGS() would be much cleaner. :(
    my %settings = %{MGA_SETTINGS()};
    foreach my $setting (keys %settings) {
        add_setting($setting, $settings{$setting}->{options}, $settings{$setting}->{default});
    }
}

sub mailer_before_send {
    my ($self, $args) = @_;
    my $email = $args->{email};

    # Include the changer's name in the "From:" field.
    if (my $changer = $email->header('X-Bugzilla-Who')) {
        $changer = Bugzilla::User->new({ name => $changer });
        return unless $changer;
        my $address = Email::Address->new($changer->name, $email->header('From'));
        $email->header_set('From', encode('MIME-Header', $address->format));
    }
}

sub template_before_process {
    my ($self, $args) = @_;
    _inline_history($args) if $args->{file} eq 'bug/comments.html.tmpl';
}

sub _inline_history {
    my $args = shift;
    my $user = Bugzilla->user;
    return if $user->setting('mga_inline_history') eq 'off';
    # Only display the bug history in chronological order.
    return if $user->setting('comment_sort_order') ne 'oldest_to_newest';

    my $bug = $args->{vars}->{bug};
    my ($history) = $bug->get_activity();
    # Filter private comments that the user is not allowed to see.
    my @comments = grep { !$_->is_private || $user->is_insider || $user->id == $_->author->id } @{ $bug->comments };
    my %h_times = map { $_->{when} => $_ } @$history;
    my %c_times = map { $_->creation_ts => $_ } @comments;
    my @all_times = sort { compare_datetimes($a, $b) } (keys %c_times, keys %h_times);
    my $curr_comment;

    foreach my $time (@all_times) {
        if ($c_times{$time}) {
            $curr_comment = $c_times{$time};
            $curr_comment->{inline_history} //= [];
        }
        if (my $activity = $h_times{$time}) {
            # If we have no visible comment to attach the activity to, then ignore it.
            # This can happen if the initial comment (comment 0) is private.
            next unless $curr_comment;
            $activity->{after} = compare_datetimes($time, $curr_comment->creation_ts);
            $activity->{who} = new Bugzilla::User({ name => $activity->{who}, cache => 1 });
            push(@{ $curr_comment->{inline_history} }, $activity);
        }
    }
}

sub user_wants_mail {
    my ($self, $args) = @_;
    return unless $args->{relationship} == REL_GLOBAL_WATCHER;

    my $wants_mail = $args->{wants_mail};
    my @events = @{ $args->{events} };
    # Do not email global watchers if the single change is about the CC list.
    $$wants_mail = (scalar(@events) == 1 && $events[0] == EVT_CC) ? 0 : 1;
}

__PACKAGE__->NAME;