Our CI adds messages (for example with verification result links) to a review by using
gerrit review (
https://gerrit-review.googlesource.com/Documentation/cmd-review.html), something like this:
ssh -p ${GERRIT_PORT} ${GERRIT_HOST} \
gerrit review ${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER} \
--message "<text>"
However, we hit a problem. This is being executed by a Groovy script under Jenkins, within sh command. This means there are many "layers" and proper escaping of the "<text>" seems challenging at best. While we do hit problems with unescaped characters that break the command line.
My original idea was to use --json instead of --message, but this doesn't work since with this mode the JSON data must be from stdin rather than a file (like I wrongly assumed for whatever reason, since the docs are clear about it...). Furthermore, either way, it will not work since the gerrit review is executed (through ssh) on the Gerrit server rather than my server, so my .json file is not there!
I was playing with the idea of first uploading the prepared .json file to the Gerrit server and then using it with cat (cat <file> | gerrit review --json) or a redirection (gerrit review --json < <file>). But firstly, I'm not sure if I would be able to upload in the first place, and secondly, both forms were rejected by the server with
fatal: Gerrit Code Review: cat: not found
and
fatal: "<" is not a valid patch set
respectively.
Then, while looking for a solution without escaping the text I found out about the Rest API (
https://gerrit-review.googlesource.com/Documentation/rest-api.html). However, reading the docs, I don't see any action to add a message as if by
gerrit review --message. (And at best, we would also vote on labels since the original code we have does so as well - there is both
--message and
--label.)
So, can we add messages with Rest API? Or maybe there are some other means to avoid the problem of escaping the argument of --message?
Adam Badura