Merge pull request #1550 from coollabsio/next
refactor: gitlab manual webhooks
This commit is contained in:
commit
40a239ddda
@ -7,7 +7,7 @@ return [
|
|||||||
|
|
||||||
// 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.163',
|
'release' => '4.0.0-beta.164',
|
||||||
// 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'),
|
||||||
|
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
return '4.0.0-beta.163';
|
return '4.0.0-beta.164';
|
||||||
|
@ -65,9 +65,9 @@ Route::get('/source/github/install', function () {
|
|||||||
});
|
});
|
||||||
Route::post('/source/gitlab/events/manual', function () {
|
Route::post('/source/gitlab/events/manual', function () {
|
||||||
try {
|
try {
|
||||||
|
$return_payloads = collect([]);
|
||||||
$payload = request()->collect();
|
$payload = request()->collect();
|
||||||
$headers = request()->headers->all();
|
$headers = request()->headers->all();
|
||||||
ray($payload, $headers);
|
|
||||||
$x_gitlab_token = data_get($headers, 'x-gitlab-token.0');
|
$x_gitlab_token = data_get($headers, 'x-gitlab-token.0');
|
||||||
$x_gitlab_event = data_get($payload, 'object_kind');
|
$x_gitlab_event = data_get($payload, 'object_kind');
|
||||||
if ($x_gitlab_event === 'push') {
|
if ($x_gitlab_event === 'push') {
|
||||||
@ -77,20 +77,27 @@ Route::post('/source/gitlab/events/manual', function () {
|
|||||||
$branch = Str::after($branch, 'refs/heads/');
|
$branch = Str::after($branch, 'refs/heads/');
|
||||||
}
|
}
|
||||||
if (!$branch) {
|
if (!$branch) {
|
||||||
return response('Nothing to do. No branch found in the request.');
|
$return_payloads->push([
|
||||||
|
'status' => 'failed',
|
||||||
|
'message' => 'Nothing to do. No branch found in the request.',
|
||||||
|
]);
|
||||||
|
return response($return_payloads);
|
||||||
}
|
}
|
||||||
ray('Manual Webhook GitLab Push Event with branch: ' . $branch);
|
ray('Manual Webhook GitLab Push Event with branch: ' . $branch);
|
||||||
}
|
}
|
||||||
if ($x_gitlab_event === 'merge_request') {
|
if ($x_gitlab_event === 'merge_request') {
|
||||||
$action = data_get($payload, 'object_attributes.action');
|
$action = data_get($payload, 'object_attributes.action');
|
||||||
ray($action);
|
|
||||||
$branch = data_get($payload, 'object_attributes.source_branch');
|
$branch = data_get($payload, 'object_attributes.source_branch');
|
||||||
$base_branch = data_get($payload, 'object_attributes.target_branch');
|
$base_branch = data_get($payload, 'object_attributes.target_branch');
|
||||||
$full_name = data_get($payload, 'project.path_with_namespace');
|
$full_name = data_get($payload, 'project.path_with_namespace');
|
||||||
$pull_request_id = data_get($payload, 'object_attributes.iid');
|
$pull_request_id = data_get($payload, 'object_attributes.iid');
|
||||||
$pull_request_html_url = data_get($payload, 'object_attributes.url');
|
$pull_request_html_url = data_get($payload, 'object_attributes.url');
|
||||||
if (!$branch) {
|
if (!$branch) {
|
||||||
return response('Nothing to do. No branch found in the request.');
|
$return_payloads->push([
|
||||||
|
'status' => 'failed',
|
||||||
|
'message' => 'Nothing to do. No branch found in the request.',
|
||||||
|
]);
|
||||||
|
return response($return_payloads);
|
||||||
}
|
}
|
||||||
ray('Webhook GitHub Pull Request Event with branch: ' . $branch . ' and base branch: ' . $base_branch . ' and pull request id: ' . $pull_request_id);
|
ray('Webhook GitHub Pull Request Event with branch: ' . $branch . ' and base branch: ' . $base_branch . ' and pull request id: ' . $pull_request_id);
|
||||||
}
|
}
|
||||||
@ -98,23 +105,41 @@ Route::post('/source/gitlab/events/manual', function () {
|
|||||||
if ($x_gitlab_event === 'push') {
|
if ($x_gitlab_event === 'push') {
|
||||||
$applications = $applications->where('git_branch', $branch)->get();
|
$applications = $applications->where('git_branch', $branch)->get();
|
||||||
if ($applications->isEmpty()) {
|
if ($applications->isEmpty()) {
|
||||||
return response("Nothing to do. No applications found with deploy key set, branch is '$branch' and Git Repository name has $full_name.");
|
$return_payloads->push([
|
||||||
|
'status' => 'failed',
|
||||||
|
'message' => "Nothing to do. No applications found with deploy key set, branch is '$branch' and Git Repository name has $full_name.",
|
||||||
|
]);
|
||||||
|
return response($return_payloads);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($x_gitlab_event === 'merge_request') {
|
if ($x_gitlab_event === 'merge_request') {
|
||||||
$applications = $applications->where('git_branch', $base_branch)->get();
|
$applications = $applications->where('git_branch', $base_branch)->get();
|
||||||
if ($applications->isEmpty()) {
|
if ($applications->isEmpty()) {
|
||||||
return response("Nothing to do. No applications found with branch '$base_branch'.");
|
$return_payloads->push([
|
||||||
|
'status' => 'failed',
|
||||||
|
'message' => "Nothing to do. No applications found with branch '$base_branch'.",
|
||||||
|
]);
|
||||||
|
return response($return_payloads);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach ($applications as $application) {
|
foreach ($applications as $application) {
|
||||||
$webhook_secret = data_get($application, 'manual_webhook_secret_gitlab');
|
$webhook_secret = data_get($application, 'manual_webhook_secret_gitlab');
|
||||||
if ($webhook_secret !== $x_gitlab_token) {
|
if ($webhook_secret !== $x_gitlab_token) {
|
||||||
|
$return_payloads->push([
|
||||||
|
'application' => $application->name,
|
||||||
|
'status' => 'failed',
|
||||||
|
'message' => 'Invalid token.',
|
||||||
|
]);
|
||||||
ray('Invalid signature');
|
ray('Invalid signature');
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$isFunctional = $application->destination->server->isFunctional();
|
$isFunctional = $application->destination->server->isFunctional();
|
||||||
if (!$isFunctional) {
|
if (!$isFunctional) {
|
||||||
|
$return_payloads->push([
|
||||||
|
'application' => $application->name,
|
||||||
|
'status' => 'failed',
|
||||||
|
'message' => 'Server is not functional',
|
||||||
|
]);
|
||||||
ray('Server is not functional: ' . $application->destination->server->name);
|
ray('Server is not functional: ' . $application->destination->server->name);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -129,6 +154,11 @@ Route::post('/source/gitlab/events/manual', function () {
|
|||||||
is_webhook: true
|
is_webhook: true
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
$return_payloads->push([
|
||||||
|
'application' => $application->name,
|
||||||
|
'status' => 'failed',
|
||||||
|
'message' => 'Deployments disabled',
|
||||||
|
]);
|
||||||
ray('Deployments disabled for ' . $application->name);
|
ray('Deployments disabled for ' . $application->name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -154,25 +184,48 @@ Route::post('/source/gitlab/events/manual', function () {
|
|||||||
git_type: 'gitlab'
|
git_type: 'gitlab'
|
||||||
);
|
);
|
||||||
ray('Deploying preview for ' . $application->name . ' with branch ' . $branch . ' and base branch ' . $base_branch . ' and pull request id ' . $pull_request_id);
|
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.');
|
$return_payloads->push([
|
||||||
|
'application' => $application->name,
|
||||||
|
'status' => 'success',
|
||||||
|
'message' => 'Preview Deployment queued',
|
||||||
|
]);
|
||||||
} else {
|
} else {
|
||||||
|
$return_payloads->push([
|
||||||
|
'application' => $application->name,
|
||||||
|
'status' => 'failed',
|
||||||
|
'message' => 'Preview deployments disabled',
|
||||||
|
]);
|
||||||
ray('Preview deployments disabled for ' . $application->name);
|
ray('Preview deployments disabled for ' . $application->name);
|
||||||
return response('Nothing to do. Preview Deployments disabled.');
|
|
||||||
}
|
}
|
||||||
}
|
} else if ($action === 'closed') {
|
||||||
if ($action === 'closed') {
|
|
||||||
$found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();
|
$found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();
|
||||||
if ($found) {
|
if ($found) {
|
||||||
$found->delete();
|
$found->delete();
|
||||||
$container_name = generateApplicationContainerName($application, $pull_request_id);
|
$container_name = generateApplicationContainerName($application, $pull_request_id);
|
||||||
// ray('Stopping container: ' . $container_name);
|
// ray('Stopping container: ' . $container_name);
|
||||||
instant_remote_process(["docker rm -f $container_name"], $application->destination->server);
|
instant_remote_process(["docker rm -f $container_name"], $application->destination->server);
|
||||||
return response('Preview Deployment closed.');
|
$return_payloads->push([
|
||||||
|
'application' => $application->name,
|
||||||
|
'status' => 'success',
|
||||||
|
'message' => 'Preview Deployment closed',
|
||||||
|
]);
|
||||||
|
return response($return_payloads);
|
||||||
}
|
}
|
||||||
return response('Nothing to do. No Preview Deployment found');
|
$return_payloads->push([
|
||||||
|
'application' => $application->name,
|
||||||
|
'status' => 'failed',
|
||||||
|
'message' => 'No Preview Deployment found',
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
$return_payloads->push([
|
||||||
|
'application' => $application->name,
|
||||||
|
'status' => 'failed',
|
||||||
|
'message' => 'No action found. Contact us for debugging.',
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return response($return_payloads);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
ray($e->getMessage());
|
ray($e->getMessage());
|
||||||
return handleError($e);
|
return handleError($e);
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
"version": "3.12.36"
|
"version": "3.12.36"
|
||||||
},
|
},
|
||||||
"v4": {
|
"v4": {
|
||||||
"version": "4.0.0-beta.163"
|
"version": "4.0.0-beta.164"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user