Skip to content

Commit 49ba21a

Browse files
committed
fix(agent-task view): display session duration
Signed-off-by: Babak K. Shandiz <babakks@github.com>
1 parent 87b310a commit 49ba21a

2 files changed

Lines changed: 99 additions & 62 deletions

File tree

pkg/cmd/agent-task/view/view.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@ func printSession(opts *ViewOptions, session *capi.Session) {
306306
fmt.Fprintf(opts.IO.Out, "Started %s\n", text.FuzzyAgo(time.Now(), session.CreatedAt))
307307
}
308308

309+
var durationNote string
310+
if session.CompletedAt.After(session.CreatedAt) {
311+
durationNote = fmt.Sprintf("Duration %s", session.CompletedAt.Sub(session.CreatedAt).Round(time.Second).String())
312+
}
313+
314+
if durationNote != "" {
315+
fmt.Fprintf(opts.IO.Out, "%s\n", cs.Muted(durationNote))
316+
}
317+
309318
if !opts.Log {
310319
fmt.Fprintln(opts.IO.Out, "")
311320
fmt.Fprintf(opts.IO.Out, "For detailed session logs, try:\ngh agent-task view '%s' --log\n", session.ID)

pkg/cmd/agent-task/view/view_test.go

Lines changed: 90 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ func TestNewCmdList(t *testing.T) {
159159

160160
func Test_viewRun(t *testing.T) {
161161
sampleDate := time.Now().Add(-6 * time.Hour) // 6h ago
162+
sampleCompletedAt := sampleDate.Add(5 * time.Minute)
162163

163164
tests := []struct {
164165
name string
@@ -212,9 +213,10 @@ func Test_viewRun(t *testing.T) {
212213
m.GetSessionFunc = func(_ context.Context, id string) (*capi.Session, error) {
213214
assert.Equal(t, "some-session-id", id)
214215
return &capi.Session{
215-
ID: "some-session-id",
216-
State: "completed",
217-
CreatedAt: sampleDate,
216+
ID: "some-session-id",
217+
State: "completed",
218+
CreatedAt: sampleDate,
219+
CompletedAt: sampleCompletedAt,
218220
PullRequest: &api.PullRequest{
219221
Title: "fix something",
220222
Number: 101,
@@ -232,6 +234,7 @@ func Test_viewRun(t *testing.T) {
232234
wantOut: heredoc.Doc(`
233235
Ready for review • fix something • OWNER/REPO#101
234236
Started on behalf of octocat about 6 hours ago
237+
Duration 5m0s
235238
236239
For detailed session logs, try:
237240
gh agent-task view 'some-session-id' --log
@@ -252,9 +255,10 @@ func Test_viewRun(t *testing.T) {
252255
m.GetSessionFunc = func(_ context.Context, id string) (*capi.Session, error) {
253256
assert.Equal(t, "some-session-id", id)
254257
return &capi.Session{
255-
ID: "some-session-id",
256-
State: "completed",
257-
CreatedAt: sampleDate,
258+
ID: "some-session-id",
259+
State: "completed",
260+
CreatedAt: sampleDate,
261+
CompletedAt: sampleCompletedAt,
258262
PullRequest: &api.PullRequest{
259263
Title: "fix something",
260264
Number: 101,
@@ -269,6 +273,7 @@ func Test_viewRun(t *testing.T) {
269273
wantOut: heredoc.Doc(`
270274
Ready for review • fix something • OWNER/REPO#101
271275
Started about 6 hours ago
276+
Duration 5m0s
272277
273278
For detailed session logs, try:
274279
gh agent-task view 'some-session-id' --log
@@ -289,9 +294,10 @@ func Test_viewRun(t *testing.T) {
289294
m.GetSessionFunc = func(_ context.Context, id string) (*capi.Session, error) {
290295
assert.Equal(t, "some-session-id", id)
291296
return &capi.Session{
292-
ID: "some-session-id",
293-
State: "completed",
294-
CreatedAt: sampleDate,
297+
ID: "some-session-id",
298+
State: "completed",
299+
CreatedAt: sampleDate,
300+
CompletedAt: sampleCompletedAt,
295301
User: &api.GitHubUser{
296302
Login: "octocat",
297303
},
@@ -301,6 +307,7 @@ func Test_viewRun(t *testing.T) {
301307
wantOut: heredoc.Doc(`
302308
Ready for review
303309
Started on behalf of octocat about 6 hours ago
310+
Duration 5m0s
304311
305312
For detailed session logs, try:
306313
gh agent-task view 'some-session-id' --log
@@ -318,15 +325,17 @@ func Test_viewRun(t *testing.T) {
318325
m.GetSessionFunc = func(_ context.Context, id string) (*capi.Session, error) {
319326
assert.Equal(t, "some-session-id", id)
320327
return &capi.Session{
321-
ID: "some-session-id",
322-
State: "completed",
323-
CreatedAt: sampleDate,
328+
ID: "some-session-id",
329+
State: "completed",
330+
CreatedAt: sampleDate,
331+
CompletedAt: sampleCompletedAt,
324332
}, nil
325333
}
326334
},
327335
wantOut: heredoc.Doc(`
328336
Ready for review
329337
Started about 6 hours ago
338+
Duration 5m0s
330339
331340
For detailed session logs, try:
332341
gh agent-task view 'some-session-id' --log
@@ -360,9 +369,10 @@ func Test_viewRun(t *testing.T) {
360369
m.GetSessionFunc = func(_ context.Context, id string) (*capi.Session, error) {
361370
assert.Equal(t, "some-session-id", id)
362371
return &capi.Session{
363-
ID: "some-session-id",
364-
State: "completed",
365-
CreatedAt: sampleDate,
372+
ID: "some-session-id",
373+
State: "completed",
374+
CreatedAt: sampleDate,
375+
CompletedAt: sampleCompletedAt,
366376
// User data is irrelevant in this case
367377
}, nil
368378
}
@@ -382,9 +392,10 @@ func Test_viewRun(t *testing.T) {
382392
m.GetSessionFunc = func(_ context.Context, id string) (*capi.Session, error) {
383393
assert.Equal(t, "some-session-id", id)
384394
return &capi.Session{
385-
ID: "some-session-id",
386-
State: "completed",
387-
CreatedAt: sampleDate,
395+
ID: "some-session-id",
396+
State: "completed",
397+
CreatedAt: sampleDate,
398+
CompletedAt: sampleCompletedAt,
388399
PullRequest: &api.PullRequest{
389400
Title: "fix something",
390401
Number: 101,
@@ -488,9 +499,10 @@ func Test_viewRun(t *testing.T) {
488499
assert.Equal(t, defaultLimit, limit)
489500
return []*capi.Session{
490501
{
491-
ID: "some-session-id",
492-
State: "completed",
493-
CreatedAt: sampleDate,
502+
ID: "some-session-id",
503+
State: "completed",
504+
CreatedAt: sampleDate,
505+
CompletedAt: sampleCompletedAt,
494506
PullRequest: &api.PullRequest{
495507
Title: "fix something",
496508
Number: 101,
@@ -509,6 +521,7 @@ func Test_viewRun(t *testing.T) {
509521
wantOut: heredoc.Doc(`
510522
Ready for review • fix something • OWNER/REPO#101
511523
Started on behalf of octocat about 6 hours ago
524+
Duration 5m0s
512525
513526
For detailed session logs, try:
514527
gh agent-task view 'some-session-id' --log
@@ -538,10 +551,11 @@ func Test_viewRun(t *testing.T) {
538551
assert.Equal(t, defaultLimit, limit)
539552
return []*capi.Session{
540553
{
541-
ID: "some-session-id",
542-
Name: "session one",
543-
State: "completed",
544-
CreatedAt: sampleDate,
554+
ID: "some-session-id",
555+
Name: "session one",
556+
State: "completed",
557+
CreatedAt: sampleDate,
558+
CompletedAt: sampleCompletedAt,
545559
PullRequest: &api.PullRequest{
546560
Title: "fix something",
547561
Number: 101,
@@ -555,10 +569,11 @@ func Test_viewRun(t *testing.T) {
555569
},
556570
},
557571
{
558-
ID: "some-other-session-id",
559-
Name: "session two",
560-
State: "completed",
561-
CreatedAt: sampleDate,
572+
ID: "some-other-session-id",
573+
Name: "session two",
574+
State: "completed",
575+
CreatedAt: sampleDate,
576+
CompletedAt: sampleCompletedAt,
562577
PullRequest: &api.PullRequest{
563578
Title: "fix something",
564579
Number: 101,
@@ -589,6 +604,7 @@ func Test_viewRun(t *testing.T) {
589604
wantOut: heredoc.Doc(`
590605
Ready for review • fix something • OWNER/REPO#101
591606
Started on behalf of octocat about 6 hours ago
607+
Duration 5m0s
592608
593609
For detailed session logs, try:
594610
gh agent-task view 'some-session-id' --log
@@ -620,10 +636,11 @@ func Test_viewRun(t *testing.T) {
620636
assert.Equal(t, defaultLimit, limit)
621637
return []*capi.Session{
622638
{
623-
ID: "some-session-id",
624-
Name: "session one",
625-
State: "completed",
626-
CreatedAt: sampleDate,
639+
ID: "some-session-id",
640+
Name: "session one",
641+
State: "completed",
642+
CreatedAt: sampleDate,
643+
CompletedAt: sampleCompletedAt,
627644
PullRequest: &api.PullRequest{
628645
Title: "fix something",
629646
Number: 101,
@@ -637,10 +654,11 @@ func Test_viewRun(t *testing.T) {
637654
},
638655
},
639656
{
640-
ID: "some-other-session-id",
641-
Name: "session two",
642-
State: "completed",
643-
CreatedAt: sampleDate,
657+
ID: "some-other-session-id",
658+
Name: "session two",
659+
State: "completed",
660+
CreatedAt: sampleDate,
661+
CompletedAt: sampleCompletedAt,
644662
PullRequest: &api.PullRequest{
645663
Title: "fix something",
646664
Number: 101,
@@ -671,6 +689,7 @@ func Test_viewRun(t *testing.T) {
671689
wantOut: heredoc.Doc(`
672690
Ready for review • fix something • OWNER/REPO#101
673691
Started on behalf of octocat about 6 hours ago
692+
Duration 5m0s
674693
675694
For detailed session logs, try:
676695
gh agent-task view 'some-session-id' --log
@@ -723,9 +742,10 @@ func Test_viewRun(t *testing.T) {
723742
assert.Equal(t, defaultLimit, limit)
724743
return []*capi.Session{
725744
{
726-
ID: "some-session-id",
727-
State: "completed",
728-
CreatedAt: sampleDate,
745+
ID: "some-session-id",
746+
State: "completed",
747+
CreatedAt: sampleDate,
748+
CompletedAt: sampleCompletedAt,
729749
PullRequest: &api.PullRequest{
730750
Title: "fix something",
731751
Number: 101,
@@ -764,10 +784,11 @@ func Test_viewRun(t *testing.T) {
764784
assert.Equal(t, defaultLimit, limit)
765785
return []*capi.Session{
766786
{
767-
ID: "some-session-id",
768-
Name: "session one",
769-
State: "completed",
770-
CreatedAt: sampleDate,
787+
ID: "some-session-id",
788+
Name: "session one",
789+
State: "completed",
790+
CreatedAt: sampleDate,
791+
CompletedAt: sampleCompletedAt,
771792
PullRequest: &api.PullRequest{
772793
Title: "fix something",
773794
Number: 101,
@@ -779,10 +800,11 @@ func Test_viewRun(t *testing.T) {
779800
// User data is irrelevant in this case
780801
},
781802
{
782-
ID: "some-other-session-id",
783-
Name: "session two",
784-
State: "completed",
785-
CreatedAt: sampleDate,
803+
ID: "some-other-session-id",
804+
Name: "session two",
805+
State: "completed",
806+
CreatedAt: sampleDate,
807+
CompletedAt: sampleCompletedAt,
786808
PullRequest: &api.PullRequest{
787809
Title: "fix something",
788810
Number: 101,
@@ -823,10 +845,11 @@ func Test_viewRun(t *testing.T) {
823845
assert.Equal(t, defaultLimit, limit)
824846
return []*capi.Session{
825847
{
826-
ID: "some-session-id",
827-
Name: "session one",
828-
State: "completed",
829-
CreatedAt: sampleDate,
848+
ID: "some-session-id",
849+
Name: "session one",
850+
State: "completed",
851+
CreatedAt: sampleDate,
852+
CompletedAt: sampleCompletedAt,
830853
PullRequest: &api.PullRequest{
831854
Title: "fix something",
832855
Number: 101,
@@ -838,10 +861,11 @@ func Test_viewRun(t *testing.T) {
838861
// User data is irrelevant in this case
839862
},
840863
{
841-
ID: "some-other-session-id",
842-
Name: "session two",
843-
State: "completed",
844-
CreatedAt: sampleDate,
864+
ID: "some-other-session-id",
865+
Name: "session two",
866+
State: "completed",
867+
CreatedAt: sampleDate,
868+
CompletedAt: sampleCompletedAt,
845869
PullRequest: &api.PullRequest{
846870
Title: "fix something",
847871
Number: 101,
@@ -870,9 +894,10 @@ func Test_viewRun(t *testing.T) {
870894
m.GetSessionFunc = func(_ context.Context, id string) (*capi.Session, error) {
871895
assert.Equal(t, "some-session-id", id)
872896
return &capi.Session{
873-
ID: "some-session-id",
874-
State: "completed",
875-
CreatedAt: sampleDate,
897+
ID: "some-session-id",
898+
State: "completed",
899+
CreatedAt: sampleDate,
900+
CompletedAt: sampleCompletedAt,
876901
User: &api.GitHubUser{
877902
Login: "octocat",
878903
},
@@ -892,6 +917,7 @@ func Test_viewRun(t *testing.T) {
892917
wantOut: heredoc.Doc(`
893918
Ready for review
894919
Started on behalf of octocat about 6 hours ago
920+
Duration 5m0s
895921
896922
To follow session logs, try:
897923
gh agent-task view 'some-session-id' --log --follow
@@ -913,9 +939,10 @@ func Test_viewRun(t *testing.T) {
913939
m.GetSessionFunc = func(_ context.Context, id string) (*capi.Session, error) {
914940
assert.Equal(t, "some-session-id", id)
915941
return &capi.Session{
916-
ID: "some-session-id",
917-
State: "completed",
918-
CreatedAt: sampleDate,
942+
ID: "some-session-id",
943+
State: "completed",
944+
CreatedAt: sampleDate,
945+
CompletedAt: sampleCompletedAt,
919946
User: &api.GitHubUser{
920947
Login: "octocat",
921948
},
@@ -949,6 +976,7 @@ func Test_viewRun(t *testing.T) {
949976
wantOut: heredoc.Doc(`
950977
Ready for review
951978
Started on behalf of octocat about 6 hours ago
979+
Duration 5m0s
952980
953981
(rendered:) <raw-logs-one>
954982
(rendered:) <raw-logs-two>

0 commit comments

Comments
 (0)