-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Merge JSON responses from gh api
#8620
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8e27e9a
Merge JSON responses from `gh api`
heaths a651036
Move jsonmerge package to internal
heaths e7f5dde
Add more JSON merge tests
heaths 310e552
Resolve test issues
heaths 48f0cd6
Replace --paginate-all with --merge-pages
heaths 0dfe6ec
Clarify --merge-pages docs
heaths f41876d
Resolve PR comments
heaths e83e049
Wrap JSON arrays, objects in array with --slurp
heaths a76af85
Resolve PR comments
heaths 4ea7bca
Run defers in queue
heaths 2758b80
Remove unnecessary --help comment
heaths File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @heaths, one of the issues with long running PRs like this is remembering all the context. When I played around with this just now I anticipated that for REST endpoints
--paginate --slurp
would be the same as--paginate
since the content of REST responses already get slurped into an array. However what happens is we end up doubly nesting the array:Maybe my assumption is bad and we always want to apply the same semantics to
--slurp
even if it produces a strange result - we already know that the whole pagination is kind of quirky. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was how it originally worked, but then someone noted it didn't "solve" their problem of merging nested arrays - only top-level arrays. I still think that's up to business logic - there's no way for us to know which level of arrays to merge - but in the discussion above I agreed to just wrap everything in an array like
jq --slurp
so that we at least return valid JSON. That's all this PR has ever been about: returning valid JSON such that you can pipe it to something that requires it - notjq
, but something like PowerShell'sConvertFrom-Json
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember that discussion as it related to the GQL requests and I agree that we cannot know that and the consumer needs to determine that themselves.
To be clear, here I'm describing the existing merging of JSON array REST responses already getting slurped by
--paginate
with no further work (as far as I can tell). Below in the screenshot you can see the difference between./bin/gh api --paginate repos/cli/go-gh/release
and./bin/gh api --paginate --slurp repos/cli/go-gh/release
. The--slurp
results in a double nesting of arrays with the top level one only having one item.It's just a bit of a weird situation that the REST pagination ends up creating a single array object that the slurp then wraps in another array. I'm not sure there is a good solution to this though, except perhaps only allowing
--slurp
on graphql paginated requests? In any case someone is liable to get confused, so maybe we just ship this and move on with our lives.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that was the compromised we discussed in our meeting with @andyfeller as well: we'd mimic
jq --slurp
exactly, which also double-nests arrays. This way, the result is consistent: it's always an array. The array items are each page of the response regardless of whether it's an array or object. No guesswork as to how best to handle the output: it's always an array of each page. If you don't need that for a REST response of arrays, don't pass--slurp
: you'll already get a merged array because of Nate's previous change based on my old PR.I'd rather have consistency than conditional output formats myself. Easier to code without having to think of whether I'm making a REST call or GraphQL call.