Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: added Order flag for release list command #8632

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/cmd/release/list/http.go
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
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
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