forked from jenkinsci/gerrit-trigger-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- use a CommandBuilder to either create a new command or default command (with placeholders - for global config.) allowing smoother manipulation of its content - remove local fields from Config and instead save the commands based on their BuildStatus
- Loading branch information
Showing
4 changed files
with
228 additions
and
68 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/config/CommandBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.sonyericsson.hudson.plugins.gerrit.trigger.config; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Builder for gerrit review commands. | ||
* @author schemuwel <[email protected]> | ||
*/ | ||
public class CommandBuilder { | ||
private Map<String, String> labels = new HashMap<>(Map.of( | ||
Constants.CODE_REVIEW_LABEL.toLowerCase(), "<CODE_REVIEW>", | ||
Constants.VERIFIED_LABEL.toLowerCase(), "<VERIFIED>" | ||
)); | ||
|
||
private String command = "gerrit review"; | ||
private String message; | ||
private String change = "<CHANGE>"; | ||
private String patchset = "<PATCHSET>"; | ||
|
||
public CommandBuilder WithChange(String change) { | ||
this.change = change; | ||
return this; | ||
} | ||
|
||
public CommandBuilder WithPatchset(String patchset) { | ||
this.patchset = patchset; | ||
return this; | ||
} | ||
|
||
public CommandBuilder WithMessage(String message) { | ||
this.message = message; | ||
return this; | ||
} | ||
|
||
public CommandBuilder WithLabel(String label) { | ||
String placeholderLabelValue = String.format("<%s>", label.toUpperCase().replace("-", "_")); | ||
this.labels.put(label.toLowerCase(), placeholderLabelValue); | ||
return this; | ||
} | ||
|
||
public CommandBuilder WithLabel(String label, String labelValue) { | ||
this.labels.put(label.toLowerCase(), labelValue); | ||
return this; | ||
} | ||
|
||
public static CommandBuilder fromString(String command, CommandBuilder defaultCommandBuilder) { | ||
if (command == null) { | ||
return defaultCommandBuilder; | ||
} | ||
|
||
return fromString(command); | ||
} | ||
|
||
public static CommandBuilder fromString(String command) { | ||
Pattern gerritReviewPattern = Pattern.compile("(\\S+),(\\S+)"); | ||
Pattern messagePattern = Pattern.compile("--message '(.*?)'"); | ||
Pattern labelPattern = Pattern.compile("--(?!message|tag\\b)(\\S+) (\\S+)"); | ||
|
||
Matcher gerritReviewMatcher = gerritReviewPattern.matcher(command); | ||
Matcher messageMatcher = messagePattern.matcher(command); | ||
Matcher labelMatcher = labelPattern.matcher(command); | ||
|
||
CommandBuilder commandBuilder = new CommandBuilder(); | ||
if (gerritReviewMatcher.find()) { | ||
String change = gerritReviewMatcher.group(1); | ||
String patchset = gerritReviewMatcher.group(2); | ||
|
||
commandBuilder.WithChange(change).WithPatchset(patchset); | ||
} | ||
|
||
if (messageMatcher.find()) { | ||
String message = messageMatcher.group(1); | ||
|
||
commandBuilder.WithMessage(message); | ||
} | ||
|
||
while (labelMatcher.find()) { | ||
String label = labelMatcher.group(1); | ||
if (commandBuilder.getLabels().containsKey(label)) { | ||
continue; | ||
} | ||
|
||
commandBuilder.WithLabel(label); | ||
} | ||
|
||
return commandBuilder; | ||
} | ||
|
||
private Map<String, String> getLabels() { | ||
return labels; | ||
} | ||
|
||
public String build() { | ||
StringBuilder gerritCommand = new StringBuilder(command); | ||
gerritCommand.append(String.format(" %s,%s", change, patchset)); | ||
gerritCommand.append(String.format(" --message '%s'", message)); | ||
for (Map.Entry<String, String> label : labels.entrySet()) { | ||
gerritCommand.append(String.format(" --%s %s", label.getKey(), label.getValue())); | ||
} | ||
gerritCommand.append(String.format(" --tag %s", Constants.TAG_VALUE)); | ||
return gerritCommand.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.