Skip to content

Merge JSON responses from gh api - #5652

Closed
heaths wants to merge 1 commit into
cli:trunkfrom
heaths:issue1268
Closed

heaths wants to merge 1 commit into
cli:trunkfrom
heaths:issue1268

Conversation

@heaths

@heaths heaths commented May 16, 2022

Copy link
Copy Markdown
Contributor

Fixes #1268

@heaths

heaths commented May 16, 2022

Copy link
Copy Markdown
Contributor Author

A WIP that still needs some refactoring to avoid duplicate code. Also depends on darccio/mergo#210 getting merged or, if we want to stick with that library, vendoring or forking it. This was more a proof of concept but was hoping for some early feedback on the concept. With the mergo change this would handle both top-level JSON arrays or objects, so both REST and GraphQL responses.

@heaths

heaths commented May 17, 2022 •

Copy link
Copy Markdown
Contributor Author

@mislav I need to add some specific tests, but wanted to see - if you have time to review - if you feel this is on the right track. I maintained writing responses as they come without pagination so that the user can see something happening. I do ponder adding a progress indicator if paginating, though, because it's pretty long. The main concern with that is that, even writing to stderr, it's likely callers have redirected at least stdout if not stderr using . Writing progress could, therefore, be a breaking change.

EDIT: Actually, considering we'd only show progress if stderr wasn't redirected, this would probably be fine. I'll make the change, but certainly can pull it if there's concern.

@heaths
heaths force-pushed the issue1268 branch 2 times, most recently from e042de9 to a7ca779 Compare May 17, 2022 07:29
@heaths
heaths marked this pull request as ready for review May 17, 2022 12:45
@heaths
heaths requested a review from a team as a code owner May 17, 2022 12:45
@heaths
heaths requested review from mislav and removed request for a team May 17, 2022 12:45
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label May 17, 2022
@heaths

heaths commented May 19, 2022

Copy link
Copy Markdown
Contributor Author

@mislav how much do we care about keeping errors printed in the same order. This is what we do currently:

{
  "message":"Not Found",
  "documentation_url":"https://docs.github.com/rest"
}
gh: Not Found (HTTP 404)
exit status 1

I'm trying to better refactor this so the code is cleaner and easier to maintain (basically, fewer branches in the code and having to pass fewer parameters) but trying to keep this error format is making that more difficult unless I just inline all the code into the for loop.

But, if the error format could be the following, there's a lot cleaner refactorings possible:

gh: Not Found (HTTP 404)
{
  "message":"Not Found",
  "documentation_url":"https://docs.github.com/rest"
}
exit status 1

Frankly, I like this format better anyway. That an error occurred is shown immediately, followed by more details from the server.

Comment thread go.mod Outdated
@heaths

heaths commented May 24, 2022

Copy link
Copy Markdown
Contributor Author

@mislav I've been using this in a few instances and not exhibited any problems. What do you think?

@heaths

heaths commented May 25, 2022

Copy link
Copy Markdown
Contributor Author

darccio/mergo#210 was merged so we can actually pick up v0.3.13 and not need a redirect.

@heaths

heaths commented Jun 1, 2022

Copy link
Copy Markdown
Contributor Author

@mislav @samcoe what do you think of this? Makes the output of paginated responses much easier to use.

@samcoe samcoe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@heaths Thanks for your patients on this review. Overall, I like this functionality and it feels straight forward with the mergo package. Just left a couple comments for your consideration.

Comment thread pkg/cmd/api/api.go Outdated
Comment thread pkg/cmd/api/api.go Outdated
@heaths

heaths commented Jun 18, 2022

Copy link
Copy Markdown
Contributor Author

@mislav @samcoe this is ready to review. Even before the refactoring, the response body would be read completely into a buffer anyway - and in some cases, multiple times - so I ended up allocating a single bytes.Reader that implements io.ReadSeeker so I could seek to the beginning as needed without additional allocations, which was possible before.

Comment thread pkg/cmd/api/api.go Outdated

@samcoe samcoe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@heaths This looks good to me. Thanks for the thorough work here.

@eddiebergman

Copy link
Copy Markdown

Hi, thanks for the work! If the pr is approved, can we get it merged to fix some downstream issues at pwntester/octo.nvim#302 :)

@heaths

heaths commented Aug 4, 2022

Copy link
Copy Markdown
Contributor Author

@samcoe @mislav is this good to merge for next release? It would sure be helpful.

@vilmibm

vilmibm commented Aug 22, 2022

Copy link
Copy Markdown
Contributor

We're still interested in this but wanted consensus; waiting on @mislav 's review

@heaths

heaths commented Aug 23, 2022

Copy link
Copy Markdown
Contributor Author

I merged upstream/trunk, or do you need a rebase on trunk? There was a lot of churn since this was first reviewed in pkg/cmd/api/api.go, or perhaps I could just squash all my commits if the history isn't important - would be easier to effectively merge trunk.

@heaths

heaths commented Sep 6, 2022

Copy link
Copy Markdown
Contributor Author

/ping @mislav

@mislav mislav removed their assignment Jun 9, 2023
@andyfeller

andyfeller commented Nov 30, 2023 •

Copy link
Copy Markdown
Contributor

@heaths: what makes the most sense regarding this PR? Are you okay with closing it until such a time that either you or the CLI team can prioritize the work?

Given the age and triage efforts to ensure PRs don't grow stale, I appreciate you understanding the desire to ensure the CLI team is staying engaged while having a up to date view of work in flight.

If you want my $0.02, I think closing this and creating a new PR when that work can be prioritized while linking it back here for posterity makes the most sense. 🤷

By the way, nice to meet you and thank you for being a consistent CLI friend and contributor! 👋 ❤️ 🙇

@heaths

heaths commented Nov 30, 2023 •

Copy link
Copy Markdown
Contributor Author

What work do you need to prioritize? The PR has been done for ages. Seems the outstanding concern was using a third party module, which is sort of absurd given how many are already in use. This module has decent usage and it's straight forward. It could even be reimplimented, but that's not very idiomatic.

There's no rush of breaking changes here. The output is invalid JSON that would be valid. Any work around like slurping wouldn't break because there'd be nothing to slurp. I see no downside.

I could rebase this and make it conditional for GraphQL.

@andyfeller

Copy link
Copy Markdown
Contributor

Thanks for the follow up, @heaths 🙇

It seems this PR needs prioritization from both sides:

  1. core maintainers end to discuss these changes and how to move forward following Mislav's departure
  2. yours on addressing the conflicting files

There is a lot of history and mentions of the related issue in other places. I admit there is a lot to catch up on here.

@heaths

heaths commented Dec 4, 2023 •

Copy link
Copy Markdown
Contributor Author

Would a quick sync over Teams be acceptable (we're on the same network)? Most of the history is just me keeping this up to date (which gets harder with the PR age, hence trying to get buy-in from Mislav) and not getting it reviewed with requested changes for many months, but we could maybe go through this PR and recap quickly.

The quick gist is that Mislav wasn't keen on the mergo dependency I took after some investigation. I think concern about this being a breaking change - given it was already broken - was resolved, but there was still some concern about a behavioral change when logging headers/logs via --verbose since it'd only happen with the first or last (I forget which we settled on off hand) now. Personally, I think that likelihood is low. Given this doesn't emit valid JSON and everyone had to do work arounds (I even wrote an extension to make it easier on non-*nix) I'd be surprised if any were doing verbose logging in automation. Seems all that would likely have been quelled or, at the very least, difficult to associate with any particular request unless people used something like tee. I feel having this emit valid JSON so it's useable "out of the box" would be worth the potential for unlikely but possible breaking behavior changes with corner case behaviors (verbose logging).

EDIT: I sent you a message over Teams in case you wanted to chat briefly tomorrow morning.

@andyfeller

Copy link
Copy Markdown
Contributor

@heaths : sorry for being slow to respond, I'm going to raise this up with @samcoe and @williammartin, seeing if we can make time next week to get on the same page and get this settled 👍

@andyfeller andyfeller added the discuss Feature changes that require discussion primarily among the GitHub CLI team label Dec 8, 2023
@andyfeller

Copy link
Copy Markdown
Contributor

@heaths : Happy new year! Just making sure to deliver on pre-holidays conversation about keeping an eye on this PR so we can prioritize it and get it shipped. No pressure or anything, just wanting to deliver on the commitment on our end 🫂

@heaths

heaths commented Jan 16, 2024

Copy link
Copy Markdown
Contributor Author

@andyfeller @samcoe I had a thought. Building on Nate's writer that merges top-level JSON arrays and a discussion we had about something similar for GraphQL, could I create a REST and GraphQL response merger (separate implementations) in github.com/cli/go-gh? Could be generally useful. To maintain streaming for REST responses, I'd more or less keep

type paginatedArrayReader struct {
. A GraphQL would cache the map until the last page is merged and then flush it to whatever writer was passed in.

I'd develop these in conjunction to test them out - make sure the design is good - but github.com/cli/go-gh would need to get merged first, of course. Thoughts?

@samcoe

samcoe commented Jan 16, 2024

Copy link
Copy Markdown
Contributor

@heaths That seems like a good idea to me, I don't know if we will get around to fully automatic pagination but having a helper(s) to merge responses is definitely a step forward for cli/go-gh#23.

heaths added a commit to heaths/go-gh that referenced this pull request Jan 25, 2024
heaths added a commit to heaths/cli that referenced this pull request Jan 25, 2024
Partly resolves cli#1268 and replaces cli#5652. Requires cli/go-gh#148 to be merged and optionally released.
@heaths heaths mentioned this pull request Jan 25, 2024
4 tasks done
@heaths

heaths commented Jan 25, 2024

Copy link
Copy Markdown
Contributor Author

Closing in lieu of #8620. @samcoe @andyfeller that and cli/go-gh#148 are ready for review.

@heaths heaths closed this Jan 25, 2024
heaths added a commit to heaths/cli that referenced this pull request Jan 31, 2024
Partly resolves cli#1268 and replaces cli#5652. Requires cli/go-gh#148 to be merged and optionally released.
heaths added a commit to heaths/cli that referenced this pull request Feb 2, 2024
Partly resolves cli#1268 and replaces cli#5652. Requires cli/go-gh#148 to be merged and optionally released.
heaths added a commit to heaths/cli that referenced this pull request Feb 15, 2024
Partly resolves cli#1268 and replaces cli#5652. Requires cli/go-gh#148 to be merged and optionally released.
heaths added a commit to heaths/cli that referenced this pull request Mar 12, 2024
Partly resolves cli#1268 and replaces cli#5652. Requires cli/go-gh#148 to be merged and optionally released.
heaths added a commit to heaths/cli that referenced this pull request Apr 4, 2024
Partly resolves cli#1268 and replaces cli#5652. Requires cli/go-gh#148 to be merged and optionally released.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

discuss Feature changes that require discussion primarily among the GitHub CLI team external pull request originating outside of the CLI core team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Output a single JSON document from api --paginate

8 participants