From c3a3ce5bee1df28c5bef67334f3d40285deded3c Mon Sep 17 00:00:00 2001 From: ffalor Date: Mon, 9 May 2022 20:47:39 +0000 Subject: [PATCH] Add `exclude-drafts` flag to `gh release list` --- pkg/cmd/release/list/http.go | 5 ++++- pkg/cmd/release/list/list.go | 6 ++++-- pkg/cmd/release/list/list_test.go | 12 +++++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/release/list/http.go b/pkg/cmd/release/list/http.go index f502b75d4..2a761396a 100644 --- a/pkg/cmd/release/list/http.go +++ b/pkg/cmd/release/list/http.go @@ -21,7 +21,7 @@ type Release struct { PublishedAt time.Time } -func fetchReleases(httpClient *http.Client, repo ghrepo.Interface, limit int) ([]Release, error) { +func fetchReleases(httpClient *http.Client, repo ghrepo.Interface, limit int, excludeDrafts bool) ([]Release, error) { type responseData struct { Repository struct { Releases struct { @@ -58,6 +58,9 @@ loop: } for _, r := range query.Repository.Releases.Nodes { + if excludeDrafts && r.IsDraft { + continue + } releases = append(releases, r) if len(releases) == limit { break loop diff --git a/pkg/cmd/release/list/list.go b/pkg/cmd/release/list/list.go index f46059a9a..1747d56b0 100644 --- a/pkg/cmd/release/list/list.go +++ b/pkg/cmd/release/list/list.go @@ -18,7 +18,8 @@ type ListOptions struct { IO *iostreams.IOStreams BaseRepo func() (ghrepo.Interface, error) - LimitResults int + LimitResults int + ExcludeDrafts bool } func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command { @@ -44,6 +45,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") return cmd } @@ -59,7 +61,7 @@ func listRun(opts *ListOptions) error { return err } - releases, err := fetchReleases(httpClient, baseRepo, opts.LimitResults) + releases, err := fetchReleases(httpClient, baseRepo, opts.LimitResults, opts.ExcludeDrafts) if err != nil { return err } diff --git a/pkg/cmd/release/list/list_test.go b/pkg/cmd/release/list/list_test.go index 28ed6d7c8..c88185aad 100644 --- a/pkg/cmd/release/list/list_test.go +++ b/pkg/cmd/release/list/list_test.go @@ -31,7 +31,16 @@ func Test_NewCmdList(t *testing.T) { args: "", isTTY: true, want: ListOptions{ - LimitResults: 30, + LimitResults: 30, + ExcludeDrafts: false, + }, + }, + { + name: "exclude drafts", + args: "--exclude-drafts", + want: ListOptions{ + LimitResults: 30, + ExcludeDrafts: true, }, }, } @@ -70,6 +79,7 @@ func Test_NewCmdList(t *testing.T) { } assert.Equal(t, tt.want.LimitResults, opts.LimitResults) + assert.Equal(t, tt.want.ExcludeDrafts, opts.ExcludeDrafts) }) } }