lasthourcloud/app/Livewire/Source/Github/Change.php

197 lines
7.8 KiB
PHP
Raw Normal View History

2023-05-08 11:36:49 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Source\Github;
2023-05-08 11:36:49 +00:00
use App\Jobs\GithubAppPermissionJob;
2023-05-08 11:36:49 +00:00
use App\Models\GithubApp;
2024-02-20 14:07:12 +00:00
use Illuminate\Support\Facades\Http;
2023-05-08 11:36:49 +00:00
use Livewire\Component;
class Change extends Component
{
2023-06-14 07:44:40 +00:00
public string $webhook_endpoint;
2024-06-10 20:43:34 +00:00
public ?string $ipv4 = null;
2024-06-10 20:43:34 +00:00
public ?string $ipv6 = null;
2024-06-10 20:43:34 +00:00
public ?string $fqdn = null;
2023-06-14 11:07:58 +00:00
2023-11-07 08:44:47 +00:00
public ?bool $default_permissions = true;
2024-06-10 20:43:34 +00:00
2023-11-07 08:44:47 +00:00
public ?bool $preview_deployment_permissions = true;
2024-06-10 20:43:34 +00:00
public ?bool $administration = false;
2023-06-14 11:07:58 +00:00
2023-05-08 11:36:49 +00:00
public $parameters;
2024-06-10 20:43:34 +00:00
public ?GithubApp $github_app = null;
2024-06-10 20:43:34 +00:00
2023-05-18 11:26:35 +00:00
public string $name;
2024-06-10 20:43:34 +00:00
2023-05-08 11:36:49 +00:00
public bool $is_system_wide;
2024-03-13 13:55:44 +00:00
public $applications;
2023-05-08 11:36:49 +00:00
protected $rules = [
'github_app.name' => 'required|string',
'github_app.organization' => 'nullable|string',
'github_app.api_url' => 'required|string',
'github_app.html_url' => 'required|string',
'github_app.custom_user' => 'required|string',
'github_app.custom_port' => 'required|int',
'github_app.app_id' => 'required|int',
2023-11-07 08:44:47 +00:00
'github_app.installation_id' => 'required|int',
'github_app.client_id' => 'required|string',
'github_app.client_secret' => 'required|string',
'github_app.webhook_secret' => 'required|string',
2023-05-08 11:36:49 +00:00
'github_app.is_system_wide' => 'required|bool',
'github_app.contents' => 'nullable|string',
'github_app.metadata' => 'nullable|string',
'github_app.pull_requests' => 'nullable|string',
'github_app.administration' => 'nullable|string',
2023-05-08 11:36:49 +00:00
];
public function checkPermissions()
{
GithubAppPermissionJob::dispatchSync($this->github_app);
$this->github_app->refresh()->makeVisible('client_secret')->makeVisible('webhook_secret');
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Github App permissions updated.');
}
2024-02-20 14:07:12 +00:00
// public function check()
// {
// Need administration:read:write permission
// https://docs.github.com/en/rest/actions/self-hosted-runners?apiVersion=2022-11-28#list-self-hosted-runners-for-a-repository
// $github_access_token = generate_github_installation_token($this->github_app);
// $repositories = Http::withToken($github_access_token)->get("{$this->github_app->api_url}/installation/repositories?per_page=100");
// $runners_by_repository = collect([]);
// $repositories = $repositories->json()['repositories'];
// foreach ($repositories as $repository) {
// $runners_downloads = Http::withToken($github_access_token)->get("{$this->github_app->api_url}/repos/{$repository['full_name']}/actions/runners/downloads");
// $runners = Http::withToken($github_access_token)->get("{$this->github_app->api_url}/repos/{$repository['full_name']}/actions/runners");
// $token = Http::withHeaders([
// 'Authorization' => "Bearer $github_access_token",
// 'Accept' => 'application/vnd.github+json'
// ])->withBody(null)->post("{$this->github_app->api_url}/repos/{$repository['full_name']}/actions/runners/registration-token");
// $token = $token->json();
// $remove_token = Http::withHeaders([
// 'Authorization' => "Bearer $github_access_token",
// 'Accept' => 'application/vnd.github+json'
// ])->withBody(null)->post("{$this->github_app->api_url}/repos/{$repository['full_name']}/actions/runners/remove-token");
// $remove_token = $remove_token->json();
// $runners_by_repository->put($repository['full_name'], [
// 'token' => $token,
// 'remove_token' => $remove_token,
// 'runners' => $runners->json(),
// 'runners_downloads' => $runners_downloads->json()
// ]);
// }
// ray($runners_by_repository);
// }
2023-06-14 07:44:40 +00:00
public function mount()
{
2023-11-07 08:44:47 +00:00
$github_app_uuid = request()->github_app_uuid;
$this->github_app = GithubApp::where('uuid', $github_app_uuid)->first();
2024-06-10 20:43:34 +00:00
if (! $this->github_app) {
2023-12-27 15:45:01 +00:00
return redirect()->route('source.all');
2023-11-07 08:44:47 +00:00
}
2024-03-13 13:55:44 +00:00
$this->applications = $this->github_app->applications;
$settings = view()->shared('instanceSettings');
2023-11-07 08:44:47 +00:00
$this->github_app->makeVisible('client_secret')->makeVisible('webhook_secret');
$this->name = str($this->github_app->name)->kebab();
$this->fqdn = $settings->fqdn;
if ($settings->public_ipv4) {
2024-06-10 20:43:34 +00:00
$this->ipv4 = 'http://'.$settings->public_ipv4.':'.config('app.port');
2023-11-07 08:44:47 +00:00
}
if ($settings->public_ipv6) {
2024-06-10 20:43:34 +00:00
$this->ipv6 = 'http://'.$settings->public_ipv6.':'.config('app.port');
2023-11-07 08:44:47 +00:00
}
if ($this->github_app->installation_id && session('from')) {
$source_id = data_get(session('from'), 'source_id');
2024-06-10 20:43:34 +00:00
if (! $source_id || $this->github_app->id !== $source_id) {
2023-11-07 08:44:47 +00:00
session()->forget('from');
} else {
$parameters = data_get(session('from'), 'parameters');
$back = data_get(session('from'), 'back');
$environment_name = data_get($parameters, 'environment_name');
$project_uuid = data_get($parameters, 'project_uuid');
$type = data_get($parameters, 'type');
$destination = data_get($parameters, 'destination');
session()->forget('from');
2024-06-10 20:43:34 +00:00
2023-12-27 15:45:01 +00:00
return redirect()->route($back, [
2023-11-07 08:44:47 +00:00
'environment_name' => $environment_name,
'project_uuid' => $project_uuid,
'type' => $type,
'destination' => $destination,
]);
}
}
$this->parameters = get_route_parameters();
2024-06-10 20:43:34 +00:00
if (isCloud() && ! isDev()) {
$this->webhook_endpoint = config('app.url');
} else {
$this->webhook_endpoint = $this->ipv4;
$this->is_system_wide = $this->github_app->is_system_wide;
}
2023-06-14 07:44:40 +00:00
}
2023-05-08 11:36:49 +00:00
public function submit()
{
try {
2023-11-07 08:44:47 +00:00
$this->github_app->makeVisible('client_secret')->makeVisible('webhook_secret');
2023-12-08 18:57:55 +00:00
$this->validate([
'github_app.name' => 'required|string',
'github_app.organization' => 'nullable|string',
'github_app.api_url' => 'required|string',
'github_app.html_url' => 'required|string',
'github_app.custom_user' => 'required|string',
'github_app.custom_port' => 'required|int',
'github_app.app_id' => 'required|int',
'github_app.installation_id' => 'required|int',
'github_app.client_id' => 'required|string',
'github_app.client_secret' => 'required|string',
'github_app.webhook_secret' => 'required|string',
'github_app.is_system_wide' => 'required|bool',
]);
2023-05-08 11:36:49 +00:00
$this->github_app->save();
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Github App updated.');
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-05-08 11:36:49 +00:00
}
}
2023-05-08 11:36:49 +00:00
public function instantSave()
{
try {
$this->github_app->makeVisible('client_secret')->makeVisible('webhook_secret');
$this->github_app->save();
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Github App updated.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
2023-05-08 11:36:49 +00:00
}
2023-06-14 07:44:40 +00:00
2023-05-09 08:01:57 +00:00
public function delete()
2023-05-08 11:36:49 +00:00
{
2023-05-09 08:01:57 +00:00
try {
2024-03-13 13:55:44 +00:00
if ($this->github_app->applications->isNotEmpty()) {
$this->dispatch('error', 'This source is being used by an application. Please delete all applications first.');
$this->github_app->makeVisible('client_secret')->makeVisible('webhook_secret');
2024-06-10 20:43:34 +00:00
2024-03-13 13:55:44 +00:00
return;
}
2023-05-09 08:01:57 +00:00
$this->github_app->delete();
2024-06-10 20:43:34 +00:00
2023-12-27 15:45:01 +00:00
return redirect()->route('source.all');
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-05-08 11:36:49 +00:00
}
}
}