0

I am modifying script for bitrise and met the problem that I cannot compare string with spaces, select( .to.name == \"$to_status\" returns false, where .to.name is "ready for qa" and to_status is "ready for qa"

 TO_STATUS="Ready to QA"

 transition_id=$(curl -s \
     -H "Authorization: Basic $token" \
     "$jira_url/rest/api/2/issue/$task/transitions" | \
     jq -r ".transitions[] | select( .to.name == \"$to_status\" ) | .id")

I tried to adopt the solution from this post, but without any luck

transition_id=$(curl -s \
    -H "Authorization: Basic $token" \
    "$jira_url/rest/api/2/issue/$task/transitions" | 
    jq -r --arg to_status "$TO_STATUS" '
    .transitions[] 
    | select(.to."name"==$to_status) 
    | .id')

json

{
  "expand": "transitions",
  "transitions": [
    {
      "id": "11",
      "name": "Backlog",
      "to": {
        "name": "Backlog",
        "id": "10510"
      }
    },
    {
      "id": "51",
      "name": "Ready to QA",
      "to": {
        "name": "Ready for QA",
        "id": "10209"        
      }
    }
  ]
}
Yarh
  • 4,459
  • 5
  • 45
  • 95
  • Without seeing your actual json is hard to determine whats wrong. Can you try escape it like this : ** ".transitions[] | select( .to.name == '$to_status' ) | .id" ** . ? – Matias Barrios Nov 26 '19 at 13:31
  • Shouldn't `.to."name"==$to_status)` be without the double quotes? –  Nov 26 '19 at 13:35
  • Please include the parts of the bash script (or shell history) relevant to the `$to_status` / `$TO_STATUS` bash variable. To make your question an [MCVE](https://stackoverflow.com/help/minimal-reproducible-example), please also replace your `curl` call by an `echo`/`printf` outputing JSON data similar to what the webservice you call would answer with, simplified to the parts relevant to the question. – Aaron Nov 26 '19 at 13:48
  • 1
    @Roadowl The OP is trying to interpolate the value of a shell parameter into the `jq` filter; `$to_status` isn't a `jq` variable. – chepner Nov 26 '19 at 13:57

1 Answers1

1

Don't interpolate; pass to_status as an argument.

to_status="ready for qa"
transition_id=$(curl -s \
   -H "Authorization: Basic $token" \
   "$jira_url/rest/api/2/issue/$task/transitions" |
     jq -r --arg t "$to_status"  '.transitions[] | select( .to.name == $t ) | .id'
)
chepner
  • 497,756
  • 71
  • 530
  • 681
  • I don't understand what is the difference with this one and my second attempt, but it finally works) – Yarh Nov 26 '19 at 14:14
  • 1
    I think your issue was the quotes around `name` (`.to.name` vs `.to."name"`). – chepner Nov 26 '19 at 14:17