Add simple Github client for downloading release assets
[MAILPOET-3471]
This commit is contained in:
committed by
Veljko V
parent
85d0851909
commit
6c2d428355
45
tasks/GithubClient.php
Normal file
45
tasks/GithubClient.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoetTasks;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class GithubClient {
|
||||
/** @var Client */
|
||||
private $httpClient;
|
||||
|
||||
private const API_BASE_URI = 'https://api.github.com/repos';
|
||||
|
||||
public function __construct($username, $token, $repo) {
|
||||
$this->httpClient = new Client([
|
||||
'auth' => [$username, $token],
|
||||
'headers' => [
|
||||
'Accept' => 'application/vnd.github.v3+json',
|
||||
],
|
||||
'base_uri' => self::API_BASE_URI . "/$repo/",
|
||||
]);
|
||||
}
|
||||
|
||||
public function downloadReleaseZip($zip, $downloadDir, $tag = null) {
|
||||
$release = $this->getRelease($tag);
|
||||
if (!$release) {
|
||||
throw new \Exception("Release $tag not found");
|
||||
}
|
||||
$assetDownloadUrl = null;
|
||||
foreach ($release['assets'] as $asset) {
|
||||
if ($asset['name'] === $zip) {
|
||||
$assetDownloadUrl = $asset['url'];
|
||||
}
|
||||
}
|
||||
if (!$assetDownloadUrl) {
|
||||
throw new \Exception("Release zip for $tag not found");
|
||||
}
|
||||
$this->httpClient->get($assetDownloadUrl, ['sink' => $downloadDir . $zip, 'headers' => ['Accept' => 'application/octet-stream']]);
|
||||
}
|
||||
|
||||
private function getRelease($tag = null) {
|
||||
$path = 'releases/' . ($tag ? "tags/$tag" : 'latest');
|
||||
$response = $this->httpClient->get($path);
|
||||
return json_decode($response->getBody()->getContents(), true);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user