drone-plugins / drone-webhook

Drone plugin for triggering webhook notifications
http://plugins.drone.io/drone-plugins/drone-webhook
Apache License 2.0
50 stars 27 forks source link

build.message will break JSON format #34

Open Vergil0327 opened 5 years ago

Vergil0327 commented 5 years ago

This is my webhook part of .drone.yml

template: | { "event": "{{ build.event }}", "owner": "{{ repo.owner }}", "repo": "{{ repo.name }}", "message": "{{ build.message }}", }

When I push commit and my commit message is "test" (git commit -m "test"), webhook will parse result as below

Output result from drone.io:

HEADERS: map[Content-Type:[application/json]] REQUEST BODY: { "event": "push", "owner": "owner", "repo": "test", "message": "test <--- new line will break JSON format and ERROR occured " }

How can I fix this ?

tboerger commented 5 years ago

How should a new line break the JSON format? It's still properly quoted and valid JSON.

kotlarz commented 5 years ago

I am also having this issue, no JSON parser (that are compliant with the JSON spec) will accept this object. For example: https://docs.python.org/3.7/library/json.html

JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 4627) and by ECMA-404, is a lightweight data interchange format inspired by JavaScript object literal syntax (although it is not a strict subset of JavaScript [1] ).

Per RFC7159, Section 7. Strings:

All Unicode characters may be placed within the quotation marks, except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).

U+000A, LINE FEED (LF), and U+000D, CARRIAGE RETURN (CR), need therefore to be escaped as per the RFC7159 spec. To add: any of the above-mentioned characters need to be escaped, so the build.message should preferably run through a JSON parser.

lpreterite commented 4 years ago

The RenderTrim method of drone-template-lib is processed, but there is no space before the newline character at the end of the message, resulting in invalid processing.

My message example:

更新个信息看看先\n

RenderTrim

func RenderTrim(template string, playload interface{}) (string, error) {
    out, err := Render(template, playload)
    return strings.Trim(out, " \n"), err
}
tboerger commented 4 years ago

That's not the issue. You are getting the implementation of trim wrong, you can see it at https://golang.org/pkg/strings/#Trim.

tboerger commented 4 years ago

Just a guess, but maybe you can use to Json from http://masterminds.github.io/sprig/defaults . html to properly format the message in json.

probwebdev commented 3 years ago

Seems that trim is a way to got - {{trim build.message}}.

ducdung8491 commented 2 years ago

Trigger from git source add \n affter commit message

You can try "message": "${DRONE_COMMIT_MESSAGE//"/}"