Skip to content
Snippets Groups Projects
Select Git revision
  • 83c634cb6df1c6546ce2414a56a9ea25ccae11a2
  • main default protected
  • issue-56
  • testing-systems
4 results

hooks.php

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    hooks.php 2.13 KiB
    <?php
    use TracToGitlab\EventController;
    use TracToGitlab\EventHandler;
    use TracToGitlab\EventHandlers;
    
    final class HooksController extends EventController
    {
        public function system_action()
        {
            if (!$this->verifySecret(Config::get()->getValue('TRAC2GITLAB_GITLAB_SYSTEMHOOK_SECRET'))) {
                throw new AccessDeniedException();
            }
    
            if (!$this->verifyEvent()) {
                throw new Exception('This is not a valid gitlab event');
            }
    
            $this->processHooks($this->getKnownSystemhooks());
        }
    
        /**
         * @return iterator|EventHandler[]
         */
        private function getKnownSystemhooks(): iterator
        {
            yield app(EventHandlers\UserCreated::class);
        }
    
        public function web_action()
        {
            if (!$this->verifySecret(Config::get()->getValue('TRAC2GITLAB_GITLAB_WEBHOOK_SECRET'))) {
                throw new AccessDeniedException();
            }
    
            $payload = $this->payload();
    
            if (
                !$this->verifyEvent()
                || !isset($payload['project']['id'])
                || $payload['project']['id'] != $this->gitlab_project_id
            ) {
                $this->set_status(500);
                $this->render_json([
                    'payload' => $payload['project']['id'] ?? 'NONE!',
                    'id' => $this->gitlab_project_id,
                    'result' => $this->verifyEvent(),
                    'event' => $_SERVER['HTTP_X_GITLAB_EVENT'] ?? 'NONE',
                    'server' => $_SERVER,
                    'names' => [
                        'event' => $payload['event_name'],
                        'object_kind' => $payload['object_kind'],
                    ],
                ]);
                return;
                throw new Exception('This is not a valid gitlab event');
            }
    
            $this->processHooks($this->getKnownWebhooks());
        }
    
        /**
         * @return iterator|EventHandler[]
         */
        private function getKnownWebhooks(): iterator
        {
            yield app(EventHandlers\AllAutomaticJobsHaveSucceeded::class);
            yield app(EventHandlers\BranchDeleted::class);
            yield app(EventHandlers\BuildImageJobSucceeded::class);
            yield app(EventHandlers\IssueCreated::class);
        }
    }