From ecfbb67e9950a387c4e5878ce263512b3fc157da Mon Sep 17 00:00:00 2001 From: "Babak K. Shandiz" Date: Fri, 19 Sep 2025 17:37:07 +0100 Subject: [PATCH] fix(agent-task view): improve session overview Signed-off-by: Babak K. Shandiz --- pkg/cmd/agent-task/view/view.go | 28 ++++++++-------- pkg/cmd/agent-task/view/view_test.go | 49 +++++++++++++++++++++------- 2 files changed, 53 insertions(+), 24 deletions(-) diff --git a/pkg/cmd/agent-task/view/view.go b/pkg/cmd/agent-task/view/view.go index 91ef5c4d8..38c1e0e12 100644 --- a/pkg/cmd/agent-task/view/view.go +++ b/pkg/cmd/agent-task/view/view.go @@ -296,17 +296,10 @@ func viewRun(opts *ViewOptions) error { func printSession(opts *ViewOptions, session *capi.Session) { cs := opts.IO.ColorScheme() - if session.PullRequest != nil { - fmt.Fprintf(opts.IO.Out, "%s • %s • %s%s\n", - shared.ColorFuncForSessionState(*session, cs)(shared.SessionStateString(session.State)), - cs.Bold(session.PullRequest.Title), - session.PullRequest.Repository.NameWithOwner, - cs.ColorFromString(prShared.ColorForPRState(*session.PullRequest))(fmt.Sprintf("#%d", session.PullRequest.Number)), - ) - } else { - // This can happen when the session is just created and a PR is not yet available for it - fmt.Fprintf(opts.IO.Out, "%s\n", shared.ColorFuncForSessionState(*session, cs)(shared.SessionStateString(session.State))) - } + fmt.Fprintf(opts.IO.Out, "%s • %s\n", + shared.ColorFuncForSessionState(*session, cs)(shared.SessionStateString(session.State)), + cs.Bold(session.Name), + ) if session.User != nil { fmt.Fprintf(opts.IO.Out, "Started on behalf of %s %s\n", session.User.Login, text.FuzzyAgo(time.Now(), session.CreatedAt)) @@ -325,6 +318,15 @@ func printSession(opts *ViewOptions, session *capi.Session) { fmt.Fprintf(opts.IO.Out, "%s%s\n", cs.Muted(usedPremiumRequestsNote), cs.Muted(durationNote)) + // Note that when the session is just created, a PR is not yet available for it. + if session.PullRequest != nil { + fmt.Fprintf(opts.IO.Out, "\n%s%s • %s\n", + session.PullRequest.Repository.NameWithOwner, + cs.ColorFromString(prShared.ColorForPRState(*session.PullRequest))(fmt.Sprintf("#%d", session.PullRequest.Number)), + cs.Bold(session.PullRequest.Title), + ) + } + if session.Error != nil { var workflowRunURL string if session.WorkflowRunID != 0 && session.PullRequest != nil { @@ -347,9 +349,9 @@ func printSession(opts *ViewOptions, session *capi.Session) { } if !opts.Log { - fmt.Fprintf(opts.IO.Out, "\nFor detailed session logs, try:\ngh agent-task view '%s' --log\n", session.ID) + fmt.Fprint(opts.IO.Out, cs.Mutedf("\nFor detailed session logs, try:\ngh agent-task view '%s' --log\n", session.ID)) } else if !opts.Follow { - fmt.Fprintf(opts.IO.Out, "\nTo follow session logs, try:\ngh agent-task view '%s' --log --follow\n", session.ID) + fmt.Fprint(opts.IO.Out, cs.Mutedf("\nTo follow session logs, try:\ngh agent-task view '%s' --log --follow\n", session.ID)) } if session.PullRequest != nil { diff --git a/pkg/cmd/agent-task/view/view_test.go b/pkg/cmd/agent-task/view/view_test.go index d6136e9e9..74ac63d4b 100644 --- a/pkg/cmd/agent-task/view/view_test.go +++ b/pkg/cmd/agent-task/view/view_test.go @@ -215,6 +215,7 @@ func Test_viewRun(t *testing.T) { return &capi.Session{ ID: "some-session-id", State: "completed", + Name: "session one", CreatedAt: sampleDate, CompletedAt: sampleCompletedAt, PremiumRequests: 1.5, @@ -233,10 +234,12 @@ func Test_viewRun(t *testing.T) { } }, wantOut: heredoc.Doc(` - Ready for review • fix something • OWNER/REPO#101 + Ready for review • session one Started on behalf of octocat about 6 hours ago Used 1.5 premium request(s) • Duration 5m0s + OWNER/REPO#101 • fix something + For detailed session logs, try: gh agent-task view 'some-session-id' --log @@ -258,6 +261,7 @@ func Test_viewRun(t *testing.T) { return &capi.Session{ ID: "some-session-id", State: "completed", + Name: "session one", CreatedAt: sampleDate, CompletedAt: sampleCompletedAt, PremiumRequests: 1.5, @@ -273,10 +277,12 @@ func Test_viewRun(t *testing.T) { } }, wantOut: heredoc.Doc(` - Ready for review • fix something • OWNER/REPO#101 + Ready for review • session one Started about 6 hours ago Used 1.5 premium request(s) • Duration 5m0s + OWNER/REPO#101 • fix something + For detailed session logs, try: gh agent-task view 'some-session-id' --log @@ -298,6 +304,7 @@ func Test_viewRun(t *testing.T) { return &capi.Session{ ID: "some-session-id", State: "completed", + Name: "session one", CreatedAt: sampleDate, CompletedAt: sampleCompletedAt, PremiumRequests: 1.5, @@ -308,7 +315,7 @@ func Test_viewRun(t *testing.T) { } }, wantOut: heredoc.Doc(` - Ready for review + Ready for review • session one Started on behalf of octocat about 6 hours ago Used 1.5 premium request(s) • Duration 5m0s @@ -330,6 +337,7 @@ func Test_viewRun(t *testing.T) { return &capi.Session{ ID: "some-session-id", State: "completed", + Name: "session one", CreatedAt: sampleDate, CompletedAt: sampleCompletedAt, PremiumRequests: 1.5, @@ -337,7 +345,7 @@ func Test_viewRun(t *testing.T) { } }, wantOut: heredoc.Doc(` - Ready for review + Ready for review • session one Started about 6 hours ago Used 1.5 premium request(s) • Duration 5m0s @@ -358,6 +366,7 @@ func Test_viewRun(t *testing.T) { return &capi.Session{ ID: "some-session-id", State: "completed", + Name: "session one", CreatedAt: sampleDate, CompletedAt: sampleCompletedAt, PremiumRequests: 0, @@ -376,10 +385,12 @@ func Test_viewRun(t *testing.T) { } }, wantOut: heredoc.Doc(` - Ready for review • fix something • OWNER/REPO#101 + Ready for review • session one Started on behalf of octocat about 6 hours ago Used 0 premium request(s) • Duration 5m0s + OWNER/REPO#101 • fix something + For detailed session logs, try: gh agent-task view 'some-session-id' --log @@ -400,6 +411,7 @@ func Test_viewRun(t *testing.T) { return &capi.Session{ ID: "some-session-id", State: "in_progress", + Name: "session one", CreatedAt: sampleDate, PremiumRequests: 1.5, PullRequest: &api.PullRequest{ @@ -417,10 +429,12 @@ func Test_viewRun(t *testing.T) { } }, wantOut: heredoc.Doc(` - In progress • fix something • OWNER/REPO#101 + In progress • session one Started on behalf of octocat about 6 hours ago Used 1.5 premium request(s) + OWNER/REPO#101 • fix something + For detailed session logs, try: gh agent-task view 'some-session-id' --log @@ -441,6 +455,7 @@ func Test_viewRun(t *testing.T) { return &capi.Session{ ID: "some-session-id", State: "failed", + Name: "session one", CreatedAt: sampleDate, PremiumRequests: 1.5, Error: &capi.SessionError{ @@ -461,10 +476,12 @@ func Test_viewRun(t *testing.T) { } }, wantOut: heredoc.Doc(` - Failed • fix something • OWNER/REPO#101 + Failed • session one Started on behalf of octocat about 6 hours ago Used 1.5 premium request(s) + OWNER/REPO#101 • fix something + X blah blah For detailed session logs, try: @@ -487,6 +504,7 @@ func Test_viewRun(t *testing.T) { return &capi.Session{ ID: "some-session-id", State: "failed", + Name: "session one", CreatedAt: sampleDate, PremiumRequests: 1.5, WorkflowRunID: 9999, @@ -508,10 +526,12 @@ func Test_viewRun(t *testing.T) { } }, wantOut: heredoc.Doc(` - Failed • fix something • OWNER/REPO#101 + Failed • session one Started on behalf of octocat about 6 hours ago Used 1.5 premium request(s) + OWNER/REPO#101 • fix something + X blah blah https://github.com/OWNER/REPO/actions/runs/9999 @@ -696,6 +716,7 @@ func Test_viewRun(t *testing.T) { return &capi.Session{ ID: "some-session-id", State: "completed", + Name: "session one", CreatedAt: sampleDate, CompletedAt: sampleCompletedAt, PremiumRequests: 1.5, @@ -714,10 +735,12 @@ func Test_viewRun(t *testing.T) { } }, wantOut: heredoc.Doc(` - Ready for review • fix something • OWNER/REPO#101 + Ready for review • session one Started on behalf of octocat about 6 hours ago Used 1.5 premium request(s) • Duration 5m0s + OWNER/REPO#101 • fix something + For detailed session logs, try: gh agent-task view 'some-session-id' --log @@ -799,10 +822,12 @@ func Test_viewRun(t *testing.T) { ) }, wantOut: heredoc.Doc(` - Ready for review • fix something • OWNER/REPO#101 + Ready for review • session one Started on behalf of octocat about 6 hours ago Used 1.5 premium request(s) • Duration 5m0s + OWNER/REPO#101 • fix something + For detailed session logs, try: gh agent-task view 'some-session-id' --log @@ -886,10 +911,12 @@ func Test_viewRun(t *testing.T) { ) }, wantOut: heredoc.Doc(` - Ready for review • fix something • OWNER/REPO#101 + Ready for review • session one Started on behalf of octocat about 6 hours ago Used 1.5 premium request(s) • Duration 5m0s + OWNER/REPO#101 • fix something + For detailed session logs, try: gh agent-task view 'some-session-id' --log