Skip to content

Commit ee19d54

Browse files
committed
fix: list Bitbucket repositories per workspace after CHANGE-2770
1 parent bc64598 commit ee19d54

2 files changed

Lines changed: 95 additions & 23 deletions

File tree

‎src/Service/Integration/BitbucketApi/RestBitbucketApi.php‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,46 @@ public function primaryEmail(string $accessToken): string
3131
throw new \RuntimeException('Primary e-mail not found.');
3232
}
3333

34+
/**
35+
* Assembled one workspace at a time because Bitbucket removed the endpoint that
36+
* listed every repository a user can reach in a single call - GET /2.0/repositories,
37+
* sunset on 2026-04-14 as part of CHANGE-2770 - and shipped no replacement for it.
38+
* Until then this was one request; it is now one per workspace the user belongs to.
39+
*/
3440
public function repositories(string $accessToken): Repositories
3541
{
3642
$this->client->authenticate(Client::AUTH_OAUTH_TOKEN, $accessToken);
3743

38-
return new Repositories(array_map(function (array $repo): Repository {
39-
return new Repository(
40-
$repo['uuid'],
41-
$repo['full_name'],
42-
$repo['links']['html']['href'].'.git'
43-
);
44-
}, $this->pager->fetchAll($this->client->repositories(), 'list', [['role' => 'member']])));
44+
$repositories = [];
45+
foreach ($this->workspaceSlugs() as $slug) {
46+
$repositories = array_merge($repositories, array_map(function (array $repo): Repository {
47+
return new Repository(
48+
$repo['uuid'],
49+
$repo['full_name'],
50+
$repo['links']['html']['href'].'.git'
51+
);
52+
}, $this->pager->fetchAll($this->client->repositories()->workspaces($slug), 'list', [['role' => 'member']])));
53+
}
54+
55+
return new Repositories($repositories);
56+
}
57+
58+
/**
59+
* GET /2.0/workspaces, which survived the change above - it is a different endpoint
60+
* from the GET /2.0/user/permissions/workspaces that was withdrawn alongside it.
61+
* Deliberately unfiltered: its default is every workspace the user belongs to, and
62+
* narrowing by role here would drop repositories that used to be listed.
63+
*
64+
* @return string[]
65+
*/
66+
private function workspaceSlugs(): array
67+
{
68+
return array_map(
69+
function (array $workspace): string {
70+
return $workspace['slug'];
71+
},
72+
$this->pager->fetchAll($this->client->currentUser(), 'listWorkspaces')
73+
);
4574
}
4675

4776
public function addHook(string $accessToken, string $fullName, string $hookUrl): void

‎tests/Unit/Service/Integration/BitbucketApi/RestBitbucketApiTest.php‎

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Buddy\Repman\Tests\Unit\Service\Integration\BitbucketApi;
66

7+
use Bitbucket\Api\AbstractApi;
78
use Bitbucket\Api\CurrentUser;
89
use Bitbucket\Api\Repositories as RepositoriesApi;
910
use Bitbucket\Client;
@@ -76,28 +77,70 @@ public function testThrowExceptionWhenPrimaryEmailNotFound(): void
7677
$this->api->primaryEmail('token');
7778
}
7879

79-
public function testFetchRepositories(): void
80+
/**
81+
* Every workspace has to be asked separately: the endpoint that answered for all of
82+
* them at once was withdrawn by Bitbucket (CHANGE-2770), so a user whose
83+
* repositories live in more than one workspace is the case that matters here.
84+
*/
85+
public function testFetchRepositoriesFromEveryWorkspace(): void
8086
{
81-
$this->pagerMock->method('fetchAll')->willReturn([
82-
[
83-
'uuid' => '099acebd-5158-459e-b05c-30e51b49a1a8',
84-
'full_name' => 'repman/left-pad',
85-
'links' => ['html' => ['href' => 'https://gitlab.com/repman/left-pad']],
86-
],
87-
[
88-
'uuid' => '74fb57b9-0820-4165-bba0-892eef8f69b8',
89-
'full_name' => 'repman/right-pad',
90-
'links' => ['html' => ['href' => 'https://gitlab.com/repman/right-pad']],
91-
],
92-
]);
93-
$this->clientMock->method('repositories')->willReturn($this->createMock(RepositoriesApi::class));
87+
$currentUser = $this->createMock(CurrentUser::class);
88+
$workspaceApis = [
89+
'repman' => $this->createMock(RepositoriesApi\Workspaces::class),
90+
'other-team' => $this->createMock(RepositoriesApi\Workspaces::class),
91+
];
92+
93+
$repos = $this->createMock(RepositoriesApi::class);
94+
$repos->method('workspaces')->willReturnCallback(
95+
fn (string $slug): RepositoriesApi\Workspaces => $workspaceApis[$slug]
96+
);
97+
$this->clientMock->method('currentUser')->willReturn($currentUser);
98+
$this->clientMock->method('repositories')->willReturn($repos);
99+
100+
$this->pagerMock->method('fetchAll')->willReturnCallback(
101+
function (AbstractApi $api, string $method, array $parameters = []) use ($currentUser, $workspaceApis): array {
102+
if ($api === $currentUser) {
103+
// unfiltered, or workspaces the user is only a member of drop out
104+
self::assertSame([], $parameters);
105+
106+
return [['slug' => 'repman'], ['slug' => 'other-team']];
107+
}
108+
109+
self::assertSame([['role' => 'member']], $parameters);
110+
111+
if ($api === $workspaceApis['repman']) {
112+
return [[
113+
'uuid' => '099acebd-5158-459e-b05c-30e51b49a1a8',
114+
'full_name' => 'repman/left-pad',
115+
'links' => ['html' => ['href' => 'https://bitbucket.org/repman/left-pad']],
116+
]];
117+
}
118+
119+
return [[
120+
'uuid' => '74fb57b9-0820-4165-bba0-892eef8f69b8',
121+
'full_name' => 'other-team/right-pad',
122+
'links' => ['html' => ['href' => 'https://bitbucket.org/other-team/right-pad']],
123+
]];
124+
}
125+
);
94126

95127
self::assertEquals(new Repositories([
96-
new Repository('099acebd-5158-459e-b05c-30e51b49a1a8', 'repman/left-pad', 'https://gitlab.com/repman/left-pad.git'),
97-
new Repository('74fb57b9-0820-4165-bba0-892eef8f69b8', 'repman/right-pad', 'https://gitlab.com/repman/right-pad.git'),
128+
new Repository('099acebd-5158-459e-b05c-30e51b49a1a8', 'repman/left-pad', 'https://bitbucket.org/repman/left-pad.git'),
129+
new Repository('74fb57b9-0820-4165-bba0-892eef8f69b8', 'other-team/right-pad', 'https://bitbucket.org/other-team/right-pad.git'),
98130
]), $this->api->repositories('token'));
99131
}
100132

133+
public function testFetchRepositoriesWhenUserBelongsToNoWorkspace(): void
134+
{
135+
$currentUser = $this->createMock(CurrentUser::class);
136+
$this->clientMock->method('currentUser')->willReturn($currentUser);
137+
// nothing to ask about, so the repositories endpoint is never reached
138+
$this->clientMock->expects(self::never())->method('repositories');
139+
$this->pagerMock->method('fetchAll')->willReturn([]);
140+
141+
self::assertEquals(new Repositories([]), $this->api->repositories('token'));
142+
}
143+
101144
public function testAddHookWhenNotExist(): void
102145
{
103146
$repos = $this->createMock(RepositoriesApi::class);

0 commit comments

Comments
 (0)