Skip to content

Commit 6f5577d

Browse files
committed
Merge branch 'master' into protection-configure
2 parents 92870e2 + e306f07 commit 6f5577d

20 files changed

+215
-44
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
44

5+
## 2.2.0
6+
7+
### Added
8+
9+
- API support for Reviews.
10+
- API support for Traffic.
11+
- API support for issue Assignees.
12+
- API support for Miscellaneous Gitignore and Emojis.
13+
- Added endpoints for issue lock, unlock and issue label show.
14+
- Added more parameters to `User::starred`.
15+
- Fluid interface by allowing `configure()` to return `$this`.
16+
- `configure()` support for issues API.
17+
18+
### Fixed
19+
20+
- Cache issue where some requests are not cached
21+
- Issue with `User::all()` creates a query with double question marks.
22+
523
## 2.1.0
624

725
### Added

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,24 @@
2424
"php-http/discovery": "^1.0",
2525
"php-http/client-implementation": "^1.0",
2626
"php-http/client-common": "^1.3",
27-
"php-http/cache-plugin": "^1.2"
27+
"php-http/cache-plugin": "^1.4"
2828
},
2929
"require-dev": {
3030
"phpunit/phpunit": "^4.0 || ^5.5",
3131
"php-http/guzzle6-adapter": "^1.0",
32+
"php-http/mock-client": "^1.0",
3233
"guzzlehttp/psr7": "^1.2",
33-
"sllh/php-cs-fixer-styleci-bridge": "^1.3"
34+
"sllh/php-cs-fixer-styleci-bridge": "^1.3",
35+
"cache/array-adapter": "^0.4"
3436
},
3537
"autoload": {
3638
"psr-4": { "Github\\": "lib/Github/" }
3739
},
40+
"minimum-stability": "dev",
41+
"prefer-stable": true,
3842
"extra": {
3943
"branch-alias": {
40-
"dev-master": "2.1.x-dev"
44+
"dev-master": "2.3.x-dev"
4145
}
4246
}
4347
}

doc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ Additional features:
4848
* [Request any Route](request_any_route.md)
4949
* [Customize `php-github-api`](customize.md)
5050
* [Running and writing tests](testing.md)
51+
* [Response caching](caching.md)
5152
* [Request / Response info](request_response_info.md)

doc/caching.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Response caching
2+
[Back to the navigation](README.md)
3+
4+
This example uses the PSR6 cache pool [redis-adapter](https://github.com/php-cache/redis-adapter). See http://www.php-cache.com/ for alternatives.
5+
6+
```php
7+
<?php
8+
9+
// This file is generated by Composer
10+
require_once 'vendor/autoload.php';
11+
12+
use Cache\Adapter\Redis\RedisCachePool;
13+
14+
$client = new \Redis();
15+
$client->connect('127.0.0.1', 6379);
16+
// Create a PSR6 cache pool
17+
$pool = new RedisCachePool($client);
18+
19+
$client = new \Github\Client();
20+
$client->addCache($pool);
21+
22+
// Do some request
23+
24+
// Stop using cache
25+
$client->removeCache();
26+
```
27+
28+
Using cache, the client will get cached responses if resources haven't changed since last time,
29+
**without** reaching the `X-Rate-Limit` [imposed by github](http://developer.github.com/v3/#rate-limiting).
30+

lib/Github/Api/Integrations.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,14 @@ public function findInstallations()
4848
*
4949
* @return array
5050
*/
51-
public function listRepositories($userId)
51+
public function listRepositories($userId = null)
5252
{
53-
return $this->get('/installation/repositories', ['user_id' => $userId]);
53+
$parameters = array();
54+
if ($userId) {
55+
$parameters['user_id'] = $userId;
56+
}
57+
58+
return $this->get('/installation/repositories', $parameters);
5459
}
5560

5661
/**

lib/Github/Api/PullRequest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
/**
1212
* API for accessing Pull Requests from your Git/Github repositories.
1313
*
14-
* @link http://developer.github.com/v3/pulls/
14+
* @see http://developer.github.com/v3/pulls/
15+
*
1516
* @author Joseph Bielawski <[email protected]>
1617
*/
1718
class PullRequest extends AbstractApi
@@ -61,7 +62,7 @@ public function all($username, $repository, array $params = array())
6162
{
6263
$parameters = array_merge(array(
6364
'page' => 1,
64-
'per_page' => 30
65+
'per_page' => 30,
6566
), $params);
6667

6768
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls', $parameters);
@@ -93,6 +94,24 @@ public function files($username, $repository, $id)
9394
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/pulls/'.rawurlencode($id).'/files');
9495
}
9596

97+
/**
98+
* All statuses which are the statuses of its head branch.
99+
*
100+
* @see http://developer.github.com/v3/pulls/
101+
*
102+
* @param string $username the username
103+
* @param string $repository the repository
104+
* @param string $id the ID of the pull request for which statuses are retrieved
105+
*
106+
* @return array array of statuses for the project
107+
*/
108+
public function status($username, $repository, $id)
109+
{
110+
$link = $this->show($username, $repository, $id)['_links']['statuses']['href'];
111+
112+
return $this->get($link);
113+
}
114+
96115
public function comments()
97116
{
98117
return new Comments($this->client);

lib/Github/HttpClient/Builder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Http\Message\RequestFactory;
1414
use Http\Message\StreamFactory;
1515
use Psr\Cache\CacheItemPoolInterface;
16+
use Http\Client\Common\Plugin\Cache\Generator\HeaderCacheKeyGenerator;
1617

1718
/**
1819
* A builder that builds the API client.
@@ -176,12 +177,15 @@ public function addHeaderValue($header, $headerValue)
176177
/**
177178
* Add a cache plugin to cache responses locally.
178179
*
179-
* @param CacheItemPoolInterface $cache
180+
* @param CacheItemPoolInterface $cachePool
180181
* @param array $config
181182
*/
182183
public function addCache(CacheItemPoolInterface $cachePool, array $config = [])
183184
{
184-
$this->cachePlugin = new Plugin\CachePlugin($cachePool, $this->streamFactory, $config);
185+
if (!isset($config['cache_key_generator'])) {
186+
$config['cache_key_generator'] = new HeaderCacheKeyGenerator(['Authorization', 'Cookie', 'Accept', 'Content-type']);
187+
}
188+
$this->cachePlugin = Plugin\CachePlugin::clientCache($cachePool, $this->streamFactory, $config);
185189
$this->httpClientModified = true;
186190
}
187191

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<groups>
2121
<exclude>
22-
<group>functional</group>
22+
<group>integration</group>
2323
</exclude>
2424
</groups>
2525

test/Github/Tests/Api/PullRequestTest.php

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,28 @@ public function shouldShowFilesFromPullRequest()
101101
$this->assertEquals($expectedArray, $api->files('ezsystems', 'ezpublish', '15'));
102102
}
103103

104+
/**
105+
* @test
106+
*/
107+
public function shouldShowStatusesFromPullRequest()
108+
{
109+
$expectedArray = array(array('id' => 'id', 'sha' => '123123'));
110+
$expectedArray['_links']['statuses']['href'] = '/repos/ezsystems/ezpublish/pulls/15/statuses';
111+
112+
$api = $this->getApiMock();
113+
$api->expects($this->at(0))
114+
->method('get')
115+
->with('/repos/ezsystems/ezpublish/pulls/15')
116+
->will($this->returnValue($expectedArray));
117+
118+
$api->expects($this->at(1))
119+
->method('get')
120+
->with('/repos/ezsystems/ezpublish/pulls/15/statuses')
121+
->will($this->returnValue($expectedArray));
122+
123+
$this->assertEquals($expectedArray, $api->status('ezsystems', 'ezpublish', '15'));
124+
}
125+
104126
/**
105127
* @test
106128
*/
@@ -187,10 +209,10 @@ public function shouldMergePullRequestWithMergeMethod()
187209
public function shouldCreatePullRequestUsingTitle()
188210
{
189211
$data = array(
190-
'base' => 'master',
191-
'head' => 'virtualtestbranch',
212+
'base' => 'master',
213+
'head' => 'virtualtestbranch',
192214
'title' => 'TITLE: Testing pull-request creation from PHP Github API',
193-
'body' => 'BODY: Testing pull-request creation from PHP Github API'
215+
'body' => 'BODY: Testing pull-request creation from PHP Github API',
194216
);
195217

196218
$api = $this->getApiMock();
@@ -207,9 +229,9 @@ public function shouldCreatePullRequestUsingTitle()
207229
public function shouldCreatePullRequestUsingIssueId()
208230
{
209231
$data = array(
210-
'base' => 'master',
211-
'head' => 'virtualtestbranch',
212-
'issue' => 25
232+
'base' => 'master',
233+
'head' => 'virtualtestbranch',
234+
'issue' => 25,
213235
);
214236

215237
$api = $this->getApiMock();
@@ -227,9 +249,9 @@ public function shouldCreatePullRequestUsingIssueId()
227249
public function shouldNotCreatePullRequestWithoutBase()
228250
{
229251
$data = array(
230-
'head' => 'virtualtestbranch',
252+
'head' => 'virtualtestbranch',
231253
'title' => 'TITLE: Testing pull-request creation from PHP Github API',
232-
'body' => 'BODY: Testing pull-request creation from PHP Github API'
254+
'body' => 'BODY: Testing pull-request creation from PHP Github API',
233255
);
234256

235257
$api = $this->getApiMock();
@@ -246,9 +268,9 @@ public function shouldNotCreatePullRequestWithoutBase()
246268
public function shouldNotCreatePullRequestWithoutHead()
247269
{
248270
$data = array(
249-
'base' => 'master',
271+
'base' => 'master',
250272
'title' => 'TITLE: Testing pull-request creation from PHP Github API',
251-
'body' => 'BODY: Testing pull-request creation from PHP Github API'
273+
'body' => 'BODY: Testing pull-request creation from PHP Github API',
252274
);
253275

254276
$api = $this->getApiMock();
@@ -265,8 +287,8 @@ public function shouldNotCreatePullRequestWithoutHead()
265287
public function shouldNotCreatePullRequestUsingTitleButWithoutBody()
266288
{
267289
$data = array(
268-
'base' => 'master',
269-
'head' => 'virtualtestbranch',
290+
'base' => 'master',
291+
'head' => 'virtualtestbranch',
270292
'title' => 'TITLE: Testing pull-request creation from PHP Github API',
271293
);
272294

@@ -284,8 +306,8 @@ public function shouldNotCreatePullRequestUsingTitleButWithoutBody()
284306
public function shouldNotCreatePullRequestWithoutIssueIdOrTitle()
285307
{
286308
$data = array(
287-
'base' => 'master',
288-
'head' => 'virtualtestbranch',
309+
'base' => 'master',
310+
'head' => 'virtualtestbranch',
289311
);
290312

291313
$api = $this->getApiMock();
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Github\Tests\Functional;
4+
5+
use Cache\Adapter\PHPArray\ArrayCachePool;
6+
use Github\Client;
7+
use GuzzleHttp\Psr7\Response;
8+
9+
/**
10+
* @group functional
11+
*
12+
* @author Tobias Nyholm <[email protected]>
13+
*/
14+
class CacheTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @test
18+
*/
19+
public function shouldServeCachedResponse()
20+
{
21+
$mockClient = new \Http\Mock\Client();
22+
$mockClient->addResponse($this->getCurrentUserResponse('nyholm'));
23+
$mockClient->addResponse($this->getCurrentUserResponse('octocat'));
24+
25+
$github = Client::createWithHttpClient($mockClient);
26+
$github->addCache(new ArrayCachePool(), ['default_ttl'=>600]);
27+
28+
$github->authenticate('fake_token_aaa', Client::AUTH_HTTP_TOKEN);
29+
$userA = $github->currentUser()->show();
30+
$this->assertEquals('nyholm', $userA['login']);
31+
32+
$userB = $github->currentUser()->show();
33+
$this->assertEquals('nyholm', $userB['login'], 'Two request following each other should be cached.');
34+
}
35+
/**
36+
* @test
37+
*/
38+
public function shouldVaryOnAuthorization()
39+
{
40+
$mockClient = new \Http\Mock\Client();
41+
$mockClient->addResponse($this->getCurrentUserResponse('nyholm'));
42+
$mockClient->addResponse($this->getCurrentUserResponse('octocat'));
43+
44+
$github = Client::createWithHttpClient($mockClient);
45+
$github->addCache(new ArrayCachePool(), ['default_ttl'=>600]);
46+
47+
$github->authenticate('fake_token_aaa', Client::AUTH_HTTP_TOKEN);
48+
$userA = $github->currentUser()->show();
49+
$this->assertEquals('nyholm', $userA['login']);
50+
51+
$github->authenticate('fake_token_bbb', Client::AUTH_HTTP_TOKEN);
52+
$userB = $github->currentUser()->show();
53+
$this->assertEquals('octocat', $userB['login'], 'We must vary on the Authorization header.');
54+
}
55+
56+
private function getCurrentUserResponse($username)
57+
{
58+
$headers = [
59+
'Content-Type' => 'application/json',
60+
];
61+
62+
$body = \GuzzleHttp\Psr7\stream_for(json_encode([
63+
'login' => $username,
64+
]));
65+
66+
return new Response(200, $headers, $body);
67+
}
68+
}

test/Github/Tests/Functional/CommitTest.php renamed to test/Github/Tests/Integration/CommitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Github\Tests\Functional;
3+
namespace Github\Tests\Integration;
44

55
/**
6-
* @group functional
6+
* @group integration
77
*/
88
class CommitTest extends TestCase
99
{

test/Github/Tests/Functional/GistTest.php renamed to test/Github/Tests/Integration/GistTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Github\Tests\Functional;
3+
namespace Github\Tests\Integration;
44

55
/**
6-
* @group functional
6+
* @group integration
77
*/
88
class GistTest extends TestCase
99
{

test/Github/Tests/Functional/IssueCommentTest.php renamed to test/Github/Tests/Integration/IssueCommentTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Github\Tests\Functional;
3+
namespace Github\Tests\Integration;
44

55
/**
6-
* @group functional
6+
* @group integration
77
*/
88
class IssueCommentTest extends TestCase
99
{

0 commit comments

Comments
 (0)