From 4f2eccc20de8b4ba8df9030ce07c836d2bae515b Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Thu, 17 Dec 2009 05:13:55 +0000 Subject: Bug 506559: WebService function to get information about fields (Bug.fields) Patch by Frank Becker r=mkanat, a=mkanat --- Bugzilla/WebService/Bug.pm | 334 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 333 insertions(+), 1 deletion(-) diff --git a/Bugzilla/WebService/Bug.pm b/Bugzilla/WebService/Bug.pm index 75c6d76b5..6051a1d1c 100644 --- a/Bugzilla/WebService/Bug.pm +++ b/Bugzilla/WebService/Bug.pm @@ -17,6 +17,7 @@ # Mads Bondo Dydensborg # Tsahi Asher # Noura Elhawary +# Frank Becker package Bugzilla::WebService::Bug; @@ -32,6 +33,9 @@ use Bugzilla::WebService::Util qw(filter validate); use Bugzilla::Bug; use Bugzilla::BugMail; use Bugzilla::Util qw(trim); +use Bugzilla::Version; +use Bugzilla::Milestone; +use Bugzilla::Status; ############# # Constants # @@ -59,6 +63,145 @@ BEGIN { # Methods # ########### +sub fields { + my ($self, $params) = validate(@_, 'ids', 'names'); + + my @fields; + if (defined $params->{ids}) { + my $ids = $params->{ids}; + foreach my $id (@$ids) { + my $loop_field = Bugzilla::Field->check({ id => $id }); + push(@fields, $loop_field); + } + } + + if (defined $params->{names}) { + my $names = $params->{names}; + foreach my $field_name (@$names) { + my $loop_field = Bugzilla::Field->check($field_name); + push(@fields, $loop_field); + } + } + + if (!defined $params->{ids} + and !defined $params->{names}) + { + @fields = @{Bugzilla::Field->match({obsolete => 0})}; + } + + my @fields_out; + foreach my $field (@fields) { + my $visibility_field = $field->visibility_field + ? $field->visibility_field->name : undef; + my $visibility_value = $field->visibility_value + ? $field->visibility_value->name : undef; + my $value_field = $field->value_field + ? $field->value_field->name : undef; + + my @values; + if ( ($field->is_select and $field->name ne 'product') + or grep($_ eq $field->name, PRODUCT_SPECIFIC_FIELDS)) + { + @values = @{ $self->_legal_field_values({ field => $field }) }; + } + + if (grep($_ eq $field->name, PRODUCT_SPECIFIC_FIELDS)) { + $value_field = 'product'; + } + + push (@fields_out, filter $params, { + id => $self->type('int', $field->id), + type => $self->type('int', $field->type), + is_custom => $self->type('boolean', $field->custom), + name => $self->type('string', $field->name), + display_name => $self->type('string', $field->description), + is_on_bug_entry => $self->type('boolean', $field->enter_bug), + visibility_field => $self->type('string', $visibility_field), + visibility_values => [$self->type('string', $visibility_value)], + value_field => $self->type('string', $value_field), + values => \@values, + }); + } + + return { fields => \@fields_out }; +} + +sub _legal_field_values { + my ($self, $params) = @_; + my $field = $params->{field}; + my $field_name = $field->name; + my $user = Bugzilla->user; + + my @result; + if (grep($_ eq $field_name, PRODUCT_SPECIFIC_FIELDS)) { + my @list; + if ($field_name eq 'version') { + @list = Bugzilla::Version->get_all; + } + elsif ($field_name eq 'component') { + @list = Bugzilla::Component->get_all; + } + else { + @list = Bugzilla::Milestone->get_all; + } + + foreach my $value (@list) { + my $sortkey = $field_name eq 'target_milestone' + ? $value->sortkey : 0; + # XXX This is very slow for large numbers of values. + my $product_name = $value->product->name; + if ($user->can_see_product($product_name)) { + push(@result, { + name => $self->type('string', $value->name), + sortkey => $self->type('int', $sortkey), + visibility_values => [$self->type('string', $product_name)], + }); + } + } + } + + elsif ($field_name eq 'bug_status') { + my @status_all = Bugzilla::Status->get_all; + foreach my $status (@status_all) { + my @can_change_to; + foreach my $change_to (@{ $status->can_change_to }) { + # There's no need to note that a status can transition + # to itself. + next if $change_to->id == $status->id; + my %change_to_hash = ( + name => $self->type('string', $change_to->name), + comment_required => $self->type('boolean', + $change_to->comment_required_on_change_from($status)), + ); + push(@can_change_to, \%change_to_hash); + } + + push (@result, { + name => $self->type('string', $status->name), + is_open => $self->type('boolean', $status->is_open), + sortkey => $self->type('int', $status->sortkey), + can_change_to => \@can_change_to, + }); + } + } + + else { + my @values = Bugzilla::Field::Choice->type($field)->get_all(); + foreach my $value (@values) { + my $visibility_value = $value->visibility_value; + my $vis_val_name = $visibility_value ? $visibility_value->name + : undef; + push(@result, { + name => $self->type('string', $value->name), + sortkey => $self->type('int' , $value->sortkey), + visibility_values => [$self->type('string', $vis_val_name)], + }); + } + } + + return \@result; +} + sub comments { my ($self, $params) = validate(@_, 'ids', 'comment_ids'); @@ -549,9 +692,198 @@ and what B, B, and B mean. =over +=item C + +B + +=over + +=item B + +Get information about valid bug fields, including the lists of legal values +for each field. + +=item B + +You can pass either field ids or field names. + +B: If neither C nor C is specified, then all +non-obsolete fields will be returned. + +In addition to the parameters below, this method also accepts the +standard L and +L arguments. + +=over + +=item C (array) - An array of integer field ids. + +=item C (array) - An array of strings representing field names. + +=back + +=item B + +A hash containing a single element, C. This is an array of hashes, +containing the following keys: + +=over + +=item C + +C An integer id uniquely idenfifying this field in this installation only. + +=item C + +C The number of the fieldtype. The following values are defined: + +=over + +=item C<0> Unknown + +=item C<1> Free Text + +=item C<2> Drop Down + +=item C<3> Multiple-Selection Box + +=item C<4> Large Text Box + +=item C<5> Date/Time + +=item C<6> Bug Id + +=item C<7> Bug URLs ("See Also") + +=back + +=item C + +C True when this is a custom field, false otherwise. + +=item C + +C The internal name of this field. This is a unique identifier for +this field. If this is not a custom field, then this name will be the same +across all Bugzilla installations. + +=item C + +C The name of the field, as it is shown in the user interface. + +=item C + +C For custom fields, this is true if the field is shown when you +enter a new bug. For standard fields, this is currently always false, +even if the field shows up when entering a bug. (To know whether or not +a standard field is valid on bug entry, see L.) + +=item C + +C The name of a field that controls the visibility of this field +in the user interface. This field only appears in the user interface when +the named field is equal to one of the values in C. +Can be null. + +=item C + +C of Cs This field is only shown when C +matches one of these values. When C is null, +then this is an empty array. + +=item C + +C The name of the field that controls whether or not particular +values of the field are shown in the user interface. Can be null. + +=item C + +This is an array of hashes, representing the legal values for +select-type (drop-down and multiple-selection) fields. This is also +populated for the C, C, and C +fields, but not for the C field (you must use +L +for that. + +For fields that aren't select-type fields, this will simply be an empty +array. + +Each hash has the following keys: + +=over + +=item C + +C The actual value--this is what you would specify for this +field in L, etc. + +=item C + +C Values, when displayed in a list, are sorted first by this integer +and then secondly by their name. + +=item C + +If C is defined for this field, then this value is only shown +if the C is set to one of the values listed in this array. +Note that for per-product fields, C is set to C<'product'> +and C will reflect which product(s) this value appears in. + +=item C + +C For C values, determines whether this status +specifies that the bug is "open" (true) or "closed" (false). This item +is only included for the C field. + +=item C + +For C values, this is an array of hashes that determines which +statuses you can transition to from this status. (This item is only included +for the C field.) + +Each hash contains the following items: + +=over + +=item C + +the name of the new status + +=item C + +this C True if a comment is required when you change a bug into +this status using this transition. + +=back + +=back + +=back + +=item B + +=over + +=item 51 (Invalid Field Name or Id) + +You specified an invalid field name or id. + +=back + +=item B + +=over + +=item Added in Bugzilla B<3.6>. + +=back + +=back + + =item C -B +B - Use L instead. =over -- cgit v1.2.3-24-g4f1b