Skip to content

Commit

Permalink
Handle removing the existing GitHub deploy key
Browse files Browse the repository at this point in the history
  • Loading branch information
mehrancodes committed Oct 27, 2024
1 parent e3de555 commit 1ca041d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/Commands/TearDownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Services\Forge\Pipeline\FindServer;
use App\Services\Forge\Pipeline\FindSiteOrFail;
use App\Services\Forge\Pipeline\RemoveDatabaseUser;
use App\Services\Forge\Pipeline\RemoveExistingDeployKey;
use App\Services\Forge\Pipeline\RemoveInertiaSupport;
use App\Services\Forge\Pipeline\RemoveTaskScheduler;
use App\Services\Forge\Pipeline\RunOptionalCommands;
Expand All @@ -43,6 +44,7 @@ public function handle(ForgeService $service): void
RunOptionalCommands::class,
RemoveTaskScheduler::class,
RemoveDatabaseUser::class,
RemoveExistingDeployKey::class,
DestroySite::class,
])
->then(fn () => $this->success('Environment teardown successful! All provisioned resources have been removed.'));
Expand Down
51 changes: 51 additions & 0 deletions app/Services/Forge/Pipeline/RemoveExistingDeployKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* This file is part of Laravel Harbor.
*
* (c) Mehran Rasulian <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Services\Forge\Pipeline;

use App\Services\Forge\ForgeService;
use App\Services\Github\GithubService;
use App\Traits\Outputifier;
use Closure;
use Illuminate\Support\Arr;

class RemoveExistingDeployKey
{
use Outputifier;

public function __construct(public GithubService $githubService)
{
//
}

public function __invoke(ForgeService $service, Closure $next)
{
if ($service->setting->githubCreateDeployKey) {
$this->information('---> Removing deploy key on GitHub repository.');

$gitHubDeployKey = $this->githubService->getDeployKey($service->getDeployKeyTitle());

if (! Arr::get($gitHubDeployKey, 'id')) {
$this->information('---> No deploy key found.');

return $next($service);
}

$this->githubService->deleteDeployKey($gitHubDeployKey['id']);

$service->site->destroyDeployKey();
}

return $next($service);
}
}
54 changes: 54 additions & 0 deletions app/Services/Github/GithubService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use App\Services\Forge\ForgeSetting;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;

class GithubService
Expand Down Expand Up @@ -75,6 +76,59 @@ public function createDeployKey(string $title, string $key, bool $readonly = tru
return json_decode($result->body(), true);
}

public function listDeployKeys(): array
{
$uri = sprintf(
self::API_BASE_URL.'/repos/%s/keys',
$this->setting->repository
);

$result = Http::withHeaders([
'accepts' => self::API_ACCEPT,
'X-GitHub-Api-Version' => self::API_VERSION,
'Authorization' => sprintf('Bearer %s', $this->setting->gitToken),
])->get($uri);

return json_decode($result->body(), true);
}

public function getDeployKey(string $title): array
{
$keys = $this->listDeployKeys();
$existingDeployKey = [];

foreach ($keys as $key) {
if (Str::contains($title, $key['title'])) {
$existingDeployKey = $key;
}
}

return $existingDeployKey;
}


public function deleteDeployKey(int $keyId): bool
{
$uri = sprintf(
self::API_BASE_URL.'/repos/%s/keys/%s',
$this->setting->repository,
$keyId
);

$result = Http::withHeaders([
'accepts' => self::API_ACCEPT,
'X-GitHub-Api-Version' => self::API_VERSION,
'Authorization' => sprintf('Bearer %s', $this->setting->gitToken),
])->delete($uri);


if ($result->failed()) {
$this->handleApiErrors($result, 'Deploy key');
}

return true;
}

protected function handleApiErrors(Response $response, $apiName): void
{
// Extract all error messages from the response
Expand Down

0 comments on commit 1ca041d

Please sign in to comment.