Skip to content

Commit

Permalink
Add REST API to Attach/Detach Tags
Browse files Browse the repository at this point in the history
Fixes #23857,  #23858
  • Loading branch information
vboctor committed Jan 27, 2018
1 parent 65dedb2 commit 66d28e6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
54 changes: 54 additions & 0 deletions api/rest/restcore/issues_rest.php
Expand Up @@ -40,6 +40,12 @@
$g_app->patch( '/{id}', 'rest_issue_update' );
$g_app->patch( '/{id}/', 'rest_issue_update' );

# 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' );

# Monitor
$g_app->post( '/{id}/monitors/', 'rest_issue_monitor_add' );
$g_app->post( '/{id}/monitors', 'rest_issue_monitor_add' );
Expand Down Expand Up @@ -370,3 +376,51 @@ function rest_issue_monitor_add( \Slim\Http\Request $p_request, \Slim\Http\Respo
return $p_response->withStatus( HTTP_STATUS_CREATED, "Users are now monitoring issue $t_issue_id" )->
withJson( array( 'issues' => array( $t_issue ) ) );
}

/**
* Attach a tag to an issue.
*
* @param \Slim\Http\Request $p_request The request.
* @param \Slim\Http\Response $p_response The response.
* @param array $p_args Arguments
* @return \Slim\Http\Response The augmented response.
*/
function rest_issue_tag_attach( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
$t_issue_id = $p_args['id'];
$t_data = array(
'query' => array( 'issue_id' => $t_issue_id ),
'payload' => $p_request->getParsedBody(),
);

$t_command = new TagAttachCommand( $t_data );
$t_command->execute();

$t_issue = mc_issue_get( /* username */ '', /* password */ '', $t_issue_id );

return $p_response->withStatus( HTTP_STATUS_CREATED, "Tag attached to issue $t_issue_id" )->
withJson( array( 'issues' => array( $t_issue ) ) );
}

/**
* Detach a tag from the issue
*
* @param \Slim\Http\Request $p_request The request.
* @param \Slim\Http\Response $p_response The response.
* @param array $p_args Arguments
* @return \Slim\Http\Response The augmented response.
*/
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_data = array(
'query' => array( 'issue_id' => $t_issue_id ),
'payload' => $p_request->getParsedBody(),
);

$t_command = new TagDetachCommand( $t_data );
$t_command->execute();

$t_issue = mc_issue_get( /* username */ '', /* password */ '', $t_issue_id );

return $p_response->withStatus( HTTP_STATUS_SUCCESS, "Tag detached from issue $t_issue_id" )->
withJson( array( 'issues' => array( $t_issue ) ) );
}
10 changes: 6 additions & 4 deletions core/commands/TagAttachCommand.php
Expand Up @@ -124,13 +124,13 @@ function validate() {
* @returns array Command response
*/
protected function process() {
$t_tags_attach = array();
$t_attached_tag_ids = array();

# Attach tags that already exist
foreach( $this->tagsToAttach as $t_tag_id ) {
if( !tag_bug_is_attached( $t_tag_id, $this->issue_id ) ) {
tag_bug_attach( $t_tag_id, $this->issue_id, $this->user_id );
$t_tags_attach[] = tag_get( $t_tag_id );
$t_attached_tag_ids[] = tag_get( $t_tag_id );
}
}

Expand All @@ -139,11 +139,13 @@ protected function process() {
$t_tag_id = tag_create( $t_tag_name, $this->user_id );
if( !tag_bug_is_attached( $t_tag_id, $this->issue_id ) ) {
tag_bug_attach( $t_tag_id, $this->issue_id, $this->user_id );
$t_tags_attach[] = tag_get( $t_tag_id );
$t_attached_tag_ids[] = tag_get( $t_tag_id );
}
}

event_signal( 'EVENT_TAG_ATTACHED', array( $this->issue_id, $t_tags_attach ) );
if( !empty( $t_attached_tag_ids ) ) {
event_signal( 'EVENT_TAG_ATTACHED', array( $this->issue_id, $t_attached_tag_ids ) );
}
}
}

8 changes: 7 additions & 1 deletion core/commands/TagDetachCommand.php
Expand Up @@ -105,12 +105,18 @@ 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 );
event_signal( 'EVENT_TAG_DETACHED', array( $this->issue_id, array( $t_tag_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 ) );
}
}
}

0 comments on commit 66d28e6

Please sign in to comment.