Skip to content

Commit 5b9a93b

Browse files
committed
Migrate to bitbucket api v2
Fixes #321 Signed-off-by: Damien Regad <[email protected]> Commits from original PR[[1]] were squashed. [1]: #321
1 parent bac190c commit 5b9a93b

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

SourceBitBucket/SourceBitBucket.php

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ class SourceBitBucketPlugin extends MantisSourcePlugin {
1515
const FRAMEWORK_VERSION_REQUIRED = '2.0.0';
1616

1717
protected $main_url = "https://bitbucket.org/";
18-
protected $api_url_10 = 'https://bitbucket.org/api/1.0/';
19-
protected $api_url_20 = 'https://bitbucket.org/api/2.0/';
18+
protected $api_url = 'https://bitbucket.org/api/2.0/';
2019

2120
public $linkPullRequest = '/pull-request/%s';
2221

@@ -175,12 +174,8 @@ public function update_repo( $p_repo ) {
175174
return $p_repo;
176175
}
177176

178-
private function api_url10( $p_path ) {
179-
return $this->api_url_10 . $p_path;
180-
}
181-
182-
private function api_url20( $p_path ) {
183-
return $this->api_url_20 . $p_path;
177+
private function api_url( $p_path ) {
178+
return $this->api_url . $p_path;
184179
}
185180

186181
private function api_json_url( $p_repo, $p_url ) {
@@ -189,6 +184,23 @@ private function api_json_url( $p_repo, $p_url ) {
189184
return $t_json;
190185
}
191186

187+
private function api_json_url_values( $p_repo, $p_url ) {
188+
$t_json = $this->api_json_url( $p_repo, $p_url );
189+
$values = [];
190+
191+
if( property_exists( $t_json, 'values' ) ) {
192+
foreach( $t_json->values as $t_item ) {
193+
$values[] = $t_item;
194+
}
195+
}
196+
197+
if( property_exists( $t_json, 'next' ) ) {
198+
$values = array_merge( $values, $this->api_json_url_values( $p_repo, $t_json->next ) );
199+
}
200+
201+
return $values;
202+
}
203+
192204
public function precommit() {
193205
return;
194206
}
@@ -209,28 +221,25 @@ public function commit( $p_repo, $p_data ) {
209221
public function import_full( $p_repo, $p_use_cache = true ) {
210222
echo '<pre>';
211223
$t_branch = $p_repo->info['master_branch'];
224+
212225
if( is_blank( $t_branch ) ) {
213226
$t_branch = 'master';
214227
}
215228

229+
$t_username = $p_repo->info['bit_username'];
230+
$t_reponame = $p_repo->info['bit_reponame'];
231+
216232
if( $t_branch != '*' ) {
217233
$t_branches = array_map( 'trim', explode( ',', $t_branch ) );
218234
} else {
219-
$t_username = $p_repo->info['bit_username'];
220-
$t_reponame = $p_repo->info['bit_reponame'];
221-
$t_uri = $this->api_url10( "repositories/$t_username/$t_reponame/branches" );
222-
$t_json = $this->api_json_url( $p_repo, $t_uri );
235+
$t_uri = $this->api_url( "repositories/$t_username/$t_reponame/refs/branches" );
236+
$t_json = $this->api_json_url_values( $p_repo, $t_uri );
223237
$t_branches = array();
224-
foreach ( $t_json as $t_branchname => $t_branch ) {
225-
if(isset($t_branchname)) {
226-
if (strpos($t_branchname, '/') !== FALSE) {
227-
$t_branches[] = $t_branch->raw_node;
228-
} else {
229-
$t_branches[] = $t_branchname;
230-
}
238+
foreach( $t_json as $t_branch ) {
239+
if( isset($t_branch->name) ) {
240+
$t_branches[] = $t_branch->name;
231241
}
232242
}
233-
$t_branches = array_unique($t_branches);
234243
}
235244
$t_changesets = array();
236245

@@ -272,7 +281,7 @@ private function load_all_commits( $p_repo, $p_commit_id, $p_next = '' ) {
272281
$t_username = $p_repo->info['bit_username'];
273282
$t_reponame = $p_repo->info['bit_reponame'];
274283

275-
$t_url = empty($p_next) ? $this->api_url20( "repositories/$t_username/$t_reponame/commits/$p_commit_id" ) : $p_next;
284+
$t_url = empty($p_next) ? $this->api_url( "repositories/$t_username/$t_reponame/commits/$p_commit_id" ) : $p_next;
276285
$t_json = $this->api_json_url( $p_repo, $t_url );
277286

278287
if( property_exists( $t_json, 'values' ) ) {
@@ -304,7 +313,7 @@ public function import_commits( $p_repo, $p_commit_ids, $p_branch = '' ) {
304313
echo "Retrieving $t_commit_id ... ";
305314
$t_json = null;
306315
if( empty($this->commits_cache[$t_commit_id]) ) {
307-
$t_url = $this->api_url20( "repositories/$t_username/$t_reponame/commit/$t_commit_id/" );
316+
$t_url = $this->api_url( "repositories/$t_username/$t_reponame/commit/$t_commit_id" );
308317
$t_json = $this->api_json_url( $p_repo, $t_url );
309318
} else {
310319
$t_json = $this->commits_cache[$t_commit_id];
@@ -372,19 +381,19 @@ private function json_commit_changeset( $p_repo, $p_json, $p_branch = '' ) {
372381
$t_username = $p_repo->info['bit_username'];
373382
$t_reponame = $p_repo->info['bit_reponame'];
374383
$t_commit_id = $p_json->hash;
375-
$t_url = $this->api_url10( "repositories/$t_username/$t_reponame/changesets/$t_commit_id/diffstat/" );
376-
$t_files = $this->api_json_url( $p_repo, $t_url );
384+
$t_url = $this->api_url( "repositories/$t_username/$t_reponame/diffstat/$t_commit_id" );
385+
$t_files = $this->api_json_url_values( $p_repo, $t_url );
377386
if( !empty($t_files) ) {
378387
foreach ( $t_files as $t_file ) {
379-
switch( $t_file->type ) {
388+
switch( $t_file->status ) {
380389
case 'added':
381-
$t_changeset->files[] = new SourceFile(0, '', $t_file->file, 'add');
390+
$t_changeset->files[] = new SourceFile(0, '', $t_file->new->path, 'add');
382391
break;
383392
case 'modified':
384-
$t_changeset->files[] = new SourceFile(0, '', $t_file->file, 'mod');
393+
$t_changeset->files[] = new SourceFile(0, '', $t_file->new->path, 'mod');
385394
break;
386395
case 'removed':
387-
$t_changeset->files[] = new SourceFile(0, '', $t_file->file, 'rm');
396+
$t_changeset->files[] = new SourceFile(0, '', $t_file->old->path, 'rm');
388397
break;
389398
}
390399
}

0 commit comments

Comments
 (0)