Skip to content

Commit

Permalink
Rework gerrit commands
Browse files Browse the repository at this point in the history
- 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
Samuel Dolean authored and shemuwel committed Dec 28, 2023
1 parent 25cf2b2 commit e919c54
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 68 deletions.
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger.config;

import com.sonyericsson.hudson.plugins.gerrit.trigger.VerdictCategory;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.BuildCancellationPolicy;
import com.sonymobile.tools.gerrit.gerritevents.GerritDefaultValues;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Provider;
Expand All @@ -33,21 +34,16 @@
import com.sonymobile.tools.gerrit.gerritevents.watchdog.WatchTimeExceptionData;
import com.sonymobile.tools.gerrit.gerritevents.watchdog.WatchTimeExceptionData.Time;
import com.sonymobile.tools.gerrit.gerritevents.watchdog.WatchTimeExceptionData.TimeSpan;
import com.sonyericsson.hudson.plugins.gerrit.trigger.VerdictCategory;

import hudson.util.Secret;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import java.io.File;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.concurrent.TimeUnit;

//CS IGNORE LineLength FOR NEXT 11 LINES. REASON: static import.
Expand Down Expand Up @@ -181,12 +177,6 @@ public class Config implements IGerritHudsonTriggerConfig {
private transient boolean gerritBuildCurrentPatchesOnly;
@Deprecated
private transient int numberOfWorkerThreads;
private String gerritVerifiedCmdBuildSuccessful;
private String gerritVerifiedCmdBuildUnstable;
private String gerritVerifiedCmdBuildFailed;
private String gerritVerifiedCmdBuildStarted;
private String gerritVerifiedCmdBuildNotBuilt;
private String gerritVerifiedCmdBuildAborted;
private String gerritFrontEndUrl;
private Integer gerritBuildStartedVerifiedValue = null;
private Integer gerritBuildSuccessfulVerifiedValue = null;
Expand All @@ -213,6 +203,7 @@ public class Config implements IGerritHudsonTriggerConfig {
private transient boolean loadProjectListOnStartup;
private int projectListFetchDelay;
private List<VerdictCategory> categories;
private Map<BuildStatus, CommandBuilder> buildStatusCommandBuilderMap = new HashMap<>();
private ReplicationConfig replicationConfig;
private int watchdogTimeoutMinutes;
private WatchTimeExceptionData watchTimeExceptionData;
Expand Down Expand Up @@ -251,12 +242,6 @@ public Config(IGerritHudsonTriggerConfig config) {
gerritBuildCurrentPatchesOnly = config.isGerritBuildCurrentPatchesOnly();
numberOfWorkerThreads = config.getNumberOfReceivingWorkerThreads();
numberOfSendingWorkerThreads = config.getNumberOfSendingWorkerThreads();
gerritVerifiedCmdBuildStarted = config.getGerritCmdBuildStarted();
gerritVerifiedCmdBuildFailed = config.getGerritCmdBuildFailed();
gerritVerifiedCmdBuildSuccessful = config.getGerritCmdBuildSuccessful();
gerritVerifiedCmdBuildUnstable = config.getGerritCmdBuildUnstable();
gerritVerifiedCmdBuildNotBuilt = config.getGerritCmdBuildNotBuilt();
gerritVerifiedCmdBuildAborted = config.getGerritCmdBuildAborted();
gerritFrontEndUrl = config.getGerritFrontEndUrl();
enableManualTrigger = config.isEnableManualTrigger();
enablePluginMessages = config.isEnablePluginMessages();
Expand All @@ -266,12 +251,21 @@ public Config(IGerritHudsonTriggerConfig config) {
enableProjectAutoCompletion = config.isEnableProjectAutoCompletion();
projectListFetchDelay = config.getProjectListFetchDelay();
projectListRefreshInterval = config.getProjectListRefreshInterval();

if (config.getCategories() != null) {
categories = new LinkedList<>();
for (VerdictCategory cat : config.getCategories()) {
categories.add(cat.clone());
}
}

buildStatusCommandBuilderMap.put(BuildStatus.STARTED, CommandBuilder.fromString(config.getGerritCmdBuildStarted()));
buildStatusCommandBuilderMap.put(BuildStatus.SUCCESSFUL, CommandBuilder.fromString(config.getGerritCmdBuildSuccessful()));
buildStatusCommandBuilderMap.put(BuildStatus.FAILED, CommandBuilder.fromString(config.getGerritCmdBuildFailed()));
buildStatusCommandBuilderMap.put(BuildStatus.UNSTABLE, CommandBuilder.fromString(config.getGerritCmdBuildUnstable()));
buildStatusCommandBuilderMap.put(BuildStatus.NOT_BUILT, CommandBuilder.fromString(config.getGerritCmdBuildNotBuilt()));
buildStatusCommandBuilderMap.put(BuildStatus.ABORTED, CommandBuilder.fromString(config.getGerritCmdBuildAborted()));

if (config.getReplicationConfig() != null) {
replicationConfig = new ReplicationConfig(config.getReplicationConfig());
}
Expand Down Expand Up @@ -320,30 +314,19 @@ public void setValues(JSONObject formData) {
numberOfSendingWorkerThreads = DEFAULT_NR_OF_SENDING_WORKER_THREADS;
}

gerritVerifiedCmdBuildStarted = formData.optString(
"gerritVerifiedCmdBuildStarted",
"gerrit review <CHANGE>,<PATCHSET> --message 'Build Started <BUILDURL> <STARTED_STATS>' "
+ "--verified <VERIFIED> --code-review <CODE_REVIEW> --tag " + Constants.TAG_VALUE);
gerritVerifiedCmdBuildFailed = formData.optString(
"gerritVerifiedCmdBuildFailed",
"gerrit review <CHANGE>,<PATCHSET> --message 'Build Failed <BUILDS_STATS>' "
+ "--verified <VERIFIED> --code-review <CODE_REVIEW> --tag " + Constants.TAG_VALUE);
gerritVerifiedCmdBuildSuccessful = formData.optString(
"gerritVerifiedCmdBuildSuccessful",
"gerrit review <CHANGE>,<PATCHSET> --message 'Build Successful <BUILDS_STATS>' "
+ "--verified <VERIFIED> --code-review <CODE_REVIEW> --tag " + Constants.TAG_VALUE);
gerritVerifiedCmdBuildUnstable = formData.optString(
"gerritVerifiedCmdBuildUnstable",
"gerrit review <CHANGE>,<PATCHSET> --message 'Build Unstable <BUILDS_STATS>' "
+ "--verified <VERIFIED> --code-review <CODE_REVIEW> --tag " + Constants.TAG_VALUE);
gerritVerifiedCmdBuildNotBuilt = formData.optString(
"gerritVerifiedCmdBuildNotBuilt",
"gerrit review <CHANGE>,<PATCHSET> --message 'No Builds Executed <BUILDS_STATS>' "
+ "--verified <VERIFIED> --code-review <CODE_REVIEW> --tag " + Constants.TAG_VALUE);
gerritVerifiedCmdBuildAborted = formData.optString(
"gerritVerifiedCmdBuildAborted",
"gerrit review <CHANGE>,<PATCHSET> --message 'Build Aborted <BUILDS_STATS>' "
+ "--verified <VERIFIED> --code-review <CODE_REVIEW> --tag " + Constants.TAG_VALUE);
buildStatusCommandBuilderMap.put(BuildStatus.STARTED, CommandBuilder.fromString(formData.optString("gerritVerifiedCmdBuildStarted", null),
new CommandBuilder().WithMessage("Build Started <BUILDURL> <STARTED_STATS>")));
buildStatusCommandBuilderMap.put(BuildStatus.SUCCESSFUL, CommandBuilder.fromString(formData.optString("gerritVerifiedCmdBuildSuccessful", null),
new CommandBuilder().WithMessage("Build Successful <BUILDS_STATS>")));
buildStatusCommandBuilderMap.put(BuildStatus.FAILED, CommandBuilder.fromString(formData.optString("gerritVerifiedCmdBuildFailed", null),
new CommandBuilder().WithMessage("Build Failed <BUILDS_STATS>")));
buildStatusCommandBuilderMap.put(BuildStatus.UNSTABLE, CommandBuilder.fromString(formData.optString("gerritVerifiedCmdBuildUnstable", null),
new CommandBuilder().WithMessage("Build Unstable <BUILDS_STATS>")));
buildStatusCommandBuilderMap.put(BuildStatus.NOT_BUILT, CommandBuilder.fromString(formData.optString("gerritVerifiedCmdBuildNotBuilt", null),
new CommandBuilder().WithMessage("No Builds Executed <BUILDS_STATS>")));
buildStatusCommandBuilderMap.put(BuildStatus.ABORTED, CommandBuilder.fromString(formData.optString("gerritVerifiedCmdBuildAborted", null),
new CommandBuilder().WithMessage("Build Aborted <BUILDS_STATS>")));

gerritFrontEndUrl = formData.optString(
"gerritFrontEndUrl",
DEFAULT_GERRIT_HOSTNAME);
Expand Down Expand Up @@ -881,14 +864,15 @@ public String getVoteSameTopicDescription() {

@Override
public String getGerritCmdBuildSuccessful() {
return gerritVerifiedCmdBuildSuccessful;
return buildStatusCommandBuilderMap.get(BuildStatus.SUCCESSFUL).build();
}

/**
* Set Gerrit Verified Cmd Build Successful.
*
* @param cmd the command
* @see #getGerritCmdBuildSuccessful()
* @deprecated use {@link Config#setGerritCmdBuildSuccessful(String) } instead.
*/
@Deprecated
public void setGerritVerifiedCmdBuildSuccessful(String cmd) {
Expand All @@ -902,19 +886,20 @@ public void setGerritVerifiedCmdBuildSuccessful(String cmd) {
* @see #getGerritCmdBuildSuccessful()
*/
public void setGerritCmdBuildSuccessful(String cmd) {
gerritVerifiedCmdBuildSuccessful = cmd;
buildStatusCommandBuilderMap.put(BuildStatus.SUCCESSFUL, CommandBuilder.fromString(cmd));
}

@Override
public String getGerritCmdBuildUnstable() {
return gerritVerifiedCmdBuildUnstable;
return buildStatusCommandBuilderMap.get(BuildStatus.UNSTABLE).build();
}

/**
* Set Gerrit Verified Cmd Build Unstable.
*
* @param cmd the command
* @see #getGerritCmdBuildUnstable()
* @deprecated use {@link Config#setGerritCmdBuildUnstable(String) } instead.
*/
@Deprecated
public void setGerritVerifiedCmdBuildUnstable(String cmd) {
Expand All @@ -928,19 +913,20 @@ public void setGerritVerifiedCmdBuildUnstable(String cmd) {
* @see #getGerritCmdBuildUnstable()
*/
public void setGerritCmdBuildUnstable(String cmd) {
gerritVerifiedCmdBuildUnstable = cmd;
buildStatusCommandBuilderMap.put(BuildStatus.UNSTABLE, CommandBuilder.fromString(cmd));
}

@Override
public String getGerritCmdBuildFailed() {
return gerritVerifiedCmdBuildFailed;
return buildStatusCommandBuilderMap.get(BuildStatus.FAILED).build();
}

/**
* Set Gerrit Verified Cmd Build Failed.
*
* @param cmd the command
* @see #getGerritCmdBuildFailed()
* @deprecated use {@link Config#setGerritCmdBuildFailed(String) } instead.
*/
@Deprecated
public void setGerritVerifiedCmdBuildFailed(String cmd) {
Expand All @@ -954,19 +940,20 @@ public void setGerritVerifiedCmdBuildFailed(String cmd) {
* @see #getGerritCmdBuildFailed()
*/
public void setGerritCmdBuildFailed(String cmd) {
gerritVerifiedCmdBuildFailed = cmd;
buildStatusCommandBuilderMap.put(BuildStatus.FAILED, CommandBuilder.fromString(cmd));
}

@Override
public String getGerritCmdBuildStarted() {
return gerritVerifiedCmdBuildStarted;
return buildStatusCommandBuilderMap.get(BuildStatus.STARTED).build();
}

/**
* Set Gerrit Verified Cmd Build Started.
*
* @param cmd the command
* @see #getGerritCmdBuildStarted()
* @deprecated use {@link Config#setGerritCmdBuildStarted(String)} } instead.
*/
@Deprecated
public void setGerritVerifiedCmdBuildStarted(String cmd) {
Expand All @@ -980,19 +967,20 @@ public void setGerritVerifiedCmdBuildStarted(String cmd) {
* @see #getGerritCmdBuildStarted()
*/
public void setGerritCmdBuildStarted(String cmd) {
gerritVerifiedCmdBuildStarted = cmd;
buildStatusCommandBuilderMap.put(BuildStatus.STARTED, CommandBuilder.fromString(cmd));
}

@Override
public String getGerritCmdBuildNotBuilt() {
return gerritVerifiedCmdBuildNotBuilt;
return buildStatusCommandBuilderMap.get(BuildStatus.NOT_BUILT).build();
}

/**
* Set Gerrit Verified Cmd Build NotBuilt.
*
* @param cmd the command
* @see #getGerritCmdBuildNotBuilt()
* @deprecated use {@link Config#setGerritCmdBuildNotBuilt(String)} } instead.
*/
@Deprecated
public void setGerritVerifiedCmdBuildNotBuilt(String cmd) {
Expand All @@ -1006,19 +994,20 @@ public void setGerritVerifiedCmdBuildNotBuilt(String cmd) {
* @see #getGerritCmdBuildNotBuilt()
*/
public void setGerritCmdBuildNotBuilt(String cmd) {
gerritVerifiedCmdBuildNotBuilt = cmd;
buildStatusCommandBuilderMap.put(BuildStatus.NOT_BUILT, CommandBuilder.fromString(cmd));
}

@Override
public String getGerritCmdBuildAborted() {
return gerritVerifiedCmdBuildAborted;
return buildStatusCommandBuilderMap.get(BuildStatus.ABORTED).build();
}

/**
* Set Gerrit Verified Cmd Build Aborted.
*
* @param cmd the command
* @see #getGerritCmdBuildAborted()
* @deprecated use {@link Config#setGerritCmdBuildAborted(String) } instead.
*/
@Deprecated
public void setGerritVerifiedCmdBuildAborted(String cmd) {
Expand All @@ -1032,7 +1021,7 @@ public void setGerritVerifiedCmdBuildAborted(String cmd) {
* @see #getGerritCmdBuildAborted()
*/
public void setGerritCmdBuildAborted(String cmd) {
gerritVerifiedCmdBuildAborted = cmd;
buildStatusCommandBuilderMap.put(BuildStatus.ABORTED, CommandBuilder.fromString(cmd));
}

/**
Expand Down Expand Up @@ -1292,6 +1281,8 @@ public List<VerdictCategory> getCategories() {
return categories;
}

public Map<BuildStatus, CommandBuilder> getBuilderStatusCommandMap() { return buildStatusCommandBuilderMap; }

/**
* Setter for the list of VerdictCategories, used to make testing easier.
* @param categories the list.
Expand Down Expand Up @@ -1504,8 +1495,8 @@ Object readResolve() {
}

/* Only set these values when dealing with an old configuration*/
if (this.gerritVerifiedCmdBuildAborted == null) {
this.gerritVerifiedCmdBuildAborted = this.gerritVerifiedCmdBuildFailed;
if (this.buildStatusCommandBuilderMap.get(BuildStatus.ABORTED) == null) {
this.buildStatusCommandBuilderMap.put(BuildStatus.ABORTED, this.buildStatusCommandBuilderMap.get(BuildStatus.FAILED));

if (this.gerritBuildAbortedCodeReviewValue == null) {
gerritBuildAbortedCodeReviewValue = gerritBuildFailedCodeReviewValue;
Expand Down
Loading

0 comments on commit e919c54

Please sign in to comment.