Skip to content

Allow for standardized output format #385

Description

@dfockler

Describe the feature or problem you’d like to solve

I think having a way to output the results of the CLI comands to JSON or some other standardized output format would be a nice way to let users extend functionality without having to change the CLI code very much.

Proposed solution

Something like

$ gh pr status 123 --output=json
{"id": 123, "title": "Some cool PR", "branch": "my-cool-branch", "description": "some .md description", "checks_passing": true, "requires_review": false}

Users could then easily parse this and perform other actions using the data.

Additional context

My initial thought was using this to be able to automatically open the issue I'm working on in JIRA by extracting the JIRA tag number from our titles, but it could also be useful for other workflows.

Activity

  1. lucasyvas commented on Feb 13, 2020

    @lucasyvas

    I haven't used it yet, but the provided example output would steer me away from ever using this. If I can't parse the output with standard tools, I can't easily leverage it in my own scripts, and therefore its utility is minimal.

    In all honesty I'd take a column based approach that I can awk easily in all cases - any other standardized output as proposed is also welcome.

  2. broady commented on Feb 13, 2020

    @broady

    @lucasyvas I don't know about your definition of "standard", but jq is pretty great.

  3. lucasyvas commented on Feb 13, 2020

    @lucasyvas

    @lucasyvas I don't know about your definition of "standard", but jq is pretty great.

    I agree it is standard - and I love jq! I was merely saying I would even be ok with something merely "familiar" that could be piped and processed easily with other tools like awk. JSON would be a totally fine option and I love command line tools that offer it.

  4. broady commented on Feb 13, 2020

    @broady

    @lucasyvas oh, right, you were talking about the tool's current output, not the proposed JSON output. Yeah, agreed, everything seems very geared toward an interactive workflow.

  5. mislav commented on Feb 19, 2020

    @mislav
    Contributor

    @lucasyvas I hear you. We do intend to have more scripting interfaces— for example, even though the default output of gh issue/pr list has color, table-based layout, and truncation adjusting to the size of the viewport, when you pipe that command to a file or a script, we switch to a machine-parseable mode: no color, tab delimiters, and no truncation.

    Right now we didn't know how to best achieve this with the output of issue/pr status as it has sections, but as we understand more about what kind of information would people want to extract in their scripts, we will offer some more easily parseable interfaces. In my opinion, that's almost the whole point of CLI tools.

  6. hkrutzer commented on Feb 23, 2020

    @hkrutzer

    I came looking for this issue because I would like to import Github issues into TaskLite. It can import JSON or I can just pipe stuff into it. When piping there is the Issues for $org/$repo first which makes fzf glitch a little. Perhaps this part should be left out when piping.

    Exporting JSON would be ideal in this case because then TaskLite can import more information as metadata, and I can more easily turn issue labels into Tasklite tags.

  7. lucasyvas commented on Feb 23, 2020

    @lucasyvas

    @lucasyvas I hear you. We do intend to have more scripting interfaces— for example, even though the default output of gh issue/pr list has color, table-based layout, and truncation adjusting to the size of the viewport, when you pipe that command to a file or a script, we switch to a machine-parseable mode: no color, tab delimiters, and no truncation.

    Right now we didn't know how to best achieve this with the output of issue/pr status as it has sections, but as we understand more about what kind of information would people want to extract in their scripts, we will offer some more easily parseable interfaces. In my opinion, that's almost the whole point of CLI tools.

    It's completely fair - you can't do everything at once. Just hoping to call it out for some others I've seen mention it as well - having some more raw output options could make the utility extremely attractive beyond the core hard work that has already been put in.

  8. mislav commented on Feb 24, 2020

    @mislav
    Contributor

    When piping there is the Issues for $org/$repo first which makes fzf glitch a little. Perhaps this part should be left out when piping.

    @hkrutzer Not a bad idea. For now, you can manually silence stderr (2>/dev/null) when piping to work around this.

  9. added
    coreThis issue is not accepting PRs from outside contributors
    on Oct 7, 2020
  10. ddonahoevibrenthealth commented on Nov 9, 2020

    @ddonahoevibrenthealth

    Has there been any update on this? It would be extremely helpful since it would allow for developers to create native wrappers around it for language specific implementations.

  11. mislav commented on Dec 1, 2020

    @mislav
    Contributor

    @ddonahoevibrenthealth We will start work on this from next week. 👍

  12. vatosarmat commented on Jan 14, 2021

    @vatosarmat

    Output seems to be awk-friendly with "\t" as field separator

    function gh__issues {
      fzfSelected=$(gh issue list -R "$1" | fzf )
      fzfResult="$?"
    
      if [ "$fzfResult" = "0" -a -n "$fzfSelected" ]; then
        gh issue view -R "$1" "$(echo "$fzfSelected" | awk -F "\t" '{print $1}')"
      fi
    }
  13. schmurfy commented on Jan 27, 2021

    @schmurfy

    no matter how the json is formatted it would be nice to have to be able to use it in scripts and CI scripts, this tool is really great.
    I thought automation was a primary goal of this tool but it looks like I was wrong :/

  14. joe-sharp commented on Jan 27, 2021

    @joe-sharp

    I thought automation was a primary goal of this tool but it looks like I was wrong :/

    I won't speak for the devs on what the primary goal of this tool is but @mislav said they would be starting on this feature request this week. Were you possibly thinking they closed this issue due to the references above your comment that were closed as duplicates of this issue?

  15. schmurfy commented on Jan 27, 2021

    @schmurfy

    No, I know this issue was not closed but I just don't understand why json output was not a feature from the start (and it would have been probably easier to do that way than to do another pass on every command).
    I saw the comment saying work on this would be started this week but that was on December 1st, a month ago, I am sure this will eventually happen but I am just really disappointed.

  16. vatosarmat commented on Jan 28, 2021

    @vatosarmat

    @schmurfy you can already get whatever you want with gh api and format that however you like with jq

  17. schmurfy commented on Feb 4, 2021

    @schmurfy

    @vatosarmat thanks for the tip, I will use that.

  18. mislav commented on Feb 24, 2021

    @mislav
    Contributor

    @schmurfy @joe-sharp The next release will be geared mostly towards more features for scripting. Stay tuned!

    but I just don't understand why json output was not a feature from the start

    Anything that we publish as output from any gh command automatically becomes the API of GitHub CLI. The bigger any API is, the harder it is to maintain. We couldn't just publish JSON output from every command because, that early on, we couldn't also guarantee that we wouldn't have to change the structure of that raw data.

    Right now, the main API to GitHub is still the GitHub API, and you have relatively easy access to it using the gh api command. But, we will make accessing all kinds of raw fields easier in the next release. 👍

  19. devlead commented on Mar 22, 2021

    @devlead

    What would be really nice for automation would be if at least all list commands like the Azure azcli had an output option
    A really Powerful feature of az is that you can everywhere get machine consumable output
    --output -o : Output format. Allowed values: json, jsonc, none, table, tsv, yaml, yamlc

    and you can even query - filter and adapt the output
    --query : JMESPath query string. See http://jmespath.org/ for more information and examples.

    this is super handy, especially in DevOps scenarios.

  20. cardonator commented on Apr 1, 2021

    @cardonator

    I was looking for an issue matching what behavior I was interested in but this is the closest I saw. In addition to standard output formats, how about bringing back the behavior of hub where you could specify an output format? I used to use hub for generating audit reports from various repos but this project makes pulling the audit data much easier. It would be nice if there was also an easy way to manually format the results in a way that it could be easily ingested into a log aggregator.

  21. mislav commented on Apr 1, 2021

    @mislav
    Contributor

    @cardonator hub's --format string was modeled after git log format syntax, but the syntax was hard to read and to author. GitHub CLI is not modeled after git at all, so we can invent our own approach. We're coming up with something better 👌

  22. cardonator commented on Apr 1, 2021

    @cardonator

    @mislav awesome! Thanks!

  23. mislav commented on Jul 27, 2021

    @mislav
    Contributor

    Closing since the --json flag is now available on most commands that list or view data. 🎉

    The few commands that still miss the --json flag will be added time-permitting. (But we accept contributions!)

    Thank you all for the patience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreThis issue is not accepting PRs from outside contributorsenhancementa request to improve CLI

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions