From 520ffb9c2c6887bb5c25c32f515a8f93077635c0 Mon Sep 17 00:00:00 2001 From: Thomas Genin Date: Tue, 5 Jul 2022 20:27:24 -0700 Subject: [PATCH 1/3] Add the ability to download raw file, needed when size > 1MB --- lib/Github/Api/Repository/Contents.php | 31 ++++++++++++++++--- .../Tests/Api/Repository/ContentsTest.php | 18 +++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/lib/Github/Api/Repository/Contents.php b/lib/Github/Api/Repository/Contents.php index bc78503b2f9..ffc27b6780e 100644 --- a/lib/Github/Api/Repository/Contents.php +++ b/lib/Github/Api/Repository/Contents.php @@ -65,10 +65,11 @@ public function readme($username, $repository, $reference = null) * @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) { @@ -77,7 +78,7 @@ public function show($username, $repository, $path = null, $reference = null) return $this->get($url, [ 'ref' => $reference, - ]); + ], $requestHeaders); } /** @@ -272,9 +273,11 @@ public function archive($username, $repository, $format, $reference = null) * * @return string|null content of file, or null in case of base64_decode failure */ - public function download($username, $repository, $path, $reference = null) + public function download($username, $repository, $path, $reference = null, $bodyType = null) { - $file = $this->show($username, $repository, $path, $reference); + $file = $this->show($username, $repository, $path, $reference,[ + 'Accept' => 'application/vnd.github.VERSION.raw' + ]); if (!isset($file['type']) || !in_array($file['type'], ['file', 'symlink'], true)) { throw new InvalidArgumentException(sprintf('Path "%s" is not a file or a symlink to a file.', $path)); @@ -294,4 +297,24 @@ 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' + ]); + } } diff --git a/test/Github/Tests/Api/Repository/ContentsTest.php b/test/Github/Tests/Api/Repository/ContentsTest.php index 122fbf5cdf9..2632c9eb649 100644 --- a/test/Github/Tests/Api/Repository/ContentsTest.php +++ b/test/Github/Tests/Api/Repository/ContentsTest.php @@ -319,6 +319,24 @@ 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 */ From ff458a2cab21cd3939eb48a2da98c0400768fb07 Mon Sep 17 00:00:00 2001 From: Thomas Genin Date: Tue, 5 Jul 2022 20:30:33 -0700 Subject: [PATCH 2/3] Restore modification committed by mistake --- lib/Github/Api/Repository/Contents.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/Github/Api/Repository/Contents.php b/lib/Github/Api/Repository/Contents.php index ffc27b6780e..8c38b569763 100644 --- a/lib/Github/Api/Repository/Contents.php +++ b/lib/Github/Api/Repository/Contents.php @@ -273,11 +273,9 @@ public function archive($username, $repository, $format, $reference = null) * * @return string|null content of file, or null in case of base64_decode failure */ - public function download($username, $repository, $path, $reference = null, $bodyType = null) + public function download($username, $repository, $path, $reference = null) { - $file = $this->show($username, $repository, $path, $reference,[ - 'Accept' => 'application/vnd.github.VERSION.raw' - ]); + $file = $this->show($username, $repository, $path, $reference); if (!isset($file['type']) || !in_array($file['type'], ['file', 'symlink'], true)) { throw new InvalidArgumentException(sprintf('Path "%s" is not a file or a symlink to a file.', $path)); From e7c393ffae6dbbfd70d455eafc887d0b85619f40 Mon Sep 17 00:00:00 2001 From: Thomas Genin Date: Thu, 11 Aug 2022 11:58:12 +0200 Subject: [PATCH 3/3] Style fix --- lib/Github/Api/Repository/Contents.php | 17 +++++++++-------- .../Tests/Api/Repository/ContentsTest.php | 1 - 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/Github/Api/Repository/Contents.php b/lib/Github/Api/Repository/Contents.php index 8c38b569763..4f9693dcbe1 100644 --- a/lib/Github/Api/Repository/Contents.php +++ b/lib/Github/Api/Repository/Contents.php @@ -61,11 +61,11 @@ 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 array $requestHeaders request headers + * @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 */ @@ -297,7 +297,7 @@ public function download($username, $repository, $path, $reference = null) } /** - * Get the raw content of a file in a repository + * 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 * @@ -310,9 +310,10 @@ public function download($username, $repository, $path, $reference = null) * * @return array|string */ - public function rawDownload($username, $repository, $path, $reference = null) { + public function rawDownload($username, $repository, $path, $reference = null) + { return $this->show($username, $repository, $path, $reference, [ - 'Accept' => 'application/vnd.github.VERSION.raw' + 'Accept' => 'application/vnd.github.VERSION.raw', ]); } } diff --git a/test/Github/Tests/Api/Repository/ContentsTest.php b/test/Github/Tests/Api/Repository/ContentsTest.php index 2632c9eb649..1cc828c370d 100644 --- a/test/Github/Tests/Api/Repository/ContentsTest.php +++ b/test/Github/Tests/Api/Repository/ContentsTest.php @@ -336,7 +336,6 @@ public function shouldRawDownloadForGivenPath() $this->assertEquals($getValue, $api->rawDownload('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php')); } - /** * @return string */