Skip to content

GitHub CLI extension to merge JSON responses to return valid JSON when passing --paginate to gh api.

License

Notifications You must be signed in to change notification settings

heaths/gh-merge-json

Repository files navigation

Merge JSON from GitHub CLI

A GitHub CLI to merge JSON responses to return valid JSON when passing --paginate to gh api:

$issues = gh api 'repos/{owner}/{repo}/issues' --paginate | gh merge-json | ConvertFrom-Json

Install

Make sure you have version 2.0 or newer of the GitHub CLI installed.

gh extension install --force heaths/gh-merge-json

The --force flag I introduced in gh version 2.25 to better support automation. It will install or upgrade an extension, or do nothing if the latest extension version is already installed.

Details

The GitHub CLI can return paginated JSON responses from both REST and GraphQL APIs; however, due to bug #1268 when --paginate is passed to gh api it does not return value JSON:

{
  "data": {
    "viewer": {
      "repositories": {
        "nodes": [
          {
            "nameWithOwner": "owner/repo1"
          },
          {
            "nameWithOwner": "owner/repo2"
          }
        ],
        "pageInfo": {
          "hasNextPage": true,
          "endCursor": "Y3Vyc29yOnYyOpHOBAaq0A=="
        }
      }
    }
  }
}
{
  "data": {
    "viewer": {
      "repositories": {
        "nodes": [
          {
            "nameWithOwner": "owner/repo3"
          },
          {
            "nameWithOwner": "owner/repo4"
          }
        ],
        "pageInfo": {
          "hasNextPage": true,
          "endCursor": "Y3Vyc29yOnYyOpHOBzdvuQ=="
        }
      }
    }
  }
}

Until PR #5652 or some other solution is merged, you can use this extension without having to install additional system utilities like jq which may not be available for your platform or otherwise difficult to obtain e.g., requiring a bunch of prerequisites.

Formatting

Like several gh commands, you can pass --template (or -t) and a Go template using most functions described in gh help formatting - specifically, those defined in source. Output is always written to standard output.

Teeing

If you would like to write formatted output e.g., colorful, indented JSON, or formatted using a --template but save compressed JSON to a file for further processing by other tools, you can pass --tee and a file path.

gh api graphql --paginate -f query='
    query($endCursor: String) {
      viewer {
        repositories(first: 100, after: $endCursor) {
          nodes { nameWithOwner }
          pageInfo {
            hasNextPage
            endCursor
          }
        }
      }
    }
  ' | gh merge-json --tee repos.json --template '{{range .data.viewer.repositories.nodes}}{{.nameWithOwner}}{{"\n"}}{{end}}'

License

Licensed under the MIT license.

About

GitHub CLI extension to merge JSON responses to return valid JSON when passing --paginate to gh api.

Topics

Resources

License

Stars

Watchers

Forks

Languages