Skip to content

Commit

Permalink
Support voting patchsets with the same topic
Browse files Browse the repository at this point in the history
  • Loading branch information
jack.mo committed Apr 20, 2021
1 parent 5502f61 commit c689bfc
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import org.slf4j.LoggerFactory;

import com.sonymobile.tools.gerrit.gerritevents.ConnectionListener;
import com.sonymobile.tools.gerrit.gerritevents.GerritCmdRunner;
import com.sonymobile.tools.gerrit.gerritevents.GerritDefaultValues;
import com.sonymobile.tools.gerrit.gerritevents.GerritEventListener;
import com.sonymobile.tools.gerrit.gerritevents.GerritHandler;
Expand All @@ -108,6 +109,7 @@
import com.sonymobile.tools.gerrit.gerritevents.ssh.SshConnectionFactory;
import com.sonymobile.tools.gerrit.gerritevents.ssh.SshUtil;
import com.sonymobile.tools.gerrit.gerritevents.watchdog.WatchTimeExceptionData;
import com.sonymobile.tools.gerrit.gerritevents.workers.cmd.AbstractSendCommandJob;
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.Config;
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.IGerritHudsonTriggerConfig;
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.ReplicationConfig;
Expand Down Expand Up @@ -256,6 +258,23 @@ public GerritQueryHandler getQueryHandler() {
return gerritQueryHnadler;
}

/**
* Get a GerritCmdRunner.
*
* @return a GerritCmdRunner.
*/
public GerritCmdRunner getCmdRunner() {
AbstractSendCommandJob sendCommandJob = new AbstractSendCommandJob(config) {

@Override
public void run() {
// unused implementation
}

};
return sendCommandJob;
}

/**
* Get the name of the server.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ public class Config implements IGerritHudsonTriggerConfig {
private WatchTimeExceptionData watchTimeExceptionData;
private Notify notificationLevel;
private BuildCancellationPolicy buildCurrentPatchesOnly;
private boolean voteSameTopic;

/**
* Constructor.
Expand Down Expand Up @@ -315,6 +316,8 @@ public void setValues(JSONObject formData) {
buildCurrentPatchesOnly = new BuildCancellationPolicy();
}

voteSameTopic = formData.optBoolean("voteSameTopic", false);

numberOfWorkerThreads = formData.optInt(
"numberOfReceivingWorkerThreads",
DEFAULT_NR_OF_RECEIVING_WORKER_THREADS);
Expand Down Expand Up @@ -412,6 +415,8 @@ public void setValues(JSONObject formData) {
useRestApi = false;
}

voteSameTopic = formData.optBoolean("voteSameTopic", false);

replicationConfig = ReplicationConfig.createReplicationConfigFromJSON(formData);
}

Expand Down Expand Up @@ -837,6 +842,20 @@ public BuildCancellationPolicy getBuildCurrentPatchesOnly() {
return this.buildCurrentPatchesOnly;
}

@Override
public boolean isVoteSameTopic() {
return voteSameTopic;
}

/**
* Standard setter for the voteSameTopic value.
*
* @param voteSameTopic true if patchsets with same topic should be voted together.
*/
public void setVoteSameTopic(boolean voteSameTopic) {
this.voteSameTopic = voteSameTopic;
}

@Override
public String getGerritCmdBuildSuccessful() {
return gerritVerifiedCmdBuildSuccessful;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@ public interface IGerritHudsonTriggerConfig extends GerritConnectionConfig2 {

/**
* If enabled, then old patch revision builds will be canceled.
*
* @return true if so.
*/
boolean isGerritBuildCurrentPatchesOnly();

/**
* If enabled, build notifications and votes will be sent to patchsets with the same topic.
*
* @return true if so.
*/
boolean isVoteSameTopic();

/**
* The object containing information regarding if old builds should
* be cancelled when new builds are triggered.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@
package com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier;

import com.sonymobile.tools.gerrit.gerritevents.GerritCmdRunner;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Change;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.PatchSet;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.ChangeBasedEvent;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.GerritTriggeredEvent;
import com.sonymobile.tools.gerrit.gerritevents.dto.rest.Topic;

import java.util.Map;

import com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer;
import com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl;
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.IGerritHudsonTriggerConfig;
import com.sonyericsson.hudson.plugins.gerrit.trigger.extensions.GerritTriggeredBuildListener;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.model.BuildMemory.MemoryImprint;
Expand Down Expand Up @@ -68,6 +76,42 @@ public GerritNotifier(IGerritHudsonTriggerConfig config, GerritCmdRunner cmdRunn
this.parameterExpander = new ParameterExpander(config, hudson);
}

/**
* Notify changes with the same topic.
*
* @param event the change based event.
* @param command the gerrit command.
*/
private void notifySameTopic(ChangeBasedEvent event, String command) {
Topic topic = event.getChange().getTopicObject();
if (topic != null) {
String pattern = "CHANGE,PATCHSET".replace("CHANGE", event.getChange().getNumber()).replace("PATCHSET",
event.getPatchSet().getNumber());
if (!command.contains(pattern)) {
logger.error("command {} has no pattern {}", command, pattern);
return;
}
for (GerritServer server : PluginImpl.getServers_()) {
if (!server.getConfig().isVoteSameTopic()) {
continue;
}
Map<Change, PatchSet> changes = topic.getChanges(server.getQueryHandler());
for (Map.Entry<Change, PatchSet> entry : changes.entrySet()) {
Change change = entry.getKey();
if (change.equals(event.getChange())) {
continue;
}
PatchSet patchSet = entry.getValue();
String substitution = "CHANGE,PATCHSET".replace("CHANGE", change.getNumber())
.replace("PATCHSET", patchSet.getNumber());
String command2 = command.replace(pattern, substitution);
logger.trace("notifySameTopic: {} {} {}", topic, change, command2);
server.getCmdRunner().sendCommand(command2);
}
}
}
}

/**
* Generates the build-started command based on configured templates and build-values and sends it to Gerrit.
* @param build the build.
Expand All @@ -80,12 +124,14 @@ public void buildStarted(Run build, TaskListener taskListener,
try {
/* Without a change, it doesn't make sense to notify gerrit */
if (event instanceof ChangeBasedEvent) {
ChangeBasedEvent changeBasedEvent = (ChangeBasedEvent)event;
String command =
parameterExpander.getBuildStartedCommand(build, taskListener, (ChangeBasedEvent)event, stats);
parameterExpander.getBuildStartedCommand(build, taskListener, changeBasedEvent, stats);
if (command != null) {
if (!command.isEmpty()) {
logger.debug("Notifying BuildStarted to gerrit: {}", command);
cmdRunner.sendCommand(command);
notifySameTopic(changeBasedEvent, command);
GerritTriggeredBuildListener.fireOnStarted(event, command);
} else {
logger.debug("BuildStarted command is empty. Gerrit will not be notified of BuildStarted");
Expand Down Expand Up @@ -116,6 +162,7 @@ public void buildCompleted(MemoryImprint memoryImprint, TaskListener listener) {
if (!command.isEmpty()) {
logger.debug("Notifying BuildCompleted to gerrit: {}", command);
cmdRunner.sendCommand(command);
notifySameTopic((ChangeBasedEvent)memoryImprint.getEvent(), command);
GerritTriggeredBuildListener.fireOnCompleted(memoryImprint, command);
} else {
logger.debug("BuildCompleted command is empty. Gerrit will not be notified of BuildCompleted");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@
default="false"/>
</f:entry>
</f:optionalBlock>
<f:entry title="${%Vote patch sets with same topic}"
help="/plugin/gerrit-trigger/help-GerritVotePatchSetsWithSameTopic.html">
<f:checkbox name="voteSameTopic"
field="voteSameTopic"
checked="${it.config.voteSameTopic}"
default="false"/>
</f:entry>
<f:validateButton title="${%Test Connection}"
progress="${%Testing...}"
method="testConnection"
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/help-GerritVotePatchSetsWithSameTopic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>If this is enabled, build notifications and votes will be sent to patchsets with the same topic.</p>
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,9 @@ public boolean isRestCodeReview() {
public boolean isRestVerified() {
return true;
}

@Override
public boolean isVoteSameTopic() {
return false;
}
}

0 comments on commit c689bfc

Please sign in to comment.