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
This commit is contained in:
Benjamin
2020-08-05 22:13:22 -06:00
parent d37650a882
commit f468b67335

View File

@ -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 * using the repository's name and owner
* *
* Example: * 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 $owner The owner of the repository
* @param string $repoName The name 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 repositorys default branch (usually master)
* @return string * @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(); $client = $this->getClient();
try { 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(); $statusCode = $response->getStatusCode();
$body = (string) $response->getBody();
if ($statusCode == 200) { 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; return false;