Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build.message will break JSON format #34

Open
Vergil0327 opened this issue Aug 30, 2018 · 7 comments
Open

build.message will break JSON format #34

Vergil0327 opened this issue Aug 30, 2018 · 7 comments

Comments

@Vergil0327
Copy link

Vergil0327 commented Aug 30, 2018

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
Copy link
Contributor

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

@kotlarz
Copy link

kotlarz commented Dec 12, 2018

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
Copy link

lpreterite commented Jun 9, 2020

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link

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

@ducdung8491
Copy link

ducdung8491 commented Mar 2, 2022

Trigger from git source add \n affter commit message

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants