I'm looking for advice on how to approach a certain scripting problem using GitHub CLI. Let's say I used the gh search prs "SPARK-" --match body
command to get a list of all prs with the body containing the SPARK- phrase. Now, I want to loop through that list of prs, and output the url of that pr along with it's SPARK number (somewhere in the body of the pr, there is text saying "SPARK-" followed by a unique 5 digit number). Any ideas on how to approach this? I'm having difficulty figuring how I might be able to construct this loop using my gh cli output (or if it's even possible?), let's say in some bash script, and figuring out how to specifically isolate the SPARK number.
CodePudding user response:
You could do everything in one gh
invocation:
gh search prs "SPARK-" --match body --json url,body --jq '
"SPARK-([[:digit:]] )" as $re
| map(select(.body | test($re)))
| .[].body |= match($re).captures[0].string
| map([.url, .body])[]
| @csv'
The --json
parameter selects URL and the description, and --jq
massages the JSON into a comma-separated list (see jq playground):
"https://github.com/just4jc/CSE6242-Data-and-Visual-Analytics/pull/32","1298180"
"https://github.com/h950059h/kafka-examples/pull/33","1298180"
"https://github.com/terrorizer1980/zeppelin/pull/75","2420837"
"https://github.com/muyenzo/reference-architectures/pull/10","574943"
"https://github.com/microsoft/az-python/pull/461","35714"
"https://github.com/radanalyticsio/streaming-amqp/pull/38","1298181"
"https://github.com/orysmudi/tensorflow/pull/78","573164"
"https://github.com/apache/iceberg/pull/4479","30669"
"https://github.com/alonsoir/strata-2016/pull/17","1298180"
The jq command first defines a regular expression SPARK-([[:digit:]] )
, then uses it to drop all PRs where SPARK
is found, but not followed by digits; then it replaces the body with the capture group of the first match of the regex, returning just the number.
Finally, it rearranges into arrays and converts to CSV.