diff --git a/.env.example b/.env.example index 2984ebd..0a2a8ff 100644 --- a/.env.example +++ b/.env.example @@ -42,3 +42,6 @@ MAILJET_APISECRET= MAILJET_SMSTOKEN= MAILJET_SMSENDPOINT= MAILJET_SMSFROM= + +WEBHOOKS_GITLAB_NOTIFY= +WEBHOOKS_GITLAB_TOKEN= \ No newline at end of file diff --git a/app/Http/Controllers/Webhooks/GitlabController.php b/app/Http/Controllers/Webhooks/GitlabController.php new file mode 100644 index 0000000..64845b2 --- /dev/null +++ b/app/Http/Controllers/Webhooks/GitlabController.php @@ -0,0 +1,40 @@ +event_type; + if ($event_type === 'merge_request') { + // Si le state est <> de opened on ne fait rien + if ($request->object_attributes['state'] !== 'opened') { + exit; + } + // Si l'action est set et egale à approved on ne fait rien + if (isset($request->object_attributes['action']) && $request->object_attributes['action'] === 'approved') { + exit; + } + + $datas = []; + $datas['user'] = $request->user['name']; + $datas['project_name'] = $request->project['name']; + $datas['project_path'] = $request->project['path_with_namespace']; + $datas['attributes_description'] = $request->object_attributes['description']; + $datas['attributes_status'] = $request->object_attributes['merge_status']; + $datas['attributes_request_status'] = $request->object_attributes['state']; + $datas['attributes_source_branch'] = $request->object_attributes['source_branch']; + $datas['attributes_target_branch'] = $request->object_attributes['target_branch']; + $datas['attributes_url'] = $request->object_attributes['url']; + $datas['last_commit_author'] = $request->object_attributes['last_commit']['author']['name']; + + Mail::to(\explode(',', config('webhooks.gitlab.notify')))->send(new Gitlab($datas)); + } + } +} diff --git a/app/Http/Middleware/VerifyCsrfToken.php b/app/Http/Middleware/VerifyCsrfToken.php index 324a166..554b13f 100644 --- a/app/Http/Middleware/VerifyCsrfToken.php +++ b/app/Http/Middleware/VerifyCsrfToken.php @@ -19,6 +19,6 @@ class VerifyCsrfToken extends Middleware * @var array */ protected $except = [ - // + 'webhooks/*' ]; } diff --git a/app/Http/Requests/Webhooks/GitlabRequest.php b/app/Http/Requests/Webhooks/GitlabRequest.php new file mode 100644 index 0000000..28e1761 --- /dev/null +++ b/app/Http/Requests/Webhooks/GitlabRequest.php @@ -0,0 +1,30 @@ +header('X-Gitlab-Token') !== null && $this->header('X-Gitlab-Token') === config('webhooks.gitlab.token'); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return [ + 'event_type' => 'required|in:merge_request' + ]; + } +} diff --git a/app/Mail/Webhooks/Gitlab.php b/app/Mail/Webhooks/Gitlab.php new file mode 100644 index 0000000..8e9b478 --- /dev/null +++ b/app/Mail/Webhooks/Gitlab.php @@ -0,0 +1,35 @@ +datas = $datas; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + return $this->text('emails.webhooks.gitlab'); + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index e413ca9..54c51c7 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -24,8 +24,6 @@ class AppServiceProvider extends ServiceProvider */ public function boot() { - if (!app()->environment('local')) { - URL::forceScheme('https'); - } + URL::forceScheme('https'); } } diff --git a/config/webhooks.php b/config/webhooks.php new file mode 100644 index 0000000..b697af9 --- /dev/null +++ b/config/webhooks.php @@ -0,0 +1,8 @@ + [ + 'notify' => env('WEBHOOKS_GITLAB_NOTIFY', 'equipe.web@ldc.fr'), + 'token' => env('WEBHOOKS_GITLAB_TOKEN') + ] +]; diff --git a/resources/views/emails/exception.blade.php b/resources/views/emails/exception.blade.php index e69347a..37f22f5 100644 --- a/resources/views/emails/exception.blade.php +++ b/resources/views/emails/exception.blade.php @@ -1,7 +1,7 @@
-