Skip to content

Commit

Permalink
Merge pull request cli#8632 from leevic31/8579-ordering-by-created-at…
Browse files Browse the repository at this point in the history
…-date-for-releases

Feature: added Order flag for release list command
  • Loading branch information
andyfeller authored Jan 31, 2024
2 parents dd25f88 + 3db1bee commit 28c3a40
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
6 changes: 4 additions & 2 deletions pkg/cmd/release/list/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package list

import (
"net/http"
"strings"
"time"

"github.com/cli/cli/v2/api"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)"`
}

Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/release/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/cmd/release/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Test_NewCmdList(t *testing.T) {
LimitResults: 30,
ExcludeDrafts: false,
ExcludePreReleases: false,
Order: "desc",
},
},
{
Expand All @@ -43,6 +44,7 @@ func Test_NewCmdList(t *testing.T) {
LimitResults: 30,
ExcludeDrafts: true,
ExcludePreReleases: false,
Order: "desc",
},
},
{
Expand All @@ -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",
},
},
}
Expand Down Expand Up @@ -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)
})
}
}
Expand Down

0 comments on commit 28c3a40

Please sign in to comment.