/**
  * 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');
 }
示例#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');
 }
 /**
  * ПОдготавливает клиент для работы с гитлабом
  *
  * @return Client
  *
  */
 protected function getGitLabClient()
 {
     if (null === $this->client) {
         $this->client = new Client($this->url);
         $this->client->authenticate($this->token);
     }
     return $this->client;
 }
示例#5
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 "";
     }
 }
 /**
  * Returns an authenticated API client.
  *
  * Requires optional Gitlab API client to be installed.
  *
  * @return Client
  */
 public function getApiClient()
 {
     if (!class_exists('\\Gitlab\\Client')) {
         throw new \LogicException(__METHOD__ . ' requires package m4tthumphrey/php-gitlab-api to be installed and autoloaded');
         // @codeCoverageIgnore
     }
     $client = new Client(rtrim($this->domain, '/') . self::PATH_API);
     return $client->authenticate($this->token->getToken(), Client::AUTH_OAUTH_TOKEN);
 }
示例#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');
 }
示例#8
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');
 }
示例#9
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
     }
 }
 public function register(Application $app)
 {
     $app['gitlab'] = $app->share(function () use($app) {
         if (!isset($app['gitlab.url'])) {
             throw new \Exception('gitlab.url undefined');
         }
         if (!isset($app['gitlab.key'])) {
             throw new \Exception('gitlab.key undefined');
         }
         $client = new \Gitlab\Client($app['gitlab.url']);
         // change here
         $client->authenticate($app['gitlab.key'], \Gitlab\Client::AUTH_URL_TOKEN);
         // change here
         return $client;
     });
 }
示例#11
0
 protected function createMilestone($githubMilestone, $project)
 {
     $gitlabMilestone = null;
     if ($this->gitlabMilestones === null) {
         $this->gitlabMilestones = $this->gitlabClient->milestones->all($project['id'], 100);
     }
     foreach ($this->gitlabMilestones as $_gitlabMilestone) {
         if ($_gitlabMilestone['title'] === $githubMilestone['title']) {
             $gitlabMilestone = $_gitlabMilestone;
             break;
         }
     }
     if (empty($gitlabMilestone)) {
         $this->output("\t" . '[milestone] Create "' . $githubMilestone['title'] . '"', self::OUTPUT_SUCCESS);
         if (!$this->dry) {
             $this->gitlabClient->authenticate(GITLAB_ADMIN_TOKEN, \Gitlab\Client::AUTH_URL_TOKEN);
             $gitlabMilestone = $this->gitlabClient->milestones->create($project['id'], ['title' => $githubMilestone['title'], 'description' => $githubMilestone['description'], 'state' => self::resolveMilestoneState($githubMilestone['state']), 'due_date' => $githubMilestone['due_on']]);
         }
     }
     return $gitlabMilestone['id'];
 }
示例#12
0
 /**
  * @inheritdoc
  */
 public function saveGit()
 {
     $client = new Client($this->enterpriseUrl);
     $client->authenticate($this->authKey, Client::AUTH_URL_TOKEN);
     $projects = $client->getHttpClient()->get('projects/')->getContent();
     foreach ($projects as $data) {
         if ($data['path_with_namespace'] == $this->owner . '/' . $this->repository) {
             $id = $data['id'];
             break;
         }
     }
     if (!isset($id)) {
         throw new HttpException(404, "Repository " . $this->repository . " not found.");
     }
     /**
      * @var Project $project
      */
     $project = Project::fromArray($client, compact('id'));
     $project->updateFile($this->remotePath, $this->content, 'master', $this->comment);
     return true;
 }
 /**
  * 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);
 }
示例#14
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);
 }
示例#15
0
 public function projects()
 {
     $projects = $this->client->api('projects')->all();
     return $projects;
 }
示例#16
0
 /**
  * @param string $path
  * @param array $parameters
  * @param array $requestHeaders
  * @return mixed
  */
 protected function delete($path, array $parameters = array(), $requestHeaders = array())
 {
     $response = $this->client->getHttpClient()->delete($path, $parameters, $requestHeaders);
     return $response->getContent();
 }
示例#17
0
 /**
  * {@inheritdoc}
  */
 public function isAuthenticated()
 {
     return is_array($this->client->api('projects')->owned());
 }
示例#18
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);
 }
示例#19
0
 /**
  * Get the main client.
  *
  * @param array $config
  *
  * @return \Gitlab\Client
  */
 protected function getClient(array $config)
 {
     $client = new Client(array_get($config, 'base_url', 'http://git.yourdomain.com/api/v3/'));
     $client->authenticate($config['token'], array_get($config, 'method', Client::AUTH_HTTP_TOKEN), array_get($config, 'sudo', null));
     return $client;
 }
示例#20
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);
 }
示例#21
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);
 }
示例#22
0
 /**
  * @return \Gitlab\Client
  */
 protected function authenticate()
 {
     $client = new GitLab($this->gitLabUrl);
     $client->authenticate($this->gitLabToken, GitLab::AUTH_URL_TOKEN);
     return $client;
 }
示例#23
0
 private function getClient(Remote $remote)
 {
     $config = $this->getRemoteConfig($remote);
     $client = new Client(rtrim($config->getUrl(), '/') . '/api/v3/');
     $client->authenticate($config->getToken(), Client::AUTH_HTTP_TOKEN);
     return $client;
 }
示例#24
0
    header('Cache-Control: max-age=0');
    if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && ($since = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) && $since >= $mtime) {
        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
示例#25
0
 /**
  * Get the main client.
  *
  * @param array $config
  *
  * @return \Gitlab\Client
  */
 protected function getClient(array $config)
 {
     $client = new Client($config['base_url']);
     $client->authenticate($config['token'], array_get($config, 'method', Client::AUTH_HTTP_TOKEN), array_get($config, 'sudo', null));
     return $client;
 }