Skip to content

Commit eea5b43

Browse files
committed
Fixed CommitApi methods and RepoApi getRepoContributors method
1 parent 6df3adf commit eea5b43

File tree

8 files changed

+117
-24
lines changed

8 files changed

+117
-24
lines changed

lib/Github/Api/Commit.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Commit extends Api
1414
{
1515
/**
1616
* List commits by username, repo and branch
17-
* http://develop.github.com/p/commits.html#listing_commits_on_a_branch
17+
* http://developer.github.com/v3/repos/commits/
1818
*
1919
* @param string $username the username
2020
* @param string $repo the repo
@@ -23,14 +23,14 @@ class Commit extends Api
2323
*/
2424
public function getBranchCommits($username, $repo, $branch)
2525
{
26-
$response = $this->get('commits/list/'.urlencode($username).'/'.urlencode($repo).'/'.urlencode($branch));
26+
$branchSha = $this->getBranchSha($username, $repo, $branch);
2727

28-
return $response['commits'];
28+
return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/commits?sha='.urlencode($branchSha));
2929
}
3030

3131
/**
3232
* List commits by username, repo, branch and path
33-
* http://develop.github.com/p/commits.html#listing_commits_for_a_file
33+
* http://developer.github.com/v3/repos/commits/
3434
*
3535
* @param string $username the username
3636
* @param string $repo the repo
@@ -40,23 +40,37 @@ public function getBranchCommits($username, $repo, $branch)
4040
*/
4141
public function getFileCommits($username, $repo, $branch, $path)
4242
{
43-
$response = $this->get('commits/list/'.urlencode($username).'/'.urlencode($repo).'/'.urlencode($branch).'/'.urlencode($path));
43+
$branchSha = $this->getBranchSha($username, $repo, $branch);
4444

45-
return $response['commits'];
45+
return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/commits?path='.urlencode($path).'&sha='.urlencode($branchSha));
4646
}
4747

4848
/**
4949
* Show a specific commit
50-
* http://develop.github.com/p/commits.html#showing_a_specific_commit
50+
* http://developer.github.com/v3/repos/commits/
5151
*
5252
* @param string $username the username
5353
* @param string $repo the repo
5454
* @param string $sha the commit sha
5555
*/
5656
public function getCommit($username, $repo, $sha)
5757
{
58-
$response = $this->get('commits/show/'.urlencode($username).'/'.urlencode($repo).'/'.urlencode($sha));
58+
return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/commits/'.urlencode($sha));
59+
}
5960

60-
return $response['commit'];
61+
/**
62+
* Fetch branch sha from branch name
63+
*
64+
* @param string $username
65+
* @param string $repoName
66+
* @param string $branchName
67+
* @return string
68+
*/
69+
private function getBranchSha($username, $repoName, $branchName)
70+
{
71+
$branchInfo = $this->get('repos/'.urlencode($username).'/'.urlencode($repoName).'/git/trees/'.urlencode($branchName));
72+
if (isset($branchInfo['sha'])) {
73+
return $branchInfo['sha'];
74+
}
6175
}
6276
}

lib/Github/Api/Object.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ public function showBlob($username, $repo, $treeSHA, $path)
7272
*/
7373
public function getRawData($username, $repo, $objectSHA)
7474
{
75-
$response = $this->get('blob/show/'.urlencode($username).'/'.urlencode($repo).'/'.urlencode($objectSHA), array(), array(
76-
'format' => 'text'
77-
));
75+
$response = $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/git/blobs/'.urlencode($objectSHA));
7876

7977
return $response;
8078
}

lib/Github/Api/Repo.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,13 @@ public function getRepoLanguages($username, $repo)
390390
*/
391391
public function getRepoContributors($username, $repo, $includingNonGithubUsers = false)
392392
{
393-
$url = 'repos/show/'.urlencode($username).'/'.urlencode($repo).'/contributors';
393+
$url = 'repos/'.urlencode($username).'/'.urlencode($repo).'/contributors';
394394
if ($includingNonGithubUsers) {
395-
$url .= '/anon';
395+
$url .= '?anon=1';
396396
}
397397
$response = $this->get($url);
398398

399-
return $response['contributors'];
399+
return $response;
400400
}
401401

402402
}

lib/Github/HttpClient/HttpClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class HttpClient implements HttpClientInterface
1818
* @var array
1919
*/
2020
protected $options = array(
21-
'url' => 'https://github.com/api/v3/:path/:format',
21+
'url' => 'https://api.github.com/:path',
2222
'user_agent' => 'php-github-api (http://github.com/KnpLabs/php-github-api)',
2323
'http_port' => 443,
2424
'timeout' => 10,
@@ -119,7 +119,7 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
119119
$response = $this->doRequest($url, $parameters, $httpMethod, $options);
120120

121121
// decode response
122-
$response = $this->decodeResponse($response);
122+
$response = $this->decodeResponse($response['response']);
123123

124124
return $response;
125125
}
@@ -179,7 +179,7 @@ protected function checkApiLimit(Response $response)
179179
{
180180
$limit = $response->getHeader('X-RateLimit-Remaining');
181181

182-
if (null !== $limit && 1 < $limit) {
182+
if (null !== $limit && 1 > $limit) {
183183
throw new \RuntimeException('You have reached GitHub hour limit! Actual limit is: '. $this->options['api_limit']);
184184
}
185185
}

test/Github/Tests/Api/CommitTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ public function testGetBranchCommits()
1010
{
1111
$api = $this->getApiMock();
1212

13-
$api->expects($this->once())
13+
$expectedValue = array('commit' => array(), 'comitter');
14+
15+
$api->expects($this->at(0))
16+
->method('get')
17+
->will($this->returnValue(array('sha' => '123')));
18+
19+
$api->expects($this->at(1))
1420
->method('get')
15-
->with('commits/list/ornicar/php-github-api/v3');
21+
->with($this->equalTo('repos/ornicar/php-github-api/commits?sha=123'))
22+
->will($this->returnValue($expectedValue));
1623

17-
$api->getBranchCommits('ornicar', 'php-github-api', 'v3');
24+
$this->assertEquals($expectedValue, $api->getBranchCommits('ornicar', 'php-github-api', 'v3'));
1825
}
1926

2027
protected function getApiClass()

test/Github/Tests/Functional/CommitTest.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
class CommitTest extends \PHPUnit_Framework_TestCase
88
{
9-
public function testGetCommits()
9+
/**
10+
* @test
11+
*/
12+
public function shouldRetrieveCommitsForRepositoryBranch()
1013
{
11-
$username = 'ornicar';
14+
$username = 'KnpLabs';
1215
$repo = 'php-github-api';
1316
$branch = 'master';
1417

@@ -17,5 +20,48 @@ public function testGetCommits()
1720
$commit = array_pop($commits);
1821

1922
$this->assertArrayHasKey('url', $commit);
23+
$this->assertArrayHasKey('committer', $commit);
24+
$this->assertArrayHasKey('author', $commit);
25+
$this->assertArrayHasKey('commit', $commit);
26+
$this->assertArrayHasKey('sha', $commit);
27+
}
28+
29+
/**
30+
* @test
31+
*/
32+
public function shouldRetrieveCommitBySha()
33+
{
34+
$username = 'KnpLabs';
35+
$repo = 'php-github-api';
36+
37+
$github = new Client();
38+
$commit = $github->getCommitApi()->getCommit($username, $repo, '6df3adf5bd16745299c6429e163265daed430fa1');
39+
40+
$this->assertArrayHasKey('url', $commit);
41+
$this->assertArrayHasKey('committer', $commit);
42+
$this->assertArrayHasKey('author', $commit);
43+
$this->assertArrayHasKey('commit', $commit);
44+
$this->assertArrayHasKey('sha', $commit);
45+
$this->assertArrayHasKey('files', $commit);
46+
}
47+
48+
/**
49+
* @test
50+
*/
51+
public function shouldRetrieveCommitsForFile()
52+
{
53+
$username = 'KnpLabs';
54+
$repo = 'php-github-api';
55+
$branch = 'api_v3';
56+
57+
$github = new Client();
58+
$commits = $github->getCommitApi()->getFileCommits($username, $repo, $branch, 'composer.json');
59+
$commit = array_pop($commits);
60+
61+
$this->assertArrayHasKey('url', $commit);
62+
$this->assertArrayHasKey('committer', $commit);
63+
$this->assertArrayHasKey('author', $commit);
64+
$this->assertArrayHasKey('commit', $commit);
65+
$this->assertArrayHasKey('sha', $commit);
2066
}
2167
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Github\Tests\Functional;
4+
5+
use Github\Client;
6+
7+
class RepoTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* @test
11+
*/
12+
public function shouldRetrieveContributorsList()
13+
{
14+
$username = 'KnpLabs';
15+
$repo = 'php-github-api';
16+
17+
$github = new Client();
18+
$contributors = $github->getRepoApi()->getRepoContributors($username, $repo);
19+
$contributor = array_pop($contributors);
20+
21+
$this->assertArrayHasKey('url', $contributor);
22+
$this->assertArrayHasKey('gravatar_id', $contributor);
23+
$this->assertArrayHasKey('contributions', $contributor);
24+
$this->assertArrayHasKey('avatar_url', $contributor);
25+
$this->assertArrayHasKey('login', $contributor);
26+
$this->assertArrayHasKey('id', $contributor);
27+
}
28+
}

test/Github/Tests/HttpClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testRequest()
6060
->getMock();
6161
$httpClient->expects($this->once())
6262
->method('doRequest')
63-
->will($this->returnValue('response'));
63+
->will($this->returnValue(array('response' => 'response')));
6464
$httpClient->expects($this->once())
6565
->method('decodeResponse')
6666
->with('response')

0 commit comments

Comments
 (0)