Add multiple Watchers to Jira issue via Rest API v3

So running into a strange issue while trying to add multiple watchers to a jira issue, always seems to remove the previous watcher and I’m not able to add more then 1


WATCHERS=('asdasd-asdasdasd-asdasdasd-asdadasdasd', 'qweqweqwe-qweqweqwe-qweqweqwe-qweqweqwe', 'zxczxcxc-zxczxcxc-zxczxcx-zxczxczxcz')

## attempt to add multiple watchers in once call, see examples below
ADD_MULTIPLE_WATCHERS=$( jq -c -n '$ARGS.positional' --arg ${WATCHERS[@]} )

POST_MULTIPLE_WATCHER=`curl --request POST \
    --url $JIRA_URL \
    --user $JIRA_USER_EMAIL:$JIRA_USER_TOKEN \
    --header 'Accept: application/json' \
    --header 'Content-Type: application/json' \
    --data "$ADD_MULTIPLE_WATCHERS"`

## no errors but no watchers added
echo "POST_MULTIPLE_WATCHER: $POST_MULTIPLE_WATCHER"


## attempt to add one watcher at a time in a loop
for watcher in "${WATCHERS[@]}"
    do
       echo "adding watcher: $watcher"

       ADD_SINGLE_WATCHER=$( jq -n \
            --arg wa $watcher \
            '$wa'
        )

        echo "ADD_SINGLE_WATCHER: $ADD_SINGLE_WATCHER"

        ## create change request
        POST_ADD_SINGLE_WATCHER=`curl --request POST \
            --url $JIRA_URL \
            --user $JIRA_USER_EMAIL:$JIRA_USER_TOKEN \
            --header 'Accept: application/json' \
            --header 'Content-Type: application/json' \
            --data "$ADD_SINGLE_WATCHER"`

        ## does add the watcher, but always removes the previous watcher, so can not add more then 1 watcher
        echo "POST_ADD_SINGLE_WATCHER: $POST_ADD_SINGLE_WATCHER"
done

example jq command you can test the output

jq -n '$ARGS.positional' --args 'asdasd-asdasdasd-asdasdasd-asdadasdasd', 'qweqweqwe-qweqweqwe-qweqweqwe-qweqweqwe', 'zxczxcxc-zxczxcxc-zxczxcx-zxczxczxcz'

output

[
  "asdasd-asdasdasd-asdasdasd-asdadasdasd,",
  "qweqweqwe-qweqweqwe-qweqweqwe-qweqweqwe,",
  "zxczxcxc-zxczxcxc-zxczxcx-zxczxczxcz"
]

it was my own fault, in Bash when you declare an array, there are no comma’s between entries

WATCHERS=('asdasd-asdasdasd-asdasdasd-asdadasdasd' 'qweqweqwe-qweqweqwe-qweqweqwe-qweqweqwe' 'zxczxcxc-zxczxcxc-zxczxcx-zxczxczxcz')

this solved my issue and now I’m able to add multiple watchers

1 Like