Using bash variables in Github API v4 queries / transferring query results

Hi Team,

I asked a question about how we can include bash variables in our queries and/or link two queries together.


I basically want to transfer the number of a PR from now query to the second which uses that to query attributes from that PR.

Is there a way I can combine the two queries to one or transfer the variable in a script?
Like I described the queries are separated in JSON files and are used in a curl call to the API.

Below the original issue:

Github seems to allow variables in its API v4. This is what I wanted to use in bash to transfer a number between two queries:

  • The first query gets the number of the last closed PR.
{
  "query": "query getLatestPulLRequests {
  search(query: "repo:<owner>/<repo> type:pr is:open sort:updated-desc", type: ISSUE, first: 10) {
    nodes {
      ... on PullRequest {
        number
        id
        title
        mergedAt
        updatedAt
      }
    }
  }
  }"
}
curl -X POST -H "Content-Type: application/json" -H "Authorization: bearer $GITHUB_TOKEN" -d @./scripts/queries/gitqueryClosedPRsByDate.json https://github.ibm.com/api/graphql | jq .data.search.nodes[0].number > latestPRnumber.json

This works beautifully and gives me the right number back. I can also just parse it into a variable (which I tried).

  • The second query is supposed to call the PR with the latestPRnumber and queries the title, body and all comments from it.
{
  "query": "query getPrTitleBodyComments{
  repository(name: "<repo>", owner: "<owner>") {
    id
    pullRequest(number: 1) {
      number
      title
      body
      comments(last: 10) {
        totalCount
        nodes {
          body
        }
      }
    }
  }
  }"
}

The second query with the number static works fine as well.

!!!
But instead of the manual number pullRequest(number: 1) in query 2, I want to use the latestPRnumber. Does anyone have an idea, how I can do that in bash?

  • I tried adding "variables": {"PRnumber": "$PRnumber"}in the JSON but since its not a script it obviously doesn’t work. Can I include the variable in the curl somehow?

  • I tried

curl -X POST -H "Content-Type: application/json" -H "Authorization: bearer $GITHUB_TOKEN" -d @./scripts/queries/gitqueryClosedPRsByDate.json -d 'PRnumber=$PRnumber'  https://github.ibm.com/api/graphql | jq .

(passing it as more data) but the ERROR is always the same:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   409  100   115  100   294    219    560 --:--:-- --:--:-- --:--:--   779
{
  "message": "Problems parsing JSON",
  "documentation_url": "https://developer.github.com/enterprise/2.21/v4"
}

resolved in

You’re passing the request to curl as a file, so a simple solution would be to create that file from a template. For example, if I replace the number you want to be dynamic with @NUM@:

{
  "query": "query getPrTitleBodyComments{
  repository(name: "<repo>", owner: "<owner>") {
    id
    pullRequest(number: @NUM@) {
      number
      title
      body
      comments(last: 10) {
        totalCount
        nodes {
          body
        }
      }
    }
  }
  }"
}

If you save that as example.json.in and have the number you want in the environment variable NUMBER, you can do this:

sed -e s/@NUM@/${NUMBER}/ example.json.in >example.json

And then send the request using the generated example.json.

1 Like