Skip to content

Commit

Permalink
Allow getting/setting tags as part of the mc_issue(get/set/add) methods
Browse files Browse the repository at this point in the history
Fixes #13446: Add tags support to soap api
  • Loading branch information
rombert committed Dec 5, 2011
1 parent 633d1d0 commit 5ccae29
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 2 deletions.
3 changes: 2 additions & 1 deletion api/soap/mantisconnect.php
Expand Up @@ -288,7 +288,8 @@
'notes' => array( 'name' => 'notes', 'type' => 'tns:IssueNoteDataArray', 'minOccurs' => '0' ),
'custom_fields' => array( 'name' => 'custom_fields', 'type' => 'tns:CustomFieldValueForIssueDataArray', 'minOccurs' => '0' ),
'due_date' => array( 'name' => 'due_date', 'type' => 'xsd:dateTime', 'minOccurs' => '0' ),
'monitors' => array( 'name' => 'monitors', 'type' => 'tns:AccountDataArray', 'minOccurs' => '0')
'monitors' => array( 'name' => 'monitors', 'type' => 'tns:AccountDataArray', 'minOccurs' => '0'),
'tags' => array( 'name' => 'tags', 'type' => 'tns:ObjectRefArray', 'minOccurs' => '0')
)
);

Expand Down
30 changes: 29 additions & 1 deletion api/soap/mc_issue_api.php
Expand Up @@ -111,6 +111,7 @@ function mc_issue_get( $p_username, $p_password, $p_issue_id ) {
$t_issue_data['notes'] = mci_issue_get_notes( $p_issue_id );
$t_issue_data['custom_fields'] = mci_issue_get_custom_fields( $p_issue_id );
$t_issue_data['monitors'] = mci_account_get_array_by_ids( bug_get_monitors ( $p_issue_id ) );
$t_issue_data['tags'] = mci_issue_get_tags_for_bug_id( $p_issue_id , $t_user_id );

return $t_issue_data;
}
Expand Down Expand Up @@ -669,6 +670,10 @@ function mc_issue_add( $p_username, $p_password, $p_issue ) {
bugnote_add( $t_issue_id, $t_note['text'], mci_get_time_tracking_from_note( $t_issue_id, $t_note ), $t_view_state_id == VS_PRIVATE, $note_type, $note_attr, $t_user_id, FALSE );
}
}

if ( isset ( $p_issue['tags']) && is_array ( $p_issue['tags']) ) {
mci_tag_set_for_issue( $t_issue_id, $p_issue['tags'] );
}

email_new_bug( $t_issue_id );

Expand Down Expand Up @@ -872,7 +877,11 @@ function mc_issue_update( $p_username, $p_password, $p_issue_id, $p_issue ) {
}
}
}


if ( isset ( $p_issue['tags']) && is_array ( $p_issue['tags']) ) {
mci_tag_set_for_issue( $p_issue_id, $p_issue['tags'] );
}

# submit the issue
return $t_bug_data->update( /* update_extended */ true, /* bypass_email */ true );

Expand Down Expand Up @@ -1272,6 +1281,25 @@ function mci_issue_data_as_array( $p_issue_data, $p_user_id, $p_lang ) {
$t_issue['relationships'] = mci_issue_get_relationships( $p_issue_data->id, $p_user_id );
$t_issue['notes'] = mci_issue_get_notes( $p_issue_data->id );
$t_issue['custom_fields'] = mci_issue_get_custom_fields( $p_issue_data->id );
$t_issue['tags'] = mci_issue_get_tags_for_bug_id( $p_issue_data->id, $p_user_id );

return $t_issue;
}

function mci_issue_get_tags_for_bug_id( $p_bug_id, $p_user_id ) {

if ( !access_has_global_level( config_get( 'tag_view_threshold' ), $p_user_id ) )
return array();

$t_tag_rows = tag_bug_get_attached( $p_bug_id );
$t_result = array();

foreach ( $t_tag_rows as $t_tag_row ) {
$t_result[] = array (
'id' => $t_tag_row['id'],
'name' => $t_tag_row['name']
);
}

return $t_result;
}
38 changes: 38 additions & 0 deletions api/soap/mc_tag_api.php
Expand Up @@ -115,3 +115,41 @@ function mc_tag_delete( $p_username, $p_password, $p_tag_id ) {

return tag_delete( $p_tag_id );
}

function mci_tag_set_for_issue ( $p_issue_id, $p_tags ) {

$t_tag_ids_to_attach = array();
$t_tag_ids_to_detach = array();

$t_submitted_tag_ids = array();
$t_attached_tags = tag_bug_get_attached( $p_issue_id );
$t_attached_tag_ids = array();
foreach ( $t_attached_tags as $t_attached_tag )
$t_attached_tag_ids[] = $t_attached_tag['id'];

foreach ( $p_tags as $t_tag ) {

$t_submitted_tag_ids[] = $t_tag['id'];

if ( in_array( $t_tag['id'], $t_attached_tag_ids) ) {
continue;
} else {
$t_tag_ids_to_attach[] = $t_tag['id'];
}
}

foreach ( $t_attached_tag_ids as $t_attached_tag_id ) {

if ( in_array ( $t_attached_tag_id, $t_submitted_tag_ids) ) {
continue;
} else {
$t_tag_ids_to_detach[] = $t_attached_tag_id;
}
}

foreach ( $t_tag_ids_to_detach as $t_tag_id )
tag_bug_detach( $t_tag_id, $p_issue_id, true);

foreach ( $t_tag_ids_to_attach as $t_tag_id )
tag_bug_attach( $t_tag_id, $p_issue_id, true);
}
23 changes: 23 additions & 0 deletions tests/soap/IssueAddTest.php
Expand Up @@ -489,4 +489,27 @@ public function testCreateIssueWithMiscNote() {
$this->assertEquals( 2, $note->note_type );
$this->assertEquals( 'attr_value', $note->note_attr );
}

public function testCreateIssueWithTags() {

// initialise tags
$tagId1 = $this->client->mc_tag_add( $this->userName, $this->password, array (
'name' => 'IssueCreateTest.createIssueWithTags'
));
$this->deleteTagAfterRun( $tagId1 );

$tagId2 = $this->client->mc_tag_add( $this->userName, $this->password, array (
'name' => 'IssueCreateTest.createIssueWithTags2'
));
$this->deleteTagAfterRun( $tagId2 );

// create issue
$issueToAdd = $this->getIssueToAdd( 'IssueCreateTest.createIssueWithTags' );
$issueToAdd['tags'] = array ( array( 'id' => $tagId1), array('id' => $tagId2 ) );
$issueId = $this->client->mc_issue_add( $this->userName, $this->password, $issueToAdd);
$this->deleteAfterRun( $issueId );
$issue = $this->client->mc_issue_get( $this->userName, $this->password, $issueId );

self::assertEquals ( 2, count ( $issue->tags ) );
}
}
50 changes: 50 additions & 0 deletions tests/soap/IssueUpdateTest.php
Expand Up @@ -458,4 +458,54 @@ public function testUpdateWithRareFields() {
$this->assertEquals( 'os', $retrievedIssue->os );
$this->assertEquals( 'os_build', $retrievedIssue->os_build);
}

public function testUpdateWithTagOperations() {

// initialise tags
$tagId1 = $this->client->mc_tag_add( $this->userName, $this->password, array (
'name' => 'IssueUpdateTest.testUpdateWithTagAdditions'
));
$this->deleteTagAfterRun( $tagId1 );

$tagId2 = $this->client->mc_tag_add( $this->userName, $this->password, array (
'name' => 'IssueUpdateTest.testUpdateWithTagAdditions2'
));
$this->deleteTagAfterRun( $tagId2 );

$tag1 = new stdClass();
$tag1->id = $tagId1;

$tag2 = new stdClass();
$tag2->id = $tagId2;

// create issue
$issueToAdd = $this->getIssueToAdd( 'IssueUpdateTest.testUpdateWithRareFields' );
$issueId = $this->client->mc_issue_add( $this->userName, $this->password, $issueToAdd);
$this->deleteAfterRun( $issueId );
$issue = $this->client->mc_issue_get( $this->userName, $this->password, $issueId );

// update from 0 to 2 tags -> test attaching tags
$issue->tags = array ( $tag1, $tag2 );
$this->client->mc_issue_update( $this->userName, $this->password, $issueId, $issue);
$issue = $this->client->mc_issue_get( $this->userName, $this->password, $issueId );
self::assertEquals(2, count ( $issue->tags ) );

// update from 2 to 1 tags -> test partially detaching tags
$issue->tags = array ( $tag1 );
$this->client->mc_issue_update( $this->userName, $this->password, $issueId, $issue);
$issue = $this->client->mc_issue_get( $this->userName, $this->password, $issueId );
self::assertEquals(1, count ( $issue->tags ) );

// update from 1 to 2 tags -> test partially attaching tags
$issue->tags = array ( $tag1, $tag2 );
$this->client->mc_issue_update( $this->userName, $this->password, $issueId, $issue);
$issue = $this->client->mc_issue_get( $this->userName, $this->password, $issueId );
self::assertEquals(2, count ( $issue->tags ) );

// update from 2 to 0 tags -> test detaching tags
$issue->tags = array();
$this->client->mc_issue_update( $this->userName, $this->password, $issueId, $issue);
$issue = $this->client->mc_issue_get( $this->userName, $this->password, $issueId );
self::assertEquals(0, count ( $issue->tags ) );
}
}

0 comments on commit 5ccae29

Please sign in to comment.