drone-webhook icon indicating copy to clipboard operation
drone-webhook copied to clipboard

build.message will break JSON format

Open Vergil0327 opened this issue 7 years ago • 7 comments

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 ?

Vergil0327 avatar Aug 30 '18 03:08 Vergil0327

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

tboerger avatar Aug 30 '18 07:08 tboerger

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.

kotlarz avatar Dec 12 '18 23:12 kotlarz

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
}

lpreterite avatar Jun 09 '20 09:06 lpreterite

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 avatar Jun 20 '20 12:06 tboerger

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.

tboerger avatar Jun 20 '20 12:06 tboerger

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

probwebdev avatar Nov 01 '20 18:11 probwebdev

Trigger from git source add \n affter commit message

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

ducdung8491 avatar Mar 02 '22 09:03 ducdung8491