From 12eb7b79859d1ee0be9f237d2b097fc4bf42d2c3 Mon Sep 17 00:00:00 2001 From: Max Kanat-Alexander Date: Tue, 13 Jul 2010 15:43:40 -0700 Subject: Bug 412074: Ability to add attachments to a bug via the WebService (Bug.add_attachment) r=timello, a=mkanat --- Bugzilla/WebService/Bug.pm | 177 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 175 insertions(+), 2 deletions(-) (limited to 'Bugzilla/WebService/Bug.pm') diff --git a/Bugzilla/WebService/Bug.pm b/Bugzilla/WebService/Bug.pm index c083bd491..78709d81e 100644 --- a/Bugzilla/WebService/Bug.pm +++ b/Bugzilla/WebService/Bug.pm @@ -50,6 +50,10 @@ use constant DATE_FIELDS => { update => ['deadline'], }; +use constant BASE64_FIELDS => { + add_attachment => ['data'], +}; + use constant READ_ONLY => qw( attachments comments @@ -592,6 +596,53 @@ sub legal_values { return { values => \@result }; } +sub add_attachment { + my ($self, $params) = validate(@_, 'ids'); + my $dbh = Bugzilla->dbh; + + Bugzilla->login(LOGIN_REQUIRED); + defined $params->{ids} + || ThrowCodeError('param_required', { param => 'ids' }); + defined $params->{data} + || ThrowCodeError('param_required', { param => 'data' }); + + my @bugs = map { Bugzilla::Bug->check($_) } @{ $params->{ids} }; + foreach my $bug (@bugs) { + Bugzilla->user->can_edit_product($bug->product_id) + || ThrowUserError("product_edit_denied", {product => $bug->product}); + } + + my @created; + $dbh->bz_start_transaction(); + foreach my $bug (@bugs) { + my $attachment = Bugzilla::Attachment->create({ + bug => $bug, + data => $params->{data}, + description => $params->{summary}, + filename => $params->{file_name}, + mimetype => $params->{content_type}, + ispatch => $params->{is_patch}, + isprivate => $params->{is_private}, + isurl => $params->{is_url}, + }); + my $comment = $params->{comment} || ''; + $attachment->bug->add_comment($comment, + { isprivate => $attachment->isprivate, + type => CMT_ATTACHMENT_CREATED, + extra_data => $attachment->id }); + push(@created, $attachment); + } + $_->bug->update($_->attached) foreach @created; + $dbh->bz_commit_transaction(); + + $_->send_changes() foreach @bugs; + + my %attachments = map { $_->id => $self->_attachment_to_hash($_, $params) } + @created; + + return { attachments => \%attachments }; +} + sub add_comment { my ($self, $params) = @_; @@ -790,6 +841,7 @@ sub _attachment_to_hash { id => $self->type('int', $attach->id), bug_id => $self->type('int', $attach->bug->id), file_name => $self->type('string', $attach->filename), + summary => $self->type('string', $attach->description), description => $self->type('string', $attach->description), content_type => $self->type('string', $attach->contenttype), is_private => $self->type('int', $attach->isprivate), @@ -1158,9 +1210,13 @@ C The numeric id of the bug that the attachment is attached to. C The file name of the attachment. -=item C +=item C + +C A short string describing the attachment. -C The description for the attachment. +Also returned as C, for backwards-compatibility with older +Bugzillas. (However, this backwards-compatibility will go away in Bugzilla +5.0.) =item C @@ -1220,6 +1276,9 @@ private attachments. =item In Bugzilla B<4.0>, the C return value was renamed to C. +=item In Bugzilla B<4.0>, the C return value was renamed to +C. + =back =back @@ -2043,6 +2102,120 @@ method. =back + +=item C + +B + +=over + +=item B + +This allows you to add an attachment to a bug in Bugzilla. + +=item B + +=over + +=item C + +B C An array of ints and/or strings--the ids +or aliases of bugs that you want to add this attachment to. +The same attachment and comment will be added to all +these bugs. + +=item C + +B C The content of the attachment. + +=item C + +B C The "file name" that will be displayed +in the UI for this attachment. + +=item C + +B C A short string describing the +attachment. + +=item C + +B C The MIME type of the attachment, like +C or C. + +=item C + +C A comment to add along with this attachment. + +=item C + +C True if Bugzilla should treat this attachment as a patch. +If you specify this, the C should be C. +(Future versions of Bugzilla will force the C setting +to C for patches and you will not have to specify it manually.) + +Defaults to False if not specified. + +=item C + +C True if the attachment should be private (restricted +to the "insidergroup"), False if the attachment should be public. + +Defaults to False if not specified. + +=item C + +C True if the attachment is just a URL, pointing to data elsewhere. +If so, the C item should just contain the URL. + +Defaults to False if not specified. + +=back + +=item B + +A single item C, which contains the created +attachments in the same format as the C return +value from L. + +=item B + +This method can throw all the same errors as L, plus: + +=over + +=item 600 (Attachment Too Large) + +You tried to attach a file that was larger than Bugzilla will accept. + +=item 601 (Invalid MIME Type) + +You specified a C argument that was blank, not a valid +MIME type, or not a MIME type that Bugzilla accepts for attachments. + +=item 602 (Illegal URL) + +You specified C as True, but the data that you attempted +to attach was not a valid URL. + +=item 603 (File Name Not Specified) + +You did not specify a valid for the C argument. + +=item 604 (Summary Required) + +You did not specify a value for the C argument. + +=item 605 (URL Attaching Disabled) + +You attempted to attach a URL, setting C to True, +but this Bugzilla does not support attaching URLs. + +=back + +=back + + =item C B -- cgit v1.2.3-24-g4f1b