Skip to content

Commit

Permalink
Resolve test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
heaths committed Jan 31, 2024
1 parent 36ea47c commit 2f6e6bb
Show file tree
Hide file tree
Showing 2 changed files with 276 additions and 18 deletions.
34 changes: 25 additions & 9 deletions pkg/cmd/api/api.go
Expand Up @@ -50,6 +50,7 @@ type ApiOptions struct {
Previews []string
ShowResponseHeaders bool
Paginate bool
PaginateAll bool
Silent bool
Template string
CacheTTL time.Duration
Expand Down Expand Up @@ -117,6 +118,8 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command
there are no more pages of results. For GraphQL requests, this requires that the
original query accepts an %[1]s$endCursor: String%[1]s variable and that it fetches the
%[1]spageInfo{ hasNextPage, endCursor }%[1]s set of fields from a collection.
Pass %[1]s--paginate-all%[1]s (experimental) instead to merge REST arrays and GraphQL objects
into valid JSON output.
`, "`"),
Example: heredoc.Doc(`
# list releases in the current repository
Expand Down Expand Up @@ -199,18 +202,26 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command
}
}

if opts.Paginate && !strings.EqualFold(opts.RequestMethod, "GET") && opts.RequestPath != "graphql" {
return cmdutil.FlagErrorf("the `--paginate` option is not supported for non-GET requests")
if opts.shouldPaginate() && !strings.EqualFold(opts.RequestMethod, "GET") && opts.RequestPath != "graphql" {
return cmdutil.FlagErrorf("the `--paginate` and `--paginate-all` options are not supported for non-GET requests")
}

if err := cmdutil.MutuallyExclusive(
"the `--paginate` option is not supported with `--input`",
opts.Paginate,
"the `--paginate` and `--paginate-all` options are not supported with `--input`",
opts.shouldPaginate(),
opts.RequestInputFile != "",
); err != nil {
return err
}

if err := cmdutil.MutuallyExclusive(
"specify only one of `--paginate` or `--paginate-all`",
opts.Paginate,
opts.PaginateAll,
); err != nil {
return err
}

if err := cmdutil.MutuallyExclusive(
"only one of `--template`, `--jq`, `--silent`, or `--verbose` may be used",
opts.Verbose,
Expand All @@ -236,6 +247,7 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command
cmd.Flags().StringSliceVarP(&opts.Previews, "preview", "p", nil, "GitHub API preview `names` to request (without the \"-preview\" suffix)")
cmd.Flags().BoolVarP(&opts.ShowResponseHeaders, "include", "i", false, "Include HTTP response status line and headers in the output")
cmd.Flags().BoolVar(&opts.Paginate, "paginate", false, "Make additional HTTP requests to fetch all pages of results")
cmd.Flags().BoolVar(&opts.PaginateAll, "paginate-all", false, "[Experimental] Make additional HTTP requests to fetch all pages and merge results")
cmd.Flags().StringVar(&opts.RequestInputFile, "input", "", "The `file` to use as body for the HTTP request (use \"-\" to read from standard input)")
cmd.Flags().BoolVar(&opts.Silent, "silent", false, "Do not print the response body")
cmd.Flags().StringVarP(&opts.Template, "template", "t", "", "Format JSON output using a Go template; see \"gh help formatting\"")
Expand All @@ -245,6 +257,10 @@ func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command
return cmd
}

func (opts *ApiOptions) shouldPaginate() bool {
return opts.Paginate || opts.PaginateAll
}

func apiRun(opts *ApiOptions) error {
params, err := parseFields(opts)
if err != nil {
Expand Down Expand Up @@ -278,12 +294,12 @@ func apiRun(opts *ApiOptions) error {
headersWriter = io.Discard
}

if opts.Paginate && !isGraphQL {
if opts.shouldPaginate() && !isGraphQL {
requestPath = addPerPage(requestPath, 100, params)
}

// Merge JSON arrays and object if paginating without a filter or template.
if opts.Paginate && opts.FilterOutput == "" && opts.Template == "" {
// Merge JSON arrays and objects if paginating without filtering or templating (experimental).
if opts.PaginateAll && opts.FilterOutput == "" && opts.Template == "" {
if isGraphQL {
opts.Merger = jsonmerge.NewObjectMerger(bodyWriter)
} else {
Expand Down Expand Up @@ -375,7 +391,7 @@ func apiRun(opts *ApiOptions) error {
}
isFirstPage = false

if !opts.Paginate {
if !opts.shouldPaginate() {
break
}

Expand Down Expand Up @@ -422,7 +438,7 @@ func processResponse(resp *http.Response, opts *ApiOptions, bodyWriter, headersW
}

var bodyCopy *bytes.Buffer
isGraphQLPaginate := isJSON && resp.StatusCode == 200 && opts.Paginate && opts.RequestPath == "graphql"
isGraphQLPaginate := isJSON && resp.StatusCode == 200 && opts.shouldPaginate() && opts.RequestPath == "graphql"
if isGraphQLPaginate {
bodyCopy = &bytes.Buffer{}
responseBody = io.TeeReader(responseBody, bodyCopy)
Expand Down

0 comments on commit 2f6e6bb

Please sign in to comment.