0
I am creating a GitHub Action workflow which will call a GitHub CLI API request using GraphQL. This gh api graphql
response is --paginate
and returns JSON Lines (ndjson).
I created the GraphQL and jq queries, and I am close to the desired output; however, my jq query needs to be modified and I can’t figure out what to change.
First, here is the desired output format I want to achieve. Notice the single object that holds all the key-value lineage information.
[
{
KEY: VALUE,
KEY: VALUE,
...
}
]
And here is the actual format of the output that I am getting. Notice that every single key-value information is wrapped within its own object.
[
{
KEY: VALUE,
},
{
KEY: VALUE,
},
...
]
I need to --slurp/-s
my jq query due to the paginated results.
I want to only include milestones which:
- have 100% progress
- don’t include the word "withdrawn" within the title
- have issues associated with them.
Also, if the milestone title contains either
or ,
, then I need to split the title. Each split will be its own key with identical values.
Here is my jq query that needs to be modified:
.[] | .data.repository as {
nameWithOwner: $name,
milestones: {
nodes: $milestones
}
}
| [
foreach $milestones[] as $milestone (
null; $milestone ;
$milestone
| select($milestone.progressPercentage == 100)
| select($milestone.title | contains("withdrawn") | not)
| select($milestone.issues.nodes[])
|
{
(($milestone.title | gsub(", "; " ") | split(" "))[]) :
[
foreach $milestone.issues.nodes[] as $issue (
{}; . + { $issue };
$issue as $issue | $issue
| (reduce $issue.labels.nodes[] as $item ([]; . + [$item.name])) as $labels
|
{
repo: $name,
issue: $issue.number,
milestone: $milestone.number,
labels: $labels
}
)
]
}
)
]
| .
Playgrounds:
-
Go to JQ Play. Make sure to check
Slurp
- For
Filter
, use jq query filter from above. - For
JSON
, use either:-
the GraphQL JSON response from the GitHub GraphQL Explorer Playground below
-
this JSON snippet which has 2 milestones but will output 3 key-value pairs (keys:
C.1
,EXAMPLE_SPLIT
, andB.1.429
):{ "data": { "repository": { "nameWithOwner": "cov-lineages/pango-designation", "milestones": { "pageInfo": { "hasNextPage": true, "endCursor": "Y3Vyc29yOnYyOpHOAGviZA==" }, "nodes": [ { "number": 1, "title": "C.1, EXAMPLE_SPLIT", "progressPercentage": 100, "issues": { "nodes": [ { "number": 2, "labels": { "nodes": [ { "name": "proposed" }, { "name": "designated" } ] } } ] } }, { "number": 2, "title": "B.1.429", "progressPercentage": 100, "issues": { "nodes": [ { "number": 3, "labels": { "nodes": [ { "name": "proposed" }, { "name": "designated" } ] } } ] } } ] } } } }
-
- For
-
To retrieve the first 100 milestones JSON Lines data, go to GitHub GraphQL API Explorer. Query:
query($owner: String!, $repository: String!, $endCursor: String) { repository(owner: $owner, name: $repository) { nameWithOwner milestones(first:100, after: $endCursor) { pageInfo { hasNextPage endCursor } nodes { number title progressPercentage issues(first: 5) { nodes { number, labels(first: 5) { nodes { name } } } } } } } }
Variables:
{ "owner": "cov-lineages", "repository": "pango-designation" }