Skip to content
This repository has been archived by the owner on Sep 27, 2020. It is now read-only.

Commit

Permalink
Merge pull request #4 from fgrosse/feature/merge-request-api
Browse files Browse the repository at this point in the history
Feature/merge request api
  • Loading branch information
fgrosse committed Sep 28, 2015
2 parents 6d78c39 + 18e1f36 commit 792f7cc
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 12 deletions.
52 changes: 52 additions & 0 deletions lib/Client/GitlabClient.php
Expand Up @@ -10,6 +10,10 @@

namespace Gitlab\Client;

use Gitlab\Entity\Comment;
use Gitlab\Entity\CommentCollection;
use Gitlab\Entity\MergeRequest;

interface GitlabClient
{
/**
Expand All @@ -25,4 +29,52 @@ interface GitlabClient
* @return \Gitlab\Entity\MergeRequest[]
*/
public function listMergeRequests($projectId, $state = null, $orderBy = null, $sort = null, $page = null, $perPage = null);

/**
* Shows information about the merge request including its files and changes.
* @param string $projectId
* @param string $mergeRequestId
* @return MergeRequest
*/
public function getMergeRequest($projectId, $mergeRequestId);

/**
* Creates a new merge request.
* @param MergeRequest $mergeRequest
* @return MergeRequest
*/
public function createMergeRequest(MergeRequest $mergeRequest);

/**
* Updates an existing merge request. You can change branches, title, or even close the MR.
* @param MergeRequest $mergeRequest
* @return MergeRequest
*/
public function updateMergeRequest(MergeRequest $mergeRequest);

/**
* Merge changes submitted with MR using this API.
* @param string $projectId
* @param int $mergeRequestId
* @param string|null $commitMessage Custom merge commit message
* @return MergeRequest
*/
public function acceptMergeRequest($projectId, $mergeRequestId, $commitMessage = null);

/**
* Adds a comment to a merge request.
* @param string $projectId
* @param int $mergeRequestId
* @param string $note Text of the comment
* @return Comment
*/
public function createMergeRequestComment($projectId, $mergeRequestId, $note);

/**
* Gets all the comments associated with a merge request.
* @param string $projectId
* @param int $mergeRequestId
* @return CommentCollection
*/
public function getMergeRequestComments($projectId, $mergeRequestId);
}
10 changes: 5 additions & 5 deletions lib/Client/GitlabGuzzleClient.php
Expand Up @@ -32,7 +32,7 @@
* @see GitlabGuzzleClient::acceptMergeRequest
* @method array getMergeRequestChanges($parameters)
* @method MergeRequest createMergeRequestComment($parameters)
* @method CommentCollection listMergeRequestComments($parameters)
* @method CommentCollection getMergeRequestComments($parameters)
*
* Commits API
* @see https://github.com/gitlabhq/gitlabhq/blob/v7.7.0/doc/api/commits.md
Expand Down Expand Up @@ -154,9 +154,9 @@ public function listMergeRequests(array $parameters)
* @param array $parameters
* @return MergeRequest
*/
public function createMergeRequest(array $parameters)
public function getMergeRequest(array $parameters)
{
$command = $this->getCommand('createMergeRequest', $parameters);
$command = $this->getCommand('getMergeRequest', $parameters);

/** @var MergeRequest $mergeRequest */
$mergeRequest = $this->execute($command);
Expand All @@ -169,9 +169,9 @@ public function createMergeRequest(array $parameters)
* @param array $parameters
* @return MergeRequest
*/
public function getMergeRequest(array $parameters)
public function createMergeRequest(array $parameters)
{
$command = $this->getCommand('getMergeRequest', $parameters);
$command = $this->getCommand('createMergeRequest', $parameters);

/** @var MergeRequest $mergeRequest */
$mergeRequest = $this->execute($command);
Expand Down
61 changes: 61 additions & 0 deletions lib/Client/HttpGitlabClient.php
Expand Up @@ -10,6 +10,8 @@

namespace Gitlab\Client;

use Gitlab\Entity\MergeRequest;

class HttpGitlabClient implements GitlabClient
{
private $client;
Expand All @@ -30,4 +32,63 @@ public function listMergeRequests($projectId, $state = null, $orderBy = null, $s
'per_page' => $perPage,
]));
}

public function getMergeRequest($projectId, $mergeRequestId)
{
return $this->client->getMergeRequest(array_filter([
'project_id' => $projectId,
'merge_request_id' => $mergeRequestId,
]));
}

public function createMergeRequest(MergeRequest $mergeRequest)
{
return $this->client->createMergeRequest(array_filter([
'project_id' => $mergeRequest->project,
'source_branch' => $mergeRequest->sourceBranch,
'target_branch' => $mergeRequest->targetBranch,
'title' => $mergeRequest->title,
'description' => $mergeRequest->description,
'assignee_id' => isset($mergeRequest->assignee) ? $mergeRequest->assignee->id : null,
]));
}

public function updateMergeRequest(MergeRequest $mergeRequest)
{
return $this->client->updateMergeRequest(array_filter([
'project_id' => $mergeRequest->project,
'merge_request_id' => $mergeRequest->id,
'source_branch' => $mergeRequest->sourceBranch,
'target_branch' => $mergeRequest->targetBranch,
'title' => $mergeRequest->title,
'description' => $mergeRequest->description,
'assignee_id' => isset($mergeRequest->assignee) ? $mergeRequest->assignee->id : null,
]));
}

public function acceptMergeRequest($projectId, $mergeRequestId, $commitMessage = null)
{
return $this->client->acceptMergeRequest(array_filter([
'project_id' => $projectId,
'merge_request_id' => $mergeRequestId,
'merge_commit_message' => $commitMessage,
]));
}

public function createMergeRequestComment($projectId, $mergeRequestId, $note)
{
return $this->client->createMergeRequestComment(array_filter([
'project_id' => $projectId,
'merge_request_id' => $mergeRequestId,
'note' => $note,
]));
}

public function getMergeRequestComments($projectId, $mergeRequestId)
{
return $this->client->getMergeRequestComments(array_filter([
'project_id' => $projectId,
'merge_request_id' => $mergeRequestId,
]));
}
}
42 changes: 42 additions & 0 deletions lib/Client/MockGitlabClient.php
Expand Up @@ -10,10 +10,52 @@

namespace Gitlab\Client;

use Gitlab\Entity\Comment;
use Gitlab\Entity\CommentCollection;
use Gitlab\Entity\MergeRequest;

class MockGitlabClient implements GitlabClient
{
public function listMergeRequests($projectId, $state = null, $orderBy = null, $sort = null, $page = null, $perPage = null)
{
return [];
}

public function getMergeRequest($projectId, $mergeRequestId)
{
return $this->createMockMergeRequest($projectId, $mergeRequestId);
}

public function createMergeRequest(MergeRequest $mergeRequest)
{
return $mergeRequest;
}

public function updateMergeRequest(MergeRequest $mergeRequest)
{
return $mergeRequest;
}

public function acceptMergeRequest($projectId, $mergeRequestId, $commitMessage = null)
{
return $this->createMockMergeRequest($projectId, $mergeRequestId);
}

public function createMergeRequestComment($projectId, $mergeRequestId, $note)
{
return new Comment($note);
}

public function getMergeRequestComments($projectId, $mergeRequestId)
{
return new CommentCollection();
}

private function createMockMergeRequest($projectId, $mergeRequestId)
{
$mergeRequest = new MergeRequest($projectId, "Mock merge request #$mergeRequestId");
$mergeRequest->id = $mergeRequestId;

return $mergeRequest;
}
}
2 changes: 1 addition & 1 deletion lib/Client/ServiceDescription/merge_requests_api.yml
Expand Up @@ -207,7 +207,7 @@ operations:
type: string
location: postField

listMergeRequestComments:
getMergeRequestComments:
extends: paginatedOperation
summary: Adds a comment to a merge request.
responseClass: Gitlab\Entity\CommentCollection
Expand Down
13 changes: 13 additions & 0 deletions lib/Entity/MergeRequest.php
Expand Up @@ -81,6 +81,19 @@ public static function fromArray(array $data)
return $mergeRequest;
}

/**
* MergeRequest constructor.
* @param string $project
* @param string $title
* @param int|null $id
*/
public function __construct($project = '', $title = '', $id = null)
{
$this->project = $project;
$this->title = $title;
$this->id = $id;
}

public function __toString()
{
return sprintf('Merge request: %s (%s)', $this->title, $this->getCrossProjectReference());
Expand Down

0 comments on commit 792f7cc

Please sign in to comment.