mirror of
https://github.com/pestphp/pest.git
synced 2026-12-06 07:42:17 +01:00
fix(tia): find GitLab baselines beyond the last 100 jobs (#1912)
The GitLab baseline lookup listed the project's 100 most recent successful jobs and filtered them client-side by name and ref. GitLab's jobs endpoint has no server-side ref or name filter, so that single page is the entire search space -- and on a busy project it covers very little wall-clock time. On a self-hosted instance the window measured 82 minutes, while baselines were published roughly once a day. The lookup therefore returned null almost always, and every developer silently fell back to recording a local baseline. Query pipelines instead, which does filter by ref and status server-side, then walk the default branch's successful pipelines newest-first until one contains a successful job with the configured name. This also aligns the lookup with what the download actually resolves. `glab job artifact <ref> <job>` reads from the latest successful *pipeline* for the ref, so a successful job inside a failed pipeline was never downloadable even when the old lookup returned it.
This commit is contained in:
@@ -13,6 +13,10 @@ final readonly class GitLabRemote extends BaseRemote
|
||||
{
|
||||
private const string DEFAULT_JOB_NAME = 'tia-baseline';
|
||||
|
||||
private const int JOBS_PER_PAGE = 100;
|
||||
|
||||
private const int PIPELINES_PER_PAGE = 20;
|
||||
|
||||
public function detect(string $projectRoot): ?string
|
||||
{
|
||||
$url = $this->readOriginUrl($projectRoot);
|
||||
@@ -72,7 +76,12 @@ final readonly class GitLabRemote extends BaseRemote
|
||||
|
||||
$process = new Process([
|
||||
'glab', 'api',
|
||||
sprintf('projects/%s/jobs?scope[]=success&per_page=100', $encodedRepo),
|
||||
sprintf(
|
||||
'projects/%s/pipelines?ref=%s&status=success&order_by=id&sort=desc&per_page=%d',
|
||||
$encodedRepo,
|
||||
rawurlencode($defaultBranch),
|
||||
self::PIPELINES_PER_PAGE,
|
||||
),
|
||||
]);
|
||||
$process->setTimeout(30.0);
|
||||
$process->run();
|
||||
@@ -81,28 +90,32 @@ final readonly class GitLabRemote extends BaseRemote
|
||||
return [null, $this->classifyError($process->getErrorOutput().$process->getOutput())];
|
||||
}
|
||||
|
||||
$runId = $this->findPipelineId($process->getOutput(), $jobName, $defaultBranch);
|
||||
foreach ($this->pipelineIds($process->getOutput()) as $pipelineId) {
|
||||
if ($this->findJob($encodedRepo, $pipelineId, $jobName) !== null) {
|
||||
return [$pipelineId, null];
|
||||
}
|
||||
}
|
||||
|
||||
return [$runId, null];
|
||||
return [null, null];
|
||||
}
|
||||
|
||||
public function artifactSize(string $repo, string $runId): ?int
|
||||
{
|
||||
$encodedRepo = rawurlencode($repo);
|
||||
$jobName = $this->jobName();
|
||||
$job = $this->findJob(rawurlencode($repo), $runId, $this->jobName());
|
||||
|
||||
$process = new Process([
|
||||
'glab', 'api',
|
||||
sprintf('projects/%s/pipelines/%s/jobs', $encodedRepo, $runId),
|
||||
]);
|
||||
$process->setTimeout(30.0);
|
||||
$process->run();
|
||||
|
||||
if (! $process->isSuccessful()) {
|
||||
if ($job === null || ! is_array($job['artifacts'] ?? null)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->findArtifactSize($process->getOutput(), $jobName);
|
||||
$size = 0;
|
||||
|
||||
foreach ($job['artifacts'] as $artifact) {
|
||||
if (is_array($artifact) && is_int($artifact['size'] ?? null)) {
|
||||
$size += $artifact['size'];
|
||||
}
|
||||
}
|
||||
|
||||
return $size;
|
||||
}
|
||||
|
||||
public function downloadCommand(string $repo, string $runId, string $destDir): array
|
||||
@@ -166,54 +179,76 @@ final readonly class GitLabRemote extends BaseRemote
|
||||
return $this->watchPatterns->baselineJob() ?? self::DEFAULT_JOB_NAME;
|
||||
}
|
||||
|
||||
private function findPipelineId(string $output, string $jobName, string $defaultBranch): ?string
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
private function pipelineIds(string $output): array
|
||||
{
|
||||
$jobs = json_decode($output, true);
|
||||
$pipelines = json_decode($output, true);
|
||||
|
||||
if (! is_array($jobs)) {
|
||||
return null;
|
||||
if (! is_array($pipelines)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ($jobs as $job) {
|
||||
if (! is_array($job)) {
|
||||
continue;
|
||||
$ids = [];
|
||||
|
||||
foreach ($pipelines as $pipeline) {
|
||||
$id = is_array($pipeline) ? ($pipeline['id'] ?? null) : null;
|
||||
|
||||
if (is_int($id) || (is_string($id) && $id !== '')) {
|
||||
$ids[] = (string) $id;
|
||||
}
|
||||
|
||||
if (($job['name'] ?? null) !== $jobName || ($job['ref'] ?? null) !== $defaultBranch) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = $job['pipeline']['id'] ?? null;
|
||||
|
||||
return is_int($id) || is_string($id) ? (string) $id : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
return $ids;
|
||||
}
|
||||
|
||||
private function findArtifactSize(string $output, string $jobName): ?int
|
||||
/**
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
private function findJob(string $encodedRepo, string $pipelineId, string $jobName): ?array
|
||||
{
|
||||
$jobs = json_decode($output, true);
|
||||
$page = 1;
|
||||
|
||||
if (! is_array($jobs)) {
|
||||
return null;
|
||||
}
|
||||
do {
|
||||
$process = new Process([
|
||||
'glab', 'api',
|
||||
sprintf(
|
||||
'projects/%s/pipelines/%s/jobs?per_page=%d&page=%d',
|
||||
$encodedRepo,
|
||||
$pipelineId,
|
||||
self::JOBS_PER_PAGE,
|
||||
$page,
|
||||
),
|
||||
]);
|
||||
$process->setTimeout(30.0);
|
||||
$process->run();
|
||||
|
||||
foreach ($jobs as $job) {
|
||||
if (! is_array($job) || ($job['name'] ?? null) !== $jobName || ! is_array($job['artifacts'] ?? null)) {
|
||||
continue;
|
||||
if (! $process->isSuccessful()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$size = 0;
|
||||
$jobs = json_decode($process->getOutput(), true);
|
||||
|
||||
foreach ($job['artifacts'] as $artifact) {
|
||||
if (is_array($artifact) && is_int($artifact['size'] ?? null)) {
|
||||
$size += $artifact['size'];
|
||||
if (! is_array($jobs)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($jobs as $job) {
|
||||
if (! is_array($job) || ($job['name'] ?? null) !== $jobName) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (($job['status'] ?? null) !== 'success') {
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @var array<string, mixed> $job */
|
||||
return $job;
|
||||
}
|
||||
|
||||
return $size;
|
||||
}
|
||||
$page++;
|
||||
} while (count($jobs) === self::JOBS_PER_PAGE);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -55,6 +55,38 @@ test('a published GitLab baseline is fetched instead of recorded locally', funct
|
||||
->and($project->graphExists())->toBeTrue();
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a published GitLab baseline is found in an older pipeline when newer ones lack the job', function (): void {
|
||||
[$project, $environment] = tiaPublishedBaselineGitLab('older-pipeline');
|
||||
|
||||
$result = $project->pestWithEnvironment($project->path(), $environment, '--tia', '--baselined');
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->output)->toContain('Downloading TIA baseline')
|
||||
->and($result->replayed())->toBe(Project::TOTAL_TESTS, $result->describe())
|
||||
->and($project->graphExists())->toBeTrue();
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a published GitLab baseline is found when its job appears on the second page', function (): void {
|
||||
[$project, $environment] = tiaPublishedBaselineGitLab('job-page-two');
|
||||
|
||||
$result = $project->pestWithEnvironment($project->path(), $environment, '--tia', '--baselined');
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->output)->toContain('Downloading TIA baseline')
|
||||
->and($result->replayed())->toBe(Project::TOTAL_TESTS, $result->describe())
|
||||
->and($project->graphExists())->toBeTrue();
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a GitLab pipeline whose baseline job did not succeed is not treated as published', function (): void {
|
||||
[$project, $environment] = tiaPublishedBaselineGitLab('job-failed');
|
||||
|
||||
$result = $project->pestWithEnvironment($project->path(), $environment, '--tia', '--baselined');
|
||||
|
||||
expect($result->exitCode)->toBe(0, $result->describe())
|
||||
->and($result->output)->toContain('No baseline published yet')
|
||||
->and($result->tally())->toContain(Project::TOTAL_TESTS.' passed');
|
||||
})->skipOnWindows();
|
||||
|
||||
test('a fetched GitLab baseline that will not decode is discarded rather than trusted', function (): void {
|
||||
[$project, $environment] = tiaPublishedBaselineGitLab('corrupt');
|
||||
|
||||
|
||||
@@ -7,17 +7,40 @@ fi
|
||||
|
||||
if [ "$1" = "api" ]; then
|
||||
case "$2" in
|
||||
*jobs\?scope*)
|
||||
*pipelines\?ref*)
|
||||
case "$GLAB_STUB_MODE" in
|
||||
no-runs) exit 0 ;;
|
||||
no-runs) printf '[]'; exit 0 ;;
|
||||
list-404) echo "404 Not Found" >&2; exit 1 ;;
|
||||
list-network) echo "could not resolve host" >&2; exit 1 ;;
|
||||
older-pipeline) printf '[{"id":111},{"id":222},{"id":987654321}]'; exit 0 ;;
|
||||
esac
|
||||
printf '[{"name":"tia-baseline","ref":"master","pipeline":{"id":987654321}}]'
|
||||
printf '[{"id":987654321}]'
|
||||
exit 0
|
||||
;;
|
||||
*pipelines/*/jobs*)
|
||||
printf '[{"name":"tia-baseline","artifacts":[{"size":2048}]}]'
|
||||
*pipelines/111/jobs*|*pipelines/222/jobs*)
|
||||
printf '[{"name":"some-other-job","status":"success","artifacts":[{"size":1}]}]'
|
||||
exit 0
|
||||
;;
|
||||
*pipelines/*/jobs*\&page=1)
|
||||
case "$GLAB_STUB_MODE" in
|
||||
job-failed) printf '[{"name":"tia-baseline","status":"failed","artifacts":[{"size":2048}]}]'; exit 0 ;;
|
||||
job-page-two)
|
||||
printf '['
|
||||
index=1
|
||||
while [ "$index" -le 100 ]; do
|
||||
[ "$index" -gt 1 ] && printf ','
|
||||
printf '{"name":"job-%d","status":"success","artifacts":[]}' "$index"
|
||||
index=$((index + 1))
|
||||
done
|
||||
printf ']'
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
printf '[{"name":"tia-baseline","status":"success","artifacts":[{"size":2048}]}]'
|
||||
exit 0
|
||||
;;
|
||||
*pipelines/*/jobs*\&page=2)
|
||||
printf '[{"name":"tia-baseline","status":"success","artifacts":[{"size":2048}]}]'
|
||||
exit 0
|
||||
;;
|
||||
projects/*)
|
||||
|
||||
Reference in New Issue
Block a user