DEV Community

Himanshu Kumar
Himanshu Kumar Subscriber

Posted on Edited on

A job retry should not hide the next job in GitLab CI

A pipeline view should help answer one question quickly: what is still running, waiting, or blocked? In glab ci view, a retry could make that answer incomplete. The retry showed up correctly, but an unrelated item later in the list could disappear. A downstream pipeline trigger could still exist in GitLab while the terminal view gave no sign of it.

This was a rendering bug. The fix does not change which CI jobs run or when a downstream pipeline starts. It changes which entries the CLI keeps when it builds the view.

Three entries are enough to reproduce it

The CLI collects pipeline jobs, then appends downstream pipeline triggers before building the display list. Here is the input order from the regression test, with the downstream entry representing a trigger:

ID  stage   name
 1  test    unit        original attempt
 2  test    unit        retry
 3  deploy  downstream distinct entry
Enter fullscreen mode Exit fullscreen mode

Expected display: unit attempt 2, then downstream (ID 3). The old code returned only unit attempt 2. That is what made the bug easy to miss: the deduplication looked correct if you only checked which retry won.

Where the row went

The old helper did two things. It recorded the last entry for each job key, then used the first repeated key to decide how many output slots to allocate. It assumed duplicates marked the beginning of a retry-only suffix. Here, the second entry repeats test/unit, so the output gets only one slot. The helper fills that slot with attempt 2. It has already seen the downstream entry, but never gives it a slot.

That assumption might hold for one neatly ordered input. It does not hold after the caller appends another stream of entries. The first duplicate tells you that one job was seen before. It tells you nothing about the next entry.

Keep the position and replace the attempt

The proposed fix gives each distinct (stage, name) pair an output index on its first appearance. Every subsequent appearance updates the job at that index. The loop continues through the entire slice, so a new key still receives a new slot after any number of repeats.

input entry       decision                   rows after the step
test/unit #1      append at index 0          [unit #1]
test/unit #2      replace index 0            [unit #2]
deploy/downstream append at index 1          [unit #2, downstream #3]
Enter fullscreen mode Exit fullscreen mode

Using a Go struct as the map key also keeps the two fields separate. The old concatenation made ("ab", "c") and ("a", "bc") both become abc. A struct key distinguishes them without a separator or escaping rule. The scan takes one pass and uses a map entry per distinct key. It preserves the first appearance order and shows the last supplied attempt for each job. This is separate from deciding the configured order of pipeline stages.

The same test failed before and passed after

To isolate the change, the focused test ran against the old helper with only the new test added, then again with the patch:

go test -count=1 -run '^Test_latestJobsKeepsJobsAfterRetry$' ./internal/commands/ci/view
Enter fullscreen mode Exit fullscreen mode
Code under test Observed result
Old helper plus new test Failed. Expected two entries, got one. The missing entry was downstream (ID 3).
Patched helper plus the same test Passed. The test checks that the retry and the downstream entry both survive, in order.

The GitLab merge request includes the patch and the regression test. The full CI-view package tests and the MR pipeline passed. The MR is still open, so this is a proposed fix.

If your own display code collapses repeated events, put an unrelated item immediately after the first duplicate in the test. Then assert the exact output order as well as the winning attempt. That variation catches truncation bugs that a list ending in retries can hide.

AI-assisted writing.

Top comments (0)