Skip to content

Commit

Permalink
Add options to create a deploy-key
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertBoes committed Jul 24, 2024
1 parent a35cd56 commit fb6fc18
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/Services/Forge/ForgeSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ class ForgeSetting
*/
public bool $inertiaSsrEnabled;

/**
* Enable github deploy key creation
*/
public bool $githubCreateDeployKey;

public function __construct()
{
$this->init(config('forge'));
Expand Down
14 changes: 13 additions & 1 deletion app/Services/Forge/Pipeline/OrCreateNewSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
namespace App\Services\Forge\Pipeline;

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

class OrCreateNewSite
{
use Outputifier;

public function __invoke(ForgeService $service, Closure $next)
public function __invoke(ForgeService $service, GithubService $githubService, Closure $next)
{
if (is_null($service->site)) {
$this->information('Creating a new site.');
Expand All @@ -30,6 +31,17 @@ public function __invoke(ForgeService $service, Closure $next)
$service->setting->server,
$this->gatherSiteData($service)
);

if ($service->setting->githubCreateDeployKey) {
$this->information('---> Creating GitHub deploy key.');

$data = $service->site->createDeployKey();

$githubService->createDeployKey(
sprintf('Preview deploy key %s', $service->getFormattedDomainName()),
$data['key']
);
}
}

return $next($service);
Expand Down
22 changes: 22 additions & 0 deletions app/Services/Github/GithubService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,26 @@ public function putCommentOnGithubPullRequest(string $body): array

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

public function createDeployKey(string $title, string $key, bool $readonly = true): 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),
])->post($uri, ['body' => json_encode([
'title' => $title,
'key' => $key,
'readonly' => $readonly,
])]);

throw_if($result->failed(), ValidationException::class, [$result->body()]);

return json_decode($result->body(), true);
}
}
3 changes: 3 additions & 0 deletions config/forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
// Git branch name.
'branch' => env('FORGE_GIT_BRANCH'),

// Creates a deploy key on github
'github_create_deploy_key' => env('FORGE_GITHUB_DEPLOY_KEY', false),

// Pattern for subdomains.
'subdomain_pattern' => env('FORGE_SUBDOMAIN_PATTERN'),

Expand Down

0 comments on commit fb6fc18

Please sign in to comment.