jsonjqgithub-apigithub-cli

Use jq to get list of files that need editing from GitHub API for each repo returned


I am trying to query the GitHub API using gh and use the returned JSON to give me a list of repos that have matches for my search, and against each repo, I want a list of the filenames that need editing.

I can get a list of the repos back from the API like so:

gh api --method=GET "search/code?q=some-specific-string" | jq -r '[.items[] | { ( .repository.full_name ) :  .path}]'

This gives me results like:

[
  {
    "org-name/helm-charts": [
      "README.md"
    ]
  },
  {
    "org-name/repo-name": [
      "file-name-1.py"
    ]
  },
  {
    "org-name/repo-name": [
      "file-name-2.py"
    ]
  }
]

I am trying to find the right way to take the results and merge them so that there are no duplicate repo names, and their lists are concatenated. So I get a result like:

[
  {
    "org-name/helm-charts": [
      "README.md"
    ]
  },
  {
    "org-name/repo-name": [
      "file-name-1.py",
      "file-name-2.py"
    ]
  }
]

How would I do that with jq? I am trying to search for the solution but the way to word what I am trying to do eludes me


Solution

  • You can even collect all into one object using reduce instead of a mapping:

    gh … | jq 'reduce .items[] as $i ({}; .[$i.repository.full_name] += [$i.path])'
    
    {
      "org-name/helm-charts": [
        "README.md"
      ],
      "org-name/repo-name": [
        "file-name-1.py",
        "file-name-2.py"
      ]
    }
    

    If you want to have an array of separate, single-field objects, append to_entries | map([.] | from_entries) to sort them out.