fix(run): show logs past jobs that have not started

run logs stopped at the first job without logs with a misleading not-found
error. Report such a job as "build (job 35): not started yet" (or that it
never started when it was canceled or skipped first) and continue with the
next job. Jobs report started_at and completed_at, null until they happen.

Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
bircni committed 2026-10-11 23:46:26 +02:00
1 parent 68da491b9e
commit b09c1aab4a
3 files changed
+94 -16

No files matched your search

+24 -3
View File
@@ -4,6 +4,7 @@
package app
import (
"cmp"
"context"
"errors"
"fmt"
@@ -86,11 +87,22 @@ func (a *application) runLogsCommand() *cobra.Command {
})
}
for _, job := range jobs {
if _, err := fmt.Fprintf(cmd.ErrOrStderr(), "Job %d: %s\n", job.ID, terminal.SafeLine(job.Name)); err != nil {
label := fmt.Sprintf("%s (job %d)", terminal.SafeLine(job.Name), job.ID)
var logs io.ReadCloser
if job.Started() {
logs, err = service.JobLogs(cmd.Context(), job.ID)
}
if !job.Started() || errors.Is(err, services.ErrJobNotStarted) {
if _, err := fmt.Fprintf(cmd.ErrOrStderr(), "%s: %s\n", label, unstarted(job)); err != nil {
return err
}
continue
}
if err != nil {
return err
}
logs, err := service.JobLogs(cmd.Context(), job.ID)
if err != nil {
if _, err := fmt.Fprintln(cmd.ErrOrStderr(), label); err != nil {
_ = logs.Close()
return err
}
_, copyErr := io.Copy(out, logs)
@@ -111,3 +123,12 @@ func (a *application) runLogsCommand() *cobra.Command {
cmd.Flags().DurationVar(&timeout, "timeout", time.Hour, "Maximum follow duration")
return cmd
}
// unstarted explains why a job has no logs: it is waiting for a runner, or it completed, such
// as by cancellation, before one started it.
func unstarted(job services.Job) string {
if job.CompletedAt == nil {
return "not started yet"
}
return "never started (" + cmp.Or(job.Conclusion, job.Status) + ")"
}
+34 -4
View File
@@ -20,7 +20,7 @@ func TestRunLogsStreamWithoutMixingDiagnosticsAndVerifyJobMembership(t *testing.
switch r.URL.Path {
case "/api/v1/repos/owner/repo/actions/runs/7/jobs":
if r.URL.Query().Get("page") == "1" {
_, _ = w.Write([]byte(`{"jobs":[{"id":9,"name":"Build"}]}`))
_, _ = w.Write([]byte(`{"jobs":[{"id":9,"name":"Build","started_at":"2028-01-01T00:00:00Z"}]}`))
} else {
_, _ = w.Write([]byte(`{"jobs":[]}`))
}
@@ -39,7 +39,7 @@ func TestRunLogsStreamWithoutMixingDiagnosticsAndVerifyJobMembership(t *testing.
args := []string{"run", "logs", "7", "-R", "owner/repo", "--job=9", "--no-input"}
require.NoError(t, Execute(context.Background(), deps, args))
require.Equal(t, "exact log\n", out.String())
require.Equal(t, "Job 9: Build\n", diagnostics.String())
require.Equal(t, "Build (job 9)\n", diagnostics.String())
require.Equal(t, 1, downloads)
out.Reset()
diagnostics.Reset()
@@ -71,7 +71,7 @@ func TestRunLogsKeepOnlyColorsOnATerminal(t *testing.T) {
switch r.URL.Path {
case "/api/v1/repos/owner/repo/actions/runs/7/jobs":
if r.URL.Query().Get("page") == "1" {
_, _ = w.Write([]byte(`{"jobs":[{"id":9,"name":"Build\u001b]0;PWNED\u0007"}]}`))
_, _ = w.Write([]byte(`{"jobs":[{"id":9,"name":"Build\u001b]0;PWNED\u0007","started_at":"2028-01-01T00:00:00Z"}]}`))
} else {
_, _ = w.Write([]byte(`{"jobs":[]}`))
}
@@ -88,6 +88,36 @@ func TestRunLogsKeepOnlyColorsOnATerminal(t *testing.T) {
deps.Streams.OutTTY = tty
require.NoError(t, Execute(context.Background(), deps, []string{"run", "logs", "7", "-R", "owner/repo", "--job=9", "--no-input"}))
require.Equal(t, want, out.String(), "tty=%t", tty)
require.Equal(t, "Job 9: Build]0;PWNED\n", diagnostics.String())
require.Equal(t, "Build]0;PWNED (job 9)\n", diagnostics.String())
}
}
func TestRunLogsContinuePastJobsThatHaveNotStarted(t *testing.T) {
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v1/repos/owner/repo/actions/runs/7/jobs":
if r.URL.Query().Get("page") != "1" {
_, _ = w.Write([]byte(`{"jobs":[]}`))
return
}
_, _ = w.Write([]byte(`{"jobs":[
{"id":8,"name":"lint","status":"completed","conclusion":"skipped","started_at":"1970-01-01T00:00:00Z","completed_at":"2028-01-01T00:01:00Z"},
{"id":9,"name":"build","status":"queued","started_at":"1970-01-01T00:00:00Z","completed_at":"1970-01-01T00:00:00Z"},
{"id":10,"name":"test","status":"in_progress","started_at":"2028-01-01T00:00:00Z"},
{"id":11,"name":"deploy","status":"in_progress","started_at":"2028-01-01T00:00:00Z"}]}`))
case "/api/v1/repos/owner/repo/actions/jobs/10/logs":
_, _ = w.Write([]byte("test output\n"))
case "/api/v1/repos/owner/repo/actions/jobs/11/logs":
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"message":"job not started"}`))
default:
t.Errorf("unexpected endpoint %s", r.URL.Path)
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
deps, out, diagnostics := serverDeps(t, server.URL)
require.NoError(t, Execute(context.Background(), deps, []string{"run", "logs", "7", "-R", "owner/repo", "--no-input"}))
require.Equal(t, "test output\n", out.String())
require.Equal(t, "lint (job 8): never started (skipped)\nbuild (job 9): not started yet\ntest (job 10)\ndeploy (job 11): not started yet\n", diagnostics.String())
}