Skip to content
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

Handle case of GitHub returning 204 No Content in some scenarios #1135

Merged
merged 5 commits into from
Mar 24, 2024
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
4 changes: 4 additions & 0 deletions lib/Github/ResultPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public function fetch(AbstractApi $api, string $method, array $parameters = []):
$api = $closure($api);
$result = $api->$method(...$parameters);

if ($result === '' && $this->client->getLastResponse()->getStatusCode() === 204) {
$result = [];
}

$this->postFetch(true);

return $result;
Expand Down
65 changes: 64 additions & 1 deletion test/Github/Tests/ResultPagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Github\Api\Issue;
use Github\Api\Organization\Members;
use Github\Api\Repo;
use Github\Api\Repository\Statuses;
use Github\Api\Search;
use Github\Client;
Expand Down Expand Up @@ -116,6 +117,40 @@ public function shouldGetAllSearchResults()
$this->assertCount($amountLoops * count($content['items']), $result);
}

/**
* @test
*/
public function shouldHandleEmptyContributorListWith204Header()
{
// Set up a 204 response with an empty body
$response = new Response(204, [], '');
$username = 'testuser';
$reponame = 'testrepo';

// Mock the HttpClient to return the empty response
$httpClientMock = $this->getMockBuilder(HttpClient::class)
->onlyMethods(['sendRequest'])
->getMock();
$httpClientMock
->method('sendRequest')
->willReturn($response);

$client = Client::createWithHttpClient($httpClientMock);

$repoApi = new Repo($client);

$paginator = $this->getMockBuilder(ResultPager::class)
->setConstructorArgs([$client]) // Pass the Client in the constructor
->onlyMethods(['fetchAll'])
->getMock();
$paginator->expects($this->once())
->method('fetchAll')
->with($repoApi, 'contributors', [$username, $reponame])
->willReturn([]);

$this->assertEquals([], $paginator->fetchAll($repoApi, 'contributors', [$username, $reponame]));
}

public function testFetch()
{
$result = ['foo'];
Expand Down Expand Up @@ -152,7 +187,7 @@ public function testFetchAllPreserveKeys()
'sha' => '43068834af7e501778708ed13106de95f782328c',
];

$response = new Response(200, ['Content-Type'=>'application/json'], Utils::streamFor(json_encode($content)));
$response = new Response(200, ['Content-Type' => 'application/json'], Utils::streamFor(json_encode($content)));

// httpClient mock
$httpClientMock = $this->getMockBuilder(HttpClient::class)
Expand Down Expand Up @@ -201,6 +236,34 @@ public function testFetchAllWithoutKeys()
$this->assertCount(9, $result);
}

public function testFetchAll()
{
$content = [
['title' => 'issue 1'],
['title' => 'issue 2'],
['title' => 'issue 3'],
];

$response = new PaginatedResponse(3, $content);

// httpClient mock
$httpClientMock = $this->getMockBuilder(HttpClient::class)
->onlyMethods(['sendRequest'])
->getMock();
$httpClientMock
->expects($this->exactly(3))
->method('sendRequest')
->willReturn($response);

$client = Client::createWithHttpClient($httpClientMock);

$api = new Issue($client);
$paginator = new ResultPager($client);
$result = $paginator->fetchAll($api, 'all', ['knplabs', 'php-github-api']);

$this->assertCount(9, $result);
}

/**
* @group legacy
*/
Expand Down