1

I am trying to create a JIRA ticket using curl POST method, but the variable substitution is not happening properly in TODAY_DATE variable. If i remove that it is working fine. ANy hint on how this can be solved?

Below is my code where only TODAY_DATE substition isnt working and failing with below error

#!/bin/bash
JIRA_CREDS="XXX:YYY"
JIRA_PROJECT="OPS"
JIRA_URL="http://jira/jira/rest/api/2/issue/"
JIRA_CONTENT_TYPE="Content-Type: application/json"
DATE_1=`date +"%d/%b/%y %r" | cut -d":" -f1`
DATE_2=`date +"%d/%b/%y %r" | cut -d":" -f2`
DATE_3=`date +"%d/%b/%y %r" | cut -d":" -f3 | cut -d" " -f2`
TODAY_DATE=`echo "${DATE_1}:${DATE_2} ${DATE_3}"`
 curl -D- -u "$JIRA_CREDS" -X POST --data '{
 "fields": {
 "project":
 {
 "key": "'$JIRA_PROJECT'"
 },
 "summary": "Security patch update",
 "description": "The instances need to be updated via yum.",
 "customfield_13339": { "value": "Bamboo" },
 "customfield_13338": { "value": "Approved" } ,
 "customfield_13337": [{ "value": "Team" }],
 "customfield_13331": [{ "value": "SITTR" }],
 "customfield_13334": { "value": "'$TODAY_DATE'" },
 "issuetype": {
 "name": "Tech Task"
 }
 }
 }' -H "$JIRA_CONTENT_TYPE" "$JIRA_URL"

curl: (7) Failed to connect to 0.0.0.11: Invalid argument curl: (23) Failed writing header

  • 2
    Your json object is an escaping nightmare. Try storing it in a variable using a here doc or use something like jo to create it. – jesse_b May 05 '21 at 15:36

2 Answers2

1

I'm going to ignore whatever quoting issues you may have and rewrite your script with the JSON construction tool jo.

#!/bin/sh

jira_creds='XXX:YYY' jira_project='OPS' jira_url='http://jira/jira/rest/api/2/issue/' jira_content_type='Content-Type: application/json'

now=$( date +'%d/%b/%y %l:%M %p' )

json_data=$( jo -d .
fields.project.key="$jira_project"
fields.summary='Security patch update'
fields.description='The instances need to be updated via yum.'
fields.customfield_13339.value='Bamboo'
fields.customfield_13338.value='Approved'
fields.customfield_13337="$( jo -a "$( jo value='Team' )" )"
fields.customfield_13331="$( jo -a "$( jo value='SITTR' )" )"
fields.customfield_13334.value="$now"
fields.issuetype.name='Tech Task' )

curl
-u "$jira_creds"
-X POST --data "$json_data"
-H "$jira_content_type"
"$jira_url"

The value of $json_data would be a properly formatted JSON document where all included variable data has been JSON-encoded if needed. Pretty-printed, it may look something like

{
  "fields": {
    "project": {
      "key": "OPS"
    },
    "summary": "Security patch update",
    "description": "The instances need to be updated via yum.",
    "customfield_13339": {
      "value": "Bamboo"
    },
    "customfield_13338": {
      "value": "Approved"
    },
    "customfield_13337": [
      {
        "value": "Team"
      }
    ],
    "customfield_13331": [
      {
        "value": "SITTR"
      }
    ],
    "customfield_13334": {
      "value": "05/May/21 11:32 PM"
    },
    "issuetype": {
      "name": "Tech Task"
    }
  }
}

And alternative way to create the JSON payload data is to start with a static YAML document (which is easy to write) and then insert the variable data into it with yq from https://kislyuk.github.io/yq/

json_data=$( yq \
        --arg proj "$jira_project" \
        '.fields.project.key |= $proj |
         .fields.customfield_13334.value |=
                (now|strflocaltime("%d/%b/%y %l:%M %p"))' <<'END_YAML'
fields:
  summary: Security patch update
  description: The instances need to be updated via yum.
  customfield_13339:
    value: Bamboo
  customfield_13338:
    value: Approved
  customfield_13337:
    - value: Team
  customfield_13331:
    - value: SITTR
  issuetype:
    name: Tech Task
END_YAML
)

Note that we let yq compute the timestamp here, so the shell variable $now is no longer needed in the script.

Kusalananda
  • 333,661
  • When I do that jo thing I am getting the following error "errors":{"customfield_13334":"Operation value must be a string"}} – sabarish jackson May 10 '21 at 16:43
  • @sabarishjackson What is the value of $json_data if you use echo "$json_data" before calling curl? – Kusalananda May 10 '21 at 17:34
  • This is what i get when i run with echo ${json_data} "fields":{"project":{"key":"OPS"},"summary":"Security patch update","description":"The instances need to be updated via yum.","customfield_13339":{"value":"Bamboo"},"customfield_13338":{"value":"Approved"},"customfield_13337":[{"value":"Team"}],"customfield_13331":[{"value":"SITTR"}],"customfield_13334":{"value":"10/May/21 9:52 PM"},"issuetype":{"name":"Tech Task"}}} {"errorMessages":[],"errors":{"customfield_13334":"Operation value must be a string"}} – sabarish jackson May 11 '21 at 01:54
0

In your script, a couple of variable expansions are unquoted:

 "key": "'$JIRA_PROJECT'"

and

 "customfield_13334": { "value": "'$TODAY_DATE'" },

Those two lines should be, respectively:

"key": "'"$JIRA_PROJECT"'"

and

"customfield_13334": { "value": "'"$TODAY_DATE"'" },

As an aside, you may also write TODAY_DATE="${DATE_1}:${DATE_2} ${DATE_3}", saving an unneeded command substitution/echo. Or, better, TODAY_DATE=$(date +"%d/%b/%y %I:%M %p"), removing the need for the DATE_n variables.

The expansion of $TODAY_DATE includes space characters:

$ DATE_1=`date +"%d/%b/%y %r" | cut -d":" -f1`
$ DATE_2=`date +"%d/%b/%y %r" | cut -d":" -f2`
$ DATE_3=`date +"%d/%b/%y %r" | cut -d":" -f3 | cut -d" " -f2`
$ TODAY_DATE=`echo "${DATE_1}:${DATE_2} ${DATE_3}"`
$ printf '%s\n' "$TODAY_DATE"
05/May/21 10:06 PM

Most likely this means that the argument to the --data option in your script is ending with the first word of TODAY_DATE's value. The following word (10:06 in this case) is being parsed by curl as the first non-option argument an hence taken as the URL to connect to (I am not able to reproduce the exact error you show, though).

See also: Why does my shell script choke on whitespace or other special characters?

fra-san
  • 10,205
  • 2
  • 22
  • 43
  • If i do this i am getting a below error {"customfield_13334":"Operation value must be a string"} That is actually Custom field for date column and it is being displayed in 05/May/21 10:06 PM format – sabarish jackson May 06 '21 at 02:46
  • @sabarishjackson Maybe it is meant to be "customfield_13334": "'"$TODAY_DATE"'",? Unfortunately I'm not familiar with JIRA tickets. – fra-san May 06 '21 at 06:30