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
|
# 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::Push::Serialise;
use 5.10.1;
use strict;
use warnings;
use Bugzilla::Constants;
use Bugzilla::Extension::Push::Util;
use Bugzilla::Version;
use Scalar::Util 'blessed';
use JSON ();
my $_instance;
sub instance {
$_instance ||= Bugzilla::Extension::Push::Serialise->_new();
return $_instance;
}
sub _new {
my ($class) = @_;
my $self = {};
bless($self, $class);
return $self;
}
# given an object, serliase to a hash
sub object_to_hash {
my ($self, $object, $is_shallow) = @_;
my $method = lc(blessed($object));
$method =~ s/::/_/g;
$method =~ s/^bugzilla//;
return unless $self->can($method);
(my $name = $method) =~ s/^_//;
# check for a cached hash
my $cache = Bugzilla->request_cache;
my $cache_id = "push." . ($is_shallow ? 'shallow.' : 'deep.') . $object;
if (exists($cache->{$cache_id})) {
return wantarray ? ($cache->{$cache_id}, $name) : $cache->{$cache_id};
}
# call the right method to serialise to a hash
my $rh = $self->$method($object, $is_shallow);
# store in cache
if ($cache_id) {
$cache->{$cache_id} = $rh;
}
return wantarray ? ($rh, $name) : $rh;
}
# given a changes hash, return an event hash
sub changes_to_event {
my ($self, $changes) = @_;
my $event = {};
# create common (created and modified) fields
$event->{'user'} = $self->object_to_hash(Bugzilla->user);
my $timestamp =
$changes->{'timestamp'}
|| Bugzilla->dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)');
$event->{'time'} = datetime_to_timestamp($timestamp);
foreach my $change (@{$changes->{'changes'}}) {
if (exists $change->{'field'}) {
# map undef to emtpy
hash_undef_to_empty($change);
# custom_fields change from undef to empty, ignore these changes
return if ($change->{'added'} || "") eq "" &&
($change->{'removed'} || "") eq "";
# use saner field serialisation
my $field = $change->{'field'};
$change->{'field'} = $field;
if ($field eq 'priority' || $field eq 'target_milestone') {
$change->{'added'} = _select($change->{'added'});
$change->{'removed'} = _select($change->{'removed'});
} elsif ($field =~ /^cf_/) {
$change->{'added'} = _custom_field($field, $change->{'added'});
$change->{'removed'} = _custom_field($field, $change->{'removed'});
}
$event->{'changes'} = [] unless exists $event->{'changes'};
push @{$event->{'changes'}}, $change;
}
}
return $event;
}
# bugzilla returns '---' or '--' for single-select fields that have no value
# selected. it makes more sense to return an empty string.
sub _select {
my ($value) = @_;
return '' if $value eq '---' or $value eq '--';
return $value;
}
# return an object which serialises to a json boolean, but still acts as a perl
# boolean
sub _boolean {
my ($value) = @_;
return $value ? JSON::true : JSON::false;
}
sub _string {
my ($value) = @_;
return defined($value) ? $value : '';
}
sub _time {
my ($value) = @_;
return defined($value) ? datetime_to_timestamp($value) : undef;
}
sub _integer {
my ($value) = @_;
return defined($value) ? $value + 0 : undef;
}
sub _array {
my ($value) = @_;
return defined($value) ? $value : [];
}
sub _custom_field {
my ($field, $value) = @_;
$field = Bugzilla::Field->new({ name => $field }) unless blessed $field;
if ($field->type == FIELD_TYPE_DATETIME) {
return _time($value);
} elsif ($field->type == FIELD_TYPE_SINGLE_SELECT) {
return _select($value);
} elsif ($field->type == FIELD_TYPE_MULTI_SELECT) {
return _array($value);
} else {
return _string($value);
}
}
#
# class mappings
# automatically derrived from the class name
# Bugzilla::Bug --> _bug, Bugzilla::User --> _user, etc
#
sub _bug {
my ($self, $bug) = @_;
my $version = $bug->can('version_obj')
? $bug->version_obj
: Bugzilla::Version->new({ name => $bug->version, product => $bug->product_obj });
my $milestone;
if (_select($bug->target_milestone) ne '') {
$milestone = $bug->can('target_milestone_obj')
? $bug->target_milestone_obj
: Bugzilla::Milestone->new({ name => $bug->target_milestone, product => $bug->product_obj });
}
my $status = $bug->can('status_obj')
? $bug->status_obj
: Bugzilla::Status->new({ name => $bug->bug_status });
my $rh = {
id => _integer($bug->bug_id),
alias => _string($bug->alias),
assigned_to => $self->_user($bug->assigned_to),
classification => _string($bug->classification),
component => $self->_component($bug->component_obj),
creation_time => _time($bug->creation_ts || $bug->delta_ts),
flags => (mapr { $self->_flag($_) } $bug->flags),
is_private => _boolean(!is_public($bug)),
keywords => (mapr { _string($_->name) } $bug->keyword_objects),
last_change_time => _time($bug->delta_ts),
operating_system => _string($bug->op_sys),
platform => _string($bug->rep_platform),
priority => _select($bug->priority),
product => $self->_product($bug->product_obj),
qa_contact => $self->_user($bug->qa_contact),
reporter => $self->_user($bug->reporter),
resolution => _string($bug->resolution),
severity => _string($bug->bug_severity),
status => $self->_status($status),
summary => _string($bug->short_desc),
target_milestone => $self->_milestone($milestone),
url => _string($bug->bug_file_loc),
version => $self->_version($version),
whiteboard => _string($bug->status_whiteboard),
};
# add custom fields
my @custom_fields = Bugzilla->active_custom_fields(
{ product => $bug->product_obj, component => $bug->component_obj });
foreach my $field (@custom_fields) {
my $name = $field->name;
$rh->{$name} = _custom_field($field, $bug->$name);
}
return $rh;
}
sub _user {
my ($self, $user) = @_;
return undef unless $user;
return {
id => _integer($user->id),
login => _string($user->login),
real_name => _string($user->name),
};
}
sub _component {
my ($self, $component) = @_;
return {
id => _integer($component->id),
name => _string($component->name),
};
}
sub _attachment {
my ($self, $attachment, $is_shallow) = @_;
my $rh = {
id => _integer($attachment->id),
content_type => _string($attachment->contenttype),
creation_time => _time($attachment->attached),
description => _string($attachment->description),
file_name => _string($attachment->filename),
flags => (mapr { $self->_flag($_) } $attachment->flags),
is_obsolete => _boolean($attachment->isobsolete),
is_patch => _boolean($attachment->ispatch),
is_private => _boolean(!is_public($attachment)),
last_change_time => _time($attachment->modification_time),
};
if (!$is_shallow) {
$rh->{bug} = $self->_bug($attachment->bug);
}
return $rh;
}
sub _comment {
my ($self, $comment, $is_shallow) = @_;
my $rh = {
id => _integer($comment->bug_id),
body => _string($comment->body),
creation_time => _time($comment->creation_ts),
is_private => _boolean($comment->is_private),
number => _integer($comment->count),
};
if (!$is_shallow) {
$rh->{bug} = $self->_bug($comment->bug);
}
return $rh;
}
sub _product {
my ($self, $product) = @_;
return {
id => _integer($product->id),
name => _string($product->name),
};
}
sub _flag {
my ($self, $flag) = @_;
my $rh = {
id => _integer($flag->id),
name => _string($flag->type->name),
value => _string($flag->status),
};
if ($flag->requestee) {
$rh->{'requestee'} = $self->_user($flag->requestee);
}
return $rh;
}
sub _version {
my ($self, $version) = @_;
return {
id => _integer($version->id),
name => _string($version->name),
};
}
sub _milestone {
my ($self, $milestone) = @_;
return undef unless $milestone;
return {
id => _integer($milestone->id),
name => _string($milestone->name),
};
}
sub _status {
my ($self, $status) = @_;
return {
id => _integer($status->id),
name => _string($status->name),
};
}
1;
|