Skip to content
Open
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
17 changes: 17 additions & 0 deletions pkg/cmd/run/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"path/filepath"
"sort"

"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/internal/safepaths"
Expand Down Expand Up @@ -113,6 +114,22 @@ func runDownload(opts *DownloadOptions) error {
return fmt.Errorf("error fetching artifacts: %w", err)
}

// Prefer newer artifacts when there are multiple artifacts with the same
// name (e.g. rerun attempts of the same workflow run can produce duplicates).
sort.SliceStable(artifacts, func(i, j int) bool {
ai, aj := artifacts[i].CreatedAt, artifacts[j].CreatedAt
if ai.IsZero() && aj.IsZero() {
return false
}
if ai.IsZero() {
return false
}
if aj.IsZero() {
return true
}
return ai.After(aj)
})

numValidArtifacts := 0
for _, a := range artifacts {
if a.Expired {
Expand Down
9 changes: 6 additions & 3 deletions pkg/cmd/run/download/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"testing"
"time"

"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/prompter"
Expand Down Expand Up @@ -581,7 +582,7 @@ func Test_runDownload(t *testing.T) {
},
},
{
name: "avoid redownloading files of the same name",
name: "when multiple artifacts have the same name, download the newest one",
opts: DownloadOptions{
RunID: "2345",
},
Expand All @@ -595,6 +596,7 @@ func Test_runDownload(t *testing.T) {
Name: "artifact-1",
DownloadURL: "http://download.com/artifact1.zip",
Expired: false,
CreatedAt: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC),
},
files: []string{
"artifact-1-file",
Expand All @@ -605,17 +607,18 @@ func Test_runDownload(t *testing.T) {
Name: "artifact-1",
DownloadURL: "http://download.com/artifact2.zip",
Expired: false,
CreatedAt: time.Date(2026, 1, 2, 0, 0, 0, 0, time.UTC),
},
files: []string{
"artifact-2-file",
"artifact-1-newer-file",
},
},
},
},
},
},
expectedFiles: []string{
filepath.Join("artifact-1", "artifact-1-file"),
filepath.Join("artifact-1", "artifact-1-newer-file"),
},
},
{
Expand Down
11 changes: 7 additions & 4 deletions pkg/cmd/run/shared/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import (
"fmt"
"net/http"
"regexp"
"time"

"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghinstance"
"github.com/cli/cli/v2/internal/ghrepo"
)

type Artifact struct {
Name string `json:"name"`
Size uint64 `json:"size_in_bytes"`
DownloadURL string `json:"archive_download_url"`
Expired bool `json:"expired"`
Name string `json:"name"`
Size uint64 `json:"size_in_bytes"`
DownloadURL string `json:"archive_download_url"`
Expired bool `json:"expired"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

type artifactsPayload struct {
Expand Down