'java: using ssh command tom make multi-line comments on gerrit

when I am using shell to publish a comment on gerrit, for example,

ssh -p 29418 gerritlink gerrit review --verified=0 '--message=[FAILURE] COMMIT MESSAGE CHECK FAIL.

PCPID not exist.

A360-ID not exist.' d9a5d29799ada8237679efd1121cd7e3a85f333d

and this works fine in gerrit comments, you can see multi-line there. but when I use java to call the command, all the sentences are in one line. how can I make the comments in multiline in java?



Solution 1:[1]

I'm quite sure if this will help but you can try separating your commands using ; or &&.

Solution 2:[2]


Here are two solutions that worked for me:

  1. Gerrit collapses single newlines, so use double newlines "\n\n" instead of "\r\n".
  2. Prefix each line with spaces and that will convince Gerrit to show them as separate mono-spaced lines. The paragraph gets formatted using HTML pre style which is useful for code samples.

Sources: Re: Multiline comments in gerrit and Comment formatting

I also had to run the ssh command using ProcessBuilder to get the above solutions to work.

E.g. Sending message using Solution #1:

String solution1_msg = "Test1\n\nThis is a test\n\nTesting has completed"
String solution2_msg = "Test2\n   This is a test\n   Testing has completed"

ProcessBuilder pb = new ProcessBuilder("ssh", "-p", "29418", "<gerrit server>",
    "gerrit", "review", "--message=\"" + solution1_message + "\"commit SHA-1>");
pb.redirectErrorStream(); //redirect stderr to stdout
Process process = pb.start();
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while((line = reader.readLine())!= null) {
  System.out.println(line);
}
process.waitFor();

Same code works for Solution #2 as well; pass solution2_message as the message instead of solution1_message.

Source: How do I run multiple commands in SSH through Java?

Solution 3:[3]

I will explain it in shell. The problem is related to the quotes that are been used incorrectly.

$> msg="- line1                               
- line2
- line3
- line4"
$> echo ${msg}
$> - line1 - line2 - line3 - line4
$> echo "${msg}"
- line1
- line2
- line3
- line4

The same thing with Gerrit command, you to have to insure that needed quotes are in the needed place.

ssh -p 29418 admin@localhost gerrit review 3,2 --project test -m "'"${msg}"'" --code-review 1

Administrator Patch Set 2:

  • hello - wordl - line3 - line4
ssh -p 29418 admin@localhost gerrit review 3,2 --project test -m "'""${msg}""'" --code-review 1

Administrator Patch Set 2:

  • line1
  • line2
  • line3
  • line4

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Muhammad Gelbana
Solution 2 Community
Solution 3 fflores