8000 Compare commits · php-api-clients/github@88a273d · GitHub
[go: up one dir, main page]

Skip to content

Commit 88a273d

Browse files
committed
Compare commits
1 parent 013e7d1 commit 88a273d

File tree

16 files changed

+567
-0
lines changed

16 files changed

+567
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types=1);
2+
use ApiClients\Client\Github\AsyncClient;
3+
use ApiClients\Client\Github\Resource\Async\Repository;
4+
use ApiClients\Client\Github\Resource\Async\User;
5+
use function ApiClients\Foundation\resource_pretty_print;
6+
use React\EventLoop\Factory;
7+
use function React\Promise\all;
8+
9+
require \dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'vendor/autoload.php';
10+
11+
$loop = Factory::create();
12+
13+
$client = AsyncClient::create($loop, require 'resolve_token.php');
14+
15+
$client->user($argv[1] ?? 'php-api-clients')->then(function (User $user) use ($argv) {
16+
resource_pretty_print($user);
17+
18+
return $user->repository($argv[2] ?? 'github');
19+
})->then(function (Repository $repository) {
20+
resource_pretty_print($repository, 1, true);
21+
$repository->compareCommits($argv[3] ?? '1434d8af925bc7f487005595a791d2554102862e', $argv[4] ?? 'HEAD')->then(function (Repository\Compare $compare) {
22+
resource_pretty_print($compare, 2, true);
23+
$compare->refresh()->then(function (Repository\Compare $compare) {
24+
resource_pretty_print($compare, 2, true);
25+
})->done(null, 'display_throwable');
26+
}, function ($error) {
27+
echo (string)$error;
28+
});
29+
})->done(null, 'display_throwable');
30+
31+
$loop->run();
32+
33+
displayState($client->getRateLimitState());
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\Command\Repository;
4+
5+
use ApiClients\Client\Github\Resource\RepositoryInterface;
6+
use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;
7+
8+
/**
9+
* @Handler("ApiClients\Client\Github\CommandBus\Handler\Repository\CompareCommitsHandler")
10+
*/
11+
final class CompareCommitsCommand
12+
{
13+
/**
14+
* @var RepositoryInterface
15+
*/
16+
private $repository;
17+
18+
/**
19+
* @var string
20+
*/
21+
private $base;
22+
23+
/**
24+
* @var string
25+
*/
26+
private $head;
27+
28+
/**
29+
* @param RepositoryInterface $repository
30+
* @param string $base
31+
* @param string $head
32+
*/
33+
public function __construct(RepositoryInterface $repository, string $base, string $head)
34+
{
35+
$this->repository = $repository;
36+
$this->base = $base;
37+
$this->head = $head;
38+
}
39+
40+
public function getRepository(): RepositoryInterface
41+
{
42+
return $this->repository;
43+
}
44+
45+
public function getBase(): string
46+
{
47+
return $this->base;
48+
}
49+
50+
public function getHead(): string
51+
{
52+
return $this->head;
53+
}
54+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Handler\Repository;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\Repository\CompareCommitsCommand;
6+
use ApiClients\Client\Github\Resource\Repository\CompareInterface;
7+
use ApiClients\Tools\Services\Client\FetchAndHydrateService;
8+
use React\Promise\PromiseInterface;
9+
10+
final class CompareCommitsHandler
11+
{
12+
/**
13+
* @var FetchAndHydrateService
14+
*/
15+
private $service;
16+
17+
/**
18+
* @param FetchAndHydrateService $service
19+
*/
20+
public function __construct(FetchAndHydrateService $service)
21+
{
22+
$this->service = $service;
23+
}
24+
25+
/**
26+
* Fetch the given repository and hydrate it.
27+
*
28+
* @param CompareCommitsCommand $command
29+
* @return PromiseInterface
30+
*/
31+
public function handle(CompareCommitsCommand $command): PromiseInterface
32+
{
33+
return $this->service->fetch(
34+
'repos/' . $command->getRepository()->fullName() . '/compare/' . $command->getBase() . '...' . $command->getHead(),
35+
'',
36+
CompareInterface::HYDRATE_CLASS
37+
);
38+
}
39+
}

src/Resource/Async/Repository.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use ApiClients\Client\Github\CommandBus\Command\Repository\CommitCommand;
1212
use ApiClients\Client\Github\CommandBus\Command\Repository\CommitsCommand;
1313
use ApiClients\Client\Github\CommandBus\Command\Repository\CommunityHealthCommand;
14+
use ApiClients\Client\Github\CommandBus\Command\Repository\CompareCommitsCommand;
1415
use ApiClients\Client\Github\CommandBus\Command\Repository\Contents\FileUploadCommand;
1516
use ApiClients\Client\Github\CommandBus\Command\Repository\ContentsCommand;
1617
use ApiClients\Client\Github\CommandBus\Command\Repository\DetailedCommitCommand;
@@ -146,6 +147,11 @@ public function pullRequests(): ObservableInterface
146147
));
147148
}
148149

150+
public function compareCommits(string $base, string $head): PromiseInterface
151+
{
152+
return $this->handleCommand(new CompareCommitsCommand($this, $base, $head));
153+
}
154+
149155
public function addWebHook(
150156
string $name,
151157
array $config,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\Resource\Async\Repository;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\RefreshCommand;
6+
use ApiClients\Client\Github\Resource\Repository\Compare as BaseCompare;
7+
use React\Promise\PromiseInterface;
8+
9+
class Compare extends BaseCompare
10+
{
11+
public function refresh(): PromiseInterface
12+
{
13+
return $this->handleCommand(new RefreshCommand($this));
14+
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\Resource\Async\Repository;
4+
5+
use ApiClients\Client\Github\Resource\Repository\EmptyCompare as BaseEmptyCompare;
6+
7+
class EmptyCompare extends BaseEmptyCompare
8+
{
9+
}

src/Resource/Repository/Compare.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\Resource\Repository;
4+
5+
use ApiClients\Foundation\Hydrator\Annotation\Collection;
6+
use ApiClients\Foundation\Hydrator\Annotation\EmptyResource;
7+
use ApiClients\Foundation\Hydrator\Annotation\Nested;
8+
use ApiClients\Foundation\Resource\AbstractResource;
9+
10+
/**
11+
* @Collection(
12+
* commits="Repository\Commit",
13+
* files="Repository\Commit\File"
14+
* )
15+
* @Nested(
16+
* base_commit="Repository\Commit",
17+
* merge_base_commit="Repository\Commit"
18+
* )
19+
* @EmptyResource("Repository\EmptyCompare")
20+
*/
21+
abstract class Compare extends AbstractResource implements CompareInterface
22+
{
23+
/**
24+
* @var string
25+
*/
26+
protected $url;
27+
28+
/**
29+
* @var Repository\Commit
30+
*/
31+
protected $base_commit;
32+
33+
/**
34+
* @var Repository\Commit
35+
*/
36+
protected $merge_base_commit;
37+
38+
/**
39+
* @var string
40+
*/
41+
protected $status;
42+
43+
/**
44+
* @var int
45+
*/
46+
prote CDA2 cted $ahead_by;
47+
48+
/**
49+
* @var int
50+
*/
51+
protected $behind_by;
52+
53+
/**
54+
* @var int
55+
*/
56+
protected $total_commits;
57+
58+
/**
59+
* @var Repository\Commit
60+
*/
61+
protected $commits;
62+
63+
/**
64+
* @var Repository\Commit\File
65+
*/
66+
protected $files;
67+
68+
/**
69+
* @return string
70+
*/
71+
public function url(): string
72+
{
73+
return $this->url;
74+
}
75+
76+
/**
77+
* @return Repository\Commit
78+
*/
79+
public function baseCommit(): Repository\Commit
80+
{
81+
return $this->base_commit;
82+
}
83+
84+
/**
85+
* @return Repository\Commit
86+
*/
87+
public function mergeBaseCommit(): Repository\Commit
88+
{
89+
return $this->merge_base_commit;
90+
}
91+
92+
/**
93+
* @return string
94+
*/
95+
public function status(): string
96+
{
97+
return $this->status;
98+
}
99+
100+
/**
101+
* @return int
102+
*/
103+
public function aheadBy(): int
104+
{
105+
return $this->ahead_by;
106+
}
107+
108+
/**
109+
* @return int
110+
*/
111+
public function behindBy(): int
112+
{
113+
return $this->behind_by;
114+
}
115+
116+
/**
117+
* @return int
118+
*/
119+
public function totalCommits(): int
120+
{
121+
return $this->total_commits;
122+
}
123+
124+
/**
125+
* @return Repository\Commit
126+
*/
127+
public function commits(): Repository\Commit
128+
{
129+
return $this->commits;
130+
}
131+
132+
/**
133+
* @return Repository\Commit\File
134+
*/
135+
public function files(): Repository\Commit\File
136+
{
137+
return $this->files;
138+
}
139+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\Resource\Repository;
4+
5+
use ApiClients\Foundation\Resource\ResourceInterface;
6+
7+
interface CompareInterface extends ResourceInterface
8+
{
9+
const HYDRATE_CLASS = 'Repository\\Compare';
10+
11+
/**
12+
* @return string
13+
*/
14+
public function url(): string;
15+
16+
/**
17+
* @return Repository\Commit
18+
*/
19+
public function baseCommit(): Repository\Commit;
20+
21+
/**
22+
* @return Repository\Commit
23+
*/
24+
public function mergeBaseCommit(): Repository\Commit;
25+
26+
/**
27+
* @return string
28+
*/
29+
public function status(): string;
30+
31+
/**
32+
* @return int
33+
*/
34+
public function aheadBy(): int;
35+
36+
/**
37+
* @return int
38+
*/
39+
public function behindBy(): int;
40+
41+
/**
42+
* @return int
43+
*/
44+
public function totalCommits(): int;
45+
46+
/**
47+
* @return Repository\Commit
48+
*/
49+
public function commits(): Repository\Commit;
50+
51+
/**
52+
* @return Repository\Commit\File
53+
*/
54+
public function files(): Repository\Commit\File;
55+
}

0 commit comments

Comments
 (0)
0