jenkinsci / google-chat-notification-plugin

Google Chat Notification Jenkins Plugin to send build status
https://plugins.jenkins.io/google-chat-notification/
MIT License
39 stars 20 forks source link

fails on simple messages which contain double quotes #45

Closed gpflaum closed 1 year ago

gpflaum commented 1 year ago

Jenkins and plugins versions report

Environment ```text Paste the output here ```

What Operating System are you using (both controller, and any agents involved in the problem)?

Linux

Reproduction steps

Run a Jenkins build with this pipeline script:

node {
    String googleWebhook = 'https://chat.googleapis.com/v1/spaces/your-url-here'
    String message = 'The plugin does not handle double quotes: " '
    googlechatnotification message: message, url: googleWebhook;
}

Expected Results

Should send a chat message.

Actual Results

The Jenkins build log shows that the string sent to the API is improperly quoted, and the Google API returns "Invalid JSON payload"

[Pipeline] googlechatnotification
Send Google Chat Notification condition is : true
Final formatted text: { "text": "The plugin does not handle double quotes: " "}
Google Chat post may have failed. Response: {
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Expected , or } after key:value pair.\nle double quotes: \" \"}\n                    ^",
    "status": "INVALID_ARGUMENT"
  }
}
 , Response Code: 400

Anything else?

The plugin's escapeSpecialCharacter function escapes some characters, but not double quotes. (It escapes single quotes, but probably doesn't need to. JSON strings are always double quoted.) Better would be to use a library function like groovy.json.JsonBuilder's toString(), replacing this code in send()

            json = "{ \"text\": \"" + responseMessageUtil.createTextMessage() + "\"}";

with something like this:

    def simpleMessage = { text: responseMessageUtil.createTextMessage() }
    String json = groovy.json.JsonBuilder(simpleMessage).toString()

and removing use of replaceJenkinsKeywords in createTextMessage().

rubal033 commented 1 year ago

On build failure condition, it is adding extra double quotes, working fine for success.

Ryudo302 commented 1 year ago

I was unable to reproduce it with the current code (v.1.7, yet to be released). May it has been fixed by another issue.