Skip to content

Commit

Permalink
Remove special handling of '-1'
Browse files Browse the repository at this point in the history
As discussed with @vboctor on Gitter [1], the Command should create the
tag if specified by name and it does not exist (and user is allowed to
do so).

IssueAddCommand now processes each provided tag as follows:

- if *ID* (or ID + name) is provided, check for existing tag and attach
  it to the issue, return 404 (ERROR_TAG_NOT_FOUND) if not found.
- if *name* is provided, check its validity (tag_name_is_valid()) and
  return 400 bad request (ERROR_TAG_NAME_INVALID) if not.
- if neither *ID* nor *name* are given, return 400 bad request
  (ERROR_TAG_NAME_INVALID).
- if the provided name does not exist, create the tag; return 404
  (ERROR_TAG_NOT_FOUND) if user is not allowed to.

If all tags pass the above checks, create the issue and attach the tags
to it.

Fixes #26077

This commits includes an adjustment to bug_report.php: the payload for
the issue's tags no longer includes the id, as negative values would
now cause the Command to throw an exception.

[1]: https://gitter.im/mantisbt?at=5d5bad020eff7d2dfee2ce73
  • Loading branch information
dregad committed Aug 25, 2019
1 parent b3a10d8 commit ef00dc1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bug_report.php
Expand Up @@ -106,7 +106,7 @@
if( !empty( $t_tags ) ) {
$t_issue['tags'] = array();
foreach( $t_tags as $t_tag ) {
$t_issue['tags'][] = array( 'id' => $t_tag['id'], 'name' => $t_tag['name'] );
$t_issue['tags'][] = array( 'name' => $t_tag['name'] );
}
}

Expand Down
22 changes: 11 additions & 11 deletions core/commands/IssueAddCommand.php
Expand Up @@ -222,7 +222,7 @@ protected function validate() {
if( isset( $t_issue['tags'] ) && is_array( $t_issue['tags'] ) ) {
foreach( $t_issue['tags'] as $t_tag ) {
$t_tag_id = $this->get_tag_id( $t_tag );
if( $t_tag_id === -1 && !tag_can_create( $this->user_id ) ) {
if( $t_tag_id === false && !tag_can_create( $this->user_id ) ) {
throw new ClientException(
sprintf( "User '%d' can't create tag '%s'.", $this->user_id, $t_tag['name'] ),
ERROR_TAG_NOT_FOUND );
Expand Down Expand Up @@ -348,7 +348,7 @@ protected function process() {
if( isset( $t_issue['tags'] ) && is_array( $t_issue['tags'] ) ) {
$t_tags = array();
foreach( $t_issue['tags'] as $t_tag ) {
if( $this->get_tag_id( $t_tag ) === -1 ) {
if( $this->get_tag_id( $t_tag ) === false ) {
$t_tag['id'] = tag_create( $t_tag['name'], $this->user_id );
log_event( LOG_WEBSERVICE,
"created new tag '" . $t_tag['name'] . "' id '" . $t_tag['id'] . "'"
Expand Down Expand Up @@ -467,14 +467,14 @@ protected function process() {
* A tag element is an array with either an 'id', a 'name' key, or both.
* If id is provided, check that it exists and return it;
* if name is supplied, look it up and return the corresponding ID, or
* -1 if not found (meaning Tag should be created).
* false if not found.
*
* @param array $p_tag Tag element
* @return integer Tag ID or -1 if tag must be created
* @return integer|false Tag ID or false if tag does not exist
* @throws ClientException
*/
private function get_tag_id( array $p_tag ) {
if( isset( $p_tag['id'] ) && $p_tag['id'] != -1 ) {
if( isset( $p_tag['id'] ) ) {
$t_tag_id = $p_tag['id'];
if( !tag_exists( $t_tag_id ) ) {
throw new ClientException(
Expand All @@ -483,15 +483,15 @@ private function get_tag_id( array $p_tag ) {
);
}
} elseif( isset( $p_tag['name'] ) ) {
$t_existing_tag = tag_get_by_name( $p_tag['name'] );
if( $t_existing_tag === false && isset( $p_tag['id'] ) && $p_tag['id'] != -1 ) {
$t_matches = array();
if( !tag_name_is_valid( $p_tag['name'], $t_matches )) {
throw new ClientException(
"Tag {$p_tag['name']} not found.",
ERROR_TAG_NOT_FOUND
"Tag name '{$p_tag['name']}' is not valid.",
ERROR_TAG_NAME_INVALID
);
}

$t_tag_id = $t_existing_tag['id'];
$t_existing_tag = tag_get_by_name( $p_tag['name'] );
$t_tag_id = $t_existing_tag === false ? false : $t_existing_tag['id'];
} else {
throw new ClientException(
'Tag without id or name.',
Expand Down

0 comments on commit ef00dc1

Please sign in to comment.