Add exclude-drafts flag to gh release list

This commit is contained in:
ffalor 2022-05-09 20:47:39 +00:00
parent c1345296ab
commit c3a3ce5bee
3 changed files with 19 additions and 4 deletions

View file

@ -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

View file

@ -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
}

View file

@ -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)
})
}
}