mirror of
https://github.com/pestphp/pest.git
synced 2026-09-05 22:33:35 +02:00
refactor
This commit is contained in:
@@ -4,56 +4,31 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest\Plugins\Tia;
|
namespace Pest\Plugins\Tia;
|
||||||
|
|
||||||
|
use Pest\Plugins\Tia\Contracts\Ci;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
final class CiDefaultBranch
|
final class CiDefaultBranch
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var array<int, class-string<Ci>>
|
||||||
|
*/
|
||||||
|
private const array CIS = [
|
||||||
|
Cis\GitLab::class,
|
||||||
|
Cis\GitHub::class,
|
||||||
|
];
|
||||||
|
|
||||||
public static function detect(): ?string
|
public static function detect(): ?string
|
||||||
{
|
{
|
||||||
return self::fromGitLab() ?? self::fromGitHubEvent();
|
foreach (self::CIS as $class) {
|
||||||
}
|
$branch = (new $class)->defaultBranch();
|
||||||
|
|
||||||
private static function fromGitLab(): ?string
|
if ($branch !== null) {
|
||||||
{
|
return $branch;
|
||||||
return self::environment('CI_DEFAULT_BRANCH');
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static function fromGitHubEvent(): ?string
|
|
||||||
{
|
|
||||||
$path = self::environment('GITHUB_EVENT_PATH');
|
|
||||||
|
|
||||||
if ($path === null || ! is_file($path) || ! is_readable($path)) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$contents = @file_get_contents($path);
|
return null;
|
||||||
|
|
||||||
if ($contents === false) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
$payload = json_decode($contents, true);
|
|
||||||
|
|
||||||
if (! is_array($payload) || ! is_array($payload['repository'] ?? null)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
$branch = $payload['repository']['default_branch'] ?? null;
|
|
||||||
|
|
||||||
return is_string($branch) && $branch !== '' ? $branch : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static function environment(string $name): ?string
|
|
||||||
{
|
|
||||||
$value = getenv($name);
|
|
||||||
|
|
||||||
if (! is_string($value)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
$value = trim($value);
|
|
||||||
|
|
||||||
return $value === '' ? null : $value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pest\Plugins\Tia\Cis\Concerns;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
trait ReadsEnvironment
|
||||||
|
{
|
||||||
|
private function environment(string $name): ?string
|
||||||
|
{
|
||||||
|
$value = getenv($name);
|
||||||
|
|
||||||
|
if (! is_string($value)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = trim($value);
|
||||||
|
|
||||||
|
return $value === '' ? null : $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pest\Plugins\Tia\Cis;
|
||||||
|
|
||||||
|
use Pest\Plugins\Tia\Cis\Concerns\ReadsEnvironment;
|
||||||
|
use Pest\Plugins\Tia\Contracts\Ci;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
final readonly class GitHub implements Ci
|
||||||
|
{
|
||||||
|
use ReadsEnvironment;
|
||||||
|
|
||||||
|
public function defaultBranch(): ?string
|
||||||
|
{
|
||||||
|
$path = $this->environment('GITHUB_EVENT_PATH');
|
||||||
|
|
||||||
|
if ($path === null || ! is_file($path) || ! is_readable($path)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$contents = @file_get_contents($path);
|
||||||
|
|
||||||
|
if ($contents === false) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$payload = json_decode($contents, true);
|
||||||
|
|
||||||
|
if (! is_array($payload) || ! is_array($payload['repository'] ?? null)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$branch = $payload['repository']['default_branch'] ?? null;
|
||||||
|
|
||||||
|
return is_string($branch) && $branch !== '' ? $branch : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pest\Plugins\Tia\Cis;
|
||||||
|
|
||||||
|
use Pest\Plugins\Tia\Cis\Concerns\ReadsEnvironment;
|
||||||
|
use Pest\Plugins\Tia\Contracts\Ci;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
final readonly class GitLab implements Ci
|
||||||
|
{
|
||||||
|
use ReadsEnvironment;
|
||||||
|
|
||||||
|
public function defaultBranch(): ?string
|
||||||
|
{
|
||||||
|
return $this->environment('CI_DEFAULT_BRANCH');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pest\Plugins\Tia\Contracts;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface Ci
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The default branch advertised by this CI, or `null` when the run
|
||||||
|
* does not happen on it — or when it exposes no such information.
|
||||||
|
*/
|
||||||
|
public function defaultBranch(): ?string;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user