/**
  * 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');
     }
 }
 /**
  * Add webhook needed for auto merging
  * @param int $id
  */
 public function handleAddWebhook($id)
 {
     $webhookUrl = $this->settingsRepository->getGitlabMergerUrl() ? $this->settingsRepository->getGitlabMergerUrl() . '/webhook' : $this->link('//Webhook:default');
     // @TODO: when gitLab api will return note_events in webhook definition check for duplicates
     $result = $this->gitlabClient->api('projects')->addHook($id, $webhookUrl, ['note_events' => true, 'push_events' => false, 'issues_events' => false, 'merge_requests_events' => false, 'tag_push_events' => false]);
     isset($result['id']) ? $this->flashMessage('Webhook successfully added', 'success') : $this->flashMessage('Failed to add webhook', 'danger');
     $this->redirect('this');
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function findProjects($name)
 {
     return $this->requestProjects($name, function () {
         $api = $this->client->api('projects');
         /* @var Projects $api */
         return $api->accessible(1, 9999);
     }, 'path_with_namespace');
 }
Exemplo n.º 4
0
 /**
  * Get the file contents
  *
  * @param string $file
  * @param string $branch
  *
  * @return string
  */
 public function getFileContents($file, $branch = 'master')
 {
     try {
         $fileContent = $this->client->api('repositories')->getFile($this->project->getVendorName() . "/" . $this->project->getPackageName(), $file, $branch);
         return $this->parseJson($fileContent);
     } catch (\Gitlab\Exception\RuntimeException $e) {
         return "";
     }
 }
Exemplo n.º 5
0
 /**
  * gets a file (content) from the repository.
  *
  * @param string $filename
  *
  * @return string
  */
 protected function getFile($filename)
 {
     try {
         $api = $this->client->api('repositories');
         /* @var Repositories $api */
         $file = $api->getFile($this->raw['path_with_namespace'], $filename, 'master');
         if ('base64' === $file['encoding']) {
             return base64_decode($file['content'], true);
         }
     } catch (\Exception $e) {
         //file not found
     }
 }
Exemplo n.º 6
0
 public function it_returns_its_issues(Client $client, Issues $issuesApi, MergeRequests $mergesApi)
 {
     $client->api('issues')->willReturn($issuesApi);
     $client->api('merge_requests')->willReturn($mergesApi);
     $issuesApi->all('foo/bar', 1, 9999)->shouldBeCalled()->willReturn([['state' => 'opened'], ['state' => 'closed']]);
     $mergesApi->all('foo/bar', 1, 9999)->shouldBeCalled()->willReturn([['state' => 'opened'], ['state' => 'closed']]);
     $result = $this->getIssues();
     $result->shouldHaveCount(2);
     $result[0]->shouldHaveType('Rs\\Issues\\Gitlab\\GitlabIssue');
     $result[0]->getType()->shouldBe('issue');
     $result[1]->shouldHaveType('Rs\\Issues\\Gitlab\\GitlabIssue');
     $result[1]->getType()->shouldBe('merge');
 }
Exemplo n.º 7
0
 public function it_returns_a_list_of_Projects_on_findProjects(Client $client, Projects $repoApi)
 {
     $client->api('projects')->willReturn($repoApi);
     $repoApi->show('foo/bar')->willReturn(['path_with_namespace' => 'foo/bar']);
     $repoApi->accessible(1, 9999)->willReturn([['path_with_namespace' => 'foo/bar']]);
     $projects = $this->findProjects('foo/(?!bazz|lol)([a-z0-9\\.-]+)$');
     $projects->shouldBeArray();
     $projects['foo/bar']->shouldHaveType('Rs\\Issues\\Project');
     $projects['foo/bar']->shouldHaveType('Rs\\Issues\\Gitlab\\GitlabProject');
 }
 /**
  * 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);
 }
Exemplo n.º 9
0
        header('HTTP/1.0 304 Not Modified');
    } else {
        readfile($file);
    }
    die;
};
// See ../confs/samples/gitlab.ini
$config_file = __DIR__ . '/../confs/gitlab.ini';
if (!file_exists($config_file)) {
    header('HTTP/1.0 500 Internal Server Error');
    die('confs/gitlab.ini missing');
}
$confs = parse_ini_file($config_file);
$client = new Client($confs['endpoint']);
$client->authenticate($confs['api_key'], Client::AUTH_URL_TOKEN);
$projects = $client->api('projects');
$repos = $client->api('repositories');
$validMethods = array('ssh', 'http');
if (isset($confs['method']) && in_array($confs['method'], $validMethods)) {
    define('method', $confs['method']);
} else {
    define('method', 'ssh');
}
/**
 * Retrieves some information about a project's composer.json
 *
 * @param array $project
 * @param string $ref commit id
 * @return array|false
 */
$fetch_composer = function ($project, $ref) use($repos) {
Exemplo n.º 10
0
 /**
  * @param Client $client
  * @param string $name
  * @param string $path
  * @return Group
  */
 public static function create(Client $client, $name, $path)
 {
     $data = $client->api('groups')->create($name, $path);
     return static::fromArray($client, $data);
 }
Exemplo n.º 11
0
 /**
  * @param Client $client
  * @param string $email
  * @param string $password
  * @param array  $params
  * @return User
  */
 public static function create(Client $client, $email, $password, array $params = array())
 {
     $data = $client->api('users')->create($email, $password, $params);
     return static::fromArray($client, $data);
 }
Exemplo n.º 12
0
 public function projects()
 {
     $projects = $this->client->api('projects')->all();
     return $projects;
 }
Exemplo n.º 13
0
 /**
  * {@inheritdoc}
  */
 public function isAuthenticated()
 {
     return is_array($this->client->api('projects')->owned());
 }
Exemplo n.º 14
0
 /**
  * @param int $user_id
  * @param Client $client
  * @param string $name
  * @param array $params
  * @return Project
  */
 public static function createForUser($user_id, Client $client, $name, array $params = array())
 {
     $data = $client->api('projects')->createForUser($user_id, $name, $params);
     return static::fromArray($client, $data);
 }
Exemplo n.º 15
0
 /**
  * @param Client $client
  * @param string $url
  * @return Hook
  */
 public static function create(Client $client, $url)
 {
     $data = $client->api('system_hooks')->create($url);
     return static::fromArray($client, $data);
 }