Skip to content

Commit

Permalink
Detect Pull Request
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Aug 29, 2020
1 parent 8536e4f commit a71aa39
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private function __construct(LoopInterface $loop, LoggerInterface $logger, Async
$this->github = $github;
}

public function wait(string $repository, string $sha, string $ignoreActions, float $checkInterval): PromiseInterface
public function wait(string $repository, string $ignoreActions, float $checkInterval, string ...$shas): PromiseInterface
{
$timer = $this->rateLimitTimer();
/**
Expand All @@ -60,7 +60,7 @@ public function wait(string $repository, string $sha, string $ignoreActions, flo
};

return unwrapObservableFromPromise((new LookUpRepository($repository, $this->logger))($this->github)->then(
new LookUpCommits($sha, $this->logger)
new LookUpCommits($this->logger, ...$shas)
))->flatMap(
new GetStatusChecksFromCommits($this->loop, $this->logger, $ignoreActions, $checkInterval)
)->map(
Expand Down
19 changes: 11 additions & 8 deletions src/LookUpCommits.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,32 @@
namespace WyriHaximus\GithubAction\WaitForStatus;

use ApiClients\Client\Github\Resource\Async\Repository;
use ApiClients\Client\Github\Resource\Async\Repository\Commit;
use Psr\Log\LoggerInterface;
use React\Promise\PromiseInterface;
use Rx\Observable;
use function ApiClients\Tools\Rx\observableFromArray;
use function implode;

final class LookUpCommits
{
private string $sha;
private LoggerInterface $logger;
/** @var string[] */
private array $shas;

public function __construct(string $sha, LoggerInterface $logger)
public function __construct(LoggerInterface $logger, string ...$shas)
{
$this->sha = $sha;
$this->shas = $shas;
$this->logger = $logger;
}

public function __invoke(Repository $repository): PromiseInterface
{
$this->logger->debug('Locating commit: ' . $this->sha);
$this->logger->debug('Locating commit: ' . implode(', ', $this->shas));

return $repository->specificCommit($this->sha)->then(static function (Commit $commit): Observable {
return observableFromArray([$commit]);
});
return observableFromArray($this->shas)->flatMap(
static fn(string $sha): Observable => Observable::fromPromise($repository->specificCommit($sha))
)->toArray()->toPromise()->then(
static fn(array $commits): Observable => observableFromArray($commits)
);
}
}
12 changes: 6 additions & 6 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function success(): void
$result = $this->await(
App::boot($loop, $logger->reveal(), (require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'etc/auth.php'))->wait(
'WyriHaximus/github-action-wait-for-status',
'd2ddfe536405fa61cd5f8ae1b3e06f192bac1d64',
'wait',
1
1,
'd2ddfe536405fa61cd5f8ae1b3e06f192bac1d64'
),
$loop,
30
Expand Down Expand Up @@ -63,9 +63,9 @@ public function failure(): void
$result = $this->await(
App::boot($loop, $logger->reveal(), (require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'etc/auth.php'))->wait(
'WyriHaximus/php-broadcast',
'67bdf304b34567e0f434bc0f9f19d3022cc1aa6c',
'wait',
1
1,
'67bdf304b34567e0f434bc0f9f19d3022cc1aa6c'
),
$loop,
30
Expand All @@ -92,9 +92,9 @@ public function error(): void
$result = $this->await(
App::boot($loop, $logger->reveal(), new Token('FAKE_TOKEN_TO_FORCE_ERROR'))->wait(
'WyriHaximus/github-action-wait-for-status',
'd2ddfe536405fa61cd5f8ae1b3e06f192bac1d64',
'wait',
1
1,
'd2ddfe536405fa61cd5f8ae1b3e06f192bac1d64'
),
$loop,
30
Expand Down
2 changes: 1 addition & 1 deletion tests/LookUpCommitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function happyFlow(): void
assert($commit instanceof Commit);
$this->repository->specificCommit(self::SHA)->shouldBeCalled()->willReturn(resolve($commit));

$promise = (new LookUpCommits(self::SHA, $logger))($repository);
$promise = (new LookUpCommits($logger, self::SHA))($repository);
$result = $this->await(unwrapObservableFromPromise($promise)->toArray()->toPromise());
self::assertSame([$commit], $result);
}
Expand Down
13 changes: 11 additions & 2 deletions wait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
use WyriHaximus\GithubAction\WaitForStatus\App;
use WyriHaximus\Monolog\FormattedPsrHandler\FormattedPsrHandler;
use WyriHaximus\React\PSR3\Stdio\StdioLogger;
use function React\Promise\all;
use function Safe\json_decode;
use function Safe\file_get_contents;

require __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

const REPOSITORY = 'GITHUB_REPOSITORY';
const TOKEN = 'GITHUB_TOKEN';
const SHA = 'GITHUB_SHA';
const EVENT = 'GITHUB_EVENT_NAME';
const EVENT_PATH = 'GITHUB_EVENT_PATH';
const ACTIONS = 'INPUT_IGNOREACTIONS';
const INTERVAL = 'INPUT_CHECKINTERVAL';

Expand All @@ -29,11 +32,17 @@
));
$logger = new Logger('wait');
$logger->pushHandler($consoleHandler);
$shas = [];
$shas[] = getenv(SHA);
if (getenv(EVENT) === 'pull_request') {
$logger->notice('Pull Request detected');
$shas[] = json_decode(file_get_contents(getenv(EVENT_PATH)))->pull_request->head->sha;
}
App::boot($loop, $logger, new Token(getenv(TOKEN)))->wait(
getenv(REPOSITORY),
getenv(SHA),
getenv(ACTIONS),
(float) getenv(INTERVAL) > 0.0 ? (float) getenv(INTERVAL) : 13,
...$shas,
)->then(function (string $state) use($logger) {
$logger->info('Final status: ' . $state);
echo PHP_EOL, '::set-output name=status::' . $state, PHP_EOL;
Expand Down

0 comments on commit a71aa39

Please sign in to comment.