-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExtension.pm
204 lines (175 loc) · 6.1 KB
/
Extension.pm
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
# Copyright(C) 2020 Lemoine Automation Technologies
#
# This file is part of Trackerzilla.
#
# Trackerzilla is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Foobar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <https://www.gnu.org/licenses/>.
package Bugzilla::Extension::Trackerzilla;
use 5.10.1;
use strict;
use warnings;
use Bugzilla::Comment;
use Bugzilla::Constants;
use Data::Dumper;
use parent qw(Bugzilla::Extension);
use Bugzilla::Extension::Trackerzilla::Util;
use Bugzilla::Extension::Trackerzilla::Config;
our $VERSION = '0.01';
## This hook is called after the creation of a hook.
# sub bug_end_of_create {
# my ($self, $args) = @_;
# my $bug = $args->{bug};
# my $id = %$bug{bug_id};
#
# my @comments = Bugzilla::Comment->match({bug_id => $id});
# my $commands = read_commands(\@comments, $id);
# #if (check_create($id)){
# # new_pivotal_story($bug);
# #}
# }
## This hook is called after updating a bug (creation included)
sub bug_end_of_update {
my ($self, $args) = @_;
my $bug = $args->{bug};
my $id = $bug->bug_id;
my $old_bug = $args->{old_bug};
my $story_id = $bug->{'cf_pivotal_story_id'};
my $comments = $bug->{added_comments};
unless (defined $comments){
$comments = Bugzilla::Comment->match({bug_id => $id});
unless (scalar(@$comments) == 1){ # we assume that if there is only one comment,
$comments = []; # the comment is the description and the bug is new.
} # else, there are just no new comments.
}
my $commands = read_commands($comments, $id);
if (($bug->{bug_status} ne $old_bug->{bug_status}) && (%create_on_status{$bug->{bug_status}})){
$commands->{create} = 1;
}
# If the story is already created, we don't need to create it again.
unless ($story_id == 0){
$commands->{create} = 0;
}
# Modify and post comments on bugzilla.
foreach my $comment (@$comments){
#$comment->remove_from_db();
}
my $new_comments = $commands->{new_comments};
foreach my $comment (@$new_comments){
Bugzilla::Comment->create({
'thetext' => $comment->{thetext},
'bug_id' => $id,
});
}
# make sure the bug has a description
my $all_comments = Bugzilla::Comment->match({bug_id => $id});
unless (scalar(@$all_comments)){
Bugzilla::Comment->create({
'thetext' => "",
'bug_id' => $id,
});
$all_comments = Bugzilla::Comment->match({bug_id => $id});
}
if (($story_id) || $commands->{create}){ # Hey, this doesn't work with the first story ! bad
if ($commands->{create}){
# Create the story.
if (defined $changed_status_on_create{$bug->{bug_status}}){
$bug->set_bug_status($changed_status_on_create{$bug->{bug_status}}, {});
}
$story_id = new_pivotal_story($bug);
my $comment_story = "story create: https://www.pivotaltracker.com/n/projects/$CONFIG{project_id}/stories/$story_id";
# It seems to be the good way to post a comment on bugzilla, but for some reason it crashs some times
# (depending on the author?)
#$bug->add_comment(
# $comment_story,
# {
# type => CMT_NORMAL,
# }
#);
#$bug->update();
# So, for now whe create the comment in the database, wich seems to be buggy with the en of lines,
# but this is a one line comment so it should be ok.
Bugzilla::Comment->create({'thetext' => $comment_story, 'bug_id' => $id});
# Post all comments
my @comments = @$all_comments[1 .. scalar(@$all_comments)-1];
foreach my $comment (@comments){
my $comment_body = $comment->body;
my $author = $comment->author->identity;
my $text = "$comment_body\n\nFrom $author on Bugzilla";
post_comment($story_id, $text);
}
}else{
# Post new comments only
foreach my $comment (@$new_comments){
my $comment_body = $comment->{thetext};
my $author = $comment->{author_};
unless (($trackerzibot_compatible) && ($comment_body =~ /\@bugs\b/)){
my $text = "$comment_body\n\nFrom $author on Bugzilla";
post_comment($story_id, $text);
}
}
}
# Update status
if ($bug->{bug_status} ne $old_bug->{bug_status}){
my $status;
if (exists($satus_bugzilla_to_pivotal{$bug->{bug_status}})){
$status = $satus_bugzilla_to_pivotal{$bug->{bug_status}};
}else{
$status = $default_pivotal_status;
}
modify_status($story_id, $status);
}
# Add labels
my $labels = $commands->{labels};
foreach my $label (@$labels){
add_label($story_id, $label);
}
}
# Remove error
if ($commands->{clear}){
remove_error($id);
}
# Add error
my $error_comments = $commands->{error_comments};
foreach my $comment (@$error_comments){
Bugzilla::Comment->create($comment);
}
$bug->{added_comments} = [];
if ($commands->{create}){
$bug->update(); # save the changes in the database
}
}
## Hook called when the db is updated (install or upgrade of bugzilla)
## Add a field cf_pivotal_story_id to the bugs.
sub install_update_db{
my $field = new Bugzilla::Field({ name => 'cf_pivotal_story_id' });
return if $field;
$field = Bugzilla::Field->create({
name => 'pivotal_story_id',
description => 'Story #',
type => FIELD_TYPE_INTEGER, # From list in Constants.pm
enter_bug => 0,
buglist => 0,
custom => 1,
});
}
sub bug_fields {
my ($self, $args) = @_;
my $fields = $args->{fields};
push(@$fields, 'cf_pivotal_story_id');
}
sub bug_columns {
my ($self, $args) = @_;
my $columns = $args->{'columns'};
push(@$columns, 'cf_pivotal_story_id');
}
__PACKAGE__->NAME;