This commit is contained in:
nuno maduro
2026-08-07 02:12:24 +01:00
parent 5a4f124199
commit b0d1fc589d
5 changed files with 119 additions and 41 deletions
+16 -41
View File
@@ -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();
if ($branch !== null) {
return $branch;
}
} }
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; 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;
}
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;
}
}
+41
View File
@@ -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;
}
}
+21
View File
@@ -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');
}
}
+17
View File
@@ -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;
}