Skip to content

Commit 7f1b364

Browse files
committed
Update a ref
1 parent a30e5f1 commit 7f1b364

File tree

4 files changed

+193
-0
lines changed

4 files changed

+193
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Command\Repository;
4+
5+
use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;
6+
7+
/**
8+
* @Handler("ApiClients\Client\Github\CommandBus\Handler\Repository\RefHandler")
9+
*/
10+
final class RefCommand
11+
{
12+
/** @var string */
13+
private $repository;
14+
15+
/** @var string */
16+
private $ref;
17+
18+
/** @var string */
19+
private $commit;
20+
21+
/**
22+
* @param string $repository
23+
* @param string $ref
24+
* @param string $commit
25+
*/
26+
public function __construct(string $repository, string $ref, string $commit)
27+
{
28+
$this->repository = $repository;
29+
$this->ref = $ref;
30+
$this->commit = $commit;
31+
}
32+
33+
/**
34+
* @return string
35+
*/
36+
public function getRepository(): string
37+
{
38+
return $this->repository;
39+
}
40+
41+
/**
42+
* @return string
43+
*/
44+
public function getRef(): string
45+
{
46+
return $this->ref;
47+
}
48+
49+
/**
50+
* @return string
51+
*/
52+
public function getCommit(): string
53+
{
54+
return $this->commit;
55+
}
56+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Handler\Repository;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\Repository\RefCommand;
6+
use ApiClients\Client\Github\Resource\Git\RefInterface;
7+
use ApiClients\Foundation\Hydrator\Hydrator;
8+
use ApiClients\Foundation\Transport\Service\RequestService;
9+
use ApiClients\Middleware\Json\JsonStream;
10+
use React\Promise\PromiseInterface;
11+
use RingCentral\Psr7\Request;
12+
13+
final class RefHandler
14+
{
15+
/**
16+
* @var RequestService
17+
*/
18+
private $requestService;
19+
20+
/**
21+
* @var Hydrator
22+
*/
23+
private $hydrator;
24+
25+
/**
26+
* @param RequestService $requestService
27+
* @param Hydrator $hydrator
28+
*/
29+
public function __construct(RequestService $requestService, Hydrator $hydrator)
30+
{
31+
$this->requestService = $requestService;
32+
$this->hydrator = $hydrator;
33+
}
34+
35+
/**
36+
* @param RefCommand $command
37+
* @return PromiseInterface
38+
*/
39+
public function handle(RefCommand $command): PromiseInterface
40+
{
41+
return $this->requestService->request(
42+
new Request(
43+
'POST',
44+
'repos/' . $command->getRepository() . '/git/' . $command->getRef(),
45+
[],
46+
new JsonStream([
47+
'sha' => $command->getCommit(),
48+
])
49+
)
50+
)->then(function ($tree) {
51+
return $this->hydrator->hydrate(RefInterface::HYDRATE_CLASS, $tree->getBody()->getParsedContents());
52+
});
53+
}
54+
}

src/Resource/Async/Repository.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use ApiClients\Client\Github\CommandBus\Command\Repository\ContentsCommand;
1616
use ApiClients\Client\Github\CommandBus\Command\Repository\LabelsCommand;
1717
use ApiClients\Client\Github\CommandBus\Command\Repository\LanguagesCommand;
18+
use ApiClients\Client\Github\CommandBus\Command\Repository\RefCommand;
1819
use ApiClients\Client\Github\CommandBus\Command\Repository\RefsCommand;
1920
use ApiClients\Client\Github\CommandBus\Command\Repository\ReleasesCommand;
2021
use ApiClients\Client\Github\CommandBus\Command\Repository\ReplaceTopicsCommand;
@@ -26,6 +27,8 @@
2627
use ApiClients\Client\Github\CommandBus\Command\Repository\UnSubscribeCommand;
2728
use ApiClients\Client\Github\CommandBus\Command\Repository\UpdateSettingsCommand;
2829
use ApiClients\Client\Github\CommandBus\Command\WebHooksCommand;
30+
use ApiClients\Client\Github\Resource\Git\CommitInterface;
31+
use ApiClients\Client\Github\Resource\Git\RefInterface;
2932
use ApiClients\Client\Github\Resource\Git\TreeInterface;
3033
use ApiClients\Client\Github\Resource\Repository as BaseRepository;
3134
use ApiClients\Client\Github\VO\NamedBlob;
@@ -219,4 +222,9 @@ public function refs(?string $namespace = null): ObservableInterface
219222
new RefsCommand($this->fullName(), $namespace)
220223
));
221224
}
225+
226+
public function ref(RefInterface $ref, CommitInterface $tree): PromiseInterface
227+
{
228+
return $this->handleCommand(new RefCommand($this->fullName(), $ref->ref(), $tree->sha()));
229+
}
222230
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tests\Github\CommandBus\Handler\Repository;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\Repository\RefCommand;
6+
use ApiClients\Client\Github\CommandBus\Handler\Repository\RefHandler;
7+
use ApiClients\Client\Github\Resource\Git\RefInterface;
8+
use ApiClients\Client\Github\Resource\Git\TreeInterface;
9+
use ApiClients\Foundation\Hydrator\Hydrator;
10+
use ApiClients\Foundation\Transport\Service\RequestService;
11+
use ApiClients\Middleware\Json\JsonStream;
12+
use ApiClients\Tools\TestUtilities\TestCase;
13+
use React\EventLoop\Factory;
14+
use RingCentral\Psr7\Request;
15+
use RingCentral\Psr7\Response;
16+
use function WyriHaximus\React\timedPromise;
17+
18+
/**
19+
* @internal
20+
*/
21+
final class RefHandlerTest extends TestCase
22+
{
23+
public function provideCommands()
24+
{
25+
yield [
26+
function () {
27+
$loop = Factory::create();
28+
$command = new RefCommand(
29+
'login/repo',
30+
'refs/heads/so-not-master',
31+
'sha1234567890'
32+
);
33+
34+
return [$loop, $command];
35+
},
36+
];
37+
}
38+
39+
/**
40+
* @dataProvider provideCommands
41+
*/
42+
public function testCommand(callable $callable)
43+
{
44+
list($loop, $command) = $callable();
45+
46+
$json = [
47+
'foo' => 'bar',
48+
];
49+
$jsonStream = new JsonStream($json);
50+
51+
$tree = $this->prophesize(TreeInterface::class)->reveal();
52+
53+
$requestService = $this->prophesize(RequestService::class);
54+
$requestService->request(new Request(
55+
'POST',
56+
'repos/login/repo/git/refs/heads/so-not-master',
57+
[],
58+
new JsonStream([
59+
'sha' => 'sha1234567890',
60+
])
61+
))->willReturn(timedPromise($loop, 1, new Response(
62+
200,
63+
[],
64+
$jsonStream
65+
)));
66+
67+
$hydrator = $this->prophesize(Hydrator::class);
68+
$hydrator->hydrate(RefInterface::HYDRATE_CLASS, $json)->shouldBeCalled()->willReturn($tree);
69+
70+
$handler = new RefHandler($requestService->reveal(), $hydrator->reveal());
71+
72+
$result = $this->await($handler->handle($command), $loop);
73+
self::assertSame($tree, $result);
74+
}
75+
}

0 commit comments

Comments
 (0)