Skip to content

add committer parameter in contents api #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions lib/Github/Api/Repository/Contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Github\Api\AbstractApi;
use Github\Exception\InvalidArgumentException;
use Github\Exception\ErrorException;
use Github\Exception\MissingArgumentException;

/**
* @link http://developer.github.com/v3/repos/contents/
Expand Down Expand Up @@ -62,10 +63,13 @@ public function show($username, $repository, $path = null, $reference = null)
* @param string $content contents of the new file
* @param string $message the commit message
* @param null|string $branch name of a branch
* @param null|array $committer information about the committer
*
* @throws MissingArgumentException
*
* @return array information about the new file
*/
public function create($username, $repository, $path, $content, $message, $branch = null)
public function create($username, $repository, $path, $content, $message, $branch = null, array $committer = null)
{
$url = 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents/'.rawurlencode($path);

Expand All @@ -78,6 +82,13 @@ public function create($username, $repository, $path, $content, $message, $branc
$parameters['branch'] = $branch;
}

if (null !== $committer) {
if (!isset($committer['name'], $committer['email'])) {
throw new MissingArgumentException(array('name', 'email'));
}
$parameters['committer'] = $committer;
}

return $this->put($url, $parameters);
}

Expand All @@ -92,10 +103,13 @@ public function create($username, $repository, $path, $content, $message, $branc
* @param string $message the commit message
* @param string $sha blob SHA of the file being replaced
* @param null|string $branch name of a branch
* @param null|array $committer information about the committer
*
* @throws MissingArgumentException
*
* @return array information about the updated file
*/
public function update($username, $repository, $path, $content, $message, $sha, $branch = null)
public function update($username, $repository, $path, $content, $message, $sha, $branch = null, array $committer = null)
{
$url = 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents/'.rawurlencode($path);

Expand All @@ -109,6 +123,13 @@ public function update($username, $repository, $path, $content, $message, $sha,
$parameters['branch'] = $branch;
}

if (null !== $committer) {
if (!isset($committer['name'], $committer['email'])) {
throw new MissingArgumentException(array('name', 'email'));
}
$parameters['committer'] = $committer;
}

return $this->put($url, $parameters);
}

Expand All @@ -123,10 +144,13 @@ public function update($username, $repository, $path, $content, $message, $sha,
* @param string $message the commit message
* @param string $sha blob SHA of the file being deleted
* @param null|string $branch name of a branch
* @param null|array $committer information about the committer
*
* @throws MissingArgumentException
*
* @return array information about the updated file
*/
public function rm($username, $repository, $path, $message, $sha, $branch = null)
public function rm($username, $repository, $path, $message, $sha, $branch = null, array $committer = null)
{
$url = 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents/'.rawurlencode($path);

Expand All @@ -139,6 +163,13 @@ public function rm($username, $repository, $path, $message, $sha, $branch = null
$parameters['branch'] = $branch;
}

if (null !== $committer) {
if (!isset($committer['name'], $committer['email'])) {
throw new MissingArgumentException(array('name', 'email'));
}
$parameters['committer'] = $committer;
}

return $this->delete($url, $parameters);
}

Expand Down
116 changes: 116 additions & 0 deletions test/Github/Tests/Api/Repository/ContentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,122 @@ public function shouldShowReadme()
$this->assertEquals($expectedValue, $api->readme('KnpLabs', 'php-github-api'));
}

/**
* @test
*/
public function shouldCreateNewFile()
{
$expectedArray = array('content' => 'some data');
$content = '<?php //..';
$message = 'a commit message';
$branch = 'master';
$committer = array('name' => 'committer name', 'email' => 'email@example.com');
$parameters = array(
'content' => base64_encode($content),
'message' => $message,
'committer' => $committer,
'branch' => $branch,
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('put')
->with('repos/KnpLabs/php-github-api/contents/test%2FGithub%2FTests%2FApi%2FRepository%2FContentsTest.php', $parameters)
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->create('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', $content, $message, $branch, $committer));
}

/**
* @test
* @expectedException Github\Exception\MissingArgumentException
* @expectedExceptionMessage One or more of required ("name", "email") parameters is missing!
*/
public function shouldThrowExceptionWhenCreateNewFileWithInvalidCommitter()
{
$committer = array('invalid_key' => 'some data');
$api = $this->getApiMock();
$api->create('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', 'some content', 'a commit message', null, $committer);
}

/**
* @test
*/
public function shouldUpdateFile()
{
$expectedArray = array('content' => 'some data');
$content = '<?php //..';
$message = 'a commit message';
$sha = 'a sha';
$branch = 'master';
$committer = array('name' => 'committer name', 'email' => 'email@example.com');
$parameters = array(
'content' => base64_encode($content),
'message' => $message,
'committer' => $committer,
'branch' => $branch,
'sha' => $sha,
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('put')
->with('repos/KnpLabs/php-github-api/contents/test%2FGithub%2FTests%2FApi%2FRepository%2FContentsTest.php', $parameters)
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->update('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', $content, $message, $sha, $branch, $committer));
}

/**
* @test
* @expectedException Github\Exception\MissingArgumentException
* @expectedExceptionMessage One or more of required ("name", "email") parameters is missing!
*/
public function shouldThrowExceptionWhenUpdateFileWithInvalidCommitter()
{
$committer = array('invalid_key' => 'some data');
$api = $this->getApiMock();
$api->update('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', 'some content', 'a commit message', null, null, $committer);
}

/**
* @test
*/
public function shouldDeleteFile()
{
$expectedArray = array('content' => 'some data');
$message = 'a commit message';
$sha = 'a sha';
$branch = 'master';
$committer = array('name' => 'committer name', 'email' => 'email@example.com');
$parameters = array(
'message' => $message,
'committer' => $committer,
'branch' => $branch,
'sha' => $sha,
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('delete')
->with('repos/KnpLabs/php-github-api/contents/test%2FGithub%2FTests%2FApi%2FRepository%2FContentsTest.php', $parameters)
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->rm('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', $message, $sha, $branch, $committer));
}

/**
* @test
* @expectedException Github\Exception\MissingArgumentException
* @expectedExceptionMessage One or more of required ("name", "email") parameters is missing!
*/
public function shouldThrowExceptionWhenDeleteFileWithInvalidCommitter()
{
$committer = array('invalid_key' => 'some data');
$api = $this->getApiMock();
$api->rm('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', 'a commit message', null, null, $committer);
}

/**
* @test
*/
Expand Down