Skip to content
Draft
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
152 changes: 146 additions & 6 deletions src/VCS/Adapter/Git/GitLab.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,53 @@ public function getInstallationRepository(string $repositoryName): array

public function searchRepositories(string $owner, int $page, int $per_page, string $search = ''): array
{
throw new Exception("Not implemented");
$ownerPath = $this->getOwnerPath($owner);
$url = "/groups/{$ownerPath}/projects?page={$page}&per_page={$per_page}";

if (!empty($search)) {
$url .= "&search=" . urlencode($search);
}

$response = $this->call(self::METHOD_GET, $url, ['PRIVATE-TOKEN' => $this->accessToken]);

$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
return [];
}

$responseBody = $response['body'] ?? [];
if (!is_array($responseBody)) {
return [];
}

$repositories = [];
foreach ($responseBody as $repo) {
$repositories[] = [
'id' => $repo['id'] ?? 0,
'name' => $repo['name'] ?? '',
'description' => $repo['description'] ?? '',
'private' => ($repo['visibility'] ?? '') === 'private',
];
}

return $repositories;
}

public function getRepositoryName(string $repositoryId): string
{
throw new Exception("Not implemented");
$url = "/projects/{$repositoryId}";

$response = $this->call(self::METHOD_GET, $url, ['PRIVATE-TOKEN' => $this->accessToken]);

$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
throw new Exception("Repository {$repositoryId} not found");
}

$responseBody = $response['body'] ?? [];
return $responseBody['path'] ?? '';
}

public function getRepositoryTree(string $owner, string $repositoryName, string $branch, bool $recursive = false): array
Expand Down Expand Up @@ -260,7 +301,18 @@ public function getUser(string $username): array

public function getOwnerName(string $installationId, ?int $repositoryId = null): string
{
throw new Exception("Not implemented");
if ($repositoryId !== null) {
$url = "/projects/{$repositoryId}";
$response = $this->call(self::METHOD_GET, $url, ['PRIVATE-TOKEN' => $this->accessToken]);
$responseBody = $response['body'] ?? [];
$namespace = $responseBody['namespace'] ?? [];
return $namespace['path'] ?? '';
}

$url = "/user";
$response = $this->call(self::METHOD_GET, $url, ['PRIVATE-TOKEN' => $this->accessToken]);
$responseBody = $response['body'] ?? [];
return $responseBody['username'] ?? '';
}

public function getPullRequest(string $owner, string $repositoryName, int $pullRequestNumber): array
Expand All @@ -280,7 +332,31 @@ public function getPullRequestFromBranch(string $owner, string $repositoryName,

public function listBranches(string $owner, string $repositoryName): array
{
throw new Exception("Not implemented");
$ownerPath = $this->getOwnerPath($owner);
$projectPath = urlencode("{$ownerPath}/{$repositoryName}");
$url = "/projects/{$projectPath}/repository/branches";

$response = $this->call(self::METHOD_GET, $url, ['PRIVATE-TOKEN' => $this->accessToken]);

$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
return [];
}

$responseBody = $response['body'] ?? [];
if (!is_array($responseBody)) {
return [];
}

$branches = [];
foreach ($responseBody as $branch) {
$branches[] = [
'name' => $branch['name'] ?? '',
];
}

return $branches;
}

public function getCommit(string $owner, string $repositoryName, string $commitHash): array
Expand Down Expand Up @@ -342,7 +418,44 @@ public function getLatestCommit(string $owner, string $repositoryName, string $b

public function updateCommitStatus(string $repositoryName, string $commitHash, string $owner, string $state, string $description = '', string $target_url = '', string $context = ''): void
{
throw new Exception("Not implemented");
$ownerPath = $this->getOwnerPath($owner);
$projectPath = urlencode("{$ownerPath}/{$repositoryName}");
$url = "/projects/{$projectPath}/statuses/{$commitHash}";

// GitLab states: pending, running, success, failed, canceled
$stateMap = [
'pending' => 'pending',
'success' => 'success',
'failure' => 'failed',
'error' => 'failed',
'cancelled' => 'canceled',
];

$gitlabState = $stateMap[$state] ?? $state;

$payload = [
'state' => $gitlabState,
];

if (!empty($description)) {
$payload['description'] = $description;
}

if (!empty($target_url)) {
$payload['target_url'] = $target_url;
}

if (!empty($context)) {
$payload['name'] = $context;
}

$response = $this->call(self::METHOD_POST, $url, ['PRIVATE-TOKEN' => $this->accessToken], $payload);

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

public function generateCloneCommand(string $owner, string $repositoryName, string $version, string $versionType, string $directory, string $rootDirectory): string
Expand Down Expand Up @@ -433,6 +546,33 @@ public function createTag(string $owner, string $repositoryName, string $tagName

public function getCommitStatuses(string $owner, string $repositoryName, string $commitHash): array
{
throw new Exception("Not implemented");
$ownerPath = $this->getOwnerPath($owner);
$projectPath = urlencode("{$ownerPath}/{$repositoryName}");
$url = "/projects/{$projectPath}/repository/commits/{$commitHash}/statuses";

$response = $this->call(self::METHOD_GET, $url, ['PRIVATE-TOKEN' => $this->accessToken]);

$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
return [];
}

$responseBody = $response['body'] ?? [];
if (!is_array($responseBody)) {
return [];
}

$statuses = [];
foreach ($responseBody as $status) {
$statuses[] = [
'state' => $status['status'] ?? '',
'description' => $status['description'] ?? '',
'target_url' => $status['target_url'] ?? '',
'context' => $status['name'] ?? '',
];
}

return $statuses;
}
}
Loading