summaryrefslogtreecommitdiffstats
path: root/Extension.pm
blob: 621236758bd59309dc16bfd2b144c9f03114ae1d (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
# 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 strict;
use base qw(Bugzilla::Extension);

use Bugzilla::Bug;
use Bugzilla::Field;
use Bugzilla::User;
use Bugzilla::Extension::Mageia::Util;

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

our $VERSION = '0.10';

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

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;

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

    if ($new_keywords_str) {
        my @new_keywords = split(/[,\s]+/, $new_keywords_str);
        if (grep { $_ eq 'validated_update' } @new_keywords) {
            if (!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 = user_id_to_login(SYSADMIN_USER_ID);

                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],
                                 Bugzilla->user->id, $args->{timestamp});
            }
        }
    }
}

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 });
        my $changer_name = $changer ? $changer->name : undef;
        my $address = Email::Address->new($changer_name, $email->header('From'));
        $email->header_set('From', encode("MIME_Header", $address->format));
    }
}

__PACKAGE__->NAME;