Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue commands #1301

Merged
merged 22 commits into from Mar 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
99 changes: 51 additions & 48 deletions api/rest/restcore/issues_rest.php
Expand Up @@ -142,10 +142,14 @@ function rest_issue_get( \Slim\Http\Request $p_request, \Slim\Http\Response $p_r
function rest_issue_add( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
$t_issue = $p_request->getParsedBody();

$t_result = mc_issue_add( /* username */ '', /* password */ '', $t_issue );
ApiObjectFactory::throwIfFault( $t_result );
if( isset( $t_issue['files'] ) ) {
$t_issue['files'] = files_base64_to_temp( $t_issue['files'] );
}

$t_issue_id = $t_result;
$t_data = array( 'payload' => array( 'issue' => $t_issue ) );
$t_command = new IssueAddCommand( $t_data );
$t_result = $t_command->execute();
$t_issue_id = (int)$t_result['issue_id'];

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

Expand Down Expand Up @@ -177,9 +181,9 @@ function rest_issue_delete( \Slim\Http\Request $p_request, \Slim\Http\Response $
}
}

# Username and password below are ignored, since middleware already done the auth.
$t_result = mc_issue_delete( /* username */ '', /* password */ '', $t_issue_id );
ApiObjectFactory::throwIfFault( $t_result );
$t_data = array( 'query' => array( 'id' => $t_issue_id ) );
$t_command = new IssueDeleteCommand( $t_data );
$t_command->execute();

return $p_response->withStatus( HTTP_STATUS_NO_CONTENT )
->withHeader( HEADER_ETAG, mc_issue_hash( $t_issue_id, null ) );
Expand All @@ -201,27 +205,8 @@ function rest_issue_file_add( \Slim\Http\Request $p_request, \Slim\Http\Response
'payload' => $p_request->getParsedBody(),
);

if( isset( $t_data['payload']['files'] ) && is_array( $t_data['payload']['files'] ) ) {
foreach( $t_data['payload']['files'] as &$t_file ) {
if( !isset( $t_file['content'] ) ) {
throw new ClientException(
'File content not set',
ERROR_INVALID_FIELD_VALUE,
array( 'files' ) );
}

$t_raw_content = base64_decode( $t_file['content'] );

do {
$t_tmp_file = realpath( sys_get_temp_dir() ) . '/' . uniqid( 'mantisbt-file' );
} while( file_exists( $t_tmp_file ) );

file_put_contents( $t_tmp_file, $t_raw_content );
$t_file['tmp_name'] = $t_tmp_file;
$t_file['size'] = filesize( $t_tmp_file );
$t_file['browser_upload'] = false;
unset( $t_file['content'] );
}
if( isset( $t_data['payload']['files'] ) ) {
$t_data['payload']['files'] = files_base64_to_temp( $t_data['payload']['files'] );
}

$t_command = new IssueFileAddCommand( $t_data );
Expand All @@ -246,27 +231,8 @@ function rest_issue_note_add( \Slim\Http\Request $p_request, \Slim\Http\Response
'payload' => $p_request->getParsedBody(),
);

if( isset( $t_data['payload']['files'] ) && is_array( $t_data['payload']['files'] ) ) {
foreach( $t_data['payload']['files'] as &$t_file ) {
if( !isset( $t_file['content'] ) ) {
throw new ClientException(
'File content not set',
ERROR_INVALID_FIELD_VALUE,
array( 'files' ) );
}

$t_raw_content = base64_decode( $t_file['content'] );

do {
$t_tmp_file = realpath( sys_get_temp_dir() ) . '/' . uniqid( 'mantisbt-file' );
} while( file_exists( $t_tmp_file ) );

file_put_contents( $t_tmp_file, $t_raw_content );
$t_file['tmp_name'] = $t_tmp_file;
$t_file['size'] = filesize( $t_tmp_file );
$t_file['browser_upload'] = false;
unset( $t_file['content'] );
}
if( isset( $t_data['payload']['files'] ) ) {
$t_data['payload']['files'] = files_base64_to_temp( $t_data['payload']['files'] );
}

$t_command = new IssueNoteAddCommand( $t_data );
Expand Down Expand Up @@ -531,3 +497,40 @@ function rest_issue_files_get( \Slim\Http\Request $p_request, \Slim\Http\Respons
return $p_response->withStatus( HTTP_STATUS_SUCCESS )->
withJson( array( 'files' => $t_files ) );
}

/**
* Convert REST API base 64 files into expected format for browser file uploads.
*
* @param array $p_files The files in REST API format.
* @return array The files in browser upload format.
*/
function files_base64_to_temp( $p_files ) {
$t_files = array();

if( isset( $p_files ) && is_array( $p_files ) ) {
foreach( $p_files as $t_file ) {
if( !isset( $t_file['content'] ) ) {
throw new ClientException(
'File content not set',
ERROR_INVALID_FIELD_VALUE,
array( 'files' ) );
}

$t_raw_content = base64_decode( $t_file['content'] );

do {
$t_tmp_file = realpath( sys_get_temp_dir() ) . '/' . uniqid( 'mantisbt-file' );
} while( file_exists( $t_tmp_file ) );

file_put_contents( $t_tmp_file, $t_raw_content );
$t_file['tmp_name'] = $t_tmp_file;
$t_file['size'] = filesize( $t_tmp_file );
$t_file['browser_upload'] = false;
unset( $t_file['content'] );

$t_files[] = $t_file;
}
}

return $t_files;
}
31 changes: 23 additions & 8 deletions api/soap/mc_api.php
Expand Up @@ -31,6 +31,7 @@

require_api( 'api_token_api.php' );

use Mantis\Exceptions\ClientException;
use Mantis\Exceptions\LegacyApiFaultException;

/**
Expand Down Expand Up @@ -287,12 +288,17 @@ static function faultFromException( Exception $p_exception ) {
* @param stdClass|array $p_object Object.
* @return array
*/
static function objectToArray($p_object ) {
if( is_object( $p_object ) ) {
return get_object_vars( $p_object );
static function objectToArray( $p_object, $p_recursive = false ) {
$t_object = is_object( $p_object ) ? get_object_vars( $p_object ) : $p_object;
if( $p_recursive && is_array( $t_object ) ) {
foreach( $t_object as $t_key => $t_value ) {
if( is_object( $t_object[$t_key] ) || is_array( $t_object[$t_key] ) ) {
$t_object[$t_key] = ApiObjectFactory::objectToArray( $t_object[$t_key], $p_recursive );
}
}
}

return $p_object;
return $t_object;
}

/**
Expand Down Expand Up @@ -353,7 +359,7 @@ static function isFault( $p_maybe_fault ) {
*/
static function throwIfFault( $p_maybe_fault ) {
if( ApiObjectFactory::isFault( $p_maybe_fault ) ) {
throw new LegacyApiFaultException( $p_maybe_fault->fault_string, $p_maybe_fault->status_code );
throw new LegacyApiFaultException( $p_maybe_fault->getMessage(), $p_maybe_fault->getCode() );
}
}
}
Expand Down Expand Up @@ -876,9 +882,10 @@ function mci_get_version( $p_version, $p_project_id ) {
*
* @param string|object $p_version The version string or object with name or id or both.
* @param int $p_project_id The project id.
* @param string $p_field_name Version field name (e.g. version, target_version, fixed_in_version)
* @return int|RestFault|SoapFault The version id, 0 if not supplied.
*/
function mci_get_version_id( $p_version, $p_project_id ) {
function mci_get_version_id( $p_version, $p_project_id, $p_field_name = 'version' ) {
$t_version_id = 0;
$t_version_for_error = '';

Expand All @@ -903,7 +910,11 @@ function mci_get_version_id( $p_version, $p_project_id ) {
$t_error_when_version_not_found = config_get( 'webservice_error_when_version_not_found' );
if( $t_error_when_version_not_found == ON ) {
$t_project_name = project_get_name( $p_project_id );
return ApiObjectFactory::faultBadRequest( "Version '$t_version_for_error' does not exist in project '$t_project_name'." );
throw new ClientException(
"Version '$t_version_for_error' does not exist in project '$t_project_name'.",
ERROR_INVALID_FIELD_VALUE,
array( 'version' )
);
}

$t_version_when_not_found = config_get( 'webservice_version_when_not_found' );
Expand Down Expand Up @@ -983,7 +994,11 @@ function mci_get_category_id( $p_category, $p_project_id ) {
$t_category_id = $fn_get_category_id_internal( $p_category, $p_project_id );
if( $t_category_id == 0 && !config_get( 'allow_no_category' ) ) {
if( !isset( $p_category ) ) {
return ApiObjectFactory::faultBadRequest( 'Category field must be supplied.' );
throw new ClientException(
'Category field must be supplied.',
ERROR_EMPTY_FIELD,
array( 'category' )
);
}

# category may be a string, array with id, array with name, or array
Expand Down