/**
  * Provide auto merge
  */
 public function actionDefault()
 {
     $payload = $this->httpRequest->getRawBody();
     if (!$payload) {
         Debugger::log('No payload data', Debugger::ERROR);
         $this->terminate();
     }
     $data = json_decode($payload, true);
     if (!$data) {
         Debugger::log('json_decode error', Debugger::ERROR);
         $this->terminate();
     }
     if ($data['object_kind'] != 'note') {
         Debugger::log('Only notes object kind processing now. *' . $data['object_kind'] . '* given', Debugger::ERROR);
         $this->terminate();
     }
     $projectId = isset($data['merge_request']['source_project_id']) ? $data['merge_request']['source_project_id'] : false;
     $mergeRequestId = isset($data['merge_request']['id']) ? $data['merge_request']['id'] : false;
     if (!$projectId || !$mergeRequestId) {
         Debugger::log('projectId or mergeRequestId missing', Debugger::ERROR);
         $this->terminate();
     }
     $project = $this->projectRepository->findByGitlabId($projectId);
     if (!$project) {
         Debugger::log('Project ' . $projectId . ' is not allowed to auto merge', Debugger::ERROR);
         $this->terminate();
     }
     $mr = $this->gitlabClient->api('mr')->show($projectId, $mergeRequestId);
     $mergeRequest = $this->mergeRequestBuilder->create($mr, $data);
     if ($mergeRequest->canBeAutoMerged($project->positive_votes)) {
         $this->gitlabClient->api('mr')->merge($projectId, $mergeRequestId, 'Auto merged');
     }
 }
 /**
  * Search projects at gitlab
  * @param string $text
  */
 public function actionSearch($text)
 {
     $enabledProjects = [];
     foreach ($this->projectRepository->findAll() as $enabledProjectsRecord) {
         $enabledProjects[$enabledProjectsRecord->gitlab_id] = $enabledProjectsRecord;
     }
     $this->template->enabledProjects = $enabledProjects;
     $this->template->search = $text;
     $this->template->projects = $this->gitlabClient->api('projects')->search($text);
 }
 /**
  * Processing form
  * @param Form $form
  * @param ArrayHash $values
  */
 public function formSuccess($form, $values)
 {
     try {
         if ($this->project) {
             $this->project->update($values);
         } elseif ($this->gitlabId) {
             $this->projectRepository->insert(['gitlab_id' => $this->gitlabId, 'url' => $this->gitlabUrl, 'name' => $this->gitlabName, 'positive_votes' => $values['positive_votes'], 'delete_source_branch' => $values['delete_source_branch']]);
         }
     } catch (PDOException $e) {
         Debugger::log($e, Debugger::ERROR);
         $form->addError('Database error occurred');
         return;
     }
     $this->onSuccess();
 }
 /**
  * Manual merge request accept
  * @param int $projectId
  * @param int $gitlabMergeRequestId
  */
 public function handleAccept($projectId, $gitlabMergeRequestId)
 {
     $project = $this->projectRepository->find($projectId);
     if (!$project) {
         $this->flashMessage('This project is not enabled to do auto merge', 'danger');
         $this->redirect('default', $projectId);
     }
     $mergeRequest = $this->gitlabClient->api('mr')->show($project->gitlab_id, $gitlabMergeRequestId);
     if (!isset($mergeRequest['id'])) {
         $this->flashMessage('Cannot fetch merge request from GitLab', 'danger');
         $this->redirect('default', $projectId);
     }
     $mergeResult = $this->gitlabClient->api('mr')->merge($mergeRequest['project_id'], $mergeRequest['id'], 'Merged via browser');
     if (!isset($mergeResult['state']) || $mergeResult['state'] != 'merged') {
         $this->flashMessage('Merge was unsuccessful', 'danger');
         $this->redirect('default', $projectId);
     }
     if ($project->delete_source_branch) {
         $this->gitlabClient->api('repositories')->deleteBranch($mergeResult['project_id'], $mergeResult['source_branch']);
     }
     $this->flashMessage('Merge request accepted', 'success');
     $this->redirect('default', $projectId);
 }