Skip to content

Compare commits #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/user-repositories-compare-commits-async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);
use ApiClients\Client\Github\AsyncClient;
use ApiClients\Client\Github\Resource\Async\Repository;
use ApiClients\Client\Github\Resource\Async\User;
use function ApiClients\Foundation\resource_pretty_print;
use React\EventLoop\Factory;
use function React\Promise\all;

require \dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'vendor/autoload.php';

$loop = Factory::create();

$client = AsyncClient::create($loop, require 'resolve_token.php');

$client->user($argv[1] ?? 'php-api-clients')->then(function (User $user) use ($argv) {
resource_pretty_print($user);

return $user->repository($argv[2] ?? 'github');
})->then(function (Repository $repository) {
resource_pretty_print($repository, 1, true);
$repository->compareCommits($argv[3] ?? '1434d8af925bc7f487005595a791d2554102862e', $argv[4] ?? 'HEAD')->then(function (Repository\Compare $compare) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line exceeds 120 characters; contains 154 characters

resource_pretty_print($compare, 2, true);
$compare->refresh()->then(function (Repository\Compare $compare) {
resource_pretty_print($compare, 2, true);
})->done(null, 'display_throwable');
}, function ($error) {
echo (string)$error;
});
})->done(null, 'display_throwable');

$loop->run();

displayState($client->getRateLimitState());
54 changes: 54 additions & 0 deletions src/CommandBus/Command/Repository/CompareCommitsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\CommandBus\Command\Repository;

use ApiClients\Client\Github\Resource\RepositoryInterface;
use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;

/**
* @Handler("ApiClients\Client\Github\CommandBus\Handler\Repository\CompareCommitsHandler")
*/
final class CompareCommitsCommand
{
/**
* @var RepositoryInterface
*/
private $repository;

/**
* @var string
*/
private $base;

/**
* @var string
*/
private $head;

/**
* @param RepositoryInterface $repository
* @param string $base
* @param string $head
*/
public function __construct(RepositoryInterface $repository, string $base, string $head)
{
$this->repository = $repository;
$this->base = $base;
$this->head = $head;
}

public function getRepository(): RepositoryInterface
{
return $this->repository;
}

public function getBase(): string
{
return $this->base;
}

public function getHead(): string
{
return $this->head;
}
}
39 changes: 39 additions & 0 deletions src/CommandBus/Handler/Repository/CompareCommitsHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\CommandBus\Handler\Repository;

use ApiClients\Client\Github\CommandBus\Command\Repository\CompareCommitsCommand;
use ApiClients\Client\Github\Resource\Repository\CompareInterface;
use ApiClients\Tools\Services\Client\FetchAndHydrateService;
use React\Promise\PromiseInterface;

final class CompareCommitsHandler
{
/**
* @var FetchAndHydrateService
*/
private $service;

/**
* @param FetchAndHydrateService $service
*/
public function __construct(FetchAndHydrateService $service)
{
$this->service = $service;
}

/**
* Fetch the given repository and hydrate it.
*
* @param CompareCommitsCommand $command
* @return PromiseInterface
*/
public function handle(CompareCommitsCommand $command): PromiseInterface
{
return $this->service->fetch(
'repos/' . $command->getRepository()->fullName() . '/compare/' . $command->getBase() . '...' . $command->getHead(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line exceeds 120 characters; contains 127 characters

'',
CompareInterface::HYDRATE_CLASS
);
}
}
6 changes: 6 additions & 0 deletions src/Resource/Async/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use ApiClients\Client\Github\CommandBus\Command\Repository\CommitCommand;
use ApiClients\Client\Github\CommandBus\Command\Repository\CommitsCommand;
use ApiClients\Client\Github\CommandBus\Command\Repository\CommunityHealthCommand;
use ApiClients\Client\Github\CommandBus\Command\Repository\CompareCommitsCommand;
use ApiClients\Client\Github\CommandBus\Command\Repository\Contents\FileUploadCommand;
use ApiClients\Client\Github\CommandBus\Command\Repository\ContentsCommand;
use ApiClients\Client\Github\CommandBus\Command\Repository\DetailedCommitCommand;
Expand Down Expand Up @@ -146,6 +147,11 @@ public function pullRequests(): ObservableInterface
));
}

public function compareCommits(string $base, string $head): PromiseInterface
{
return $this->handleCommand(new CompareCommitsCommand($this, $base, $head));
}

public function addWebHook(
string $name,
array $config,
Expand Down
15 changes: 15 additions & 0 deletions src/Resource/Async/Repository/Compare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\Resource\Async\Repository;

use ApiClients\Client\Github\CommandBus\Command\RefreshCommand;
use ApiClients\Client\Github\Resource\Repository\Compare as BaseCompare;
use React\Promise\PromiseInterface;

class Compare extends BaseCompare
{
public function refresh(): PromiseInterface
{
return $this->handleCommand(new RefreshCommand($this));
}
}
9 changes: 9 additions & 0 deletions src/Resource/Async/Repository/EmptyCompare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\Resource\Async\Repository;

use ApiClients\Client\Github\Resource\Repository\EmptyCompare as BaseEmptyCompare;

class EmptyCompare extends BaseEmptyCompare
{
}
139 changes: 139 additions & 0 deletions src/Resource/Repository/Compare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\Resource\Repository;

use ApiClients\Foundation\Hydrator\Annotation\Collection;
use ApiClients\Foundation\Hydrator\Annotation\EmptyResource;
use ApiClients\Foundation\Hydrator\Annotation\Nested;
use ApiClients\Foundation\Resource\AbstractResource;

/**
* @Collection(
* commits="Repository\Commit",
* files="Repository\Commit\File"
* )
* @Nested(
* base_commit="Repository\Commit",
* merge_base_commit="Repository\Commit"
* )
* @EmptyResource("Repository\EmptyCompare")
*/
abstract class Compare extends AbstractResource implements CompareInterface
{
/**
* @var string
*/
protected $url;

/**
* @var Repository\Commit
*/
protected $base_commit;

/**
* @var Repository\Commit
*/
protected $merge_base_commit;

/**
* @var string
*/
protected $status;

/**
* @var int
*/
protected $ahead_by;

/**
* @var int
*/
protected $behind_by;

/**
* @var int
*/
protected $total_commits;

/**
* @var Repository\Commit
*/
protected $commits;

/**
* @var Repository\Commit\File
*/
protected $files;

/**
* @return string
*/
public function url(): string
{
return $this->url;
}

/**
* @return Repository\Commit
*/
public function baseCommit(): Repository\Commit
{
return $this->base_commit;
}

/**
* @return Repository\Commit
*/
public function mergeBaseCommit(): Repository\Commit
{
return $this->merge_base_commit;
}

/**
* @return string
*/
public function status(): string
{
return $this->status;
}

/**
* @return int
*/
public function aheadBy(): int
{
return $this->ahead_by;
}

/**
* @return int
*/
public function behindBy(): int
{
return $this->behind_by;
}

/**
* @return int
*/
public function totalCommits(): int
{
return $this->total_commits;
}

/**
* @return Repository\Commit
*/
public function commits(): Repository\Commit
{
return $this->commits;
}

/**
* @return Repository\Commit\File
*/
public function files(): Repository\Commit\File
{
return $this->files;
}
}
55 changes: 55 additions & 0 deletions src/Resource/Repository/CompareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\Resource\Repository;

use ApiClients\Foundation\Resource\ResourceInterface;

interface CompareInterface extends ResourceInterface
{
const HYDRATE_CLASS = 'Repository\\Compare';

/**
* @return string
*/
public function url(): string;

/**
* @return Repository\Commit
*/
public function baseCommit(): Repository\Commit;

/**
* @return Repository\Commit
*/
public function mergeBaseCommit(): Repository\Commit;

/**
* @return string
*/
public function status(): string;

/**
* @return int
*/
public function aheadBy(): int;

/**
* @return int
*/
public function behindBy(): int;

/**
* @return int
*/
public function totalCommits(): int;

/**
* @return Repository\Commit
*/
public function commits(): Repository\Commit;

/**
* @return Repository\Commit\File
*/
public function files(): Repository\Commit\File;
}
Loading