Skip to content
Snippets Groups Projects
Commit 1c7fce3d authored by Jürgen Haas's avatar Jürgen Haas
Browse files

Issue #3310438 by jurgenhaas: Update dependencies and code style cleanup

parent d211fa3d
Branches
Tags
No related merge requests found
......@@ -7,6 +7,7 @@ use Drupal\Core\Config\ImmutableConfig;
use Drupal\gitlab_api\Entity\GitlabServer;
use Gitlab\Client;
use Gitlab\ResultPager;
use Http\Client\Exception;
/**
* GitLab API wrapper class.
......@@ -25,9 +26,9 @@ class Api {
/**
* The GitLab client.
*
* @var \Gitlab\Client
* @var \Gitlab\Client|null
*/
protected Client $client;
protected ?Client $client;
/**
* The GitLab server entity.
......@@ -53,17 +54,18 @@ class Api {
if (!isset($this->server)) {
$this->switchServer();
}
if (!isset($this->client)) {
$this->client = Client::createWithHttpClient();
$this->client = Client::create($this->server->getUrl());
$this->client->authenticate($this->server->getAuthToken(), Client::AUTH_URL_TOKEN);
if ($this->client === NULL) {
$client = new Client();
$client->setUrl($this->server->getUrl());
$client->authenticate($this->server->getAuthToken(), Client::AUTH_HTTP_TOKEN);
}
}
/**
* Allow to switch between gitlab server.
* @param string $server_id
*
* @param string|null $server_id
* The GitLab server config entity ID.
*/
public function switchServer(string $server_id = NULL): void {
if ($server_id && $server = GitlabServer::load($server_id)) {
......@@ -76,18 +78,24 @@ class Api {
}
/**
* Creates a new GitLab project.
*
* @param string $namespace
* The namespace.
* @param string $path
* The path.
* @param string $name
* The name.
*
* @return array
* The created project.
*/
public function createProject($namespace, $path, $name): array {
public function createProject(string $namespace, string $path, string $name): array {
$this->init();
$gitlab_namespace = $this->client->namespaces()->show($namespace);
if (!$gitlab_namespace) {
throw new InvalidArgumentException('Invalid namespace');
throw new \InvalidArgumentException('Invalid namespace');
}
return $this->client->projects()->create($name, [
......@@ -97,124 +105,209 @@ class Api {
}
/**
* @param int|string $project_id
* Triggers a GitLab pipeline.
*
* @param int $project_id
* The project ID.
* @param string $commit_ref
* @param array|null $variables
* The commit reference.
* @param array $variables
* Optional extra variables.
*
* @return array
* The created pipeline.
*/
public function createPipeline($project_id, $commit_ref, $variables = null): array {
public function createPipeline(int $project_id, string $commit_ref, array $variables = []): array {
$this->init();
return $this->client->projects()->createPipeline($project_id, $commit_ref, $variables);
return $this->client->projects()
->createPipeline($project_id, $commit_ref, $variables);
}
/**
* Create an issue.
*
* @param int $project_id
* @param array $additionalParams
* The project ID.
* @param string $title
* The issue title.
* @param string $body
* The issue body text.
* @param int[] $assignee_ids
* The list of user ids of assignees.
* @param \DateTime|null $due_date
* The due date.
* @param array $labels
* The issue labels.
*
* @return array
* @throws \Http\Client\Exception
* The issue.
*/
public function listPipelines(int $project_id, $additionalParams = []): array {
public function createIssue(int $project_id, string $title, string $body, array $assignee_ids = [], \DateTime $due_date = NULL, array $labels = []): array {
$this->init();
$pager = new ResultPager($this->client);
$params = [
'title' => $title,
'description' => $body,
'assignee_ids' => $assignee_ids,
];
if ($due_date) {
$params['due_date'] = $due_date->format('Y-m-d');
}
if ($labels && count($labels) > 0) {
$params['labels'] = implode(',', $labels);
}
$params = [];
$params += $additionalParams;
return $pager->fetchAll($this->client->projects(), 'pipelines', [$project_id, $params]);
return $this->client->issues()->create($project_id, $params);
}
/**
* @param int|string $project_id
* @param int $pipeline_id
* Gets a list of namespaces.
*
* @return mixed
* @return array
* The list of namespaces.
*/
public function getPipeline($project_id, $pipeline_id): array {
public function namespaces(): array {
$this->init();
return $this->client->projects()->pipeline($project_id, $pipeline_id);
return $this->client->namespaces()->all();
}
/**
* @param int|string $project_id
* @param int $pipeline_id
* Gets a list of projects.
*
* @param bool $simple
* If TRUE, only limited number of fields for each projects get returned.
* @param bool $includeArchived
* If TRUE, also archived projects will be returned.
* @param array $additionalParams
* Optional extra arguments.
*
* @return mixed
* @return array
* The list of projects.
*/
public function getPipelineJobs($project_id, $pipeline_id): array {
public function projects(bool $simple = TRUE, bool $includeArchived = FALSE, array $additionalParams = []): array {
$this->init();
return $this->client->jobs()->pipelineJobs($project_id, $pipeline_id);
$pager = new ResultPager($this->client);
$params = [
'simple' => $simple,
'archived' => FALSE,
];
if ($includeArchived) {
unset($params['archived']);
}
$params += $additionalParams;
try {
return $pager->fetchAll($this->client->projects(), 'all', [$params]);
}
catch (Exception $e) {
return [];
}
}
/**
* @param int|string $project_id
* @param int $job_id
* Gets a project.
*
* @return mixed
* @param int $project_id
* The project ID.
*
* @return array
* The project.
*/
public function getPipelineJob($project_id, $job_id): array {
public function project(int $project_id): array {
$this->init();
return $this->client->jobs()->show($project_id, $job_id);
return $this->client->projects()->show($project_id);
}
/**
* @param int|string $project_id
* Gets a list of project pipelines.
*
* @return mixed
* @param int $project_id
* The project ID.
* @param array $additionalParams
* Optional extra arguments.
*
* @return array
* The list of project pipelines.
*/
public function listBranches($project_id): array {
public function pipelines(int $project_id, array $additionalParams = []): array {
$this->init();
return $this->client->repositories->branches($project_id);
$pager = new ResultPager($this->client);
$params = [];
$params += $additionalParams;
try {
return $pager->fetchAll($this->client->projects(), 'pipelines', [
$project_id,
$params,
]);
}
catch (Exception $e) {
return [];
}
}
/**
* @param int|string $project_id
* @param string $branch
* @return mixed
* Get a project pipeline.
*
* @param int $project_id
* The project ID.
* @param int $pipeline_id
* The pipeline ID.
*
* @return array
* The project pipeline.
*/
public function listBranch($project_id, string $branch): array {
public function pipeline(int $project_id, int $pipeline_id): array {
$this->init();
return $this->client->repositories->branch($project_id);
return $this->client->projects()->pipeline($project_id, $pipeline_id);
}
/**
* Get the jobs of a pipeline.
*
* @param int $project_id
* @param string $title
* @param string $body
* @param array $assignee_ids
* @param DateTime|null $due_date
* @param array $labels
* The project ID.
* @param int $pipeline_id
* The pipeline ID.
*
* @return array
* The list of pipeline jobs.
*/
public function createIssue(int $project_id, string $title, string $body, $assignee_ids = [], DateTime $due_date = NULL, array $labels = []): array {
public function jobs(int $project_id, int $pipeline_id): array {
$this->init();
$params = [
'title' => $title,
'description' => $body,
'assignee_ids' => $assignee_ids,
];
if ($due_date) {
$params['due_date'] = $due_date->format('Y-m-d');
}
if ($labels && count($labels) > 0) {
$params['labels'] = implode(',', $labels);
}
return $this->client->jobs()->pipelineJobs($project_id, $pipeline_id);
}
return $this->client->issues->create($project_id, $params);
/**
* Get a project's pipeline job.
*
* @param int $project_id
* The project ID.
* @param int $job_id
* The job ID.
*
* @return array
* The job.
*/
public function job(int $project_id, int $job_id): array {
$this->init();
return $this->client->jobs()->show($project_id, $job_id);
}
/**
* Gets a list of project issues.
*
* @param int $project_id
* The project ID.
* @param string|null $state
* The state of issues to retrieve, can be "opened" or "closed".
* @param int|null $assignee_id
* The assignee ID.
* @param array $additionalParams
* Optional extra arguments.
*
* @return array
* @throws \Http\Client\Exception
* The list of project issues.
*/
public function listIssues(int $project_id, string $state = NULL, int $assignee_id = NULL, $additionalParams = []): array {
public function issues(int $project_id, string $state = NULL, int $assignee_id = NULL, array $additionalParams = []): array {
$this->init();
$pager = new ResultPager($this->client);
$params = [];
......@@ -225,77 +318,86 @@ class Api {
$params['assignee_id'] = $assignee_id;
}
$params += $additionalParams;
return $pager->fetchAll($this->client->issues(), 'all', [$project_id, $params]);
try {
return $pager->fetchAll($this->client->issues(), 'all', [
$project_id,
$params,
]);
}
catch (Exception $e) {
return [];
}
}
/**
* Return an issue by it's project and id.
* Gets a project issue.
*
* @param int $project_id
* The project ID.
* @param int $issue_iid
* The issue ID.
*
* @return mixed
*/
public function showIssue(int $project_id, int $issue_iid) {
$this->init();
return $this->client->issues()->show($project_id, $issue_iid);
}
/**
* Returns all namespaces on the server
* @return array
* The issue.
*/
public function listNamespaces() : array{
public function issue(int $project_id, int $issue_iid): array {
$this->init();
return $this->client->namespaces()->all();
return $this->client->issues()->show($project_id, $issue_iid);
}
/**
* Returns all projects on the server
* Gets a list of issue links.
*
* @param bool $simple
* @param bool $includeArchived
* @param array $additionalParams
* @param int $project_id
* The project ID.
* @param int $issue_iid
* The issue ID.
*
* @return array
* @throws \Http\Client\Exception
* The issue links.
*/
public function listProjects(bool $simple = TRUE, $includeArchived = FALSE, $additionalParams = []) : array {
public function issueLinks(int $project_id, int $issue_iid): array {
$this->init();
$pager = new ResultPager($this->client);
$params = [
'simple' => $simple,
'archived' => FALSE,
];
if ($includeArchived) {
unset($params['archived']);
try {
return $pager->fetchAll($this->client->issueLinks(), 'all', [
$project_id,
$issue_iid,
]);
}
catch (Exception $e) {
return [];
}
$params += $additionalParams;
return $pager->fetchAll($this->client->projects(), 'all', [$params]);
}
/**
* Returns information about a given project.
* Get a list of project branches.
*
* @param $project_id
* @param int $project_id
* The project ID.
*
* @return array
* The list of branches.
*/
public function showProject($project_id) : array {
public function branches(int $project_id): array {
$this->init();
return $this->client->projects->show($project_id);
return $this->client->repositories()->branches($project_id);
}
/**
* Get a project branch.
*
* @param int $project_id
* @param int $issue_iid
* The project ID.
* @param string $branch
* The branch.
*
* @return array
* @throws \Http\Client\Exception
* The branch.
*/
public function listIssueLinks(int $project_id, int $issue_iid): array {
public function branch(int $project_id, string $branch): array {
$this->init();
$pager = new ResultPager($this->client);
return $pager->fetchAll($this->client->issueLinks(), 'all', [$project_id, $issue_iid]);
return $this->client->repositories()->branch($project_id, $branch);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment