diff --git a/src/Plugins/Tia/CiDefaultBranch.php b/src/Plugins/Tia/CiDefaultBranch.php index 3db30f5f..682a506e 100644 --- a/src/Plugins/Tia/CiDefaultBranch.php +++ b/src/Plugins/Tia/CiDefaultBranch.php @@ -4,56 +4,31 @@ declare(strict_types=1); namespace Pest\Plugins\Tia; +use Pest\Plugins\Tia\Contracts\Ci; + /** * @internal */ final class CiDefaultBranch { + /** + * @var array> + */ + private const array CIS = [ + Cis\GitLab::class, + Cis\GitHub::class, + ]; + public static function detect(): ?string { - return self::fromGitLab() ?? self::fromGitHubEvent(); - } + foreach (self::CIS as $class) { + $branch = (new $class)->defaultBranch(); - private static function fromGitLab(): ?string - { - 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; + if ($branch !== null) { + return $branch; + } } - $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; - } - - private static function environment(string $name): ?string - { - $value = getenv($name); - - if (! is_string($value)) { - return null; - } - - $value = trim($value); - - return $value === '' ? null : $value; + return null; } } diff --git a/src/Plugins/Tia/Cis/Concerns/ReadsEnvironment.php b/src/Plugins/Tia/Cis/Concerns/ReadsEnvironment.php new file mode 100644 index 00000000..60ea4053 --- /dev/null +++ b/src/Plugins/Tia/Cis/Concerns/ReadsEnvironment.php @@ -0,0 +1,24 @@ +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; + } +} diff --git a/src/Plugins/Tia/Cis/GitLab.php b/src/Plugins/Tia/Cis/GitLab.php new file mode 100644 index 00000000..06db0f1b --- /dev/null +++ b/src/Plugins/Tia/Cis/GitLab.php @@ -0,0 +1,21 @@ +environment('CI_DEFAULT_BRANCH'); + } +} diff --git a/src/Plugins/Tia/Contracts/Ci.php b/src/Plugins/Tia/Contracts/Ci.php new file mode 100644 index 00000000..53fc2a6f --- /dev/null +++ b/src/Plugins/Tia/Contracts/Ci.php @@ -0,0 +1,17 @@ +