summaryrefslogtreecommitdiffstats
path: root/extensions/UserStory/Extension.pm
blob: e70b86f5ad8e0a1dbfbae872734c7391022a3621 (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
# 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::UserStory;
use strict;
use warnings;

use base qw(Bugzilla::Extension);
our $VERSION = '1';

use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Extension::UserStory::Constants;
use Bugzilla::Extension::BMO::FakeBug;

use Text::Diff;

BEGIN {
    *Bugzilla::Bug::user_story_group = \&_bug_user_story_group;
    *Bugzilla::Extension::BMO::FakeBug::user_story_group = \&_bug_user_story_group;
}

sub _bug_user_story_group {
    my ($self) = @_;
    if (!exists $self->{user_story_group}) {
        my ($product, $component) = ($self->product, $self->component);
        my $edit_group = '';
        my $components = [];
        if (exists USER_STORY->{$product}) {
            $components = USER_STORY->{$product}->{components};
            if (!$component
                || scalar(@$components) == 0
                || grep { $_ eq $component } @$components)
            {
                $edit_group = USER_STORY->{$product}->{group};
            }
        }
        $self->{user_story_group}      = $edit_group;
        $self->{user_story_components} = $components;
    }
    return ($self->{user_story_group}, $self->{user_story_components});
}

# ensure user is allowed to edit the story
sub bug_check_can_change_field {
    my ($self, $args) = @_;
    my ($bug, $field, $priv_results) = @$args{qw(bug field priv_results)};
    return unless $field eq 'cf_user_story';

    my $user = Bugzilla->user;
    my ($group) = $bug->user_story_group();
    $group || return;
    if (!$user->in_group($group)) {
        push (@$priv_results, PRIVILEGES_REQUIRED_EMPOWERED);
    }
}

# store just a diff of the changes in the bugs_activity table
sub bug_update_before_logging {
    my ($self, $args) = @_;
    my $changes = $args->{changes};
    return unless exists $changes->{cf_user_story};
    my $diff = diff(
        \$changes->{cf_user_story}->[0],
        \$changes->{cf_user_story}->[1],
        {
            CONTEXT => 0,
        },
    );
    $changes->{cf_user_story} = [ '', $diff ];
}

# stop inline-history from displaying changes to the user story
sub inline_history_activtiy {
    my ($self, $args) = @_;
    foreach my $activity (@{ $args->{activity} }) {
        foreach my $change (@{ $activity->{changes} }) {
            if ($change->{fieldname} eq 'cf_user_story') {
                $change->{removed} = '';
                $change->{added} = '(updated)';
            }
        }
    }
}

# create cf_user_story field
sub install_update_db {
    my ($self, $args) = @_;
    return if Bugzilla::Field->new({ name => 'cf_user_story'});
    Bugzilla::Field->create({
        name        => 'cf_user_story',
        description => 'User Story',
        type        => FIELD_TYPE_TEXTAREA,
        mailhead    => 0,
        enter_bug   => 0,
        obsolete    => 0,
        custom      => 1,
        buglist     => 0,
    });
}

__PACKAGE__->NAME;