From 3adba20eab9f647ed2f0a11af817e43c71f38664 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sat, 12 Oct 2019 17:49:07 +0200 Subject: [PATCH] Get detailed commit from branch --- examples/user-repositories-branches-async.php | 7 ++- .../Repository/DetailedCommitCommand.php | 47 +++++++++++++++++++ .../Repository/DetailedCommitHandler.php | 37 +++++++++++++++ src/Resource/Async/Repository/Branch.php | 10 ++++ 4 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 src/CommandBus/Command/Repository/DetailedCommitCommand.php create mode 100644 src/CommandBus/Handler/Repository/DetailedCommitHandler.php diff --git a/examples/user-repositories-branches-async.php b/examples/user-repositories-branches-async.php index ed6b72a7f9..e9cbcd2af5 100644 --- a/examples/user-repositories-branches-async.php +++ b/examples/user-repositories-branches-async.php @@ -17,8 +17,11 @@ return $user->repository($argv[2] ?? 'github'); })->then(function (Repository $repository) { resource_pretty_print($repository, 1, true); - $repository->branches()->subscribe(function ($labels) { - resource_pretty_print($labels, 2, true); + $repository->branches()->subscribe(function (Repository\Branch $branch) { + resource_pretty_print($branch, 2, true); + $branch->detailedCommit()->then(function (Repository\Commit $commit) { + resource_pretty_print($commit); + })->done(null, 'display_throwable'); }, function ($error) { echo (string)$error; }); diff --git a/src/CommandBus/Command/Repository/DetailedCommitCommand.php b/src/CommandBus/Command/Repository/DetailedCommitCommand.php new file mode 100644 index 0000000000..66e658d500 --- /dev/null +++ b/src/CommandBus/Command/Repository/DetailedCommitCommand.php @@ -0,0 +1,47 @@ +fullName = $fullName; + $this->sha = $sha; + } + + /** + * @return string + */ + public function getFullName(): string + { + return $this->fullName; + } + + /** + * @return string + */ + public function getSha(): string + { + return $this->sha; + } +} diff --git a/src/CommandBus/Handler/Repository/DetailedCommitHandler.php b/src/CommandBus/Handler/Repository/DetailedCommitHandler.php new file mode 100644 index 0000000000..e7222f4fac --- /dev/null +++ b/src/CommandBus/Handler/Repository/DetailedCommitHandler.php @@ -0,0 +1,37 @@ +fetchAndHydrateService = $fetchAndHydrateService; + } + + /** + * @param DetailedCommitCommand $command + * @return PromiseInterface + */ + public function handle(DetailedCommitCommand $command): PromiseInterface + { + return $this->fetchAndHydrateService->fetch( + 'repos/' . $command->getFullName() . '/commits/' . $command->getSha(), + '', + CommitInterface::HYDRATE_CLASS + ); + } +} diff --git a/src/Resource/Async/Repository/Branch.php b/src/Resource/Async/Repository/Branch.php index bf3c4ef1ea..a19541e7c7 100644 --- a/src/Resource/Async/Repository/Branch.php +++ b/src/Resource/Async/Repository/Branch.php @@ -2,7 +2,9 @@ namespace ApiClients\Client\Github\Resource\Async\Repository; +use ApiClients\Client\Github\CommandBus\Command\Repository\DetailedCommitCommand; use ApiClients\Client\Github\Resource\Repository\Branch as BaseBranch; +use React\Promise\PromiseInterface; class Branch extends BaseBranch { @@ -10,4 +12,12 @@ public function refresh(): Branch { throw new \Exception('TODO: create refresh method!'); } + + public function detailedCommit(): PromiseInterface + { + return $this->handleCommand(new DetailedCommitCommand( + \str_replace('/branches/' . $this->name . '/protection', '', \explode('/repos/', $this->protection_url)[1]), + $this->commit->sha() + )); + } }