coolify/app/Http/Livewire/Project/New/GithubPrivateRepository.php

164 lines
6.1 KiB
PHP
Raw Normal View History

2023-05-08 11:51:03 +02:00
<?php
namespace App\Http\Livewire\Project\New;
use App\Models\Application;
use App\Models\GithubApp;
use App\Models\Project;
2023-05-11 15:20:02 +02:00
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
2023-05-08 11:51:03 +02:00
use Illuminate\Support\Facades\Http;
use Livewire\Component;
class GithubPrivateRepository extends Component
{
public $current_step = 'github_apps';
2023-05-08 11:51:03 +02:00
public $github_apps;
public GithubApp $github_app;
public $parameters;
2023-05-11 15:20:02 +02:00
public $query;
2023-05-09 11:33:50 +02:00
public $type;
2023-05-08 11:51:03 +02:00
public int $selected_repository_id;
2023-06-05 13:50:34 +02:00
public int $selected_github_app_id;
2023-05-08 11:51:03 +02:00
public string $selected_repository_owner;
public string $selected_repository_repo;
public string $selected_branch_name = 'main';
public string $token;
public $repositories;
public int $total_repositories_count = 0;
public $branches;
public int $total_branches_count = 0;
2023-06-05 13:50:34 +02:00
public int $port = 3000;
public bool $is_static = false;
public string|null $publish_directory = null;
protected int $page = 1;
2023-06-05 13:50:34 +02:00
2023-06-13 10:59:27 +02:00
public function mount()
{
$this->parameters = get_route_parameters();
2023-06-13 10:59:27 +02:00
$this->query = request()->query();
$this->repositories = $this->branches = collect();
$this->github_apps = GithubApp::private();
}
2023-05-08 11:51:03 +02:00
public function loadRepositories($github_app_id)
{
$this->repositories = collect();
$this->page = 1;
2023-06-05 13:50:34 +02:00
$this->selected_github_app_id = $github_app_id;
2023-05-08 11:51:03 +02:00
$this->github_app = GithubApp::where('id', $github_app_id)->first();
2023-05-09 14:42:10 +02:00
$this->token = generate_github_installation_token($this->github_app);
2023-05-08 11:51:03 +02:00
$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'];
$this->current_step = 'repository';
2023-05-08 11:51:03 +02:00
}
protected function loadRepositoryByPage()
{
$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']));
}
2023-05-08 11:51:03 +02:00
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();
}
}
}
protected function loadBranchByPage()
{
ray('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));
}
2023-05-08 11:51:03 +02:00
public function submit()
{
try {
2023-05-11 15:20:02 +02:00
$destination_uuid = $this->query['destination'];
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
if (!$destination) {
$destination = SwarmDocker::where('uuid', $destination_uuid)->first();
}
if (!$destination) {
throw new \Exception('Destination not found. What?!');
2023-05-09 11:33:50 +02:00
}
2023-05-11 15:20:02 +02:00
$destination_class = $destination->getMorphClass();
$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
2023-05-08 11:51:03 +02:00
$application = Application::create([
2023-06-05 13:50:34 +02:00
'name' => generate_application_name($this->selected_repository_owner . '/' . $this->selected_repository_repo, $this->selected_branch_name),
2023-05-11 15:20:02 +02:00
'repository_project_id' => $this->selected_repository_id,
2023-05-08 11:51:03 +02:00
'git_repository' => "{$this->selected_repository_owner}/{$this->selected_repository_repo}",
'git_branch' => $this->selected_branch_name,
'build_pack' => 'nixpacks',
2023-06-05 13:50:34 +02:00
'ports_exposes' => $this->port,
'publish_directory' => $this->publish_directory,
2023-05-08 11:51:03 +02:00
'environment_id' => $environment->id,
2023-05-11 15:20:02 +02:00
'destination_id' => $destination->id,
'destination_type' => $destination_class,
2023-05-08 11:51:03 +02:00
'source_id' => $this->github_app->id,
'source_type' => $this->github_app->getMorphClass()
2023-05-08 11:51:03 +02:00
]);
2023-06-05 13:50:34 +02:00
$application->settings->is_static = $this->is_static;
$application->settings->save();
2023-05-11 15:20:02 +02:00
2023-05-08 11:51:03 +02:00
redirect()->route('project.application.configuration', [
'application_uuid' => $application->uuid,
2023-06-05 13:50:34 +02:00
'environment_name' => $environment->name,
2023-05-08 11:51:03 +02:00
'project_uuid' => $project->uuid,
]);
} catch (\Exception $e) {
2023-06-09 15:55:21 +02:00
return general_error_handler(err: $e, that: $this);
2023-05-08 11:51:03 +02:00
}
}
2023-06-05 13:50:34 +02:00
public function instantSave()
{
if ($this->is_static) {
$this->port = 80;
$this->publish_directory = '/dist';
} else {
$this->port = 3000;
$this->publish_directory = null;
}
2023-06-15 10:48:13 +02:00
$this->emit('success', 'Application settings updated!');
2023-06-05 13:50:34 +02:00
}
2023-05-08 11:51:03 +02:00
}