From f468b673351426b5f0f432b595913d3805434503 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Wed, 5 Aug 2020 22:13:22 -0600 Subject: [PATCH] Repositories - Updated the `getRawFile()` method + Renamed the `getRawFile()` method to `getFileContents` + Updated the method to make it use the `/repos/{owner}/{repo}/contents/{filepath}` API route --- src/Api/Repositories.php | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Api/Repositories.php b/src/Api/Repositories.php index 0dab64b..1653224 100644 --- a/src/Api/Repositories.php +++ b/src/Api/Repositories.php @@ -144,28 +144,41 @@ class Repositories extends AbstractAllApiRequester } /** - * Get the raw contents of a file stored inside a repository + * Get the contents of a file in a certain in repository commit/branch/tag * using the repository's name and owner * * Example: * ``` - * $client->repositories()->getRawFile($owner, $repoName, "README.md"); + * $client->repositories()->getRawFile($owner, $repoName, "README.md", "v2.0.0"); * ``` * * @param string $owner The owner of the repository * @param string $repoName The name of the repository - * @param string $filepath The path to the raw file (relative to the repository root) + * @param string $filepath The path to the file (relative to the repository root) + * @param string $ref The name of the commit/branch/tag. Default the repository’s default branch (usually master) * @return string */ - public function getRawFile(string $owner, string $repoName, string $filepath) + public function getFileContents(string $owner, string $repoName, string $filepath, string $ref="") { $client = $this->getClient(); try { - $response = $this->get("repos/$owner/$repoName/raw/$filepath"); + if ($ref !== "") { + $response = $this->get("repos/$owner/$repoName/contents/$filepath",[ + "ref" => $ref + ]); + } else { + $response = $this->get("repos/$owner/$repoName/contents/$filepath"); + } + $statusCode = $response->getStatusCode(); - $body = (string) $response->getBody(); if ($statusCode == 200) { - return $body; + $body = (string) $response->getBody(); + $jsonObj = json_decode($body, true); + if (array_key_exists("content", $jsonObj)) { + $base64FileContents = $jsonObj["content"]; + $fileContents = base64_decode($base64FileContents); + return $fileContents; + } } return false;