Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

feat: installation/update github apps

Andras Bacsai19ad184cd66a
diff --git a/app/Http/Livewire/PrivateKey/Change.php b/app/Http/Livewire/PrivateKey/Change.php
index 4f3382d51e..d8d9e3ffd6 100644
--- a/app/Http/Livewire/PrivateKey/Change.php
+++ b/app/Http/Livewire/PrivateKey/Change.php
@@ -35,7 +35,7 @@ class Change extends Component
$this->private_key->save();
session('currentTeam')->privateKeys = PrivateKey::where('team_id', session('currentTeam')->id)->get();
} catch (\Exception $e) {
- return generalErrorHandlerLivewire($e, $this);
+ return generalErrorHandler($e, $this);
}
}
}
diff --git a/app/Http/Livewire/Project/Application/EnvironmentVariable/All.php b/app/Http/Livewire/Project/Application/EnvironmentVariable/All.php
index a3e8457831..4d7836b5fd 100644
--- a/app/Http/Livewire/Project/Application/EnvironmentVariable/All.php
+++ b/app/Http/Livewire/Project/Application/EnvironmentVariable/All.php
@@ -26,7 +26,7 @@ class All extends Component
$this->application->refresh();
$this->emit('clearAddEnv');
} catch (\Exception $e) {
- return generalErrorHandlerLivewire($e, $this);
+ return generalErrorHandler($e, $this);
}
}
}
diff --git a/app/Http/Livewire/Project/Application/Storages/All.php b/app/Http/Livewire/Project/Application/Storages/All.php
index 9c8e50b3a7..e48111614a 100644
--- a/app/Http/Livewire/Project/Application/Storages/All.php
+++ b/app/Http/Livewire/Project/Application/Storages/All.php
@@ -28,7 +28,7 @@ class All extends Component
$this->application->refresh();
$this->emit('clearAddStorage');
} catch (\Exception $e) {
- return generalErrorHandlerLivewire($e, $this);
+ return generalErrorHandler($e, $this);
}
}
}
diff --git a/app/Http/Livewire/Project/Delete.php b/app/Http/Livewire/Project/Delete.php
new file mode 100644
index 0000000000..9677052eb1
--- /dev/null
+++ b/app/Http/Livewire/Project/Delete.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace App\Http\Livewire\Project;
+
+use App\Models\Project;
+use Livewire\Component;
+
+class Delete extends Component
+{
+ public int $project_id;
+ public int $resource_count = 0;
+
+ public function delete()
+ {
+ $this->validate([
+ 'project_id' => 'required|int',
+ ]);
+ $project = Project::findOrFail($this->project_id);
+ if ($project->applications->count() > 0) {
+ return $this->emit('error', 'Project has applications, please delete them first.');
+ }
+ $project->delete();
+ return redirect()->route('dashboard');
+ }
+}
diff --git a/app/Http/Livewire/Project/New/GithubPrivateRepository.php b/app/Http/Livewire/Project/New/GithubPrivateRepository.php
index 6b46119d3e..4e05636ca5 100644
--- a/app/Http/Livewire/Project/New/GithubPrivateRepository.php
+++ b/app/Http/Livewire/Project/New/GithubPrivateRepository.php
@@ -16,6 +16,7 @@ class GithubPrivateRepository extends Component
public $github_apps;
public GithubApp $github_app;
public $parameters;
+ public $type;
public int $selected_repository_id;
public string $selected_repository_owner;
@@ -110,8 +111,16 @@ class GithubPrivateRepository extends Component
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();
+ if ($this->type === 'project') {
+ $project = Project::create([
+ 'name' => generateRandomName(),
+ 'team_id' => session('currentTeam')->id
+ ]);
+ $environment = $project->load(['environments'])->environments->first();
+ } else {
+ $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}",
@@ -130,7 +139,7 @@ class GithubPrivateRepository extends Component
'environment_name' => $environment->name
]);
} catch (\Exception $e) {
- return generalErrorHandlerLivewire($e, $this);
+ return generalErrorHandler($e, $this);
}
}
public function mount()
diff --git a/app/Http/Livewire/Server/Form.php b/app/Http/Livewire/Server/Form.php
index c0eba05032..923006789e 100644
--- a/app/Http/Livewire/Server/Form.php
+++ b/app/Http/Livewire/Server/Form.php
@@ -54,7 +54,7 @@ class Form extends Component
$this->dockerComposeVersion = 'Not installed.';
}
} catch (\Exception $e) {
- return generalErrorHandlerLivewire($e, $this);
+ return generalErrorHandler($e, $this);
}
}
public function delete()
diff --git a/app/Http/Livewire/Source/Create.php b/app/Http/Livewire/Source/Create.php
index a36c633014..f7f09bba72 100644
--- a/app/Http/Livewire/Source/Create.php
+++ b/app/Http/Livewire/Source/Create.php
@@ -31,7 +31,7 @@ class Create extends Component
"custom_port" => 'required|int',
"is_system_wide" => 'required|bool',
]);
- GithubApp::create([
+ $github_app = GithubApp::create([
'name' => $this->name,
'organization' => $this->organization,
'api_url' => $this->api_url,
@@ -41,8 +41,9 @@ class Create extends Component
'is_system_wide' => $this->is_system_wide,
'team_id' => session('currentTeam')->id,
]);
+ redirect()->route('source.github.show', ['github_app_uuid' => $github_app->uuid]);
} catch (\Exception $e) {
- return generalErrorHandlerLivewire($e, $this);
+ return generalErrorHandler($e, $this);
}
}
}
diff --git a/app/Http/Livewire/Source/Github/Change.php b/app/Http/Livewire/Source/Github/Change.php
index f7267d80b3..ad4a9b0c96 100644
--- a/app/Http/Livewire/Source/Github/Change.php
+++ b/app/Http/Livewire/Source/Github/Change.php
@@ -36,7 +36,7 @@ class Change extends Component
$this->validate();
$this->github_app->save();
} catch (\Exception $e) {
- return generalErrorHandlerLivewire($e, $this);
+ return generalErrorHandler($e, $this);
}
}
public function instantSave()
@@ -46,7 +46,7 @@ class Change extends Component
$this->github_app->save();
$this->emit('saved', 'GitHub settings updated!');
} catch (\Exception $e) {
- return generalErrorHandlerLivewire($e, $this);
+ return generalErrorHandler($e, $this);
}
}
public function mount()
@@ -64,7 +64,7 @@ class Change extends Component
$this->github_app->delete();
redirect()->route('dashboard');
} catch (\Exception $e) {
- return generalErrorHandlerLivewire($e, $this);
+ return generalErrorHandler($e, $this);
}
}
}
diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php
index bd2368a5a3..0c4bfec72d 100644
--- a/app/Providers/RouteServiceProvider.php
+++ b/app/Providers/RouteServiceProvider.php
@@ -31,6 +31,9 @@ class RouteServiceProvider extends ServiceProvider
->prefix('api')
->group(base_path('routes/api.php'));
+ Route::prefix('webhooks')
+ ->group(base_path('routes/webhooks.php'));
+
Route::middleware('web')
->group(base_path('routes/web.php'));
});
diff --git a/bootstrap/helpers.php b/bootstrap/helpers.php
index 0cc76a5c42..f7796bd477 100644
--- a/bootstrap/helpers.php
+++ b/bootstrap/helpers.php
@@ -14,19 +14,23 @@ use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Spatie\Activitylog\Contracts\Activity;
-if (!function_exists('generalErrorHandlerLivewire')) {
- function generalErrorHandlerLivewire(\Throwable $e, $that)
+if (!function_exists('generalErrorHandler')) {
+ function generalErrorHandler(\Throwable $e, $that = null)
{
- if ($e instanceof QueryException) {
- if ($e->errorInfo[0] === '23505') {
- $that->emit('error', 'Duplicate entry found.');
- } else if (count($e->errorInfo) === 4) {
- $that->emit('error', $e->errorInfo[3]);
+ if ($that) {
+ if ($e instanceof QueryException) {
+ if ($e->errorInfo[0] === '23505') {
+ $that->emit('error', 'Duplicate entry found.');
+ } else if (count($e->errorInfo) === 4) {
+ $that->emit('error', $e->errorInfo[3]);
+ } else {
+ $that->emit('error', $e->errorInfo[2]);
+ }
} else {
- $that->emit('error', $e->errorInfo[2]);
+ $that->emit('error', $e->getMessage());
}
} else {
- $that->emit('error', $e);
+ dump($e);
}
}
}
diff --git a/resources/views/components/inputs/button.blade.php b/resources/views/components/inputs/button.blade.php
index 8b9e5cd61f..db2314256a 100644
--- a/resources/views/components/inputs/button.blade.php
+++ b/resources/views/components/inputs/button.blade.php
@@ -1,19 +1,25 @@
@props([
'isWarning' => null,
+ 'disabled' => null,
'defaultClass' => 'text-white bg-neutral-800 hover:bg-violet-600 h-8',
'defaultWarningClass' => 'text-white bg-red-500 hover:bg-red-600 h-8',
+ 'disabledClass' => 'text-neutral-400 bg-neutral-900 h-8',
'loadingClass' => 'text-black bg-green-500 h-8',
'confirm' => null,
'confirmAction' => null,
])
<button {{ $attributes }} @class([
- $defaultClass => !$confirm && !$isWarning,
- $defaultWarningClass => $confirm || $isWarning,
-]) @if ($attributes->whereStartsWith('wire:click'))
+ $defaultClass => !$confirm && !$isWarning && !$disabled,
+ $defaultWarningClass => ($confirm || $isWarning) && !$disabled,
+ $disabledClass => $disabled,
+]) @if ($attributes->whereStartsWith('wire:click') && !$disabled)
wire:target="{{ explode('(', $attributes->whereStartsWith('wire:click')->first())[0] }}"
wire:loading.delay.class="{{ $loadingClass }}" wire:loading.delay.attr="disabled"
wire:loading.delay.class.remove="{{ $defaultClass }} {{ $attributes->whereStartsWith('class')->first() }}"
@endif
+ @if ($disabled !== null)
+ disabled title="{{ $disabled }}"
+ @endif
@isset($confirm)
x-on:click="toggleConfirmModal('{{ $confirm }}', '{{ explode('(', $confirmAction)[0] }}')"
@endisset
diff --git a/resources/views/livewire/project/delete.blade.php b/resources/views/livewire/project/delete.blade.php
new file mode 100644
index 0000000000..3c07ee77e0
--- /dev/null
+++ b/resources/views/livewire/project/delete.blade.php
@@ -0,0 +1,13 @@
+<div x-data="{ deleteProject: false }">
+ <x-naked-modal show="deleteProject" message='Are you sure you would like to delete this project?' />
+ @if ($resource_count > 0)
+ <x-inputs.button isWarning disabled="First delete all resources.">
+ Delete
+ </x-inputs.button>
+ @else
+ <x-inputs.button isWarning x-on:click.prevent="deleteProject = true">
+ Delete
+ </x-inputs.button>
+ @endif
+
+</div>
diff --git a/resources/views/livewire/project/new/github-private-repository.blade.php b/resources/views/livewire/project/new/github-private-repository.blade.php
index cfb6dc268b..2d162aba85 100644
--- a/resources/views/livewire/project/new/github-private-repository.blade.php
+++ b/resources/views/livewire/project/new/github-private-repository.blade.php
@@ -2,7 +2,9 @@
@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>
+ <x-inputs.button wire:key="{{ $ghapp->id }}" wire:click="loadRepositories({{ $ghapp->id }})">
+ {{ $ghapp->name }}
+ </x-inputs.button>
@endforeach
<div>
@if ($repositories->count() > 0)
diff --git a/resources/views/livewire/source/github/change.blade.php b/resources/views/livewire/source/github/change.blade.php
index 3e73a115b2..0f6fbc2ff0 100644
--- a/resources/views/livewire/source/github/change.blade.php
+++ b/resources/views/livewire/source/github/change.blade.php
@@ -21,6 +21,9 @@
<x-inputs.input id="github_app.client_secret" label="Client Secret" type="password" disabled />
<x-inputs.input id="github_app.webhook_secret" label="Webhook Secret" type="password" disabled />
<x-inputs.button type="submit">Save</x-inputs.button>
+ <x-inputs.button isWarning x-on:click.prevent="deleteSource = true">
+ Delete
+ </x-inputs.button>
@else
<div class="py-2">
<x-inputs.button type="submit">Save</x-inputs.button>
@@ -30,6 +33,4 @@
</div>
@endif
</form>
-
-
</div>
diff --git a/resources/views/project/new.blade.php b/resources/views/project/new.blade.php
index 2c14aa1e9b..14ab4b1663 100644
--- a/resources/views/project/new.blade.php
+++ b/resources/views/project/new.blade.php
@@ -18,7 +18,7 @@
<livewire:project.new.public-git-repository :type="$type" />
</div>
<div x-cloak x-show="activeTab === 'github-private-repo'">
- <livewire:project.new.github-private-repository />
+ <livewire:project.new.github-private-repository :type="$type" />
</div>
</div>
</x-layout>
diff --git a/resources/views/project/resources.blade.php b/resources/views/project/resources.blade.php
index 113e78dc50..bd8a3fbb5a 100644
--- a/resources/views/project/resources.blade.php
+++ b/resources/views/project/resources.blade.php
@@ -1,8 +1,14 @@
<x-layout>
- <h1>Resources <a href="{{ route('project.resources.new', Route::current()->parameters()) }}">
+ <div class="flex items-center gap-2">
+ <h1>Resources</h1>
+ <a href="{{ route('project.resources.new', Route::current()->parameters()) }}">
<x-inputs.button>New</x-inputs.button>
</a>
- </h1>
+ <livewire:project.delete :project_id="$project->id" :resource_count="$project->applications->count()" />
+ </div>
+ @if ($environment->applications->count() === 0)
+ <p>No resources yet.</p>
+ @endif
<div>
@foreach ($environment->applications as $application)
<p>
diff --git a/resources/views/source/github/show.blade.php b/resources/views/source/github/show.blade.php
index fecc66deb5..b0d705a43d 100644
--- a/resources/views/source/github/show.blade.php
+++ b/resources/views/source/github/show.blade.php
@@ -25,13 +25,14 @@
name,
url: baseUrl,
hook_attributes: {
- url: `${webhookBaseUrl}/github/events`
+ url: `${webhookBaseUrl}/source/github/events`,
+ active: true,
},
- redirect_url: `${webhookBaseUrl}/github`,
+ redirect_url: `${webhookBaseUrl}/source/github/redirect`,
callback_urls: [`${baseUrl}/login/github/app`],
public: false,
request_oauth_on_install: false,
- setup_url: `${webhookBaseUrl}/github/install?source=${uuid}`,
+ setup_url: `${webhookBaseUrl}/source/github/install?source=${uuid}`,
setup_on_update: true,
default_permissions: {
contents: 'read',
@@ -54,5 +55,14 @@
form.submit();
}
</script>
+ @elseif($github_app->app_id && !$github_app->installation_id)
+ <a href="{{ $installation_url }}">
+ <x-inputs.button>Install Repositories</x-inputs.button>
+ </a>
+ @elseif($github_app->app_id && $github_app->installation_id)
+ <a href="{{ $installation_url }}">
+ <x-inputs.button>Update Repositories</x-inputs.button>
+ </a>
@endif
+
</x-layout>
diff --git a/routes/api.php b/routes/api.php
index 501d5aa19c..07528bf576 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -1,5 +1,6 @@
<?php
+use App\Models\GithubApp;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
@@ -17,6 +18,13 @@ use Illuminate\Support\Facades\Route;
Route::get('/health', function () {
return 'OK';
});
+Route::get('/webhooks/source/github/redirect', function () {
+ $code = request()->get('code');
+ $state = request()->get('state');
+ $github_app = GithubApp::where('uuid', $state)->firstOrFail();
+ return 'OK';
+});
+
// Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
// return $request->user();
// });
diff --git a/routes/web.php b/routes/web.php
index bc697d1aa4..d0910b0a8b 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -85,16 +85,20 @@ Route::middleware(['auth'])->group(function () {
Route::middleware(['auth'])->group(function () {
Route::get('/source/new', fn () => view('source.new'))->name('source.new');
Route::get('/source/github/{github_app_uuid}', function (Request $request) {
+ $github_app = GithubApp::where('uuid', request()->github_app_uuid)->first();
+ $name = Str::kebab('coolify' . $github_app->name);
$settings = InstanceSettings::first();
$host = $request->schemeAndHttpHost();
if ($settings->fqdn) {
$host = $settings->fqdn;
}
- $github_app = GithubApp::where('uuid', request()->github_app_uuid)->first();
+ $installation_path = $github_app->html_url === 'https://github.com' ? 'apps' : 'github-apps';
+ $installation_url = "$github_app->html_url/$installation_path/$name/installations/new";
return view('source.github.show', [
'github_app' => $github_app,
'host' => $host,
- 'name' => Str::kebab('coolify' . $github_app->name)
+ 'name' => $name,
+ 'installation_url' => $installation_url,
]);
})->name('source.github.show');
});
diff --git a/routes/webhooks.php b/routes/webhooks.php
new file mode 100644
index 0000000000..54d51dfb7e
--- /dev/null
+++ b/routes/webhooks.php
@@ -0,0 +1,52 @@
+<?php
+
+use App\Models\PrivateKey;
+use App\Models\GithubApp;
+use Illuminate\Support\Facades\Http;
+use Illuminate\Support\Facades\Route;
+
+Route::get('/source/github/redirect', function () {
+ try {
+ $code = request()->get('code');
+ $state = request()->get('state');
+ $github_app = GithubApp::where('uuid', $state)->firstOrFail();
+ $api_url = data_get($github_app, 'api_url');
+ $data = Http::withBody(null)->accept('application/vnd.github+json')->post("$api_url/app-manifests/$code/conversions")->throw()->json();
+ $id = data_get($data, 'id');
+ $slug = data_get($data, 'slug');
+ $client_id = data_get($data, 'client_id');
+ $client_secret = data_get($data, 'client_secret');
+ $private_key = data_get($data, 'pem');
+ $webhook_secret = data_get($data, 'webhook_secret');
+ $private_key = PrivateKey::create([
+ 'name' => $slug,
+ 'private_key' => $private_key,
+ 'team_id' => $github_app->team_id
+ ]);
+ $github_app->app_id = $id;
+ $github_app->client_id = $client_id;
+ $github_app->client_secret = $client_secret;
+ $github_app->webhook_secret = $webhook_secret;
+ $github_app->private_key_id = $private_key->id;
+ $github_app->save();
+ return redirect()->route('source.github.show', ['github_app_uuid' => $github_app->uuid]);
+ } catch (\Exception $e) {
+ return generalErrorHandler($e);
+ }
+});
+
+Route::get('/source/github/install', function () {
+ try {
+ $installation_id = request()->get('installation_id');
+ $source = request()->get('source');
+ $setup_action = request()->get('setup_action');
+ $github_app = GithubApp::where('uuid', $source)->firstOrFail();
+ if ($setup_action === 'install') {
+ $github_app->installation_id = $installation_id;
+ $github_app->save();
+ }
+ return redirect()->route('source.github.show', ['github_app_uuid' => $github_app->uuid]);
+ } catch (\Exception $e) {
+ return generalErrorHandler($e);
+ }
+});