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

Add the ability to download raw file, needed when size > 1MB #1075

Merged
merged 3 commits into from
Aug 15, 2022
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
34 changes: 28 additions & 6 deletions lib/Github/Api/Repository/Contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ public function readme($username, $repository, $reference = null)
*
* @link http://developer.github.com/v3/repos/contents/
*
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param string|null $path path to file or directory
* @param string|null $reference reference to a branch or commit
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param string|null $path path to file or directory
* @param string|null $reference reference to a branch or commit
* @param array $requestHeaders request headers
*
* @return array|string information for file | information for each item in directory
*/
public function show($username, $repository, $path = null, $reference = null)
public function show($username, $repository, $path = null, $reference = null, $requestHeaders = [])
{
$url = '/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents';
if (null !== $path) {
Expand All @@ -77,7 +78,7 @@ public function show($username, $repository, $path = null, $reference = null)

return $this->get($url, [
'ref' => $reference,
]);
], $requestHeaders);
}

/**
Expand Down Expand Up @@ -294,4 +295,25 @@ public function download($username, $repository, $path, $reference = null)

return base64_decode($file['content']) ?: null;
}

/**
* Get the raw content of a file in a repository.
*
* Use this method instead of the download method if your file is bigger than 1MB
*
* @see https://docs.github.com/en/rest/repos/contents
*
* @param string $username the user who owns the repository
* @param string $repository the name of the repository
* @param string $path path to file
* @param string|null $reference reference to a branch or commit
*
* @return array|string
*/
public function rawDownload($username, $repository, $path, $reference = null)
{
return $this->show($username, $repository, $path, $reference, [
'Accept' => 'application/vnd.github.VERSION.raw',
]);
}
}
17 changes: 17 additions & 0 deletions test/Github/Tests/Api/Repository/ContentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,23 @@ public function shouldDownloadForSpacedPath()
$this->assertEquals($expectedValue, $api->download('mads379', 'scala.tmbundle', 'Syntaxes/Simple Build Tool.tmLanguage'));
}

/**
* @test
*/
public function shouldRawDownloadForGivenPath()
{
// The show() method return
$getValue = include __DIR__.'/fixtures/ContentsDownloadFixture.php';

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('/repos/KnpLabs/php-github-api/contents/test%2FGithub%2FTests%2FApi%2FRepository%2FContentsTest.php', ['ref' => null])
->will($this->returnValue($getValue));

$this->assertEquals($getValue, $api->rawDownload('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php'));
}

/**
* @return string
*/
Expand Down