Skip to content

Commit

Permalink
Merge pull request #68 from mathieu-wang/master
Browse files Browse the repository at this point in the history
[JENKINS-17850] Add multiple Gerrit server support
  • Loading branch information
rsandell committed Oct 2, 2013
2 parents 0295703 + 28fd078 commit 3de78e4
Show file tree
Hide file tree
Showing 56 changed files with 2,992 additions and 1,281 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,9 +64,9 @@ 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";
private static final Logger logger = LoggerFactory.getLogger(GerritConnection.class);
private String gerritName;
private String gerritHostName;
private int gerritSshPort;
private String gerritProxy;
Expand All @@ -83,6 +84,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 @@ -91,7 +93,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 @@ -102,14 +105,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 @@ -118,36 +124,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 @@ -157,21 +168,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 @@ -181,12 +195,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 @@ -227,7 +243,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 @@ -243,15 +259,15 @@ public void run() {
Reader reader = sshConnection.executeCommandReader(CMD_STREAM_EVENTS);
br = new BufferedReader(reader);
String line = "";
notifyConnectionEstablished();
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");
notifyConnectionEstablished();
logger.info("Ready to receive data from Gerrit: " + gerritName);
do {
logger.debug("Data-line from Gerrit: {}", line);
if (line != null && line.length() > 0) {
Expand Down
Expand Up @@ -38,6 +38,13 @@ public interface GerritConnectionConfig2 extends GerritConnectionConfig {
*
* @return the timeout setting in minutes for the watchdog.
*/
int getWatchdogTimeoutMinutes();

/**
* Gets the time in seconds before the watchdog times out.
*
* @return the timeout setting in seconds for the watchdog.
*/
int getWatchdogTimeoutSeconds();

/**
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 @@ -93,7 +93,7 @@ public static void setUp() throws Exception {
PowerMockito.mockStatic(SshConnectionFactory.class);
PowerMockito.doReturn(sshConnectionMock).when(SshConnectionFactory.class, "getConnection",
isA(String.class), isA(Integer.class), isA(String.class), isA(Authentication.class));
connection = new GerritConnection("localhost", 29418, new Authentication(null, ""));
connection = new GerritConnection("", "localhost", 29418, new Authentication(null, ""));
connection.setHandler(new HandlerMock());
connection.start();
try {
Expand Down
Expand Up @@ -69,7 +69,7 @@ public void testFullTimeoutFlow() throws IOException, InterruptedException, NoSu
server.returnCommandFor(GERRIT_STREAM_EVENTS, WaitLongTimeCommand.class, true,
new Object[]{MINUTES.toMillis(5)}, new Class<?>[]{Long.class});
server.returnCommandFor(GERRIT_STREAM_EVENTS, SshdServerMock.CommandMock.class);
GerritConnection connection = new GerritConnection("localhost", SshdServerMock.GERRIT_SSH_PORT, "",
GerritConnection connection = new GerritConnection("", "localhost", SshdServerMock.GERRIT_SSH_PORT, "",
new Authentication(sshKey, "jenkins"), 20,
new WatchTimeExceptionData(new int[0], Collections.<WatchTimeExceptionData.TimeSpan>emptyList()));
GerritHandler handler = new GerritHandler();
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 3de78e4

Please sign in to comment.