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

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