Skip to content

Commit

Permalink
Add a "type" parameter for commitRefs
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertrand Jamin authored and m1guelpf committed Feb 3, 2019
1 parent 73c12f2 commit 04251cf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/Gitlab/Api/Repositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

class Repositories extends AbstractApi
{
const TYPE_BRANCH = 'branch';
const TYPE_TAG = 'tag';

/**
* @param int $project_id
* @param array $parameters
Expand Down Expand Up @@ -182,11 +185,16 @@ public function commit($project_id, $sha)
/**
* @param int $project_id
* @param $sha
* @param array $parameters
*
* @return mixed
*/
public function commitRefs($project_id, $sha)
public function commitRefs($project_id, $sha, array $parameters = [])
{
return $this->get($this->getProjectPath($project_id, 'repository/commits/'.$this->encodePath($sha).'/refs'));
$resolver = $this->createOptionsResolver();

return $this->get($this->getProjectPath($project_id, 'repository/commits/' . $this->encodePath($sha) . '/refs'),
$resolver->resolve($parameters));
}

/**
Expand Down Expand Up @@ -471,4 +479,19 @@ public function mergeBase($project_id, $refs)
{
return $this->get($this->getProjectPath($project_id, 'repository/merge_base'), array('refs' => $refs));
}

protected function createOptionsResolver()
{
$allowedTypeValues = [
self::TYPE_BRANCH,
self::TYPE_TAG
];

$resolver = parent::createOptionsResolver();
$resolver->setDefined('type')
->setAllowedTypes('type', 'string')
->setAllowedValues('type', $allowedTypeValues);

return $resolver;
}
}
34 changes: 34 additions & 0 deletions test/Gitlab/Tests/Api/RepositoriesTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Gitlab\Tests\Api;

use Gitlab\Api\AbstractApi;
use Gitlab\Api\Repositories;

class RepositoriesTest extends TestCase
{
Expand Down Expand Up @@ -322,6 +323,39 @@ public function shouldGetCommitRefs()
$this->assertEquals($expectedArray, $api->commitRefs(1, 'abcd1234'));
}

/**
* @dataProvider dataGetCommitRefsWithParams
* @test
*
* @param string $type
* @param array $expectedArray
*/
public function shouldGetCommitRefsWithParams($type, array $expectedArray)
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('projects/1/repository/commits/abcd1234/refs', ['type' => $type])
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->commitRefs(1, 'abcd1234', ['type' => $type]));
}

public function dataGetCommitRefsWithParams()
{
return [
'type_tag' => [
'type' => Repositories::TYPE_TAG,
'expectedArray' => [['type' => 'tag', 'name' => 'v1.1.0']]
],
'type_branch' => [
'type' => Repositories::TYPE_BRANCH,
'expectedArray' => [['type' => 'branch', 'name' => 'master']]
],
];
}

/**
* @test
*/
Expand Down

0 comments on commit 04251cf

Please sign in to comment.