Bug.pm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. # -*- Mode: perl; indent-tabs-mode: nil -*-
  2. #
  3. # The contents of this file are subject to the Mozilla Public
  4. # License Version 1.1 (the "License"); you may not use this file
  5. # except in compliance with the License. You may obtain a copy of
  6. # the License at http://www.mozilla.org/MPL/
  7. #
  8. # Software distributed under the License is distributed on an "AS
  9. # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10. # implied. See the License for the specific language governing
  11. # rights and limitations under the License.
  12. #
  13. # The Original Code is the Bugzilla Bug Tracking System.
  14. #
  15. # Contributor(s): Marc Schumann <wurblzap@gmail.com>
  16. # Max Kanat-Alexander <mkanat@bugzilla.org>
  17. # Mads Bondo Dydensborg <mbd@dbc.dk>
  18. # Tsahi Asher <tsahi_75@yahoo.com>
  19. package Bugzilla::WebService::Bug;
  20. use strict;
  21. use base qw(Bugzilla::WebService);
  22. import SOAP::Data qw(type);
  23. use Bugzilla::Constants;
  24. use Bugzilla::Error;
  25. use Bugzilla::Field;
  26. use Bugzilla::WebService::Constants;
  27. use Bugzilla::Bug;
  28. use Bugzilla::BugMail;
  29. use Bugzilla::Util qw(trim);
  30. #############
  31. # Constants #
  32. #############
  33. # This maps the names of internal Bugzilla bug fields to things that would
  34. # make sense to somebody who's not intimately familiar with the inner workings
  35. # of Bugzilla. (These are the field names that the WebService uses.)
  36. use constant FIELD_MAP => {
  37. status => 'bug_status',
  38. severity => 'bug_severity',
  39. description => 'comment',
  40. summary => 'short_desc',
  41. platform => 'rep_platform',
  42. };
  43. use constant GLOBAL_SELECT_FIELDS => qw(
  44. bug_severity
  45. bug_status
  46. op_sys
  47. priority
  48. rep_platform
  49. resolution
  50. );
  51. use constant PRODUCT_SPECIFIC_FIELDS => qw(version target_milestone component);
  52. ######################################################
  53. # Add aliases here for old method name compatibility #
  54. ######################################################
  55. BEGIN { *get_bugs = \&get }
  56. ###########
  57. # Methods #
  58. ###########
  59. sub get {
  60. my ($self, $params) = @_;
  61. my $ids = $params->{ids};
  62. defined $ids || ThrowCodeError('param_required', { param => 'ids' });
  63. my @return;
  64. foreach my $bug_id (@$ids) {
  65. ValidateBugID($bug_id);
  66. my $bug = new Bugzilla::Bug($bug_id);
  67. # Timetracking fields are deleted if the user doesn't belong to
  68. # the corresponding group.
  69. unless (Bugzilla->user->in_group(Bugzilla->params->{'timetrackinggroup'})) {
  70. delete $bug->{'estimated_time'};
  71. delete $bug->{'remaining_time'};
  72. delete $bug->{'deadline'};
  73. }
  74. # This is done in this fashion in order to produce a stable API.
  75. # The internals of Bugzilla::Bug are not stable enough to just
  76. # return them directly.
  77. my $creation_ts = $self->datetime_format($bug->creation_ts);
  78. my $delta_ts = $self->datetime_format($bug->delta_ts);
  79. my %item;
  80. $item{'creation_time'} = type('dateTime')->value($creation_ts);
  81. $item{'last_change_time'} = type('dateTime')->value($delta_ts);
  82. $item{'internals'} = $bug;
  83. $item{'id'} = type('int')->value($bug->bug_id);
  84. $item{'summary'} = type('string')->value($bug->short_desc);
  85. if (Bugzilla->params->{'usebugaliases'}) {
  86. $item{'alias'} = type('string')->value($bug->alias);
  87. }
  88. else {
  89. # For API reasons, we always want the value to appear, we just
  90. # don't want it to have a value if aliases are turned off.
  91. $item{'alias'} = undef;
  92. }
  93. push(@return, \%item);
  94. }
  95. return { bugs => \@return };
  96. }
  97. sub create {
  98. my ($self, $params) = @_;
  99. Bugzilla->login(LOGIN_REQUIRED);
  100. my %field_values;
  101. foreach my $field (keys %$params) {
  102. my $field_name = FIELD_MAP->{$field} || $field;
  103. $field_values{$field_name} = $params->{$field};
  104. }
  105. # WebService users can't set the creation date of a bug.
  106. delete $field_values{'creation_ts'};
  107. my $bug = Bugzilla::Bug->create(\%field_values);
  108. Bugzilla::BugMail::Send($bug->bug_id, { changer => $bug->reporter->login });
  109. return { id => type('int')->value($bug->bug_id) };
  110. }
  111. sub legal_values {
  112. my ($self, $params) = @_;
  113. my $field = FIELD_MAP->{$params->{field}} || $params->{field};
  114. my @custom_select = Bugzilla->get_fields(
  115. {custom => 1, type => [FIELD_TYPE_SINGLE_SELECT, FIELD_TYPE_MULTI_SELECT]});
  116. # We only want field names.
  117. @custom_select = map {$_->name} @custom_select;
  118. my $values;
  119. if (grep($_ eq $field, GLOBAL_SELECT_FIELDS, @custom_select)) {
  120. $values = get_legal_field_values($field);
  121. }
  122. elsif (grep($_ eq $field, PRODUCT_SPECIFIC_FIELDS)) {
  123. my $id = $params->{product_id};
  124. defined $id || ThrowCodeError('param_required',
  125. { function => 'Bug.legal_values', param => 'product_id' });
  126. grep($_->id eq $id, @{Bugzilla->user->get_accessible_products})
  127. || ThrowUserError('product_access_denied', { product => $id });
  128. my $product = new Bugzilla::Product($id);
  129. my @objects;
  130. if ($field eq 'version') {
  131. @objects = @{$product->versions};
  132. }
  133. elsif ($field eq 'target_milestone') {
  134. @objects = @{$product->milestones};
  135. }
  136. elsif ($field eq 'component') {
  137. @objects = @{$product->components};
  138. }
  139. $values = [map { $_->name } @objects];
  140. }
  141. else {
  142. ThrowCodeError('invalid_field_name', { field => $params->{field} });
  143. }
  144. my @result;
  145. foreach my $val (@$values) {
  146. push(@result, type('string')->value($val));
  147. }
  148. return { values => \@result };
  149. }
  150. sub add_comment {
  151. my ($self, $params) = @_;
  152. #The user must login in order add a comment
  153. Bugzilla->login(LOGIN_REQUIRED);
  154. # Check parameters
  155. defined $params->{id}
  156. || ThrowCodeError('param_required', { param => 'id' });
  157. ValidateBugID($params->{id});
  158. my $comment = $params->{comment};
  159. (defined $comment && trim($comment) ne '')
  160. || ThrowCodeError('param_required', { param => 'comment' });
  161. my $bug = new Bugzilla::Bug($params->{id});
  162. Bugzilla->user->can_edit_product($bug->product_id)
  163. || ThrowUserError("product_edit_denied", {product => $bug->product});
  164. # Append comment
  165. $bug->add_comment($comment, { isprivate => $params->{private},
  166. work_time => $params->{work_time} });
  167. $bug->update();
  168. # Send mail.
  169. Bugzilla::BugMail::Send($bug->bug_id, { changer => Bugzilla->user->login });
  170. return undef;
  171. }
  172. 1;
  173. __END__
  174. =head1 NAME
  175. Bugzilla::Webservice::Bug - The API for creating, changing, and getting the
  176. details of bugs.
  177. =head1 DESCRIPTION
  178. This part of the Bugzilla API allows you to file a new bug in Bugzilla,
  179. or get information about bugs that have already been filed.
  180. =head1 METHODS
  181. See L<Bugzilla::WebService> for a description of how parameters are passed,
  182. and what B<STABLE>, B<UNSTABLE>, and B<EXPERIMENTAL> mean.
  183. =head2 Utility Functions
  184. =over
  185. =item C<legal_values>
  186. B<EXPERIMENTAL>
  187. =over
  188. =item B<Description>
  189. Tells you what values are allowed for a particular field.
  190. =item B<Params>
  191. =over
  192. =item C<field> - The name of the field you want information about.
  193. This should be the same as the name you would use in L</create>, below.
  194. =item C<product_id> - If you're picking a product-specific field, you have
  195. to specify the id of the product you want the values for.
  196. =back
  197. =item B<Returns>
  198. C<values> - An array of strings: the legal values for this field.
  199. The values will be sorted as they normally would be in Bugzilla.
  200. =item B<Errors>
  201. =over
  202. =item 106 (Invalid Product)
  203. You were required to specify a product, and either you didn't, or you
  204. specified an invalid product (or a product that you can't access).
  205. =item 108 (Invalid Field Name)
  206. You specified a field that doesn't exist or isn't a drop-down field.
  207. =back
  208. =back
  209. =back
  210. =head2 Bug Information
  211. =over
  212. =item C<get>
  213. B<EXPERIMENTAL>
  214. =over
  215. =item B<Description>
  216. Gets information about particular bugs in the database.
  217. Note: Can also be called as "get_bugs" for compatibilty with Bugzilla 3.0 API.
  218. =item B<Params>
  219. =over
  220. =item C<ids>
  221. An array of numbers and strings.
  222. If an element in the array is entirely numeric, it represents a bug_id
  223. from the Bugzilla database to fetch. If it contains any non-numeric
  224. characters, it is considered to be a bug alias instead, and the bug with
  225. that alias will be loaded.
  226. Note that it's possible for aliases to be disabled in Bugzilla, in which
  227. case you will be told that you have specified an invalid bug_id if you
  228. try to specify an alias. (It will be error 100.)
  229. =back
  230. =item B<Returns>
  231. A hash containing a single element, C<bugs>. This is an array of hashes.
  232. Each hash contains the following items:
  233. =over
  234. =item id
  235. C<int> The numeric bug_id of this bug.
  236. =item alias
  237. C<string> The alias of this bug. If there is no alias or aliases are
  238. disabled in this Bugzilla, this will be an empty string.
  239. =item summary
  240. C<string> The summary of this bug.
  241. =item creation_time
  242. C<dateTime> When the bug was created.
  243. =item last_change_time
  244. C<dateTime> When the bug was last changed.
  245. =item internals B<UNSTABLE>
  246. A hash. The internals of a L<Bugzilla::Bug> object. This is extremely
  247. unstable, and you should only rely on this if you absolutely have to. The
  248. structure of the hash may even change between point releases of Bugzilla.
  249. =back
  250. =item B<Errors>
  251. =over
  252. =item 100 (Invalid Bug Alias)
  253. If you specified an alias and either: (a) the Bugzilla you're querying
  254. doesn't support aliases or (b) there is no bug with that alias.
  255. =item 101 (Invalid Bug ID)
  256. The bug_id you specified doesn't exist in the database.
  257. =item 102 (Access Denied)
  258. You do not have access to the bug_id you specified.
  259. =back
  260. =back
  261. =back
  262. =head2 Bug Creation and Modification
  263. =over
  264. =item C<create> B<EXPERIMENTAL>
  265. =over
  266. =item B<Description>
  267. This allows you to create a new bug in Bugzilla. If you specify any
  268. invalid fields, they will be ignored. If you specify any fields you
  269. are not allowed to set, they will just be set to their defaults or ignored.
  270. You cannot currently set all the items here that you can set on enter_bug.cgi.
  271. The WebService interface may allow you to set things other than those listed
  272. here, but realize that anything undocumented is B<UNSTABLE> and will very
  273. likely change in the future.
  274. =item B<Params>
  275. Some params must be set, or an error will be thrown. These params are
  276. marked B<Required>.
  277. Some parameters can have defaults set in Bugzilla, by the administrator.
  278. If these parameters have defaults set, you can omit them. These parameters
  279. are marked B<Defaulted>.
  280. Clients that want to be able to interact uniformly with multiple
  281. Bugzillas should always set both the params marked B<Required> and those
  282. marked B<Defaulted>, because some Bugzillas may not have defaults set
  283. for B<Defaulted> parameters, and then this method will throw an error
  284. if you don't specify them.
  285. The descriptions of the parameters below are what they mean when Bugzilla is
  286. being used to track software bugs. They may have other meanings in some
  287. installations.
  288. =over
  289. =item C<product> (string) B<Required> - The name of the product the bug
  290. is being filed against.
  291. =item C<component> (string) B<Required> - The name of a component in the
  292. product above.
  293. =item C<summary> (string) B<Required> - A brief description of the bug being
  294. filed.
  295. =item C<version> (string) B<Required> - A version of the product above;
  296. the version the bug was found in.
  297. =item C<description> (string) B<Defaulted> - The initial description for
  298. this bug. Some Bugzilla installations require this to not be blank.
  299. =item C<op_sys> (string) B<Defaulted> - The operating system the bug was
  300. discovered on.
  301. =item C<platform> (string) B<Defaulted> - What type of hardware the bug was
  302. experienced on.
  303. =item C<priority> (string) B<Defaulted> - What order the bug will be fixed
  304. in by the developer, compared to the developer's other bugs.
  305. =item C<severity> (string) B<Defaulted> - How severe the bug is.
  306. =item C<alias> (string) - A brief alias for the bug that can be used
  307. instead of a bug number when accessing this bug. Must be unique in
  308. all of this Bugzilla.
  309. =item C<assigned_to> (username) - A user to assign this bug to, if you
  310. don't want it to be assigned to the component owner.
  311. =item C<cc> (array) - An array of usernames to CC on this bug.
  312. =item C<qa_contact> (username) - If this installation has QA Contacts
  313. enabled, you can set the QA Contact here if you don't want to use
  314. the component's default QA Contact.
  315. =item C<status> (string) - The status that this bug should start out as.
  316. Note that only certain statuses can be set on bug creation.
  317. =item C<target_milestone> (string) - A valid target milestone for this
  318. product.
  319. =back
  320. In addition to the above parameters, if your installation has any custom
  321. fields, you can set them just by passing in the name of the field and
  322. its value as a string.
  323. =item B<Returns>
  324. A hash with one element, C<id>. This is the id of the newly-filed bug.
  325. =item B<Errors>
  326. =over
  327. =item 51 (Invalid Object)
  328. The component you specified is not valid for this Product.
  329. =item 103 (Invalid Alias)
  330. The alias you specified is invalid for some reason. See the error message
  331. for more details.
  332. =item 104 (Invalid Field)
  333. One of the drop-down fields has an invalid value, or a value entered in a
  334. text field is too long. The error message will have more detail.
  335. =item 105 (Invalid Component)
  336. You didn't specify a component.
  337. =item 106 (Invalid Product)
  338. Either you didn't specify a product, this product doesn't exist, or
  339. you don't have permission to enter bugs in this product.
  340. =item 107 (Invalid Summary)
  341. You didn't specify a summary for the bug.
  342. =item 504 (Invalid User)
  343. Either the QA Contact, Assignee, or CC lists have some invalid user
  344. in them. The error message will have more details.
  345. =back
  346. =item B<History>
  347. =over
  348. =item Before B<3.0.4>, parameters marked as B<Defaulted> were actually
  349. B<Required>, due to a bug in Bugzilla.
  350. =back
  351. =back
  352. =item C<add_comment>
  353. B<EXPERIMENTAL>
  354. =over
  355. =item B<Description>
  356. This allows you to add a comment to a bug in Bugzilla.
  357. =item B<Params>
  358. =over
  359. =item C<id> (int) B<Required> - The id or alias of the bug to append a
  360. comment to.
  361. =item C<comment> (string) B<Required> - The comment to append to the bug.
  362. If this is empty or all whitespace, an error will be thrown saying that
  363. you did not set the C<comment> parameter.
  364. =item C<private> (boolean) - If set to true, the comment is private, otherwise
  365. it is assumed to be public.
  366. =item C<work_time> (double) - Adds this many hours to the "Hours Worked"
  367. on the bug. If you are not in the time tracking group, this value will
  368. be ignored.
  369. =back
  370. =item B<Errors>
  371. =over
  372. =item 100 (Invalid Bug Alias)
  373. If you specified an alias and either: (a) the Bugzilla you're querying
  374. doesn't support aliases or (b) there is no bug with that alias.
  375. =item 101 (Invalid Bug ID)
  376. The id you specified doesn't exist in the database.
  377. =item 108 (Bug Edit Denied)
  378. You did not have the necessary rights to edit the bug.
  379. =back
  380. =item B<History>
  381. =over
  382. =item Added in Bugzilla B<3.2>.
  383. =back
  384. =back
  385. =back