drone-webhook
drone-webhook copied to clipboard
build.message will break JSON format
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 ?
How should a new line break the JSON format? It's still properly quoted and valid JSON.
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.
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
func RenderTrim(template string, playload interface{}) (string, error) {
out, err := Render(template, playload)
return strings.Trim(out, " \n"), err
}
That's not the issue. You are getting the implementation of trim wrong, you can see it at https://golang.org/pkg/strings/#Trim.
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.
Seems that trim is a way to got - {{trim build.message}}.
Trigger from git source add \n affter commit message
You can try
"message": "${DRONE_COMMIT_MESSAGE//"/}"