Skip to content

Commit

Permalink
New method IssueAddCommand::get_tag_id()
Browse files Browse the repository at this point in the history
The new method throws ClientException if the given Tag element is
invalid, thus preventing issue creation via REST API in this case.

Previously the issue would be created even though the API call was
failing with a 404 error caused by the invalid tag.

Fixes #26076
  • Loading branch information
dregad committed Aug 25, 2019
1 parent 83bc60f commit b3a10d8
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions core/commands/IssueAddCommand.php
Expand Up @@ -218,13 +218,15 @@ protected function validate() {
}
}

if( isset( $t_issue['tags'] ) && is_array( $t_issue['tags'] ) && !tag_can_create( $this->user_id ) ) {
# Validate tags and make sure user is allowed to create them if needed
if( isset( $t_issue['tags'] ) && is_array( $t_issue['tags'] ) ) {
foreach( $t_issue['tags'] as $t_tag ) {
if( isset( $t_tag['id'] ) && $t_tag['id'] === -1 ) {
$t_tag_id = $this->get_tag_id( $t_tag );
if( $t_tag_id === -1 && !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 @@ -346,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( isset( $t_tag['id'] ) && $t_tag['id'] === -1 ) {
if( $this->get_tag_id( $t_tag ) === -1 ) {
$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 @@ -458,5 +460,45 @@ protected function process() {

return array( 'issue_id' => $t_issue_id );
}

/**
* Retrieves the Tag ID for the given Tag element.
*
* 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).
*
* @param array $p_tag Tag element
* @return integer Tag ID or -1 if tag must be created
* @throws ClientException
*/
private function get_tag_id( array $p_tag ) {
if( isset( $p_tag['id'] ) && $p_tag['id'] != -1 ) {
$t_tag_id = $p_tag['id'];
if( !tag_exists( $t_tag_id ) ) {
throw new ClientException(
"Tag with id $t_tag_id not found.",
ERROR_TAG_NOT_FOUND
);
}
} 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 ) {
throw new ClientException(
"Tag {$p_tag['name']} not found.",
ERROR_TAG_NOT_FOUND
);
}

$t_tag_id = $t_existing_tag['id'];
} else {
throw new ClientException(
'Tag without id or name.',
ERROR_TAG_NAME_INVALID
);
}
return $t_tag_id;
}
}

0 comments on commit b3a10d8

Please sign in to comment.