diff --git a/pkg/cmd/release/list/http.go b/pkg/cmd/release/list/http.go index 6ef5f03288b..0d14df278d4 100644 --- a/pkg/cmd/release/list/http.go +++ b/pkg/cmd/release/list/http.go @@ -2,6 +2,7 @@ package list import ( "net/http" + "strings" "time" "github.com/cli/cli/v2/api" @@ -34,7 +35,7 @@ func (r *Release) ExportData(fields []string) map[string]interface{} { return cmdutil.StructExportData(r, fields) } -func fetchReleases(httpClient *http.Client, repo ghrepo.Interface, limit int, excludeDrafts bool, excludePreReleases bool) ([]Release, error) { +func fetchReleases(httpClient *http.Client, repo ghrepo.Interface, limit int, excludeDrafts bool, excludePreReleases bool, order string) ([]Release, error) { type responseData struct { Repository struct { Releases struct { @@ -43,7 +44,7 @@ func fetchReleases(httpClient *http.Client, repo ghrepo.Interface, limit int, ex HasNextPage bool EndCursor string } - } `graphql:"releases(first: $perPage, orderBy: {field: CREATED_AT, direction: DESC}, after: $endCursor)"` + } `graphql:"releases(first: $perPage, orderBy: {field: CREATED_AT, direction: $direction}, after: $endCursor)"` } `graphql:"repository(owner: $owner, name: $name)"` } @@ -57,6 +58,7 @@ func fetchReleases(httpClient *http.Client, repo ghrepo.Interface, limit int, ex "name": githubv4.String(repo.RepoName()), "perPage": githubv4.Int(perPage), "endCursor": (*githubv4.String)(nil), + "direction": githubv4.OrderDirection(strings.ToUpper(order)), } gql := api.NewClientFromHTTP(httpClient) diff --git a/pkg/cmd/release/list/list.go b/pkg/cmd/release/list/list.go index 8d88e95a7ca..24ec41840e1 100644 --- a/pkg/cmd/release/list/list.go +++ b/pkg/cmd/release/list/list.go @@ -23,6 +23,7 @@ type ListOptions struct { LimitResults int ExcludeDrafts bool ExcludePreReleases bool + Order string } func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command { @@ -50,6 +51,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman cmd.Flags().IntVarP(&opts.LimitResults, "limit", "L", 30, "Maximum number of items to fetch") cmd.Flags().BoolVar(&opts.ExcludeDrafts, "exclude-drafts", false, "Exclude draft releases") cmd.Flags().BoolVar(&opts.ExcludePreReleases, "exclude-pre-releases", false, "Exclude pre-releases") + cmdutil.StringEnumFlag(cmd, &opts.Order, "order", "O", "desc", []string{"asc", "desc"}, "Order of releases returned") cmdutil.AddJSONFlags(cmd, &opts.Exporter, releaseFields) return cmd @@ -66,7 +68,7 @@ func listRun(opts *ListOptions) error { return err } - releases, err := fetchReleases(httpClient, baseRepo, opts.LimitResults, opts.ExcludeDrafts, opts.ExcludePreReleases) + releases, err := fetchReleases(httpClient, baseRepo, opts.LimitResults, opts.ExcludeDrafts, opts.ExcludePreReleases, opts.Order) if err != nil { return err } diff --git a/pkg/cmd/release/list/list_test.go b/pkg/cmd/release/list/list_test.go index aafccd11e9a..79d6a601c3a 100644 --- a/pkg/cmd/release/list/list_test.go +++ b/pkg/cmd/release/list/list_test.go @@ -34,6 +34,7 @@ func Test_NewCmdList(t *testing.T) { LimitResults: 30, ExcludeDrafts: false, ExcludePreReleases: false, + Order: "desc", }, }, { @@ -43,6 +44,7 @@ func Test_NewCmdList(t *testing.T) { LimitResults: 30, ExcludeDrafts: true, ExcludePreReleases: false, + Order: "desc", }, }, { @@ -52,6 +54,17 @@ func Test_NewCmdList(t *testing.T) { LimitResults: 30, ExcludeDrafts: false, ExcludePreReleases: true, + Order: "desc", + }, + }, + { + name: "with order", + args: "--order asc", + want: ListOptions{ + LimitResults: 30, + ExcludeDrafts: false, + ExcludePreReleases: false, + Order: "asc", }, }, } @@ -92,6 +105,7 @@ func Test_NewCmdList(t *testing.T) { assert.Equal(t, tt.want.LimitResults, opts.LimitResults) assert.Equal(t, tt.want.ExcludeDrafts, opts.ExcludeDrafts) assert.Equal(t, tt.want.ExcludePreReleases, opts.ExcludePreReleases) + assert.Equal(t, tt.want.Order, opts.Order) }) } }