Gitlab Self-managed Support (#56)

* Update UI to ask for URL for Gitlab as provider.

* Create state for each source control in factory.

* Save url for SourceControl when given.

* Make Gitlab dynamically use correct URL (custom or default)

* Update SourceControlsTest to check for custom url when given.

* Style fixes.

---------

Co-authored-by: Saeed Vaziry <61919774+saeedvaziry@users.noreply.github.com>
This commit is contained in:
Koen Hendriks
2023-09-25 10:11:01 +02:00
committed by GitHub
parent 7d98986f52
commit 1e1204fe40
7 changed files with 176 additions and 14 deletions

View File

@ -10,12 +10,14 @@
class Gitlab extends AbstractSourceControlProvider
{
protected string $apiUrl = 'https://gitlab.com/api/v4';
protected string $defaultApiHost = 'https://gitlab.com/';
protected string $apiVersion = 'api/v4';
public function connect(): bool
{
$res = Http::withToken($this->sourceControl->access_token)
->get($this->apiUrl.'/projects');
->get($this->getApiUrl().'/projects');
return $res->successful();
}
@ -27,7 +29,7 @@ public function getRepo(string $repo = null): mixed
{
$repository = $repo ? urlencode($repo) : null;
$res = Http::withToken($this->sourceControl->access_token)
->get($this->apiUrl.'/projects/'.$repository.'/repository/commits');
->get($this->getApiUrl().'/projects/'.$repository.'/repository/commits');
$this->handleResponseErrors($res, $repo);
@ -36,7 +38,9 @@ public function getRepo(string $repo = null): mixed
public function fullRepoUrl(string $repo, string $key): string
{
return sprintf('git@gitlab.com-%s:%s.git', $key, $repo);
$host = parse_url($this->getApiUrl())['host'];
return sprintf('git@%s-%s:%s.git', $host, $key, $repo);
}
/**
@ -46,7 +50,7 @@ public function deployHook(string $repo, array $events, string $secret): array
{
$repository = urlencode($repo);
$response = Http::withToken($this->sourceControl->access_token)->post(
$this->apiUrl.'/projects/'.$repository.'/hooks',
$this->getApiUrl().'/projects/'.$repository.'/hooks',
[
'description' => 'deploy',
'url' => url('/git-hooks?secret='.$secret),
@ -81,7 +85,7 @@ public function destroyHook(string $repo, string $hookId): void
{
$repository = urlencode($repo);
$response = Http::withToken($this->sourceControl->access_token)->delete(
$this->apiUrl.'/projects/'.$repository.'/hooks/'.$hookId
$this->getApiUrl().'/projects/'.$repository.'/hooks/'.$hookId
);
if ($response->status() != 204) {
@ -96,7 +100,7 @@ public function getLastCommit(string $repo, string $branch): ?array
{
$repository = urlencode($repo);
$res = Http::withToken($this->sourceControl->access_token)
->get($this->apiUrl.'/projects/'.$repository.'/repository/commits?ref_name='.$branch);
->get($this->getApiUrl().'/projects/'.$repository.'/repository/commits?ref_name='.$branch);
$this->handleResponseErrors($res, $repo);
@ -123,7 +127,7 @@ public function deployKey(string $title, string $repo, string $key): void
{
$repository = urlencode($repo);
$response = Http::withToken($this->sourceControl->access_token)->post(
$this->apiUrl.'/projects/'.$repository.'/deploy_keys',
$this->getApiUrl().'/projects/'.$repository.'/deploy_keys',
[
'title' => $title,
'key' => $key,
@ -135,4 +139,13 @@ public function deployKey(string $title, string $repo, string $key): void
throw new FailedToDeployGitKey(json_decode($response->body())->message);
}
}
public function getApiUrl(): string
{
$host = $this->sourceControl->url === null
? $this->defaultApiHost
: $this->sourceControl->url;
return $host.$this->apiVersion;
}
}