Skip to content

Commit

Permalink
DELETE tag REST API shouldn’t have a payload
Browse files Browse the repository at this point in the history
Fixes #23858
  • Loading branch information
vboctor committed Jan 27, 2018
1 parent 66d28e6 commit 627c8a2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 64 deletions.
9 changes: 5 additions & 4 deletions api/rest/restcore/issues_rest.php
Expand Up @@ -43,8 +43,8 @@
# Tags
$g_app->post( '/{id}/tags', 'rest_issue_tag_attach' );
$g_app->post( '/{id}/tags/', 'rest_issue_tag_attach' );
$g_app->delete( '/{id}/tags', 'rest_issue_tag_detach' );
$g_app->delete( '/{id}/tags/', 'rest_issue_tag_detach' );
$g_app->delete( '/{id}/tags/{tag_id}', 'rest_issue_tag_detach' );
$g_app->delete( '/{id}/tags/{tag_id}/', 'rest_issue_tag_detach' );

# Monitor
$g_app->post( '/{id}/monitors/', 'rest_issue_monitor_add' );
Expand Down Expand Up @@ -411,9 +411,10 @@ function rest_issue_tag_attach( \Slim\Http\Request $p_request, \Slim\Http\Respon
*/
function rest_issue_tag_detach( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
$t_issue_id = $p_args['id'];
$t_tag_id = $p_args['tag_id'];

$t_data = array(
'query' => array( 'issue_id' => $t_issue_id ),
'payload' => $p_request->getParsedBody(),
'query' => array( 'issue_id' => $t_issue_id, 'tag_id' => $t_tag_id )
);

$t_command = new TagDetachCommand( $t_data );
Expand Down
66 changes: 14 additions & 52 deletions core/commands/TagDetachCommand.php
Expand Up @@ -27,21 +27,7 @@
* A command that detaches a tag from an issue.
*
* {
* "query": { "issue_id" => 1234 },
* "payload": {
* "tags": [
* {
* "id": 1
* },
* {
* "name": "tag2"
* },
* {
* "id": 3,
* "name": "tag3"
* }
* ]
* }
* "query": { "issue_id" => 1234, "tag_id" => 1 }
* }
*/
class TagDetachCommand extends Command {
Expand All @@ -51,14 +37,14 @@ class TagDetachCommand extends Command {
private $issue_id;

/**
* @var integer logged in user id
* @var integer tag id
*/
private $user_id;
private $tag_id;

/**
* @var array Array of tag ids to be detached.
* @var integer logged in user id
*/
private $tagsToDetach = array();
private $user_id;

/**
* @param array $p_data The command data.
Expand All @@ -72,30 +58,14 @@ function __construct( array $p_data ) {
*/
function validate() {
$this->issue_id = helper_parse_issue_id( $this->query( 'issue_id' ) );
$this->tag_id = $this->query( 'tag_id' );
$this->user_id = auth_get_current_user_id();

$t_tags = $this->payload( 'tags', array() );
if( !is_array( $t_tags ) || empty( $t_tags ) ) {
throw new ClientException( 'Invalid tags array', ERROR_INVALID_FIELD_VALUE, array( 'tags' ) );
}

foreach( $t_tags as $t_tag ) {
if( isset( $t_tag['id'] ) ) {
$this->tagsToDetach[] = (int)$t_tag['id'];
} else if( isset( $t_tag['name'] ) ) {
$t_tag_row = tag_get_by_name( $t_tag['name'] );
if( $t_tag_row === false ) {
throw new ClientException(
sprintf( "Tag '%s' not found", $t_tag['name'] ),
ERROR_INVALID_FIELD_VALUE,
array( 'tags' ) );
} else {
$this->tagsToDetach[] = (int)$t_tag_row['id'];
}
} else {
# invalid tag with no id or name.
throw new ClientException( "Invalid tag with no id or name", ERROR_INVALID_FIELD_VALUE, array( 'tags' ) );
}
if( !is_numeric( $this->tag_id ) ) {
throw new ClientException(
sprintf( "Invalid tag id '%s'", $this->tag_id ),
ERROR_INVALID_FIELD_VALUE,
array( 'tag_id' ) );
}
}

Expand All @@ -105,17 +75,9 @@ function validate() {
* @returns array Command response
*/
protected function process() {
$t_detached_tag_ids = array();

foreach( $this->tagsToDetach as $t_tag_id ) {
if( tag_bug_is_attached( $t_tag_id, $this->issue_id ) ) {
tag_bug_detach( $t_tag_id, $this->issue_id );
$t_detached_tag_ids[] = $t_tag_id;
}
}

if( !empty( $t_detached_tag_ids ) ) {
event_signal( 'EVENT_TAG_DETACHED', array( $this->issue_id, $t_detached_tag_ids ) );
if( tag_bug_is_attached( $this->tag_id, $this->issue_id ) ) {
tag_bug_detach( $this->tag_id, $this->issue_id );
event_signal( 'EVENT_TAG_DETACHED', array( $this->issue_id, array( $this->tag_id ) ) );
}
}
}
Expand Down
9 changes: 1 addition & 8 deletions tag_detach.php
Expand Up @@ -40,14 +40,7 @@
$f_bug_id = gpc_get_int( 'bug_id' );

$t_data = array(
'query' => array( 'issue_id' => $f_bug_id ),
'payload' => array(
'tags' => array(
array(
'id' => $f_tag_id
)
)
)
'query' => array( 'issue_id' => $f_bug_id, 'tag_id' => $f_tag_id )
);

$t_command = new TagDetachCommand( $t_data );
Expand Down

0 comments on commit 627c8a2

Please sign in to comment.