Merge branch 'v4-next' into notifications

This commit is contained in:
Andras Bacsai
2023-06-01 08:22:07 +02:00
204 changed files with 5744 additions and 2545 deletions

View File

@@ -81,7 +81,7 @@ Route::middleware(['auth'])->group(function () {
if ($is_new_project) {
$project = Project::create([
'name' => request()->query('name') ?? generateRandomName(),
'name' => request()->query('name') ?? generate_random_name(),
'team_id' => $id,
]);
return response()->json([
@@ -92,7 +92,7 @@ Route::middleware(['auth'])->group(function () {
$environment = Project::where('uuid', request()->query('project'))->first()->environments->where('name', request()->query('name'))->first();
if (!$environment) {
$environment = Environment::create([
'name' => request()->query('name') ?? generateRandomName(),
'name' => request()->query('name') ?? generate_random_name(),
'project_id' => Project::where('uuid', request()->query('project'))->first()->id,
]);
}
@@ -104,7 +104,7 @@ Route::middleware(['auth'])->group(function () {
'magic' => true,
]);
} catch (\Throwable $e) {
return generalErrorHandler($e, isJson: true);
return general_error_handler($e, isJson: true);
}
});
Route::get('/', function () {
@@ -133,7 +133,7 @@ Route::middleware(['auth'])->group(function () {
})->name('team');
Route::get('/settings', function () {
$isRoot = auth()->user()->isRoot();
$isRoot = auth()->user()->isPartOfRootTeam();
if ($isRoot) {
$settings = InstanceSettings::get();
return view('settings', [
@@ -189,6 +189,9 @@ Route::middleware(['auth'])->group(function () {
})->name('source.github.show');
});
Route::middleware(['auth'])->group(function () {
Route::get('/servers', fn () => view('servers', [
'servers' => Server::validated(),
]))->name('servers');
Route::get('/server/new', fn () => view('server.new', [
'private_keys' => PrivateKey::where('team_id', session('currentTeam')->id)->get(),
]))->name('server.new');
@@ -235,10 +238,15 @@ Route::middleware(['auth'])->group(function () {
});
Route::middleware(['auth'])->group(function () {
Route::get(
'/projects',
[ProjectController::class, 'all']
)->name('projects');
Route::get(
'/project/{project_uuid}',
[ProjectController::class, 'environments']
)->name('project.environments');
[ProjectController::class, 'show']
)->name('project.show');
Route::get(
'/project/{project_uuid}/{environment_name}/new',

View File

@@ -1,7 +1,8 @@
<?php
use App\Jobs\DeployApplicationJob;
use App\Jobs\ApplicationDeploymentJob;
use App\Models\Application;
use App\Models\ApplicationPreview;
use App\Models\PrivateKey;
use App\Models\GithubApp;
use App\Models\GithubEventsApplications;
@@ -36,7 +37,7 @@ Route::get('/source/github/redirect', function () {
$github_app->save();
return redirect()->route('source.github.show', ['github_app_uuid' => $github_app->uuid]);
} catch (\Exception $e) {
return generalErrorHandler($e);
return general_error_handler($e);
}
});
@@ -52,7 +53,7 @@ Route::get('/source/github/install', function () {
}
return redirect()->route('source.github.show', ['github_app_uuid' => $github_app->uuid]);
} catch (\Exception $e) {
return generalErrorHandler($e);
return general_error_handler($e);
}
});
Route::post('/source/github/events', function () {
@@ -85,26 +86,86 @@ Route::post('/source/github/events', function () {
if (Str::isMatch('/refs\/heads\/*/', $branch)) {
$branch = Str::after($branch, 'refs/heads/');
}
ray('Webhook GitHub Push Event: ' . $id . ' with branch: ' . $branch);
}
if ($x_github_event === 'pull_request') {
$id = data_get($payload, 'pull_request.base.repo.id');
$branch = data_get($payload, 'pull_request.base.ref');
$action = data_get($payload, 'action');
$id = data_get($payload, 'repository.id');
$pull_request_id = data_get($payload, 'number');
$pull_request_html_url = data_get($payload, 'pull_request.html_url');
$branch = data_get($payload, 'pull_request.head.ref');
$base_branch = data_get($payload, 'pull_request.base.ref');
ray('Webhook GitHub Pull Request Event: ' . $id . ' with branch: ' . $branch . ' and base branch: ' . $base_branch . ' and pull request id: ' . $pull_request_id);
}
if (!$id || !$branch) {
return response('not cool');
return response('Nothing to do. No id or branch found.');
}
$applications = Application::where('repository_project_id', $id);
if ($x_github_event === 'push') {
$applications = $applications->where('git_branch', $branch)->get();
}
if ($x_github_event === 'pull_request') {
$applications = $applications->where('git_branch', $base_branch)->get();
}
if ($applications->isEmpty()) {
return response('Nothing to do. No applications found.');
}
$applications = Application::where('repository_project_id', $id)->where('git_branch', $branch)->get();
foreach ($applications as $application) {
if ($application->isDeployable()) {
$deployment_uuid = new Cuid2(7);
dispatch(new DeployApplicationJob(
deployment_uuid: $deployment_uuid,
application_uuid: $application->uuid,
force_rebuild: false,
));
if ($x_github_event === 'push') {
if ($application->isDeployable()) {
ray('Deploying ' . $application->name . ' with branch ' . $branch);
$deployment_uuid = new Cuid2(7);
queue_application_deployment(
application_id: $application->id,
deployment_uuid: $deployment_uuid,
force_rebuild: false,
is_webhook: true
);
} else {
ray('Deployments disabled for ' . $application->name);
}
}
if ($x_github_event === 'pull_request') {
if ($action === 'opened') {
if ($application->isPRDeployable()) {
$deployment_uuid = new Cuid2(7);
$found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();
if (!$found) {
ApplicationPreview::create([
'application_id' => $application->id,
'pull_request_id' => $pull_request_id,
'pull_request_html_url' => $pull_request_html_url
]);
}
queue_application_deployment(
application_id: $application->id,
pull_request_id: $pull_request_id,
deployment_uuid: $deployment_uuid,
force_rebuild: false,
is_webhook: true
);
ray('Deploying preview for ' . $application->name . ' with branch ' . $branch . ' and base branch ' . $base_branch . ' and pull request id ' . $pull_request_id);
return response('Preview Deployment queued.');
} else {
ray('Preview deployments disabled for ' . $application->name);
return response('Nothing to do. Preview Deployments disabled.');
}
}
if ($action === 'closed') {
$found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();
if ($found) {
$found->delete();
$container_name = generate_container_name($application->uuid, $pull_request_id);
ray('Stopping container: ' . $container_name);
remote_process(["docker rm -f $container_name"], $application->destination->server);
return response('Preview Deployment closed.');
}
return response('Nothing to do. No Preview Deplyoment found');
}
}
}
} catch (\Exception $e) {
return generalErrorHandler($e);
return general_error_handler($e);
}
});