Skip to content

Commit

Permalink
[JENKINS-17850] Add multiple Gerrit server support
Browse files Browse the repository at this point in the history
Refactor methods related to one Gerrit server to a new class GerritServer and
keep a list of GerritServer objects in PluginImpl.
Refactor all related classes and tests.

Change-Id: I11d3f0d694b3670e879a6ecf10513967603c6e13
  • Loading branch information
mathieu-wang committed Aug 5, 2013
1 parent 45230cb commit 99462d8
Show file tree
Hide file tree
Showing 48 changed files with 1,720 additions and 756 deletions.
3 changes: 3 additions & 0 deletions README.txt
Expand Up @@ -30,6 +30,9 @@ pushed from Gerrit instead of pulled as scm-triggers usually are.
Multiple builds can be triggered by one change-event, and one consolidated
report is sent back to Gerrit.

Multiple Gerrit server connections can be established per Jenkins instance.
Each job can be configured with one Gerrit server.

=============
Maintainers

Expand Down
Expand Up @@ -45,6 +45,7 @@
//CS IGNORE LineLength FOR NEXT 7 LINES. REASON: static import.
import static com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritDefaultValues.DEFAULT_GERRIT_AUTH_KEY_FILE;
import static com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritDefaultValues.DEFAULT_GERRIT_AUTH_KEY_FILE_PASSWORD;
import static com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritDefaultValues.DEFAULT_GERRIT_NAME;
import static com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritDefaultValues.DEFAULT_GERRIT_HOSTNAME;
import static com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritDefaultValues.DEFAULT_GERRIT_SSH_PORT;
import static com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritDefaultValues.DEFAULT_GERRIT_PROXY;
Expand All @@ -63,7 +64,6 @@ public class GerritConnection extends Thread implements Connector {
public static final int CONNECT_SLEEP = 2000;
private static final String CMD_STREAM_EVENTS = "gerrit stream-events";
private static final String GERRIT_VERSION_PREFIX = "gerrit version ";
private static final String GERRIT_NAME = "gerrit";
private static final String GERRIT_PROTOCOL_NAME = "ssh";
/**
* The amount of milliseconds to pause when brute forcing the shutdown flag to true.
Expand All @@ -79,6 +79,7 @@ public class GerritConnection extends Thread implements Connector {
*/
protected static final int BRUTE_FORCE_TRIES = 10;
private static final Logger logger = LoggerFactory.getLogger(GerritConnection.class);
private String gerritName;
private String gerritHostName;
private int gerritSshPort;
private String gerritProxy;
Expand All @@ -97,6 +98,7 @@ public class GerritConnection extends Thread implements Connector {
/**
* Creates a GerritHandler with all the default values set.
*
* @see GerritDefaultValues#DEFAULT_GERRIT_NAME
* @see GerritDefaultValues#DEFAULT_GERRIT_HOSTNAME
* @see GerritDefaultValues#DEFAULT_GERRIT_SSH_PORT
* @see GerritDefaultValues#DEFAULT_GERRIT_PROXY
Expand All @@ -105,7 +107,8 @@ public class GerritConnection extends Thread implements Connector {
* @see GerritDefaultValues#DEFAULT_GERRIT_AUTH_KEY_FILE_PASSWORD
*/
public GerritConnection() {
this(DEFAULT_GERRIT_HOSTNAME,
this(DEFAULT_GERRIT_NAME,
DEFAULT_GERRIT_HOSTNAME,
DEFAULT_GERRIT_SSH_PORT,
DEFAULT_GERRIT_PROXY,
new Authentication(DEFAULT_GERRIT_AUTH_KEY_FILE,
Expand All @@ -116,14 +119,17 @@ public GerritConnection() {
/**
* Creates a GerritHandler with the specified values.
*
* @param gerritName the name of the gerrit server.
* @param gerritHostName the hostName
* @param gerritSshPort the ssh port that the gerrit server listens to.
* @param authentication the authentication credentials.
*/
public GerritConnection(String gerritHostName,
public GerritConnection(String gerritName,
String gerritHostName,
int gerritSshPort,
Authentication authentication) {
this(gerritHostName,
this(gerritName,
gerritHostName,
gerritSshPort,
DEFAULT_GERRIT_PROXY,
authentication);
Expand All @@ -132,36 +138,41 @@ public GerritConnection(String gerritHostName,
/**
* Standard Constructor.
*
* @param gerritName the name of the gerrit server.
* @param config the configuration containing the connection values.
*/
public GerritConnection(GerritConnectionConfig config) {
this(config, DEFAULT_GERRIT_PROXY, 0, null);
public GerritConnection(String gerritName, GerritConnectionConfig config) {
this(gerritName, config, DEFAULT_GERRIT_PROXY, 0, null);
}

/**
* Standard Constructor.
*
* @param gerritName the name of the gerrit server.
* @param config the configuration containing the connection values.
*/
public GerritConnection(GerritConnectionConfig2 config) {
this(config, config.getGerritProxy(), config.getWatchdogTimeoutSeconds(), config.getExceptionData());
public GerritConnection(String gerritName, GerritConnectionConfig2 config) {
this(gerritName, config, config.getGerritProxy(), config.getWatchdogTimeoutSeconds(), config.getExceptionData());
}

/**
* Creates a GerritHandler with the specified values.
*
* @param gerritName the name of the gerrit server.
* @param config the configuration containing the connection values.
* @param gerritProxy the URL of gerrit proxy.
* @param watchdogTimeoutSeconds number of seconds before the connection watch dog restarts the connection set to 0
* or less to disable it.
* @param exceptionData time info for when the watch dog's timeout should not be in effect. set to null to
* disable the watch dog.
*/
public GerritConnection(GerritConnectionConfig config,
public GerritConnection(String gerritName,
GerritConnectionConfig config,
String gerritProxy,
int watchdogTimeoutSeconds,
WatchTimeExceptionData exceptionData) {
this(config.getGerritHostName(),
this(gerritName,
config.getGerritHostName(),
config.getGerritSshPort(),
gerritProxy,
config.getGerritAuthentication(),
Expand All @@ -171,21 +182,24 @@ public GerritConnection(GerritConnectionConfig config,
/**
* Standard Constructor.
*
* @param gerritName the name of the gerrit server.
* @param gerritHostName the hostName for gerrit.
* @param gerritSshPort the ssh port that the gerrit server listens to.
* @param gerritProxy the proxy url socks5|http://host:port.
* @param authentication the authentication credentials.
*/
public GerritConnection(String gerritHostName,
public GerritConnection(String gerritName,
String gerritHostName,
int gerritSshPort,
String gerritProxy,
Authentication authentication) {
this(gerritHostName, gerritSshPort, gerritProxy, authentication, 0, null);
this(gerritName, gerritHostName, gerritSshPort, gerritProxy, authentication, 0, null);
}

/**
* Standard Constructor.
*
* @param gerritName the name of the gerrit server.
* @param gerritHostName the hostName for gerrit.
* @param gerritSshPort the ssh port that the gerrit server listens to.
* @param gerritProxy the proxy url socks5|http://host:port.
Expand All @@ -195,12 +209,14 @@ public GerritConnection(String gerritHostName,
* @param exceptionData time info for when the watch dog's timeout should not be in effect.
* set to null to disable the watch dog.
*/
public GerritConnection(String gerritHostName,
public GerritConnection(String gerritName,
String gerritHostName,
int gerritSshPort,
String gerritProxy,
Authentication authentication,
int watchdogTimeoutSeconds,
WatchTimeExceptionData exceptionData) {
this.gerritName = gerritName;
this.gerritHostName = gerritHostName;
this.gerritSshPort = gerritSshPort;
this.gerritProxy = gerritProxy;
Expand Down Expand Up @@ -241,7 +257,7 @@ public String getGerritVersion() {
*/
@Override
public void run() {
logger.info("Starting Up...");
logger.info("Starting Up " + gerritName);
do {
sshConnection = connect();
if (sshConnection == null) {
Expand All @@ -258,13 +274,13 @@ public void run() {
br = new BufferedReader(reader);
String line = "";
Provider provider = new Provider(
GERRIT_NAME,
gerritName,
gerritHostName,
String.valueOf(gerritSshPort),
GERRIT_PROTOCOL_NAME,
DEFAULT_GERRIT_HOSTNAME,
getGerritVersionString());
logger.info("Ready to receive data from Gerrit");
logger.info("Ready to receive data from Gerrit: " + gerritName);
notifyConnectionEstablished();
do {
logger.debug("Data-line from Gerrit: {}", line);
Expand Down
Expand Up @@ -38,6 +38,10 @@ public final class GerritDefaultValues {
private GerritDefaultValues() {

}
/**
* The default gerrit name.
*/
public static final String DEFAULT_GERRIT_NAME = "";
/**
* The default gerrit hostname.
*/
Expand Down
Expand Up @@ -22,7 +22,8 @@
public class ChangeIdAnnotator extends ChangeLogAnnotator {
@Override
public void annotate(AbstractBuild<?, ?> build, Entry change, MarkupText text) {
IGerritHudsonTriggerConfig config = PluginImpl.getInstance().getConfig();
String serverName = GerritTrigger.getTrigger(build.getProject()).getServerName();
IGerritHudsonTriggerConfig config = PluginImpl.getInstance().getServer(serverName).getConfig();
annotate(build.getProject(), text, config);
}

Expand Down

0 comments on commit 99462d8

Please sign in to comment.