2023-05-08 09:51:03 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\New;
|
2023-05-08 09:51:03 +00:00
|
|
|
|
|
|
|
use App\Models\Application;
|
|
|
|
use App\Models\GithubApp;
|
|
|
|
use App\Models\Project;
|
2023-05-11 13:20:02 +00:00
|
|
|
use App\Models\StandaloneDocker;
|
|
|
|
use App\Models\SwarmDocker;
|
2023-08-30 12:46:51 +00:00
|
|
|
use App\Traits\SaveFromRedirect;
|
2023-05-08 09:51:03 +00:00
|
|
|
use Illuminate\Support\Facades\Http;
|
2023-09-19 13:51:13 +00:00
|
|
|
use Illuminate\Support\Facades\Route;
|
2023-05-08 09:51:03 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class GithubPrivateRepository extends Component
|
|
|
|
{
|
2023-08-30 12:46:51 +00:00
|
|
|
use SaveFromRedirect;
|
2023-07-26 11:23:47 +00:00
|
|
|
public $current_step = 'github_apps';
|
2023-05-08 09:51:03 +00:00
|
|
|
public $github_apps;
|
|
|
|
public GithubApp $github_app;
|
|
|
|
public $parameters;
|
2023-08-30 12:46:51 +00:00
|
|
|
public $currentRoute;
|
2023-05-11 13:20:02 +00:00
|
|
|
public $query;
|
2023-05-09 09:33:50 +00:00
|
|
|
public $type;
|
2023-05-08 09:51:03 +00:00
|
|
|
|
|
|
|
public int $selected_repository_id;
|
2023-06-05 11:50:34 +00:00
|
|
|
public int $selected_github_app_id;
|
2023-05-08 09:51:03 +00: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 11:50:34 +00:00
|
|
|
public int $port = 3000;
|
|
|
|
public bool $is_static = false;
|
|
|
|
public string|null $publish_directory = null;
|
2023-08-08 09:51:36 +00:00
|
|
|
protected int $page = 1;
|
2023-06-05 11:50:34 +00:00
|
|
|
|
2023-08-30 12:46:51 +00:00
|
|
|
|
2023-06-13 08:59:27 +00:00
|
|
|
public function mount()
|
|
|
|
{
|
2023-08-30 12:46:51 +00:00
|
|
|
$this->currentRoute = Route::currentRouteName();
|
2023-08-09 13:57:53 +00:00
|
|
|
$this->parameters = get_route_parameters();
|
2023-06-13 08:59:27 +00:00
|
|
|
$this->query = request()->query();
|
|
|
|
$this->repositories = $this->branches = collect();
|
|
|
|
$this->github_apps = GithubApp::private();
|
|
|
|
}
|
2023-05-08 09:51:03 +00:00
|
|
|
public function loadRepositories($github_app_id)
|
|
|
|
{
|
|
|
|
$this->repositories = collect();
|
|
|
|
$this->page = 1;
|
2023-06-05 11:50:34 +00:00
|
|
|
$this->selected_github_app_id = $github_app_id;
|
2023-05-08 09:51:03 +00:00
|
|
|
$this->github_app = GithubApp::where('id', $github_app_id)->first();
|
2023-05-09 12:42:10 +00:00
|
|
|
$this->token = generate_github_installation_token($this->github_app);
|
2023-05-08 09:51:03 +00: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'];
|
2023-07-26 11:23:47 +00:00
|
|
|
$this->current_step = 'repository';
|
2023-05-08 09:51:03 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00: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) {
|
2023-12-07 18:06:32 +00:00
|
|
|
return $this->dispatch('error', $json['message']);
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($json['total_count'] === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->total_repositories_count = $json['total_count'];
|
|
|
|
$this->repositories = $this->repositories->concat(collect($json['repositories']));
|
|
|
|
}
|
|
|
|
|
2023-05-08 09:51:03 +00: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();
|
|
|
|
}
|
|
|
|
}
|
2023-09-30 13:57:30 +00:00
|
|
|
$this->selected_branch_name = data_get($this->branches,'0.name');
|
2023-05-08 09:51:03 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
|
|
|
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) {
|
2023-12-07 18:06:32 +00:00
|
|
|
return $this->dispatch('error', $json['message']);
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->total_branches_count = count($json);
|
|
|
|
$this->branches = $this->branches->concat(collect($json));
|
|
|
|
}
|
|
|
|
|
2023-05-08 09:51:03 +00:00
|
|
|
public function submit()
|
|
|
|
{
|
|
|
|
try {
|
2023-05-11 13:20:02 +00: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 09:33:50 +00:00
|
|
|
}
|
2023-05-11 13:20:02 +00: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 09:51:03 +00:00
|
|
|
$application = Application::create([
|
2023-06-05 11:50:34 +00:00
|
|
|
'name' => generate_application_name($this->selected_repository_owner . '/' . $this->selected_repository_repo, $this->selected_branch_name),
|
2023-05-11 13:20:02 +00:00
|
|
|
'repository_project_id' => $this->selected_repository_id,
|
2023-05-08 09:51:03 +00:00
|
|
|
'git_repository' => "{$this->selected_repository_owner}/{$this->selected_repository_repo}",
|
|
|
|
'git_branch' => $this->selected_branch_name,
|
|
|
|
'build_pack' => 'nixpacks',
|
2023-06-05 11:50:34 +00:00
|
|
|
'ports_exposes' => $this->port,
|
|
|
|
'publish_directory' => $this->publish_directory,
|
2023-05-08 09:51:03 +00:00
|
|
|
'environment_id' => $environment->id,
|
2023-05-11 13:20:02 +00:00
|
|
|
'destination_id' => $destination->id,
|
|
|
|
'destination_type' => $destination_class,
|
2023-05-08 09:51:03 +00:00
|
|
|
'source_id' => $this->github_app->id,
|
2023-08-08 09:51:36 +00:00
|
|
|
'source_type' => $this->github_app->getMorphClass()
|
2023-05-08 09:51:03 +00:00
|
|
|
]);
|
2023-06-05 11:50:34 +00:00
|
|
|
$application->settings->is_static = $this->is_static;
|
|
|
|
$application->settings->save();
|
2023-05-11 13:20:02 +00:00
|
|
|
|
2023-09-30 13:39:40 +00:00
|
|
|
$fqdn = generateFqdn($destination->server, $application->uuid);
|
|
|
|
$application->fqdn = $fqdn;
|
|
|
|
|
2023-09-19 13:51:13 +00:00
|
|
|
$application->name = generate_application_name($this->selected_repository_owner . '/' . $this->selected_repository_repo, $this->selected_branch_name, $application->uuid);
|
|
|
|
$application->save();
|
|
|
|
|
2023-12-07 21:56:55 +00:00
|
|
|
return $this->redirectRoute('project.application.configuration', [
|
2023-05-08 09:51:03 +00:00
|
|
|
'application_uuid' => $application->uuid,
|
2023-06-05 11:50:34 +00:00
|
|
|
'environment_name' => $environment->name,
|
2023-05-08 09:51:03 +00:00
|
|
|
'project_uuid' => $project->uuid,
|
2023-12-07 21:56:55 +00:00
|
|
|
], navigate: true);
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-05-08 09:51:03 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-05 11:50:34 +00:00
|
|
|
public function instantSave()
|
|
|
|
{
|
|
|
|
if ($this->is_static) {
|
|
|
|
$this->port = 80;
|
|
|
|
$this->publish_directory = '/dist';
|
|
|
|
} else {
|
|
|
|
$this->port = 3000;
|
|
|
|
$this->publish_directory = null;
|
|
|
|
}
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('success', 'Application settings updated!');
|
2023-06-05 11:50:34 +00:00
|
|
|
}
|
2023-05-08 09:51:03 +00:00
|
|
|
}
|