Skip to content

Commit 41cc100

Browse files
committed
Merge pull request #129 from jdohuutin/master
add committer parameter in contents api
2 parents e2ef6bc + 779e83b commit 41cc100

File tree

2 files changed

+150
-3
lines changed

2 files changed

+150
-3
lines changed

lib/Github/Api/Repository/Contents.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Github\Api\AbstractApi;
66
use Github\Exception\InvalidArgumentException;
77
use Github\Exception\ErrorException;
8+
use Github\Exception\MissingArgumentException;
89

910
/**
1011
* @link http://developer.github.com/v3/repos/contents/
@@ -62,10 +63,13 @@ public function show($username, $repository, $path = null, $reference = null)
6263
* @param string $content contents of the new file
6364
* @param string $message the commit message
6465
* @param null|string $branch name of a branch
66+
* @param null|array $committer information about the committer
67+
*
68+
* @throws MissingArgumentException
6569
*
6670
* @return array information about the new file
6771
*/
68-
public function create($username, $repository, $path, $content, $message, $branch = null)
72+
public function create($username, $repository, $path, $content, $message, $branch = null, array $committer = null)
6973
{
7074
$url = 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents/'.rawurlencode($path);
7175

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

85+
if (null !== $committer) {
86+
if (!isset($committer['name'], $committer['email'])) {
87+
throw new MissingArgumentException(array('name', 'email'));
88+
}
89+
$parameters['committer'] = $committer;
90+
}
91+
8192
return $this->put($url, $parameters);
8293
}
8394

@@ -92,10 +103,13 @@ public function create($username, $repository, $path, $content, $message, $branc
92103
* @param string $message the commit message
93104
* @param string $sha blob SHA of the file being replaced
94105
* @param null|string $branch name of a branch
106+
* @param null|array $committer information about the committer
107+
*
108+
* @throws MissingArgumentException
95109
*
96110
* @return array information about the updated file
97111
*/
98-
public function update($username, $repository, $path, $content, $message, $sha, $branch = null)
112+
public function update($username, $repository, $path, $content, $message, $sha, $branch = null, array $committer = null)
99113
{
100114
$url = 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents/'.rawurlencode($path);
101115

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

126+
if (null !== $committer) {
127+
if (!isset($committer['name'], $committer['email'])) {
128+
throw new MissingArgumentException(array('name', 'email'));
129+
}
130+
$parameters['committer'] = $committer;
131+
}
132+
112133
return $this->put($url, $parameters);
113134
}
114135

@@ -123,10 +144,13 @@ public function update($username, $repository, $path, $content, $message, $sha,
123144
* @param string $message the commit message
124145
* @param string $sha blob SHA of the file being deleted
125146
* @param null|string $branch name of a branch
147+
* @param null|array $committer information about the committer
148+
*
149+
* @throws MissingArgumentException
126150
*
127151
* @return array information about the updated file
128152
*/
129-
public function rm($username, $repository, $path, $message, $sha, $branch = null)
153+
public function rm($username, $repository, $path, $message, $sha, $branch = null, array $committer = null)
130154
{
131155
$url = 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents/'.rawurlencode($path);
132156

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

166+
if (null !== $committer) {
167+
if (!isset($committer['name'], $committer['email'])) {
168+
throw new MissingArgumentException(array('name', 'email'));
169+
}
170+
$parameters['committer'] = $committer;
171+
}
172+
142173
return $this->delete($url, $parameters);
143174
}
144175

test/Github/Tests/Api/Repository/ContentsTest.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,122 @@ public function shouldShowReadme()
3838
$this->assertEquals($expectedValue, $api->readme('KnpLabs', 'php-github-api'));
3939
}
4040

41+
/**
42+
* @test
43+
*/
44+
public function shouldCreateNewFile()
45+
{
46+
$expectedArray = array('content' => 'some data');
47+
$content = '<?php //..';
48+
$message = 'a commit message';
49+
$branch = 'master';
50+
$committer = array('name' => 'committer name', 'email' => 'email@example.com');
51+
$parameters = array(
52+
'content' => base64_encode($content),
53+
'message' => $message,
54+
'committer' => $committer,
55+
'branch' => $branch,
56+
);
57+
58+
$api = $this->getApiMock();
59+
$api->expects($this->once())
60+
->method('put')
61+
->with('repos/KnpLabs/php-github-api/contents/test%2FGithub%2FTests%2FApi%2FRepository%2FContentsTest.php', $parameters)
62+
->will($this->returnValue($expectedArray));
63+
64+
$this->assertEquals($expectedArray, $api->create('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', $content, $message, $branch, $committer));
65+
}
66+
67+
/**
68+
* @test
69+
* @expectedException Github\Exception\MissingArgumentException
70+
* @expectedExceptionMessage One or more of required ("name", "email") parameters is missing!
71+
*/
72+
public function shouldThrowExceptionWhenCreateNewFileWithInvalidCommitter()
73+
{
74+
$committer = array('invalid_key' => 'some data');
75+
$api = $this->getApiMock();
76+
$api->create('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', 'some content', 'a commit message', null, $committer);
77+
}
78+
79+
/**
80+
* @test
81+
*/
82+
public function shouldUpdateFile()
83+
{
84+
$expectedArray = array('content' => 'some data');
85+
$content = '<?php //..';
86+
$message = 'a commit message';
87+
$sha = 'a sha';
88+
$branch = 'master';
89+
$committer = array('name' => 'committer name', 'email' => 'email@example.com');
90+
$parameters = array(
91+
'content' => base64_encode($content),
92+
'message' => $message,
93+
'committer' => $committer,
94+
'branch' => $branch,
95+
'sha' => $sha,
96+
);
97+
98+
$api = $this->getApiMock();
99+
$api->expects($this->once())
100+
->method('put')
101+
->with('repos/KnpLabs/php-github-api/contents/test%2FGithub%2FTests%2FApi%2FRepository%2FContentsTest.php', $parameters)
102+
->will($this->returnValue($expectedArray));
103+
104+
$this->assertEquals($expectedArray, $api->update('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', $content, $message, $sha, $branch, $committer));
105+
}
106+
107+
/**
108+
* @test
109+
* @expectedException Github\Exception\MissingArgumentException
110+
* @expectedExceptionMessage One or more of required ("name", "email") parameters is missing!
111+
*/
112+
public function shouldThrowExceptionWhenUpdateFileWithInvalidCommitter()
113+
{
114+
$committer = array('invalid_key' => 'some data');
115+
$api = $this->getApiMock();
116+
$api->update('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', 'some content', 'a commit message', null, null, $committer);
117+
}
118+
119+
/**
120+
* @test
121+
*/
122+
public function shouldDeleteFile()
123+
{
124+
$expectedArray = array('content' => 'some data');
125+
$message = 'a commit message';
126+
$sha = 'a sha';
127+
$branch = 'master';
128+
$committer = array('name' => 'committer name', 'email' => 'email@example.com');
129+
$parameters = array(
130+
'message' => $message,
131+
'committer' => $committer,
132+
'branch' => $branch,
133+
'sha' => $sha,
134+
);
135+
136+
$api = $this->getApiMock();
137+
$api->expects($this->once())
138+
->method('delete')
139+
->with('repos/KnpLabs/php-github-api/contents/test%2FGithub%2FTests%2FApi%2FRepository%2FContentsTest.php', $parameters)
140+
->will($this->returnValue($expectedArray));
141+
142+
$this->assertEquals($expectedArray, $api->rm('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', $message, $sha, $branch, $committer));
143+
}
144+
145+
/**
146+
* @test
147+
* @expectedException Github\Exception\MissingArgumentException
148+
* @expectedExceptionMessage One or more of required ("name", "email") parameters is missing!
149+
*/
150+
public function shouldThrowExceptionWhenDeleteFileWithInvalidCommitter()
151+
{
152+
$committer = array('invalid_key' => 'some data');
153+
$api = $this->getApiMock();
154+
$api->rm('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', 'a commit message', null, null, $committer);
155+
}
156+
41157
/**
42158
* @test
43159
*/

0 commit comments

Comments
 (0)