fix: github source view

This commit is contained in:
Andras Bacsai 2023-11-07 09:44:47 +01:00
parent 5cec50efbe
commit ad7aa2eed6
6 changed files with 174 additions and 175 deletions

View File

@ -3,17 +3,18 @@
namespace App\Http\Livewire\Source\Github; namespace App\Http\Livewire\Source\Github;
use App\Models\GithubApp; use App\Models\GithubApp;
use App\Models\InstanceSettings;
use Livewire\Component; use Livewire\Component;
class Change extends Component class Change extends Component
{ {
public string $webhook_endpoint; public string $webhook_endpoint;
public string|null $ipv4; public ?string $ipv4;
public string|null $ipv6; public ?string $ipv6;
public string|null $fqdn; public ?string $fqdn;
public bool|null $default_permissions = true; public ?bool $default_permissions = true;
public bool|null $preview_deployment_permissions = true; public ?bool $preview_deployment_permissions = true;
public $parameters; public $parameters;
public GithubApp $github_app; public GithubApp $github_app;
@ -28,29 +29,68 @@ class Change extends Component
'github_app.custom_user' => 'required|string', 'github_app.custom_user' => 'required|string',
'github_app.custom_port' => 'required|int', 'github_app.custom_port' => 'required|int',
'github_app.app_id' => 'required|int', 'github_app.app_id' => 'required|int',
'github_app.installation_id' => 'nullable', 'github_app.installation_id' => 'required|int',
'github_app.client_id' => 'nullable', 'github_app.client_id' => 'required|string',
'github_app.client_secret' => 'nullable', 'github_app.client_secret' => 'required|string',
'github_app.webhook_secret' => 'nullable', 'github_app.webhook_secret' => 'required|string',
'github_app.is_system_wide' => 'required|bool', 'github_app.is_system_wide' => 'required|bool',
]; ];
public function mount() public function mount()
{ {
$github_app_uuid = request()->github_app_uuid;
$this->github_app = GithubApp::where('uuid', $github_app_uuid)->first();
if (!$this->github_app) {
return redirect()->route('source.all');
}
$settings = InstanceSettings::get();
$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) {
$this->ipv4 = 'http://' . $settings->public_ipv4 . ':' . config('app.port');
}
if ($settings->public_ipv6) {
$this->ipv6 = 'http://' . $settings->public_ipv6 . ':' . config('app.port');
}
if ($this->github_app->installation_id && session('from')) {
$source_id = data_get(session('from'), 'source_id');
if (!$source_id || $this->github_app->id !== $source_id) {
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');
return redirect()->route($back, [
'environment_name' => $environment_name,
'project_uuid' => $project_uuid,
'type' => $type,
'destination' => $destination,
]);
}
}
$this->parameters = get_route_parameters();
if (isCloud() && !isDev()) { if (isCloud() && !isDev()) {
$this->webhook_endpoint = config('app.url'); $this->webhook_endpoint = config('app.url');
} else { } else {
$this->webhook_endpoint = $this->ipv4; $this->webhook_endpoint = $this->ipv4;
$this->is_system_wide = $this->github_app->is_system_wide; $this->is_system_wide = $this->github_app->is_system_wide;
} }
$this->parameters = get_route_parameters();
} }
public function submit() public function submit()
{ {
try { try {
$this->github_app->makeVisible('client_secret')->makeVisible('webhook_secret');
$this->validate(); $this->validate();
$this->github_app->save(); $this->github_app->save();
$this->emit('success', 'Github App updated successfully.');
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e, $this); return handleError($e, $this);
} }

View File

@ -7,7 +7,7 @@
// The release version of your application // The release version of your application
// Example with dynamic git hash: trim(exec('git --git-dir ' . base_path('.git') . ' log --pretty="%h" -n1 HEAD')) // Example with dynamic git hash: trim(exec('git --git-dir ' . base_path('.git') . ' log --pretty="%h" -n1 HEAD'))
'release' => '4.0.0-beta.111', 'release' => '4.0.0-beta.112',
// When left empty or `null` the Laravel environment will be used // When left empty or `null` the Laravel environment will be used
'environment' => config('app.env'), 'environment' => config('app.env'),

View File

@ -1,3 +1,3 @@
<?php <?php
return '4.0.0-beta.111'; return '4.0.0-beta.112';

View File

@ -4,32 +4,26 @@
<p>This source will be deleted. It is not reversible. <br>Please think again.</p> <p>This source will be deleted. It is not reversible. <br>Please think again.</p>
</x-slot:modalBody> </x-slot:modalBody>
</x-modal> </x-modal>
<form wire:submit.prevent='submit' x-data> @if (data_get($github_app, 'app_id'))
<div class="flex items-center gap-2"> <form wire:submit.prevent='submit'>
<h1>GitHub App</h1> <div class="flex items-center gap-2">
<div class="flex gap-2"> <h1>GitHub App</h1>
@if ($github_app->app_id) <div class="flex gap-2">
<x-forms.button type="submit">Save</x-forms.button>
@if (data_get($github_app, 'installation_id')) @if (data_get($github_app, 'installation_id'))
<x-forms.button type="submit">Save</x-forms.button>
<a href="{{ get_installation_path($github_app) }}"> <a href="{{ get_installation_path($github_app) }}">
<x-forms.button> <x-forms.button>
Update Repositories Update Repositories
<x-external-link /> <x-external-link />
</x-forms.button> </x-forms.button>
</a> </a>
@endif @endif
@else <x-forms.button isError isModal modalId="deleteSource">
<x-forms.button disabled type="submit">Save</x-forms.button> Delete
@endif </x-forms.button>
<x-forms.button isError isModal modalId="deleteSource"> </div>
Delete
</x-forms.button>
</div> </div>
</div> <div class="subtitle">Your Private GitHub App for private repositories.</div>
<div class="subtitle">Your Private GitHub App for private repositories.</div>
@if (data_get($github_app, 'app_id'))
@if (!data_get($github_app, 'installation_id')) @if (!data_get($github_app, 'installation_id'))
<div class="mb-10 rounded alert alert-warning"> <div class="mb-10 rounded alert alert-warning">
<svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6 stroke-current shrink-0" fill="none" <svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6 stroke-current shrink-0" fill="none"
@ -39,7 +33,7 @@
</svg> </svg>
<span>You must complete this step before you can use this source!</span> <span>You must complete this step before you can use this source!</span>
</div> </div>
<a class="justify-center box" href="{{ get_installation_path($github_app) }}"> <a class="items-center justify-center box" href="{{ get_installation_path($github_app) }}">
Install Repositories on GitHub Install Repositories on GitHub
</a> </a>
@else @else
@ -78,110 +72,118 @@
<x-forms.input id="github_app.webhook_secret" label="Webhook Secret" type="password" /> <x-forms.input id="github_app.webhook_secret" label="Webhook Secret" type="password" />
</div> </div>
@endif @endif
@else </form>
<div class="mb-10 rounded alert alert-warning"> @else
<svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6 stroke-current shrink-0" fill="none" <div class="flex items-center gap-2 pb-4">
viewBox="0 0 24 24"> <h1>GitHub App</h1>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" <div class="flex gap-2">
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" /> <x-forms.button isError isModal modalId="deleteSource">
</svg> Delete
<span>You must complete this step before you can use this source!</span> </x-forms.button>
</div> </div>
<form class="flex gap-4"> </div>
<h2>Register a GitHub App</h2> <div class="mb-10 rounded alert alert-warning">
<div class="pt-1 pb-2 ">You need to register a GitHub App before using this source.</div> <svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6 stroke-current shrink-0" fill="none"
<div class="pt-2 pb-10"> viewBox="0 0 24 24">
@if (!isCloud() || isDev()) <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
<div class="flex items-end gap-2"> d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
<x-forms.select wire:model='webhook_endpoint' label="Webhook Endpoint" </svg>
helper="All Git webhooks will be sent to this endpoint. <br><br>If you would like to use domain instead of IP address, set your Coolify instance's FQDN in the Settings menu."> <span>You must complete this step before you can use this source!</span>
@if ($ipv4) </div>
<option value="{{ $ipv4 }}">Use {{ $ipv4 }}</option> <div class="flex flex-col">
@endif <h2 >Register a GitHub App</h2>
@if ($ipv6) <div >You need to register a GitHub App before using this source.</div>
<option value="{{ $ipv6 }}">Use {{ $ipv6 }}</option> <div class="py-10">
@endif @if (!isCloud() || isDev())
@if ($fqdn) <div class="flex items-end gap-2">
<option value="{{ $fqdn }}">Use {{ $fqdn }}</option> <x-forms.select wire:model='webhook_endpoint' label="Webhook Endpoint"
@endif helper="All Git webhooks will be sent to this endpoint. <br><br>If you would like to use domain instead of IP address, set your Coolify instance's FQDN in the Settings menu.">
@if (config('app.url')) @if ($ipv4)
<option value="{{ config('app.url') }}">Use {{ config('app.url') }}</option> <option value="{{ $ipv4 }}">Use {{ $ipv4 }}</option>
@endif @endif
</x-forms.select> @if ($ipv6)
<x-forms.button <option value="{{ $ipv6 }}">Use {{ $ipv6 }}</option>
x-on:click.prevent="createGithubApp('{{ $webhook_endpoint }}','{{ $preview_deployment_permissions }}')"> @endif
Register @if ($fqdn)
</x-forms.button> <option value="{{ $fqdn }}">Use {{ $fqdn }}</option>
</div> @endif
@else @if (config('app.url'))
<option value="{{ config('app.url') }}">Use {{ config('app.url') }}</option>
@endif
</x-forms.select>
<x-forms.button <x-forms.button
x-on:click.prevent="createGithubApp('{{ $webhook_endpoint }}','{{ $preview_deployment_permissions }}')"> x-on:click.prevent="createGithubApp('{{ $webhook_endpoint }}','{{ $preview_deployment_permissions }}')">
Register Now Register
</x-forms.button> </x-forms.button>
@endif
<div class="flex flex-col gap-2 pt-4">
<x-forms.checkbox disabled instantSave id="default_permissions" label="Default Permissions"
helper="Contents: read<br>Metadata: read<br>Email: read" />
<x-forms.checkbox instantSave id="preview_deployment_permissions"
label="Preview Deployments Permission"
helper="Necessary for updating pull requests with useful comments (deployment status, links, etc.)<br><br>Pull Request: read & write" />
</div> </div>
@else
<x-forms.button
x-on:click.prevent="createGithubApp('{{ $webhook_endpoint }}','{{ $preview_deployment_permissions }}')">
Register Now
</x-forms.button>
@endif
<div class="flex flex-col gap-2 pt-4">
<x-forms.checkbox disabled instantSave id="default_permissions" label="Default Permissions"
helper="Contents: read<br>Metadata: read<br>Email: read" />
<x-forms.checkbox instantSave id="preview_deployment_permissions"
label="Preview Deployments Permission"
helper="Necessary for updating pull requests with useful comments (deployment status, links, etc.)<br><br>Pull Request: read & write" />
</div> </div>
</form> </div>
<script> </div>
function createGithubApp(webhook_endpoint, preview_deployment_permissions) { <script>
const { function createGithubApp(webhook_endpoint, preview_deployment_permissions) {
organization, const {
uuid, organization,
html_url uuid,
} = @json($github_app); html_url
let baseUrl = webhook_endpoint; } = @json($github_app);
const name = @js($name); let baseUrl = webhook_endpoint;
const isDev = @js(config('app.env')) === const name = @js($name);
'local'; const isDev = @js(config('app.env')) ===
const devWebhook = @js(config('coolify.dev_webhook')); 'local';
if (isDev && devWebhook) { const devWebhook = @js(config('coolify.dev_webhook'));
baseUrl = devWebhook; if (isDev && devWebhook) {
} baseUrl = devWebhook;
const webhookBaseUrl = `${baseUrl}/webhooks`;
const path = organization ? `organizations/${organization}/settings/apps/new` : 'settings/apps/new';
const default_permissions = {
contents: 'read',
metadata: 'read',
emails: 'read'
};
if (preview_deployment_permissions) {
default_permissions.pull_requests = 'write';
}
const data = {
name,
url: baseUrl,
hook_attributes: {
url: `${webhookBaseUrl}/source/github/events`,
active: true,
},
redirect_url: `${webhookBaseUrl}/source/github/redirect`,
callback_urls: [`${baseUrl}/login/github/app`],
public: false,
request_oauth_on_install: false,
setup_url: `${webhookBaseUrl}/source/github/install?source=${uuid}`,
setup_on_update: true,
default_permissions,
default_events: ['pull_request', 'push']
};
const form = document.createElement('form');
form.setAttribute('method', 'post');
form.setAttribute('action', `${html_url}/${path}?state=${uuid}`);
const input = document.createElement('input');
input.setAttribute('id', 'manifest');
input.setAttribute('name', 'manifest');
input.setAttribute('type', 'hidden');
input.setAttribute('value', JSON.stringify(data));
form.appendChild(input);
document.getElementsByTagName('body')[0].appendChild(form);
form.submit();
} }
</script> const webhookBaseUrl = `${baseUrl}/webhooks`;
@endif const path = organization ? `organizations/${organization}/settings/apps/new` : 'settings/apps/new';
</form> const default_permissions = {
contents: 'read',
metadata: 'read',
emails: 'read'
};
if (preview_deployment_permissions) {
default_permissions.pull_requests = 'write';
}
const data = {
name,
url: baseUrl,
hook_attributes: {
url: `${webhookBaseUrl}/source/github/events`,
active: true,
},
redirect_url: `${webhookBaseUrl}/source/github/redirect`,
callback_urls: [`${baseUrl}/login/github/app`],
public: false,
request_oauth_on_install: false,
setup_url: `${webhookBaseUrl}/source/github/install?source=${uuid}`,
setup_on_update: true,
default_permissions,
default_events: ['pull_request', 'push']
};
const form = document.createElement('form');
form.setAttribute('method', 'post');
form.setAttribute('action', `${html_url}/${path}?state=${uuid}`);
const input = document.createElement('input');
input.setAttribute('id', 'manifest');
input.setAttribute('name', 'manifest');
input.setAttribute('type', 'hidden');
input.setAttribute('value', JSON.stringify(data));
form.appendChild(input);
document.getElementsByTagName('body')[0].appendChild(form);
form.submit();
}
</script>
@endif
</div> </div>

View File

@ -20,11 +20,10 @@
use App\Http\Livewire\Server\Proxy\Show as ProxyShow; use App\Http\Livewire\Server\Proxy\Show as ProxyShow;
use App\Http\Livewire\Server\Proxy\Logs as ProxyLogs; use App\Http\Livewire\Server\Proxy\Logs as ProxyLogs;
use App\Http\Livewire\Server\Show; use App\Http\Livewire\Server\Show;
use App\Http\Livewire\Source\Github\Change as GitHubChange;
use App\Http\Livewire\Subscription\Show as SubscriptionShow; use App\Http\Livewire\Subscription\Show as SubscriptionShow;
use App\Http\Livewire\Waitlist\Index as WaitlistIndex; use App\Http\Livewire\Waitlist\Index as WaitlistIndex;
use App\Models\GithubApp;
use App\Models\GitlabApp; use App\Models\GitlabApp;
use App\Models\InstanceSettings;
use App\Models\PrivateKey; use App\Models\PrivateKey;
use App\Models\Server; use App\Models\Server;
use App\Models\StandaloneDocker; use App\Models\StandaloneDocker;
@ -178,49 +177,7 @@
'sources' => $sources, 'sources' => $sources,
]); ]);
})->name('source.all'); })->name('source.all');
Route::get('/source/github/{github_app_uuid}', function (Request $request) { Route::get('/source/github/{github_app_uuid}', GitHubChange::class)->name('source.github.show');
$github_app = GithubApp::where('uuid', request()->github_app_uuid)->first();
if (!$github_app) {
abort(404);
}
$github_app->makeVisible('client_secret')->makeVisible('webhook_secret');
$settings = InstanceSettings::get();
$name = Str::of(Str::kebab($github_app->name));
if ($settings->public_ipv4) {
$ipv4 = 'http://' . $settings->public_ipv4 . ':' . config('app.port');
}
if ($settings->public_ipv6) {
$ipv6 = 'http://' . $settings->public_ipv6 . ':' . config('app.port');
}
if ($github_app->installation_id && session('from')) {
$source_id = data_get(session('from'), 'source_id');
if (!$source_id || $github_app->id !== $source_id) {
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');
return redirect()->route($back, [
'environment_name' => $environment_name,
'project_uuid' => $project_uuid,
'type' => $type,
'destination' => $destination,
]);
}
}
return view('source.github.show', [
'github_app' => $github_app,
'name' => $name,
'ipv4' => $ipv4 ?? null,
'ipv6' => $ipv6 ?? null,
'fqdn' => $settings->fqdn,
]);
})->name('source.github.show');
Route::get('/source/gitlab/{gitlab_app_uuid}', function (Request $request) { Route::get('/source/gitlab/{gitlab_app_uuid}', function (Request $request) {
$gitlab_app = GitlabApp::where('uuid', request()->gitlab_app_uuid)->first(); $gitlab_app = GitlabApp::where('uuid', request()->gitlab_app_uuid)->first();
return view('source.gitlab.show', [ return view('source.gitlab.show', [

View File

@ -4,7 +4,7 @@
"version": "3.12.36" "version": "3.12.36"
}, },
"v4": { "v4": {
"version": "4.0.0-beta.111" "version": "4.0.0-beta.112"
} }
} }
} }