0

I have written an API method to create a Bitbucket webhook:

def call (){

    script {

        CRUMB = "https://api.bitbucket.org/2.0/repositories/$NAMESPACE/$BB_REPO/hooks"
        DATA = "'{\n    \"description\": \"Webhook Jenkins\",\n    \"url\": \"$URL\",\n    \"active\": true,\n    \"verify\": false,\n    \"events\": [\n        \"$TRIGGER_EVENTS\"\n    ]\n}'"

        CONTENT = "\"Content-Type: application/json\""

        withCredentials([usernamePassword(credentialsId: 'bb_management', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
            final String response = sh(script: "curl --request POST -v -u $USERNAME:$PASSWORD -H $CONTENT -d $DATA $CRUMB ")
        } 
    }       
 }

However, the URL variable:

\"url\": \"$URL\",\n

Is not working, and I've received this error when a Jenkins job was triggered.

{"type": "error", "error": {"fields": {"url": ["The specified URL is invalid."]}, "message": "Bad request"}}

Could it be a syntax error, how would I resolve this error please?

  • Probably same problem as this guy ... [groovy is interpolating the `$` before the shell](https://stackoverflow.com/a/68950050/598141). Try escaping the `$`. – Ian W Aug 28 '21 at 04:50
  • I tried escaping, it removed the message of the URL being invalid and instead gave me the Bad Request error message. – Thomas Demehin Sep 02 '21 at 08:53

0 Answers0