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

Commit

Permalink
Implement remainder of merge requests API in GitlabClient interface
Browse files Browse the repository at this point in the history
  • Loading branch information
fgrosse committed Sep 28, 2015
1 parent a9821ee commit 18e1f36
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 5 deletions.
21 changes: 20 additions & 1 deletion lib/Client/GitlabClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Gitlab\Client;

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

interface GitlabClient
Expand Down Expand Up @@ -54,8 +56,25 @@ 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
* @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);
}
2 changes: 1 addition & 1 deletion lib/Client/GitlabGuzzleClient.php
Original file line number Diff line number Diff line change
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
17 changes: 17 additions & 0 deletions lib/Client/HttpGitlabClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,21 @@ public function acceptMergeRequest($projectId, $mergeRequestId, $commitMessage =
'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,
]));
}
}
12 changes: 12 additions & 0 deletions lib/Client/MockGitlabClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Gitlab\Client;

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

class MockGitlabClient implements GitlabClient
Expand Down Expand Up @@ -39,6 +41,16 @@ public function acceptMergeRequest($projectId, $mergeRequestId, $commitMessage =
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");
Expand Down
2 changes: 1 addition & 1 deletion lib/Client/ServiceDescription/merge_requests_api.yml
Original file line number Diff line number Diff line change
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
55 changes: 55 additions & 0 deletions tests/Client/HttpGitlabClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Gitlab\Client\GitlabClient;
use Gitlab\Client\GitlabGuzzleClient;
use Gitlab\Client\HttpGitlabClient;
use Gitlab\Entity\Comment;
use Gitlab\Entity\CommentCollection;
use Gitlab\Entity\MergeRequest;
use Gitlab\Entity\User;
use Mockery;
Expand Down Expand Up @@ -128,4 +130,57 @@ public function testUpdateMergeRequest()
$actualResult = $this->client->updateMergeRequest($mergeRequest);
$this->assertEquals($expectedResult, $actualResult);
}

public function testAcceptMergeRequest()
{
$expectedResult = new MergeRequest('fgrosse/example', 'MR No. 42', 42);
$expectedResult->sourceBranch = 'develop';
$expectedResult->targetBranch = 'master';
$expectedResult->description = 'My description';

$this->guzzle->shouldReceive('acceptMergeRequest')->once()->with(Mockery::on(function ($params) {
$this->assertEquals([
'project_id' => 'fgrosse/example',
'merge_request_id' => 42,
'merge_commit_message' => 'Okey dokey',
], $params);
return true;
}))->andReturn($expectedResult);

$actualResult = $this->client->acceptMergeRequest('fgrosse/example', 42, 'Okey dokey');
$this->assertEquals($expectedResult, $actualResult);
}

public function testCreateMergeRequestComment()
{
$expectedResult = new Comment('Nice work joe!');
$this->guzzle->shouldReceive('createMergeRequestComment')->once()->with(Mockery::on(function ($params) {
$this->assertEquals([
'project_id' => 'fgrosse/example',
'merge_request_id' => 42,
'note' => 'Nice work joe!',
], $params);
return true;
}))->andReturn($expectedResult);

$actualResult = $this->client->createMergeRequestComment('fgrosse/example', 42, 'Nice work joe!');
$this->assertEquals($expectedResult, $actualResult);
}

public function testGetMergeRequestComments()
{
$expectedResult = new CommentCollection();
$expectedResult[] = new Comment('Foo');
$expectedResult[] = new Comment('Bar');
$this->guzzle->shouldReceive('getMergeRequestComments')->once()->with(Mockery::on(function ($params) {
$this->assertEquals([
'project_id' => 'fgrosse/example',
'merge_request_id' => 42,
], $params);
return true;
}))->andReturn($expectedResult);

$actualResult = $this->client->getMergeRequestComments('fgrosse/example', 42);
$this->assertEquals($expectedResult, $actualResult);
}
}
4 changes: 2 additions & 2 deletions tests/Client/MergeRequestsAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ public function testCreateMergeRequestComment()
$this->assertInstanceOf(Comment::class, $comment);
}

public function testListMergeRequestComments()
public function testGetMergeRequestComments()
{
$this->setMockResponse(__DIR__.'/fixtures/merge_requests/list_merge_request_comments.http');
$projectId = 'fgrosse/example-project';
$mergeRequestId = 42;
$comments = $this->client->listMergeRequestComments([
$comments = $this->client->getMergeRequestComments([
'project_id' => $projectId,
'merge_request_id' => $mergeRequestId,
'page' => 3,
Expand Down

0 comments on commit 18e1f36

Please sign in to comment.