Sniddl / discord-commits

Send Repository Activity using Dynamic Discord Templates
MIT License
81 stars 15 forks source link

"Unexpected token" error when using message containing specific characters #5

Closed Andre601 closed 1 year ago

Andre601 commented 4 years ago

When you have setup an action, that f.e. takes the commit message of commit, will this action sometimes fail with "[error]Unexpected token" in the logs.

This so far only happened when the commit was made by Dependabot.

My guess is, that something - either the action (github) or the software (JavaScript) doesn't like <> being used in Strings.

Example:

ZebTheWizard commented 4 years ago

I just pulled code from dependabot into this repo. It had <> just like your commit did and everything worked.

The only thing I think would cause this is a double quote inside the commit. But when I looked at your commit logs, there were no quotes in any of the commits.

The only thing that sticks out is this line. Pairing $ and {{ together maybe triggering some sort of injection attack flag on GitHub.

"[`${{ github.event.after }}`](${{ github.event.compare }})"

If you change this line, does it work?

Other than that, the only difference between your file and my mine is I put everything on one line and surround it in single quotes.

Unfortunately there is no way to test the action locally, so debugging is a real pain.

Andre601 commented 4 years ago

Those two placeholders you mentioned shouldn't be the cause as GitHub parses them into the commit hash of the new (current) commit and the url to compare it.

If anything would ${{ github.event.commit[0].message }} probably more be the issue as the message contains a fair amount of various markdown formatting.

Andre601 commented 3 years ago

I may have found a possible cause?

It seems to happen when the message is having line breaks in it?

The logs show the linebreaks literally and I would assume it is a possible cause as it breaks the raw json... Not sure, but perhaps replacing \n with \n could help?

ZebTheWizard commented 3 years ago

Make a PR if you found the solution.

I also overlooked a problem with that line I mentioned above.

"[`${{ github.event.after }}`](${{ github.event.compare }})"

You're using string interpolation by having the backticks and `${...}` I'm guessing the line above would work if you concatenated the strings.

Andre601 commented 3 years ago

Make a PR if you found the solution.

I also overlooked a problem with that line I mentioned above.

"[`${{ github.event.after }}`](${{ github.event.compare }})"

You're using string interpolation by having the backticks and ${...} I'm guessing the line above would work if you concatenated the strings.

I mentioned it before that the placeholders pretty much are not the cause but rather what is returned by them. GitHub turns ${{ github.event.after }} into the commit SHA that is created after a merge or push (Aka the latest SHA from that commit) and ${{ github.event.compare }} returns a URL for comparing the changes from the latest push.

As you can see from the image below did it work just fine recently. image

But my guess now is, that for whatever reason the JSON is malformed through the usage of newlines within a commit message

The latest failed run I have does mention "Unexpected token in JSON at position 140" and this may be around the newlines... It's just a guess on my end and I don't have the knowledge to PR a "fix" myself but I really doubt that the placeholders are the cause of it since the are replaced way before the action even starts.