Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-logs-processed-runs-shadowing.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions pkg/cli/logs_orchestrator_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ func cancelLogsDownload(cancel context.CancelFunc) {
}
}

// logsFetchWorkflowRunBatch and logsProcessWorkflowRunBatch are indirection points so
// tests can exercise the batch collection loop without hitting the GitHub API.
var (
logsFetchWorkflowRunBatch = fetchWorkflowRunBatch
logsProcessWorkflowRunBatch = processWorkflowRunBatch
)

func collectProcessedWorkflowRuns(runtime logsDownloadRuntime, opts LogsDownloadOptions) ([]ProcessedRun, bool, bool, error) {
var processedRuns []ProcessedRun
var beforeDate string
Expand All @@ -189,7 +196,7 @@ func collectProcessedWorkflowRuns(runtime logsDownloadRuntime, opts LogsDownload
}
iteration++
logLogsIterationFetch(opts, runtime.fetchAllInRange, iteration, len(processedRuns))
batch, err := fetchWorkflowRunBatch(runtime.activeCtx, opts, beforeDate, len(processedRuns), runtime.fetchAllInRange)
batch, err := logsFetchWorkflowRunBatch(runtime.activeCtx, opts, beforeDate, len(processedRuns), runtime.fetchAllInRange)
if err != nil {
// Context deadline exceeded means our own timeout fired mid-call.
// Treat it like a graceful timeout: return whatever was collected so far.
Expand Down Expand Up @@ -218,7 +225,10 @@ func collectProcessedWorkflowRuns(runtime logsDownloadRuntime, opts LogsDownload
}
}
logWorkflowRunBatchFound(batch, iteration, opts.Verbose)
processedRuns, batchProcessed, allRunsConsumed, timedOut := processWorkflowRunBatch(runtime.activeCtx, batch, processedRuns, processWorkflowRunBatchOptions{
var batchProcessed int
var allRunsConsumed, batchTimedOut bool
// Assign (not :=) so the accumulated runs are not shadowed by a loop-scoped variable.
processedRuns, batchProcessed, allRunsConsumed, batchTimedOut = logsProcessWorkflowRunBatch(runtime.activeCtx, batch, processedRuns, processWorkflowRunBatchOptions{
count: opts.Count,
outputDir: opts.OutputDir,
verbose: opts.Verbose,
Expand All @@ -229,7 +239,7 @@ func collectProcessedWorkflowRuns(runtime logsDownloadRuntime, opts LogsDownload
parse: opts.Parse,
filters: runtime.filters,
})
timeoutReached = timeoutReached || timedOut
timeoutReached = timeoutReached || batchTimedOut
logProcessedWorkflowRunBatch(opts, runtime.fetchAllInRange, iteration, batchProcessed, len(processedRuns), opts.Verbose)
if allRunsConsumed {
if cursor, ok := selectPaginationCursorDate(batch.runs, batch.oldestFetchedCreatedAt); ok {
Expand Down
46 changes: 46 additions & 0 deletions pkg/cli/logs_orchestrator_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,49 @@ func TestHandleEmptyWorkflowRunBatch(t *testing.T) {
assert.False(t, shouldStop)
})
}

// TestCollectProcessedWorkflowRunsAccumulatesBatches is a regression test for a bug where
// the batch results were assigned to a loop-scoped copy of processedRuns, so every
// processed run was discarded and `gh aw logs` reported "No workflow runs with artifacts
// found matching the specified criteria" even though artifacts had been downloaded.
func TestCollectProcessedWorkflowRunsAccumulatesBatches(t *testing.T) {
batches := [][]WorkflowRun{
{{DatabaseID: 1}, {DatabaseID: 2}},
{{DatabaseID: 3}},
}
fetchCalls := 0

originalFetch := logsFetchWorkflowRunBatch
originalProcess := logsProcessWorkflowRunBatch
t.Cleanup(func() {
logsFetchWorkflowRunBatch = originalFetch
logsProcessWorkflowRunBatch = originalProcess
})

logsFetchWorkflowRunBatch = func(_ context.Context, _ LogsDownloadOptions, _ string, _ int, _ bool) (workflowRunBatch, error) {
if fetchCalls >= len(batches) {
return workflowRunBatch{runs: nil, totalFetched: 0, batchSize: 2}, nil
}
runs := batches[fetchCalls]
fetchCalls++
// totalFetched == batchSize keeps pagination going after the first batch.
return workflowRunBatch{runs: runs, totalFetched: len(runs), batchSize: 2}, nil
}
logsProcessWorkflowRunBatch = func(_ context.Context, batch workflowRunBatch, processedRuns []ProcessedRun, _ processWorkflowRunBatchOptions) ([]ProcessedRun, int, bool, bool) {
for _, run := range batch.runs {
processedRuns = append(processedRuns, ProcessedRun{Run: run})
}
return processedRuns, len(batch.runs), true, false
}

runs, timeoutReached, countLimitReached, err := collectProcessedWorkflowRuns(
logsDownloadRuntime{activeCtx: context.Background(), fetchAllInRange: true},
LogsDownloadOptions{Count: 100, StartDate: "-1d"},
)
require.NoError(t, err)
assert.False(t, timeoutReached)
assert.False(t, countLimitReached)
require.Len(t, runs, 3, "runs from every batch should accumulate across iterations")
assert.Equal(t, int64(1), runs[0].Run.DatabaseID)
assert.Equal(t, int64(3), runs[2].Run.DatabaseID)
}
Loading