Skip to content

Commit

Permalink
Detect GitHub API from environment
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Sep 23, 2022
1 parent b0e3964 commit ff3e834
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ unit:
$(DOCKER_RUN) vendor/bin/phpunit --colors=always -c phpunit.xml.dist --coverage-text --coverage-html covHtml --coverage-clover ./build/logs/clover.xml

infection:
$(DOCKER_RUN) vendor/bin/infection --ansi --min-msi=100 --min-covered-msi=100 --threads=$(nproc)
$(DOCKER_RUN) vendor/bin/infection --ansi --min-msi=100 --min-covered-msi=100 --threads=$(nproc) || (cat infection-log.txt && false)

composer-require-checker:
$(DOCKER_RUN) vendor/bin/composer-require-checker --ignore-parse-errors --ansi -vvv --config-file=composer-require-checker.json
Expand Down
3 changes: 2 additions & 1 deletion composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"Safe\\sprintf", "ApiClients\\Tools\\Rx\\unwrapObservableFromPromise",
"WyriHaximus\\Constants\\Boolean\\FALSE_", "WyriHaximus\\Constants\\Boolean\\TRUE_",
"WyriHaximus\\Constants\\HTTPStatusCodes\\NOT_FOUND",
"WyriHaximus\\Constants\\Numeric\\ONE", "WyriHaximus\\Constants\\Numeric\\ZERO"
"WyriHaximus\\Constants\\Numeric\\ONE", "WyriHaximus\\Constants\\Numeric\\ZERO",
"ApiClients\\Foundation\\Options", "ApiClients\\Foundation\\Transport\\Options"
],
"php-core-extensions" : [
"Core",
Expand Down
16 changes: 16 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,24 @@
"mutators": {
"@default": true,
"InstanceOf_": false,
"LogicalNot": {
"ignore": [
"WyriHaximus\\GithubAction\\WaitForStatus\\App::boot"
]
},
"ArrayItemRemoval": {
"ignore": [
"WyriHaximus\\GithubAction\\WaitForStatus\\App::boot"
]
},
"Foreach_": {
"ignore": [
"WyriHaximus\\GithubAction\\WaitForStatus\\App::boot"
]
},
"Continue_": {
"ignore": [
"WyriHaximus\\GithubAction\\WaitForStatus\\App::boot",
"WyriHaximus\\GithubAction\\WaitForStatus\\StatusCheck\\Checks::refresh"
]
},
Expand Down
32 changes: 30 additions & 2 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use ApiClients\Client\Github\AsyncClientInterface;
use ApiClients\Client\Github\AuthenticationInterface;
use ApiClients\Client\Github\RateLimitState;
use ApiClients\Foundation\Options as FoundationOptions;
use ApiClients\Foundation\Transport\Options as TransportOptions;
use Clue\React\Buzz\Message\ResponseException;
use Psr\Log\LoggerInterface;
use React\EventLoop\Loop;
Expand All @@ -19,9 +21,11 @@
use WyriHaximus\PSR3\CallableThrowableLogger\CallableThrowableLogger;

use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
use function array_key_exists;
use function React\Promise\all;
use function React\Promise\resolve;
use function React\Promise\Stream\buffer;
use function Safe\parse_url;
use function Safe\sprintf;
use function strpos;

Expand All @@ -36,9 +40,33 @@ final class App

private AsyncClientInterface $github;

public static function boot(LoggerInterface $logger, AuthenticationInterface $auth): App
public static function boot(LoggerInterface $logger, AuthenticationInterface $auth, string $apiBaseUrl): App
{
return new self($logger, AsyncClient::create(Loop::get(), $auth));
$transportOptions = [];
$url = parse_url($apiBaseUrl);
foreach (
[
'host' => TransportOptions::HOST,
'scheme' => TransportOptions::SCHEMA,
'path' => TransportOptions::PATH,
'port' => TransportOptions::PORT,
] as $key => $otherKey
) {
if (! array_key_exists($key, $url)) {
continue;
}

$transportOptions[$otherKey] = $url[$key];
}

return new self(
$logger,
AsyncClient::create(
Loop::get(),
$auth,
[FoundationOptions::TRANSPORT_OPTIONS => $transportOptions],
)
);
}

private function __construct(LoggerInterface $logger, AsyncClientInterface $github)
Expand Down
6 changes: 3 additions & 3 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function success(): void
$logger->debug('Check "qa (7.3)" has the following status "completed" and conclusion "success"')->shouldBeCalled();
$logger->debug('All checks completed, marking resolve and success')->shouldBeCalled();
$result = $this->await(
App::boot($logger->reveal(), (require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'etc/auth.php'))->wait(
App::boot($logger->reveal(), (require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'etc/auth.php'), 'https://api.github.com')->wait(
'WyriHaximus/github-action-wait-for-status',
'wait',
1,
Expand Down Expand Up @@ -62,7 +62,7 @@ public function failure(): void
$logger->debug('Check "Travis CI - Branch" has the following status "completed" and conclusion "action_required"')->shouldBeCalled();
$logger->debug('Check (Travis CI - Branch) failed, marking resolve and failure')->shouldBeCalled();
$result = $this->await(
App::boot($logger->reveal(), (require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'etc/auth.php'))->wait(
App::boot($logger->reveal(), (require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'etc/auth.php'), 'https://api.github.com')->wait(
'WyriHaximus/php-broadcast',
'wait',
1,
Expand Down Expand Up @@ -90,7 +90,7 @@ public function error(): void
Argument::type('array')
)->shouldBeCalled();
$result = $this->await(
App::boot($logger->reveal(), new Token('FAKE_TOKEN_TO_FORCE_ERROR'))->wait(
App::boot($logger->reveal(), new Token('FAKE_TOKEN_TO_FORCE_ERROR'), 'https://api.github.com')->wait(
'WyriHaximus/github-action-wait-for-status',
'wait',
1,
Expand Down
3 changes: 2 additions & 1 deletion wait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
const SHA = 'GITHUB_SHA';
const EVENT = 'GITHUB_EVENT_NAME';
const EVENT_PATH = 'GITHUB_EVENT_PATH';
const API_BASE_URL = 'GITHUB_API_URL';
const ACTIONS = 'INPUT_IGNOREACTIONS';
const INTERVAL = 'INPUT_CHECKINTERVAL';
const WAIT_FOR_CHECK = 'INPUT_WAITFORCHECK';
Expand All @@ -38,7 +39,7 @@
$logger->notice('Pull Request detected');
$shas[] = json_decode(file_get_contents(getenv(EVENT_PATH)))->pull_request->head->sha;
}
App::boot($logger, new Token(getenv(TOKEN)))->wait(
App::boot($logger, new Token(getenv(TOKEN)), getenv(API_BASE_URL))->wait(
getenv(REPOSITORY),
getenv(ACTIONS),
(float) getenv(INTERVAL) > 0.0 ? (float) getenv(INTERVAL) : 13,
Expand Down

0 comments on commit ff3e834

Please sign in to comment.