feat: installation/update github apps

This commit is contained in:
Andras Bacsai
2023-05-09 11:33:50 +02:00
parent db92dc3636
commit 19ad184cd6
20 changed files with 179 additions and 35 deletions

View File

@@ -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();
// });

View File

@@ -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');
});

52
routes/webhooks.php Normal file
View File

@@ -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);
}
});