1

I have the results of a Github Commandline call with the following JSON format:

[
  {
    "name": "repository-1",
    "pullRequests": {
      "totalCount": 129
    }
  },
  {
    "name": "repository-2",
    "pullRequests": {
      "totalCount": 1143
    }
  },
  {
    "name": "repository-3",
    "pullRequests": {
      "totalCount": 78
    }
  },
  {
    "name": "repository-4",
    "pullRequests": {
      "totalCount": 0
    }
  }
]

This is the output of gh repo list ORG-REPO --json pullRequests,name

I have many many more repositories in there and my goal is to get a list of all repositories that have 0 pull requests so that I can act on those (archive).

How can I do this utilizing jq?

I've tried:

.[].pullRequests.totalCount -> This gives me the numeric value of each record
.[].pullRequests.totalCount,.[].name -> This gives me the list from above and then the list of repository names

How can I filter the list of repositories to only show the repository name where pullRequests.totalCount = 0

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
NewGuy
  • 3,273
  • 7
  • 43
  • 61

1 Answers1

3

Use select to filter by criteria, here .pullRequests.totalCount == 0, and map that onto the input array:

jq 'map(select(.pullRequests.totalCount == 0))'
[
  {
    "name": "repository-4",
    "pullRequests": {
      "totalCount": 0
    }
  }
]

Demo

If you are only interested in the names, add that to the mapping:

jq 'map(select(.pullRequests.totalCount == 0).name)'
[
  "repository-4"
]

Demo

If you want the names as a list of newline-separated raw texts rather than a JSON array of strings, iterate over the elements and use the --raw-output or -r for the output:

jq -r '.[] | select(.pullRequests.totalCount == 0).name'
repository-4

Demo

pmf
  • 24,478
  • 2
  • 22
  • 31