feat: add private gh repos
This commit is contained in:
parent
f421bcb2c9
commit
a37f748639
@ -23,7 +23,7 @@ class Deploy extends Component
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->parameters = Route::current()->parameters();
|
||||
$this->parameters = saveParameters();
|
||||
$this->application = Application::where('id', $this->applicationId)->first();
|
||||
$this->destination = $this->application->destination->getMorphClass()::where('id', $this->application->destination->id)->first();
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ class Add extends Component
|
||||
];
|
||||
public function mount()
|
||||
{
|
||||
$this->parameters = Route::current()->parameters();
|
||||
$this->parameters = saveParameters();
|
||||
}
|
||||
public function submit()
|
||||
{
|
||||
|
@ -20,7 +20,7 @@ class Show extends Component
|
||||
];
|
||||
public function mount()
|
||||
{
|
||||
$this->parameters = Route::current()->parameters();
|
||||
$this->parameters = saveParameters();
|
||||
}
|
||||
public function submit()
|
||||
{
|
||||
|
@ -23,7 +23,7 @@ class Add extends Component
|
||||
];
|
||||
public function mount()
|
||||
{
|
||||
$this->parameters = Route::current()->parameters();
|
||||
$this->parameters = saveParameters();
|
||||
}
|
||||
public function submit()
|
||||
{
|
||||
|
142
app/Http/Livewire/Project/New/GithubPrivateRepository.php
Normal file
142
app/Http/Livewire/Project/New/GithubPrivateRepository.php
Normal file
@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Project\New;
|
||||
|
||||
use App\Models\Application;
|
||||
use App\Models\GithubApp;
|
||||
use App\Models\Project;
|
||||
use App\Models\Server;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Livewire\Component;
|
||||
|
||||
class GithubPrivateRepository extends Component
|
||||
{
|
||||
public $github_apps;
|
||||
public GithubApp $github_app;
|
||||
public $parameters;
|
||||
|
||||
public int $selected_repository_id;
|
||||
public string $selected_repository_owner;
|
||||
public string $selected_repository_repo;
|
||||
|
||||
public string $selected_branch_name = 'main';
|
||||
|
||||
public int $selected_server_id;
|
||||
public int $selected_destination_id;
|
||||
public string $selected_destination_class;
|
||||
|
||||
public string $token;
|
||||
|
||||
protected int $page = 1;
|
||||
|
||||
public $servers;
|
||||
public $destinations;
|
||||
public $repositories;
|
||||
public int $total_repositories_count = 0;
|
||||
|
||||
public $branches;
|
||||
public int $total_branches_count = 0;
|
||||
|
||||
protected function loadRepositoryByPage()
|
||||
{
|
||||
Log::info('Loading page ' . $this->page);
|
||||
$response = Http::withToken($this->token)->get("{$this->github_app->api_url}/installation/repositories?per_page=100&page={$this->page}");
|
||||
$json = $response->json();
|
||||
if ($response->status() !== 200) {
|
||||
return $this->emit('error', $json['message']);
|
||||
}
|
||||
|
||||
if ($json['total_count'] === 0) {
|
||||
return;
|
||||
}
|
||||
$this->total_repositories_count = $json['total_count'];
|
||||
$this->repositories = $this->repositories->concat(collect($json['repositories']));
|
||||
}
|
||||
protected function loadBranchByPage()
|
||||
{
|
||||
Log::info('Loading page ' . $this->page);
|
||||
$response = Http::withToken($this->token)->get("{$this->github_app->api_url}/repos/{$this->selected_repository_owner}/{$this->selected_repository_repo}/branches?per_page=100&page={$this->page}");
|
||||
$json = $response->json();
|
||||
if ($response->status() !== 200) {
|
||||
return $this->emit('error', $json['message']);
|
||||
}
|
||||
|
||||
$this->total_branches_count = count($json);
|
||||
$this->branches = $this->branches->concat(collect($json));
|
||||
}
|
||||
public function loadRepositories($github_app_id)
|
||||
{
|
||||
$this->repositories = collect();
|
||||
$this->page = 1;
|
||||
$this->github_app = GithubApp::where('id', $github_app_id)->first();
|
||||
$this->token = generate_github_token($this->github_app);
|
||||
$this->loadRepositoryByPage();
|
||||
if ($this->repositories->count() < $this->total_repositories_count) {
|
||||
while ($this->repositories->count() < $this->total_repositories_count) {
|
||||
$this->page++;
|
||||
$this->loadRepositoryByPage();
|
||||
}
|
||||
}
|
||||
$this->selected_repository_id = $this->repositories[0]['id'];
|
||||
}
|
||||
public function loadBranches()
|
||||
{
|
||||
$this->selected_repository_owner = $this->repositories->where('id', $this->selected_repository_id)->first()['owner']['login'];
|
||||
$this->selected_repository_repo = $this->repositories->where('id', $this->selected_repository_id)->first()['name'];
|
||||
$this->branches = collect();
|
||||
$this->page = 1;
|
||||
$this->loadBranchByPage();
|
||||
if ($this->total_branches_count === 100) {
|
||||
while ($this->total_branches_count === 100) {
|
||||
$this->page++;
|
||||
$this->loadBranchByPage();
|
||||
}
|
||||
}
|
||||
}
|
||||
public function loadServers()
|
||||
{
|
||||
$this->servers = Server::validated();
|
||||
$this->selected_server_id = $this->servers[0]['id'];
|
||||
}
|
||||
public function loadDestinations()
|
||||
{
|
||||
$server = $this->servers->where('id', $this->selected_server_id)->first();
|
||||
$this->destinations = $server->standaloneDockers->merge($server->swarmDockers);
|
||||
$this->selected_destination_id = $this->destinations[0]['id'];
|
||||
$this->selected_destination_class = $this->destinations[0]->getMorphClass();
|
||||
}
|
||||
public function submit()
|
||||
{
|
||||
try {
|
||||
$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
|
||||
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
|
||||
$application = Application::create([
|
||||
'name' => "{$this->selected_repository_owner}/{$this->selected_repository_repo}:{$this->selected_branch_name}",
|
||||
'git_repository' => "{$this->selected_repository_owner}/{$this->selected_repository_repo}",
|
||||
'git_branch' => $this->selected_branch_name,
|
||||
'build_pack' => 'nixpacks',
|
||||
'ports_exposes' => '3000',
|
||||
'environment_id' => $environment->id,
|
||||
'destination_id' => $this->selected_destination_id,
|
||||
'destination_type' => $this->selected_destination_class,
|
||||
'source_id' => $this->github_app->id,
|
||||
'source_type' => GithubApp::class,
|
||||
]);
|
||||
redirect()->route('project.application.configuration', [
|
||||
'application_uuid' => $application->uuid,
|
||||
'project_uuid' => $project->uuid,
|
||||
'environment_name' => $environment->name
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->emit('error', $e->getMessage());
|
||||
}
|
||||
}
|
||||
public function mount()
|
||||
{
|
||||
$this->parameters = saveParameters();
|
||||
$this->repositories = $this->branches = $this->servers = $this->destinations = collect();
|
||||
$this->github_apps = GithubApp::private();
|
||||
}
|
||||
}
|
@ -42,7 +42,7 @@ public function mount()
|
||||
$this->public_repository_url = 'https://github.com/coollabsio/coolify-examples/tree/nodejs-fastify';
|
||||
$this->port = 3000;
|
||||
}
|
||||
$this->parameters = Route::current()->parameters();
|
||||
$this->parameters = saveParameters();
|
||||
$this->servers = session('currentTeam')->load(['servers'])->servers;
|
||||
}
|
||||
public function chooseServer($server)
|
||||
|
@ -20,7 +20,7 @@ public function setPrivateKey($private_key_id)
|
||||
}
|
||||
public function mount()
|
||||
{
|
||||
$this->parameters = Route::current()->parameters();
|
||||
$this->parameters = saveParameters();
|
||||
$this->private_keys = ModelsPrivateKey::where('team_id', session('currentTeam')->id)->get();
|
||||
}
|
||||
}
|
||||
|
@ -372,29 +372,6 @@ private function generate_healthcheck_commands()
|
||||
return implode(' ', $generated_healthchecks_commands);
|
||||
}
|
||||
|
||||
private function generate_jwt_token_for_github()
|
||||
{
|
||||
$signingKey = InMemory::plainText($this->source->privateKey->private_key);
|
||||
$algorithm = new Sha256();
|
||||
$tokenBuilder = (new Builder(new JoseEncoder(), ChainedFormatter::default()));
|
||||
$now = new DateTimeImmutable();
|
||||
$now = $now->setTime($now->format('H'), $now->format('i'));
|
||||
$issuedToken = $tokenBuilder
|
||||
->issuedBy($this->source->app_id)
|
||||
->issuedAt($now)
|
||||
->expiresAt($now->modify('+10 minutes'))
|
||||
->getToken($algorithm, $signingKey)
|
||||
->toString();
|
||||
$token = Http::withHeaders([
|
||||
'Authorization' => "Bearer $issuedToken",
|
||||
'Accept' => 'application/vnd.github.machine-man-preview+json'
|
||||
])->post("{$this->source->api_url}/app/installations/{$this->source->installation_id}/access_tokens");
|
||||
if ($token->failed()) {
|
||||
throw new \Exception("Failed to get access token for $this->application->name from " . $this->source->name . " with error: " . $token->json()['message']);
|
||||
}
|
||||
return $token->json()['token'];
|
||||
}
|
||||
|
||||
private function set_labels_for_applications()
|
||||
{
|
||||
$labels = [];
|
||||
@ -472,8 +449,7 @@ private function gitImport()
|
||||
$source_html_url_scheme = $url['scheme'];
|
||||
|
||||
$git_clone_command = "git clone -q -b {$this->application->git_branch}";
|
||||
|
||||
if ($this->application->source->getMorphClass() == 'App\Models\GithubApp') {
|
||||
if ($this->source->getMorphClass() == 'App\Models\GithubApp') {
|
||||
if ($this->source->is_public) {
|
||||
$git_clone_command = "{$git_clone_command} {$this->source->html_url}/{$this->application->git_repository} {$this->workdir}";
|
||||
$git_clone_command = $this->setGitImportSettings($git_clone_command);
|
||||
@ -481,12 +457,11 @@ private function gitImport()
|
||||
$this->execute_in_builder($git_clone_command)
|
||||
];
|
||||
} else {
|
||||
if (!$this->application->source->app_id) {
|
||||
$private_key = base64_encode($this->application->source->privateKey->private_key);
|
||||
if (!$this->source->app_id) {
|
||||
$private_key = base64_encode($this->source->privateKey->private_key);
|
||||
|
||||
$git_clone_command = "GIT_SSH_COMMAND=\"ssh -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" {$git_clone_command} git@$source_html_url_host:{$this->application->git_repository}.git {$this->workdir}";
|
||||
$git_clone_command = $this->setGitImportSettings($git_clone_command);
|
||||
|
||||
return [
|
||||
$this->execute_in_builder("mkdir -p /root/.ssh"),
|
||||
$this->execute_in_builder("echo '{$private_key}' | base64 -d > /root/.ssh/id_rsa"),
|
||||
@ -494,7 +469,7 @@ private function gitImport()
|
||||
$this->execute_in_builder($git_clone_command)
|
||||
];
|
||||
} else {
|
||||
$github_access_token = $this->generate_jwt_token_for_github();
|
||||
$github_access_token = generate_github_token($this->source);
|
||||
return [
|
||||
$this->execute_in_builder("git clone -q -b {$this->application->git_branch} $source_html_url_scheme://x-access-token:$github_access_token@$source_html_url_host/{$this->application->git_repository}.git {$this->workdir}")
|
||||
];
|
||||
|
@ -15,4 +15,12 @@ public function privateKey()
|
||||
{
|
||||
return $this->belongsTo(PrivateKey::class);
|
||||
}
|
||||
static public function public()
|
||||
{
|
||||
return GithubApp::where('team_id', session('currentTeam')->id)->where('is_public', true)->get();
|
||||
}
|
||||
static public function private()
|
||||
{
|
||||
return GithubApp::where('team_id', session('currentTeam')->id)->where('is_public', false)->get();
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
use App\Actions\CoolifyTask\PrepareCoolifyTask;
|
||||
use App\Data\CoolifyTaskArgs;
|
||||
use App\Models\GithubApp;
|
||||
use App\Models\Server;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\QueryException;
|
||||
@ -9,6 +10,7 @@
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Process;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Spatie\Activitylog\Contracts\Activity;
|
||||
|
||||
@ -174,3 +176,40 @@ function generateRandomName()
|
||||
return $generator->getName();
|
||||
}
|
||||
}
|
||||
|
||||
use Lcobucci\JWT\Encoding\ChainedFormatter;
|
||||
use Lcobucci\JWT\Encoding\JoseEncoder;
|
||||
use Lcobucci\JWT\Signer\Key\InMemory;
|
||||
use Lcobucci\JWT\Signer\Rsa\Sha256;
|
||||
use Lcobucci\JWT\Token\Builder;
|
||||
|
||||
if (!function_exists('generate_github_token')) {
|
||||
function generate_github_token(GithubApp $source)
|
||||
{
|
||||
$signingKey = InMemory::plainText($source->privateKey->private_key);
|
||||
$algorithm = new Sha256();
|
||||
$tokenBuilder = (new Builder(new JoseEncoder(), ChainedFormatter::default()));
|
||||
$now = new DateTimeImmutable();
|
||||
$now = $now->setTime($now->format('H'), $now->format('i'));
|
||||
$issuedToken = $tokenBuilder
|
||||
->issuedBy($source->app_id)
|
||||
->issuedAt($now)
|
||||
->expiresAt($now->modify('+10 minutes'))
|
||||
->getToken($algorithm, $signingKey)
|
||||
->toString();
|
||||
$token = Http::withHeaders([
|
||||
'Authorization' => "Bearer $issuedToken",
|
||||
'Accept' => 'application/vnd.github.machine-man-preview+json'
|
||||
])->post("{$source->api_url}/app/installations/{$source->installation_id}/access_tokens");
|
||||
if ($token->failed()) {
|
||||
throw new \Exception("Failed to get access token for " . $source->name . " with error: " . $token->json()['message']);
|
||||
}
|
||||
return $token->json()['token'];
|
||||
}
|
||||
}
|
||||
if (!function_exists('saveParameters')) {
|
||||
function saveParameters()
|
||||
{
|
||||
return Route::current()->parameters();
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ public function run(): void
|
||||
$github_public_source = GithubApp::where('name', 'Public GitHub')->first();
|
||||
|
||||
Application::create([
|
||||
'name' => 'Public application (from GitHub)',
|
||||
'name' => 'coollabsio/coolify-examples:nodejs-fastify',
|
||||
'git_repository' => 'coollabsio/coolify-examples',
|
||||
'git_branch' => 'nodejs-fastify',
|
||||
'build_pack' => 'nixpacks',
|
||||
|
@ -31,7 +31,7 @@ public function run(): void
|
||||
'html_url' => 'https://github.com',
|
||||
'is_public' => false,
|
||||
'app_id' => 292941,
|
||||
'installation_id' => 34157139,
|
||||
'installation_id' => 37267016,
|
||||
'client_id' => 'Iv1.220e564d2b0abd8c',
|
||||
'client_secret' => '96b1b31f36ce0a34386d11798ff35b9b6d8aba3a',
|
||||
'webhook_secret' => '326a47b49054f03288f800d81247ec9414d0abf3',
|
||||
|
@ -13,11 +13,10 @@ class LocalPersistentVolumeSeeder extends Seeder
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$application = Application::where('name', 'Public application (from GitHub)')->first();
|
||||
LocalPersistentVolume::create([
|
||||
'name' => 'test-pv',
|
||||
'mount_path' => '/data',
|
||||
'resource_id' => $application->id,
|
||||
'resource_id' => 1,
|
||||
'resource_type' => Application::class,
|
||||
]);
|
||||
}
|
||||
|
@ -11,12 +11,13 @@
|
||||
<x-inputs.input id="application.start_command" label="Start Command" />
|
||||
<x-inputs.select id="application.build_pack" label="Build Pack" required>
|
||||
<option value="nixpacks">Nixpacks</option>
|
||||
<option value="docker">Docker</option>
|
||||
<option disabled value="docker">Docker</option>
|
||||
<option disabled value="compose">Compose</option>
|
||||
</x-inputs.select>
|
||||
@if ($application->settings->is_static)
|
||||
<x-inputs.select id="application.static_image" label="Static Image" required>
|
||||
<option value="nginx:alpine">nginx:alpine</option>
|
||||
<option value="apache:alpine">apache:alpine</option>
|
||||
<option disabled value="apache:alpine">apache:alpine</option>
|
||||
</x-inputs.select>
|
||||
@endif
|
||||
</div>
|
||||
@ -42,15 +43,16 @@
|
||||
Submit
|
||||
</x-inputs.button>
|
||||
</form>
|
||||
<div class="flex flex-col pt-4 text-right w-52">
|
||||
<div class="flex flex-col pt-4">
|
||||
<x-inputs.input instantSave type="checkbox" id="is_static" label="Static website?" />
|
||||
<x-inputs.input instantSave type="checkbox" id="is_auto_deploy" label="Auto Deploy?" />
|
||||
<x-inputs.input instantSave type="checkbox" id="is_dual_cert" label="Dual Certs?" />
|
||||
<x-inputs.input instantSave type="checkbox" id="is_previews" label="Previews?" />
|
||||
<x-inputs.input instantSave type="checkbox" id="is_custom_ssl" label="Is Custom SSL?" />
|
||||
<x-inputs.input instantSave type="checkbox" id="is_http2" label="Is Http2?" />
|
||||
<x-inputs.input instantSave type="checkbox" id="is_git_submodules_allowed" label="Git Submodules Allowed?" />
|
||||
<x-inputs.input instantSave type="checkbox" id="is_git_lfs_allowed" label="Git LFS Allowed?" />
|
||||
<x-inputs.input instantSave type="checkbox" id="is_debug" label="Debug" />
|
||||
<x-inputs.input disabled instantSave type="checkbox" id="is_auto_deploy" label="Auto Deploy?" />
|
||||
<x-inputs.input disabled instantSave type="checkbox" id="is_dual_cert" label="Dual Certs?" />
|
||||
<x-inputs.input disabled instantSave type="checkbox" id="is_previews" label="Previews?" />
|
||||
<x-inputs.input disabled instantSave type="checkbox" id="is_custom_ssl" label="Is Custom SSL?" />
|
||||
<x-inputs.input disabled instantSave type="checkbox" id="is_http2" label="Is Http2?" />
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
@ -0,0 +1,79 @@
|
||||
<div>
|
||||
@if ($github_apps->count() > 0)
|
||||
<h1>Choose a GitHub App</h1>
|
||||
@foreach ($github_apps as $ghapp)
|
||||
<x-inputs.button wire:click="loadRepositories({{ $ghapp->id }})">{{ $ghapp->name }}</x-inputs.button>
|
||||
@endforeach
|
||||
<div>
|
||||
@if ($repositories->count() > 0)
|
||||
<h3>Choose a Repository</h3>
|
||||
<select wire:model.defer="selected_repository_id">
|
||||
@foreach ($repositories as $repo)
|
||||
@if ($loop->first)
|
||||
<option selected value="{{ data_get($repo, 'id') }}">{{ data_get($repo, 'name') }}</option>
|
||||
@else
|
||||
<option value="{{ data_get($repo, 'id') }}">{{ data_get($repo, 'name') }}</option>
|
||||
@endif
|
||||
@endforeach
|
||||
</select>
|
||||
<x-inputs.button wire:click="loadBranches">Select Repository</x-inputs.button>
|
||||
@endif
|
||||
</div>
|
||||
<div>
|
||||
@if ($branches->count() > 0)
|
||||
<h3>Choose a Branch</h3>
|
||||
<select wire:model.defer="selected_branch_name">
|
||||
<option disabled>Choose a branch</option>
|
||||
@foreach ($branches as $branch)
|
||||
@if ($loop->first)
|
||||
<option selected value="{{ data_get($branch, 'name') }}">{{ data_get($branch, 'name') }}
|
||||
</option>
|
||||
@else
|
||||
<option value="{{ data_get($branch, 'name') }}">{{ data_get($branch, 'name') }}</option>
|
||||
@endif
|
||||
@endforeach
|
||||
</select>
|
||||
<x-inputs.button wire:click="loadServers">Select Branch</x-inputs.button>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
<div>
|
||||
@if ($servers->count() > 0)
|
||||
<h3>Choose a Server</h3>
|
||||
<select wire:model.defer="selected_server_id">
|
||||
<option disabled>Choose a server</option>
|
||||
@foreach ($servers as $server)
|
||||
@if ($loop->first)
|
||||
<option selected value="{{ data_get($server, 'id') }}">{{ data_get($server, 'name') }}
|
||||
</option>
|
||||
@else
|
||||
<option value="{{ data_get($server, 'id') }}">{{ data_get($server, 'name') }}</option>
|
||||
@endif
|
||||
@endforeach
|
||||
</select>
|
||||
<x-inputs.button wire:click="loadDestinations">Select Server</x-inputs.button>
|
||||
@endif
|
||||
</div>
|
||||
<div>
|
||||
@if ($destinations->count() > 0)
|
||||
<h3>Choose a Destination</h3>
|
||||
<select wire:model.defer="selected_destination_id">
|
||||
<option disabled>Choose a destination</option>
|
||||
@foreach ($destinations as $destination)
|
||||
@if ($loop->first)
|
||||
<option selected value="{{ data_get($destination, 'id') }}">
|
||||
{{ data_get($destination, 'network') }}</option>
|
||||
@else
|
||||
<option value="{{ data_get($destination, 'id') }}">{{ data_get($destination, 'network') }}
|
||||
</option>
|
||||
@endif
|
||||
@endforeach
|
||||
</select>
|
||||
<x-inputs.button wire:click="submit">Select Destination</x-inputs.button>
|
||||
@endif
|
||||
</div>
|
||||
@else
|
||||
Add new github app
|
||||
@endif
|
||||
|
||||
</div>
|
@ -18,7 +18,7 @@
|
||||
<livewire:project.new.public-git-repository :type="$type" />
|
||||
</div>
|
||||
<div x-cloak x-show="activeTab === 'github-private-repo'">
|
||||
github-private-repo
|
||||
<livewire:project.new.github-private-repository />
|
||||
</div>
|
||||
</div>
|
||||
</x-layout>
|
||||
|
Loading…
Reference in New Issue
Block a user