Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  {
    "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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading