Skip to content

Commit b666c7d

Browse files
committed
Catch BitBucket API error
If the JSON returned by the API call can't be decoded, we now trigger an error. This properly catches the case of the user/password authentication which was removed on 2022-03-01. Fixes #399
1 parent e374fec commit b666c7d

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

SourceBitBucket/SourceBitBucket.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class SourceBitBucketPlugin extends MantisSourceGitBasePlugin {
1212
const PLUGIN_VERSION = '2.2.0';
1313
const FRAMEWORK_VERSION_REQUIRED = '2.5.0';
1414

15+
/**
16+
* Error constants
17+
*/
18+
const ERROR_BITBUCKET_API = 'bitbucket_api';
19+
1520
protected $main_url = "https://bitbucket.org/";
1621
protected $api_url = 'https://bitbucket.org/api/2.0/';
1722

@@ -26,6 +31,18 @@ public function register() {
2631
$this->contact = 'sergey@mzsl.ru';
2732
}
2833

34+
function errors() {
35+
$t_errors_list = array(
36+
self::ERROR_BITBUCKET_API,
37+
);
38+
39+
foreach( $t_errors_list as $t_error ) {
40+
$t_errors[$t_error] = plugin_lang_get( 'error_' . $t_error );
41+
}
42+
43+
return array_merge( parent::errors(), $t_errors );
44+
}
45+
2946
public function show_type() {
3047
return plugin_lang_get( 'bitbucket' );
3148
}
@@ -201,7 +218,12 @@ private function api_url( $p_path ) {
201218

202219
private function api_json_url( $p_repo, $p_url ) {
203220
$t_data = $this->url_get( $p_repo, $p_url );
204-
return json_decode( utf8_encode( $t_data ) );
221+
$t_json = json_decode( utf8_encode( $t_data ) );
222+
if( json_last_error() != JSON_ERROR_NONE ) {
223+
error_parameters( $t_data );
224+
plugin_error( self::ERROR_BITBUCKET_API );
225+
}
226+
return $t_json;
205227
}
206228

207229
private function api_json_url_values( $p_repo, $p_url ) {
@@ -342,7 +364,7 @@ public function import_commits( $p_repo, $p_commit_ids, $p_branch = '' ) {
342364
echo "Looking up target commit for $t_branch ... ";
343365
$t_url = $this->api_url( "repositories/$t_username/$t_reponame/refs/branches/$t_branch" );
344366
$t_json = $this->api_json_url( $p_repo, $t_url );
345-
if( false !== $t_json ) {
367+
if( null !== $t_json ) {
346368
$t_branch = $t_json->target->hash;
347369
}
348370
}

SourceBitBucket/lang/strings_english.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ $s_plugin_SourceBitBucket_repo_authorization_failed = 'Sorry, MantisBT could not
1616

1717
$s_plugin_SourceBitBucket_oauth_authorization = 'BitBucket OAuth Authorization';
1818
$s_plugin_SourceBitBucket_back_repo = 'Back to Repository';
19+
20+
$s_plugin_SourceBitBucket_error_bitbucket_api = 'BitBucket API call failed with the following error message:<br><em>%1$s</em>';

0 commit comments

Comments
 (0)