-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathCreateReleaseThroughApiCall.php
75 lines (60 loc) · 2.27 KB
/
CreateReleaseThroughApiCall.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace Laminas\AutomaticReleases\Github\Api\V3;
use Laminas\AutomaticReleases\Git\Value\SemVerVersion;
use Laminas\AutomaticReleases\Github\Value\RepositoryName;
use Laminas\Diactoros\Uri;
use Psl;
use Psl\Json;
use Psl\Type;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\UriInterface;
final class CreateReleaseThroughApiCall implements CreateRelease
{
private const API_ROOT = 'https://api.github.com/';
private RequestFactoryInterface $messageFactory;
private ClientInterface $client;
/** @psalm-var non-empty-string */
private string $apiToken;
/** @psalm-param non-empty-string $apiToken */
public function __construct(
RequestFactoryInterface $messageFactory,
ClientInterface $client,
string $apiToken
) {
$this->messageFactory = $messageFactory;
$this->client = $client;
$this->apiToken = $apiToken;
}
public function __invoke(
RepositoryName $repository,
SemVerVersion $version,
string $releaseNotes
): UriInterface {
$request = $this->messageFactory
->createRequest(
'POST',
self::API_ROOT . 'repos/' . $repository->owner() . '/' . $repository->name() . '/releases'
)
->withAddedHeader('Content-Type', 'application/json')
->withAddedHeader('User-Agent', 'Ocramius\'s minimal API V3 client')
->withAddedHeader('Authorization', 'token ' . $this->apiToken);
$request
->getBody()
->write(Json\encode([
'tag_name' => $version->fullReleaseName(),
'name' => $version->fullReleaseName(),
'body' => $releaseNotes,
]));
$response = $this->client->sendRequest($request);
Psl\invariant($response->getStatusCode() >= 200 || $response->getStatusCode() <= 299, 'Failed to create release through GitHub API.');
$responseBody = $response
->getBody()
->__toString();
$responseData = Json\typed($responseBody, Type\shape([
'html_url' => Type\non_empty_string(),
]));
return new Uri($responseData['html_url']);
}
}