blib/lib/BZ/Client/Bug/Attachment.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 22 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 192 CC 16 SCAR 29.2
CC
16
Cov
0%
get
56.0
add
42.0
data
12.0
CRAP 18.6
1 #!/bin/false
2 # PODNAME: BZ::Client::Bug::Attachment
3 # ABSTRACT: Client side representation of an Attachment to a Bug in Bugzilla
4 # vim: softtabstop=4 tabstop=4 shiftwidth=4 ft=perl expandtab smarttab
5  
6 use strict;
7 use warnings 'all';
8  
9 package BZ::Client::Bug::Attachment;
10 $BZ::Client::Bug::Attachment::VERSION = '4.9';
11 use parent qw( BZ::Client::API );
12  
13 use File::Basename qw( basename );
14  
15 # See https://www.bugzilla.org/docs/tip/en/html/api/Bugzilla/WebService/Bug.html
16 # These are in order as per the above
17  
18 ## functions
19  
20 sub new {
21 my $class = shift;
22 my $self = $class->SUPER::new(@_);
23 $self->data($self->{data}) # This will make it a ::base64 object
24 if $self->{data};
25 return $self
26 }
27  
28 sub get {
29 my($class, $client, $params) = @_;
30 $client->log('debug', __PACKAGE__ . "::get: Asking for $params");
31 my $result = $class->api_call($client, 'Bug.attachments', $params);
32  
33 if (my $attachments = $result->{attachments}) {
34 $class->error($client,
35 'Invalid reply by server, expected hash of attachments.')
36 unless ($attachments and 'HASH' eq ref($attachments));
37 for my $id (keys %{$attachments}) {
38 $attachments->{$id}->{data} =
39 BZ::Client::XMLRPC::base64->new64($attachments->{$id}->{data});
40 $attachments->{$id} = __PACKAGE__
41 ->new( %{$attachments->{$id}} );
42 }
43 }
44  
45 if (my $bugs = $result->{bugs}) {
46 $class->error($client,
47 'Invalid reply by server, expected array of bugs.')
48 unless ($bugs and 'HASH' eq ref($bugs));
49 for my $id (keys %{$bugs}) {
50 $bugs->{$id} = [
51 map { __PACKAGE__->new( %{$_} ) }
52 map { $_->{data} = BZ::Client::XMLRPC::base64->new64($_->{data}) if $_->{data}; $_ }
53 @{$bugs->{$id}} ];
54 }
55 }
56  
57 $client->log('debug', __PACKAGE__ . '::get: Got keys: ' . join(', ', sort keys %{$result}));
58  
59 return wantarray ? %{$result} : $result
60 }
61  
62 sub add {
63 my($class, $client, $params) = @_;
64 # $params = { ids => [], file_name => basename($file), content_type => '?', summary=> $filename, data => \$content
65 $client->log('debug', __PACKAGE__ . '::add: Attaching a file');
66 if ( ref $params eq 'HASH' ) {
67 my $filename;
68 if ( ! $params->{data}
69 and $filename = $params->{file_name}
70 and -f $filename ) {
71 $params->{data} = do {
72 local $/;
73 open( my $fh, '<', $filename )
74 or $class->error($client, "Cannot open $filename: $!");
75 binmode $fh;
76 <$fh>
77 };
78 $params->{file_name} = basename($params->{file_name});
79 }
80 $params->{data} = BZ::Client::XMLRPC::base64->new($params->{data})
81 if (exists $params->{data} and ref $params->{data} eq '');
82 }
83 my $result = $class->api_call($client, 'Bug.add_attachment', $params);
84 my $ids = $result->{'ids'};
85 $class->error($client, 'Invalid reply by server, expected attachment ID.')
86 unless $ids;
87 return wantarray ? @$ids : $ids
88 }
89  
90 sub update {
91 my($class, $client, $params) = @_;
92 return $class->_returns_array($client, 'Bug.update_attachment', $params, 'attachments');
93 }
94  
95  
96 ## rw methods
97  
98 sub id { shift->_accessor('id', @_) }
99  
100 sub data {
101 my $self = shift;
102 if (@_) {
103 my $data = shift;
104 $self->{'data'} = ref $data ?
105 $data : BZ::Client::XMLRPC::base64->new($data);
106 }
107 else {
108 return $self->{'data'}
109 }
110 }
111  
112 sub file_name { shift->_accessor('file_name', @_) }
113  
114 sub description { goto &summary }
115  
116 sub summary {
117 my $self = shift;
118 if (@_) {
119 $self->{'summary'} = shift;
120 }
121 else {
122 return $self->{'summary'} || $self->{'description'}
123 }
124 }
125  
126 sub content_type { shift->_accessor('content_type', @_) }
127  
128 sub comment { shift->_accessor('comment', @_) }
129  
130 sub is_patch { shift->_accessor('is_patch', @_) }
131  
132 sub is_private { shift->_accessor('is_private', @_) }
133  
134 sub is_url { shift->_accessor('is_url', @_) }
135  
136 ## ro methods
137  
138 sub size { my $self = shift; return $self->{'size'} }
139  
140 sub creation_time { my $self = shift; return $self->{'creation_time'} }
141  
142 sub last_change_time { my $self = shift; return $self->{'last_change_time'} }
143  
144 sub bug_id { my $self = shift; return $self->{'bug_id'} }
145  
146 sub creator { my $self = shift; return $self->{'creator'} || $self->{'attacher'} }
147  
148 sub attacher { goto &creator }
149  
150 sub flags { my $self = shift; return $self->{'flags'} }
151  
152 1;
153  
154 __END__
155  
156 =pod
157  
158 =encoding UTF-8
159  
160 =head1 NAME
161  
162 BZ::Client::Bug::Attachment - Client side representation of an Attachment to a Bug in Bugzilla
163  
164 =head1 VERSION
165  
166 version 4.9
167  
168 =head1 SYNOPSIS
169  
170 This class provides methods for accessing and managing attachments in Bugzilla. Instances of this class are returned by L<BZ::Client::Bug::Attachment::get>.
171  
172 my $client = BZ::Client->new(
173 url => $url,
174 user => $user,
175 password => $password
176 );
177  
178 my $comments = BZ::Client::Bug::Attachment->get( $client, $ids );
179  
180 =head1 CLASS METHODS
181  
182 This section lists the class methods, which are available in this module.
183  
184 =head2 get
185  
186 It allows you to get data about attachments, given a list of bugs and/or attachment ids.
187  
188 Note: Private attachments will only be returned if you are in the insidergroup or if you are the submitter of the attachment.
189  
190 Actual Bugzilla API method is "attachments".
191  
192 =head3 History
193  
194 Added in Bugzilla 3.6.
195  
196 =head3 Parameters
197  
198 Note: At least one of L</ids> or L</attachment_ids> is required.
199  
200 In addition to the parameters below, this method also accepts the standard L<BZ::Client::Bug/include_fields> and L<BZ::Client::Bug/exclude_fields> arguments.
201  
202 =over 4
203  
204 =item ids
205  
206 I<ids> (array) - An array that can contain both bug IDs and bug aliases. All of the attachments (that are visible to you) will be returned for the specified bugs.
207  
208 =item attachment_ids
209  
210 I<attachment_ids> (array) - An array of integer attachment ID's.
211  
212 =back
213  
214 =head3 Returns
215  
216 A hash containing two items is returned:
217  
218 =over 4
219  
220 =item bugs
221  
222 This is used for bugs specified in L</ids>. This is a hash, where the keys are the numeric ID's of the bugs and the value is an array of attachment obejcts.
223  
224 Note that any individual bug will only be returned once, so if you specify an ID multiple times in L</ids>, it will still only be returned once.
225  
226 =item attachments
227  
228 Each individual attachment requested in L</attachment_ids> is returned here, in a hash where the numeric L</attachment_id> is the key, and the value is the attachment object.
229  
230 =back
231  
232 The return value looks like this:
233  
234 {
235 bugs => {
236 1345 => [
237 { (attachment) },
238 { (attachment) }
239 ],
240 9874 => [
241 { (attachment) },
242 { (attachment) }
243 ],
244 },
245  
246 attachments => {
247 234 => { (attachment) },
248 123 => { (attachment) },
249 },
250 }
251  
252 An "attachment" as shown above is an object instance of this package.
253  
254 =head3 Errors
255  
256 =over 4
257  
258 =item 100 - Invalid Bug Alias
259  
260 If you specified an alias and there is no bug with that alias.
261  
262 =item 101 - Invalid Bug ID
263  
264 The C<bug_id> you specified doesn't exist in the database.
265  
266 =item 102 - Access Denied
267  
268 You do not have access to the C<bug_id> you specified.
269  
270 =item 304 - Auth Failure, Attachment is Private
271  
272 You specified the ID of a private attachment in the L</attachment_ids> argument, and you are not in the "insidergroup" that can see private attachments.
273  
274 =back
275  
276 =head2 add
277  
278 This allows you to add an attachment to a bug in Bugzilla.
279  
280 Actual Bugzilla API method is "add_attachment".
281  
282 =head3 History
283  
284 Added in Bugzilla 4.0.
285  
286 The return value has changed in Bugzilla 4.4.
287  
288 =head3 Parameters
289  
290 An instance of this package or a hash containing:
291  
292 =over 4
293  
294 =item ids
295  
296 I<ids> (array) Required - An array of ints and/or strings - the ID's or aliases of bugs that you want to add this attachment to. The same attachment and comment will be added to all these bugs.
297  
298 =item data
299  
300 I<data> (string or base64) Mostly Required - The content of the attachment.
301  
302 The content will be base64 encoded. Of you can do it yourself by providing this option as a L<BZ::Client::XMPRPC::base64> object.
303  
304 What is I<"Mostly Required"> you ask? If you provide L</file_name> only, this module will attempt to slurp it to provide this I<data> parameter. See L</file_name> options for more details.
305  
306 =item file_name
307  
308 I<file_name> (string) Required - The "file name" that will be displayed in the UI for this attachment.
309  
310 If no I</data> parameter is provided, this module will attempt to open, slurp the contents of a file with path I<file_name>, base64 encod that data, placed it into the I</data> parameter, then I<file_name> is truncted to just the files basename.
311  
312 Failures to open the file (for anyreason) will be silently ignored and the I<file_name> parameter will not be touched.
313  
314 =item summary
315  
316 I<summary> (string) Required - A short string describing the attachment.
317  
318 =item content_type
319  
320 I<content_type> (string) Required - The MIME type of the attachment, like I<text/plain> or I<image/png>.
321  
322 =item comment
323  
324 I<comment> (string) - A comment to add along with this attachment.
325  
326 =item is_patch
327  
328 I<is_patch> (boolean) - True if Bugzilla should treat this attachment as a patch. If you specify this, you do not need to specify a L</content_type>. The L</content_type> of the attachment will be forced to C<text/plain>.
329  
330 Defaults to False if not specified.
331  
332 =item is_private
333  
334 I<is_private> (boolean) - True if the attachment should be private (restricted to the "insidergroup"), False if the attachment should be public.
335  
336 Defaults to False if not specified.
337  
338 =item flags
339  
340 An array of hashes with flags to add to the attachment. to create a flag, at least the C<status> and the C<type_id> or C<name> must be provided. An optional requestee can be passed if the flag type is requestable to a specific user.
341  
342 =over 4
343  
344 =item name
345  
346 I<name> (string) - The name of the flag type.
347  
348 =item type_id
349  
350 I<type_id> (int) - THe internal flag type ID.
351  
352 =item status
353  
354 I<status> (string) - The flags new status (i.e. "?", "+", "-" or "X" to clear a flag).
355  
356 =item requestee
357  
358 I<requestee> (string) - The login of the requestee if the flag type is requestable to a specific user.
359  
360 =back
361  
362 =back
363  
364 =head3 Returns
365  
366 An array of the attachment ID's created.
367  
368 =head3 Errors
369  
370 =over
371  
372 =item 100 - Invalid Bug Alias
373  
374 If you specified an alias and there is no bug with that alias.
375  
376 =item 101 - Invalid Bug ID
377  
378 The C<bug_id> you specified doesn't exist in the database.
379  
380 =item 102 - Access Denied
381  
382 You do not have access to the C<bug_id> you specified.
383  
384 =item 129 - Flag Status Invalid
385  
386 The flag status is invalid.
387  
388 =item 130 - Flag Modification Denied
389  
390 You tried to request, grant, or deny a flag but only a user with the required permissions may make the change.
391  
392 =item 131 - Flag not Requestable from Specific Person
393  
394 You can't ask a specific person for the flag.
395  
396 =item 133 - Flag Type not Unique
397  
398 The flag type specified matches several flag types. You must specify the type id value to update or add a flag.
399  
400 =item 134 - Inactive Flag Type
401  
402 The flag type is inactive and cannot be used to create new flags.
403  
404 =item 600 - Attachment Too Large
405  
406 You tried to attach a file that was larger than Bugzilla will accept.
407  
408 =item 601 - Invalid MIME Type
409  
410 You specified a L</content_type> argument that was blank, not a valid MIME type, or not a MIME type that Bugzilla accepts for attachments.
411  
412 =item 603 - File Name Not Specified
413  
414 You did not specify a valid for the L</file_name> argument.
415  
416 =item 604 - Summary Required
417  
418 You did not specify a value for the L</summary> argument.
419  
420 =item 606 - Empty Data
421  
422 You set the C<data> field to an empty string.
423  
424 =back
425  
426 =head2 render
427  
428 Returns the HTML rendering of the provided comment text.
429  
430 Actual Bugzilla API method is "render_comment".
431  
432 Note: this all takes place on your Bugzilla server.
433  
434 =head3 History
435  
436 Added in Bugzilla 5.0.
437  
438 =head3 Parameters
439  
440 =over 4
441  
442 =item text
443  
444 I<text> (string) Required - Text comment text to render
445  
446 =item id
447  
448 I<id> The ID of the bug to render the comment against.
449  
450 =back
451  
452 =head3 Returns
453  
454 The HTML rendering
455  
456 =head3 Errors
457  
458 =over 4
459  
460 =item 100 - Invalid Bug Alias
461  
462 If you specified an alias and there is no bug with that alias.
463  
464 =item 101 - Invalid Bug ID
465  
466 The C<bug_id> you specified doesn't exist in the database.
467  
468 =item 102 - Access Denied
469  
470 You do not have access to the C<bug_id> you specified.
471  
472 =back
473  
474 =head2 update
475  
476 This allows you to update attachment metadata in Bugzilla.
477  
478 Actual Bugzilla API method is "update_attachments".
479  
480 =head3 History
481  
482 Added in Bugzilla 5.0.
483  
484 =head3 Parameters
485  
486 =over 4
487  
488 =item ids
489  
490 I<ids> (array) - An array that can contain both bug IDs and bug aliases. All of the attachments (that are visible to you) will be returned for the specified bugs.
491  
492 =item file_name
493  
494 I<file_name> (string) Required - The "file name" that will be displayed in the UI for this attachment.
495  
496 =item summary
497  
498 I<summary> (string) Required - A short string describing the attachment.
499  
500 =item comment
501  
502 I<comment> (string) - A comment to add along with this attachment.
503  
504 =item content_type
505  
506 I<content_type> (string) - The MIME type of the attachment, like C<text/plain> or C<image/png>.
507  
508 =item is_patch
509  
510 I<is_patch> (boolean) - True if Bugzilla should treat this attachment as a patch. If you specify this, you do not need to specify a L</content_type>. The L</content_type> of the attachment will be forced to C<text/plain>.
511  
512 =item is_private
513  
514 I<is_private> (boolean) - True if the attachment should be private (restricted to the "insidergroup"), False if the attachment should be public.
515  
516 =item is_obsolete
517  
518 I<is_obsolete> (boolean) - True if the attachment is obsolete, False otherwise.
519  
520 =item flags
521  
522 An array of hashes with flags to add to the attachment. to create a flag, at least the status and the type_id or name must be provided. An optional requestee can be passed if the flag type is requestable to a specific user.
523  
524 =over 4
525  
526 =item name
527  
528 I<name> (string) - The name of the flag type.
529  
530 =item type_id
531  
532 I<type_id> (int) - THe internal flag type id.
533  
534 =item status
535  
536 I<status> (string) - The flags new status (i.e. "?", "+", "-" or "X" to clear a flag).
537  
538 =item requestee
539  
540 I<requestee> (string) - The login of the requestee if the flag type is requestable to a specific user.
541  
542 =item id
543  
544 I<id> (int) - Use C<id> to specify the flag to be updated. You will need to specify the C<id> if more than one flag is set of the same name.
545  
546 =item new
547  
548 I<new> (boolean) - Set to true if you specifically want a new flag to be created.
549  
550 =back
551  
552 =back
553  
554 =head3 Returns
555  
556 An array of hashes with the following fields:
557  
558 =over 4
559  
560 =item id
561  
562 I<id> (int) The id of the attachment that was updated.
563  
564 =item last_change_time
565  
566 I<last_change_time> (L<DateTime>) - The exact time that this update was done at, for this attachment. If no update was done (that is, no fields had their values changed and no comment was added) then this will instead be the last time the attachment was updated.
567  
568 =item changes
569  
570 I<changes> (hash) - The changes that were actually done on this bug. The keys are the names of the fields that were changed, and the values are a hash with two keys:
571  
572 =over 4
573  
574 =item added
575  
576 I<added> (string) - The values that were added to this field. possibly a comma-and-space-separated list if multiple values were added.
577  
578 =item removed
579  
580 I<removed> (string) - The values that were removed from this field.
581  
582 =back
583  
584 =back
585  
586 Here is an example of what a return value might look like:
587  
588 [
589 {
590 id => 123,
591 last_change_time => '2010-01-01T12:34:56',
592 changes => {
593 summary => {
594 removed => 'Sample ptach',
595 added => 'Sample patch'
596 },
597 is_obsolete => {
598 removed => '0',
599 added => '1',
600 },
601 },
602 },
603 ]
604  
605 =head3 Errors
606  
607 =over 4
608  
609 =item 100 - Invalid Bug Alias
610  
611 If you specified an alias and there is no bug with that alias.
612  
613 =item 101 - Invalid Bug ID
614  
615 The bug_id you specified doesn't exist in the database.
616  
617 =item 102 - Access Denied
618  
619 You do not have access to the C<bug_id> you specified.
620  
621 =item 129 - Flag Status Invalid
622  
623 The flag status is invalid.
624  
625 =item 130 - Flag Modification Denied
626  
627 You tried to request, grant, or deny a flag but only a user with the required permissions may make the change.
628  
629 =item 131 - Flag not Requestable from Specific Person
630  
631 You can't ask a specific person for the flag.
632  
633 =item 133 - Flag Type not Unique
634  
635 The flag type specified matches several flag types. You must specify the type ID value to update or add a flag.
636  
637 =item 134 - Inactive Flag Type
638  
639 The flag type is inactive and cannot be used to create new flags.
640  
641 =item 601 - Invalid MIME Type
642  
643 You specified a L</content_type> argument that was blank, not a valid MIME type, or not a MIME type that Bugzilla accepts for attachments.
644  
645 =item 603 - File Name Not Specified
646  
647 You did not specify a valid for the L</file_name> argument.
648  
649 =item 604 - Summary Required
650  
651 You did not specify a value for the L</summary> argument.
652  
653 =back
654  
655 =head2 new
656  
657 my $comment = BZ::Client::Bug::Comment->new(
658 id => $bug_id,
659 comment => $comment,
660 is_private => 1 || 0,
661 work_time => 3.5
662 );
663  
664 Creates a new instance with the given details. Doesn't actually touch your Bugzilla Server - see L</add> for that.
665  
666 =head1 INSTANCE METHODS
667  
668 This section lists the modules instance methods.
669  
670 =head2 bug_id
671  
672 I<bug_id> (int) - The ID of the bug that this attachment is on when reading
673  
674 I<bug_id> (int or string) - The ID or alias of the bug to append a attachment to when writing. B<Required>.
675  
676 =head2 data
677  
678 I<data> (base64 or string) The content of the attachment.
679  
680 When writing, either provide a string (which will be C<base46> encoded for you) or a L<BZ::Client::XMLRPC::base64> object if you'd like to DIY.
681  
682 When reading, a L<BZ::Client::XMLRPC::base64> object will be returned. To save you the trip, this object has a C<raw()> and a C<base64()> method. Here is an example.
683  
684 my $data = $attachment->data();
685 my $file_content_base64_encoded = $data->base64();
686 my $original_file_content = $data->raw();
687  
688 B<Required>, Read and Write.
689  
690 =head2 file_name
691  
692 I<file_name> (string) The "file name" that will be displayed in the UI for this attachment.
693  
694 B<Required>, Read and Write.
695  
696 =head2 summary
697  
698 I<summary> (string) A short string describing the attachment.
699  
700 B<Required>, Read and Write.
701  
702 =head2 content_type
703  
704 I<content_type> (string) The MIME type of the attachment, like C<text/plain> or C<image/png>.
705  
706 B<Required>, Read and Write.
707  
708 =head2 comment
709  
710 I<comment> (string or hash) A comment to add along with this attachment. If C<comment> is a hash, it has the following keys:
711  
712 Only useful when adding attachments.
713  
714 =over 4
715  
716 =item body
717  
718 I<body> (string) The body of the comment.
719  
720 =item is_markdown
721  
722 I<is_markdown> (boolean) If set to true, the comment has Markdown structures; otherwise, it is an ordinary text.
723  
724 =back
725  
726 =head2 is_patch
727  
728 I<is_patch> (boolean) True if the attachment should be private (restricted to the "insidergroup"), False if the attachment should be public.
729  
730 =head2 is_private
731  
732 I<is_private> (boolean) True if the attachment is private (only visible to a certain group called the "insidergroup"), False otherwise.
733  
734 =head2 is_obsolete
735  
736 I<is_obsolete> (boolean) - True if the attachment is obsolete, False otherwise.
737  
738 =head2 flags
739  
740 I<flags> (array) An array of hashes with flags to add to the attachment. to create a flag, at least the status and the type_id or name must be provided. An optional requestee can be passed if the flag type is requestable to a specific user.
741  
742 Read and Write.
743  
744 =over 4
745  
746 =item id
747  
748 I<id> (name) The ID of the flag.
749  
750 =item name
751  
752 I<name> (string) The name flag type.
753  
754 Read and Write.
755  
756 =item type_id
757  
758 I<type_id> (int) The internal flag type ID.
759  
760 Read and Write.
761  
762 =item creation_date
763  
764 I<creation_date> (L<DateTime>) The timestamp when this flag was originally created.
765  
766 Read only.
767  
768 =item modification_date
769  
770 I<modification_date> (L<DateTime>) The timestamp when the flag was last modified.
771  
772 Read only.
773  
774 =item status
775  
776 I<status> (string) The flags new status (i.e. "?", "+", "-" or "X" to clear a flag).
777  
778 Read and Write.
779  
780 =item setter
781  
782 I<setter> (string) The login name of the user who created or last modified the flag.
783  
784 Read only.
785  
786 =item requestee
787  
788 I<requestee> (string) The login of the requestee if the flag type is requestable to a specific user.
789  
790 =back
791  
792 =head2 size
793  
794 I<size> (int) The length (in bytes) of the attachment.
795  
796 Read only.
797  
798 =head2 creation_time
799  
800 I<creation_time> (L<DateTime>) The time the attachment was created.
801  
802 Read only.
803  
804 =head2 last_change_time
805  
806 I<last_change_time> (L<DateTime>) The last time the attachment was modified.
807  
808 =head2 attachment_id
809  
810 I<attachment_id> (int) The numeric id of the attachment.
811  
812 =head2 creator
813  
814 I<creator> (string) The login name of the user that created the attachment.
815  
816 =head1 AUTHORS
817  
818 =over 4
819  
820 =item *
821  
822 Dean Hamstead <dean@bytefoundry.com.au>
823  
824 =item *
825  
826 Jochen Wiedmann <jochen.wiedmann@gmail.com>
827  
828 =back
829  
830 =head1 COPYRIGHT AND LICENSE
831  
832 This software is copyright (c) 2026 by Dean Hamstad.
833  
834 This is free software; you can redistribute it and/or modify it under
835 the same terms as the Perl 5 programming language system itself.
836  
837 =cut