Skip to content

Commit a579b00

Browse files
author
Nate Smith
authored
Merge pull request cli#3408 from cli/fix-run-selection
Make run selection unique
2 parents 861fd5b + 4dd8a44 commit a579b00

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

pkg/cmd/run/shared/shared.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,15 @@ func GetJobs(client *api.Client, repo ghrepo.Interface, run Run) ([]Job, error)
257257

258258
func PromptForRun(cs *iostreams.ColorScheme, runs []Run) (string, error) {
259259
var selected int
260+
now := time.Now()
260261

261262
candidates := []string{}
262263

263264
for _, run := range runs {
264265
symbol, _ := Symbol(cs, run.Status, run.Conclusion)
265266
candidates = append(candidates,
266267
// TODO truncate commit message, long ones look terrible
267-
fmt.Sprintf("%s %s, %s (%s)", symbol, run.CommitMsg(), run.Name, run.HeadBranch))
268+
fmt.Sprintf("%s %s, %s (%s) %s", symbol, run.CommitMsg(), run.Name, run.HeadBranch, preciseAgo(now, run.CreatedAt)))
268269
}
269270

270271
// TODO consider custom filter so it's fuzzier. right now matches start anywhere in string but
@@ -380,3 +381,14 @@ func PullRequestForRun(client *api.Client, repo ghrepo.Interface, run Run) (int,
380381

381382
return number, nil
382383
}
384+
385+
func preciseAgo(now time.Time, createdAt time.Time) string {
386+
ago := now.Sub(createdAt)
387+
388+
if ago < 30*24*time.Hour {
389+
s := ago.Truncate(time.Second).String()
390+
return fmt.Sprintf("%s ago", s)
391+
}
392+
393+
return createdAt.Format("Jan _2, 2006")
394+
}

pkg/cmd/run/shared/shared_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package shared
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestPreciseAgo(t *testing.T) {
9+
const form = "2006-Jan-02 15:04:05"
10+
now, _ := time.Parse(form, "2021-Apr-12 14:00:00")
11+
12+
cases := map[string]string{
13+
"2021-Apr-12 14:00:00": "0s ago",
14+
"2021-Apr-12 13:59:30": "30s ago",
15+
"2021-Apr-12 13:59:00": "1m0s ago",
16+
"2021-Apr-12 13:30:15": "29m45s ago",
17+
"2021-Apr-12 13:00:00": "1h0m0s ago",
18+
"2021-Apr-12 02:30:45": "11h29m15s ago",
19+
"2021-Apr-11 14:00:00": "24h0m0s ago",
20+
"2021-Apr-01 14:00:00": "264h0m0s ago",
21+
"2021-Mar-12 14:00:00": "Mar 12, 2021",
22+
}
23+
24+
for createdAt, expected := range cases {
25+
d, _ := time.Parse(form, createdAt)
26+
got := preciseAgo(now, d)
27+
if got != expected {
28+
t.Errorf("expected %s but got %s for %s", expected, got, createdAt)
29+
}
30+
}
31+
}

pkg/cmd/run/view/view.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func runView(opts *ViewOptions) error {
256256
}
257257

258258
opts.IO.StartProgressIndicator()
259-
runLogZip, err := getRunLog(opts.RunLogCache, httpClient, repo, run.ID)
259+
runLogZip, err := getRunLog(opts.RunLogCache, httpClient, repo, run)
260260
opts.IO.StopProgressIndicator()
261261
if err != nil {
262262
return fmt.Errorf("failed to get run log: %w", err)
@@ -408,13 +408,13 @@ func getLog(httpClient *http.Client, logURL string) (io.ReadCloser, error) {
408408
return resp.Body, nil
409409
}
410410

411-
func getRunLog(cache runLogCache, httpClient *http.Client, repo ghrepo.Interface, runID int) (*zip.ReadCloser, error) {
412-
filename := fmt.Sprintf("run-log-%d.zip", runID)
411+
func getRunLog(cache runLogCache, httpClient *http.Client, repo ghrepo.Interface, run *shared.Run) (*zip.ReadCloser, error) {
412+
filename := fmt.Sprintf("run-log-%d-%d.zip", run.ID, run.CreatedAt.Unix())
413413
filepath := filepath.Join(os.TempDir(), "gh-cli-cache", filename)
414414
if !cache.Exists(filepath) {
415415
// Run log does not exist in cache so retrieve and store it
416416
logURL := fmt.Sprintf("%srepos/%s/actions/runs/%d/logs",
417-
ghinstance.RESTPrefix(repo.RepoHost()), ghrepo.FullName(repo), runID)
417+
ghinstance.RESTPrefix(repo.RepoHost()), ghrepo.FullName(repo), run.ID)
418418

419419
resp, err := getLog(httpClient, logURL)
420420
if err != nil {
@@ -498,6 +498,9 @@ func displayRunLog(io *iostreams.IOStreams, jobs []shared.Job, failed bool) erro
498498
if failed && !shared.IsFailureState(step.Conclusion) {
499499
continue
500500
}
501+
if step.Log == nil {
502+
continue
503+
}
501504
prefix := fmt.Sprintf("%s\t%s\t", job.Name, step.Name)
502505
f, err := step.Log.Open()
503506
if err != nil {

0 commit comments

Comments
 (0)