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:
Caleb White
2026-11-13 10:01:45 -06:00
committed by GitHub
parent 65796f4939
commit 207a542547
3 changed files with 139 additions and 49 deletions
+79 -44
View File
@@ -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;
}