Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion packages/vcs/src/VCS/Adapter/Git/Bitbucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class Bitbucket extends Git

protected string $accessToken;

/** @var array<string, mixed>|null */
private ?array $authenticatedUser = null;

/**
* Global Headers
*
Expand Down Expand Up @@ -376,6 +379,68 @@ public function searchRepositories(string $owner, int $page, int $per_page, stri
];
}

/**
* A workspace carries no personal-vs-team flag, so the account's own uuid
* identifies the personal one.
*
* @return array{items: array<array<string, mixed>>, total: int}
*/
public function listNamespaces(int $page, int $per_page, string $search = ''): array
{
$url = "/user/workspaces?page={$page}&pagelen={$per_page}";

if ($search !== '' && $search !== '0') {
$escaped = str_replace(['\\', '"'], ['\\\\', '\\"'], $search);
$url .= '&q=' . urlencode("slug~\"{$escaped}\"");
}

$response = $this->call(self::METHOD_GET, $url, ['Authorization' => $this->authorizationHeader()]);

$responseHeaders = $response['headers'] ?? [];
$statusCode = $responseHeaders['status-code'] ?? 0;
if ($statusCode >= 400) {
throw new Exception("Failed to list namespaces: HTTP {$statusCode}", $statusCode);
}

$responseBody = $response['body'] ?? [];
if (!\is_array($responseBody)) {
return ['items' => [], 'total' => 0];
}

$user = $this->getAuthenticatedUser();
$accountUuid = (string) ($user['uuid'] ?? '');
$accountSlug = (string) ($user['username'] ?? ($user['nickname'] ?? ''));

$namespaces = [];
foreach (($responseBody['values'] ?? []) as $entry) {
$workspace = \is_array($entry) && \is_array($entry['workspace'] ?? null)
? $entry['workspace']
: [];

$slug = (string) ($workspace['slug'] ?? '');
if ($slug === '') {
continue;
}

$uuid = (string) ($workspace['uuid'] ?? '');
$personal = ($accountUuid !== '' && $uuid === $accountUuid)
|| ($accountSlug !== '' && $slug === $accountSlug);

$namespaces[] = [
'id' => $uuid !== '' ? $uuid : $slug,
'name' => (string) ($workspace['name'] ?? $slug),
'path' => $slug,
'kind' => $personal ? 'user' : 'group',
'avatarUrl' => (string) ($workspace['links']['avatar']['href'] ?? ''),
];
}

return [
'items' => $namespaces,
'total' => (int) ($responseBody['size'] ?? \count($namespaces)),
];
}

public function getRepositoryTree(string $owner, string $repositoryName, string $branch, bool $recursive = false): array
{
$suffix = $recursive ? '&max_depth=' . self::MAX_TREE_DEPTH : '';
Expand Down Expand Up @@ -1328,6 +1393,10 @@ public function getUser(string $username): array
*/
protected function getAuthenticatedUser(): array
{
if ($this->authenticatedUser !== null) {
return $this->authenticatedUser;
}

$response = $this->call(self::METHOD_GET, '/user', ['Authorization' => $this->authorizationHeader()]);

$responseHeaders = $response['headers'] ?? [];
Expand All @@ -1338,7 +1407,7 @@ protected function getAuthenticatedUser(): array

$responseBody = $response['body'] ?? [];

return \is_array($responseBody) ? $responseBody : [];
return $this->authenticatedUser = \is_array($responseBody) ? $responseBody : [];
}

/**
Expand Down
3 changes: 0 additions & 3 deletions packages/vcs/tests/E2E/BitbucketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ final class BitbucketTest extends Base
// reports the account the token belongs to
protected static bool $resolvesOwnerFromRepositoryId = false;

// Workspaces have no personal-vs-team distinction to report as 'kind'
protected static bool $supportsNamespaceListing = false;

// Accounts are looked up by uuid, not by handle
protected static bool $supportsUserLookup = false;

Expand Down
Loading