Skip to content
Permalink
Browse files

Add Pseudo Mode to server configuration

GerritServer needs to be enabled in order to use it at several sections.
It needs actual gerrit connection. But it is inconvenient to post events
from external.

This patch adds pseudo mode to server configuration. This can start
GerritServer without any gerrit connections.
  • Loading branch information...
rinrinne committed Jan 15, 2014
1 parent b2028be commit 0e735d659533bf6331a40fe42449be5ecca0ed6c
@@ -120,6 +120,7 @@
private static final int THREADS_FOR_TEST_CONNECTION = 1;
private static final int TIMEOUT_FOR_TEST_CONNECTION = 10;
private String name;
private boolean pseudoMode;
private transient boolean started;
private transient String connectionResponse = "";
private transient GerritHandler gerritEventManager;
@@ -158,7 +159,18 @@ public String getUrl() {
* @param name the name of the server.
*/
public GerritServer(String name) {
this(name, false);
}

/**
* Constructor.
*
* @param name the name of the server.
* @param pseudoMode if pseudo mode or not.
*/
public GerritServer(String name, boolean pseudoMode) {
this.name = name;
this.pseudoMode = pseudoMode;
config = new Config();
}

@@ -189,6 +201,24 @@ public String getName() {
return name;
}

/**
* If pseudo mode or not.
*
* @return true if so.
*/
public boolean isPseudoMode() {
return pseudoMode;
}

/**
* Sets pseudo mode.
*
* @param pseudoMode true if pseudoMode connection.
*/
public void setPseudoMode(boolean pseudoMode) {
this.pseudoMode = pseudoMode;
}

@Override
public String getIconFileName() {
return PLUGIN_IMAGES_URL + "icon24.png";
@@ -254,13 +284,20 @@ public void start() {
config.setCategories(categories);
gerritEventManager = PluginImpl.getInstance().getHandler();

if (pseudoMode) {
logger.info(name + " started (pseudo mode)");
started = true;
return;
}

initializeConnectionListener();

projectListUpdater = new GerritProjectListUpdater(name);
projectListUpdater.start();

//Starts unreviewed patches listener
unreviewedPatchesListener = new UnreviewedPatchesListener(name);

logger.info(name + " started");
started = true;
}
@@ -281,6 +318,13 @@ private void initializeConnectionListener() {
*/
public void stop() {
logger.info("Stopping GerritServer " + name);

if (pseudoMode) {
logger.info(name + " stopped (pseudo mode)");
started = false;
return;
}

if (projectListUpdater != null) {
projectListUpdater.shutdown();
try {
@@ -356,6 +400,11 @@ public GerritConnectionListener getGerritConnectionListener() {
* @see DescriptorImpl#doConnectionSubmit(StaplerRequest, StaplerResponse)
*/
public synchronized void startConnection() {
if (pseudoMode) {
gerritEventManager.setIgnoreEMail(name, config.getGerritEMail());
return;
}

if (!config.hasDefaultValues()) {
if (gerritConnection == null) {
logger.debug("Starting Gerrit connection...");
@@ -377,6 +426,11 @@ public synchronized void startConnection() {
* @see DescriptorImpl#doConnectionSubmit(StaplerRequest, StaplerResponse)
*/
public synchronized void stopConnection() {
if (pseudoMode) {
gerritEventManager.setIgnoreEMail(name, null);
return;
}

if (gerritConnection != null) {
gerritConnection.shutdown(true);
gerritConnection.removeListener(gerritConnectionListener);
@@ -385,7 +439,6 @@ public synchronized void stopConnection() {
} else {
logger.warn("Was told to shutdown again?");
}

}

/**
@@ -395,6 +448,9 @@ public synchronized void stopConnection() {
*/

public synchronized boolean isConnected() {
if (pseudoMode) {
return true;
}
if (gerritConnection != null) {
return gerritConnection.isConnected();
}
@@ -407,6 +463,9 @@ public synchronized boolean isConnected() {
* @see DescriptorImpl#doConnectionSubmit(StaplerRequest, StaplerResponse)
*/
public void restartConnection() {
if (pseudoMode) {
return;
}
stopConnection();
startConnection();
}
@@ -480,6 +539,7 @@ public String getDisplayName() {

/**
* Tests if the provided parameters can connect to Gerrit.
* @param pseudoMode if pseudo mode or not.
* @param gerritHostName the hostname
* @param gerritSshPort the ssh-port
* @param gerritProxy the proxy url
@@ -490,12 +550,16 @@ public String getDisplayName() {
* {@link FormValidation#error(java.lang.String) } otherwise.
*/
public FormValidation doTestConnection(
@QueryParameter("pseudoMode") final boolean pseudoMode,
@QueryParameter("gerritHostName") final String gerritHostName,
@QueryParameter("gerritSshPort") final int gerritSshPort,
@QueryParameter("gerritProxy") final String gerritProxy,
@QueryParameter("gerritUserName") final String gerritUserName,
@QueryParameter("gerritAuthKeyFile") final String gerritAuthKeyFile,
@QueryParameter("gerritAuthKeyFilePassword") final String gerritAuthKeyFilePassword) {
if (pseudoMode) {
return FormValidation.ok(Messages.Success());
}
if (logger.isDebugEnabled()) {
logger.debug("gerritHostName = {}\n"
+ "gerritSshPort = {}\n"
@@ -612,6 +676,7 @@ public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws Servl
logger.debug("submit {}", req.toString());
}
JSONObject form = req.getSubmittedForm();
pseudoMode = form.getBoolean("pseudoMode");

String newName = form.getString("name");
boolean renamed = false;
@@ -23,6 +23,12 @@
value="${it.name}"
checkUrl="'${rootURL}/${serverURL}/nameFreeCheck?value='+escape(this.value)"/>
</f:entry>
<f:entry title="${%Pseudo Mode}"
help="/plugin/gerrit-trigger/help-GerritPseudoMode.html">
<f:checkbox name="pseudoMode"
checked="${it.pseudoMode}"
default="false"/>
</f:entry>
<f:entry title="${%Hostname}"
help="/plugin/gerrit-trigger/help-GerritHostname.html">
<f:textbox name="gerritHostName"
@@ -6,6 +6,10 @@ Global\ Configuration=\
\u5168\u4f53\u8a2d\u5b9a
Gerrit\ Server=\
Gerrit\u30b5\u30fc\u30d0\u30fc
Name=\
\u540d\u524d
Pseudo\ Mode=\
\u64ec\u4f3c\u30e2\u30fc\u30c9
Hostname=\
\u30db\u30b9\u30c8\u540d
Frontend\ URL=\
@@ -0,0 +1 @@
Check if this server works on pseudo mode.
@@ -324,4 +324,23 @@ private void addNewServerByCopyingConfig(String newServerName, String fromServer
form.submit(null);
}

/**
* Test triggering job with events from a Gerrit server with Pseudo mode.
* @throws Exception Error creating job.
*/
@Test
public void testTriggeringFromGerritServersWithPseudoMode() throws Exception {
GerritServer gerritServerOne = new GerritServer(gerritServerOneName, true);
PluginImpl.getInstance().addServer(gerritServerOne);
gerritServerOne.start();
FreeStyleProject projectOne = DuplicatesUtil.createGerritTriggeredJob(this, projectOneName, gerritServerOneName);
PluginImpl.getInstance().getHandler().post(Setup.createPatchsetCreated(gerritServerOneName));
RunList<FreeStyleBuild> buildsOne = DuplicatesUtil.waitForBuilds(projectOne, 1, timeToBuild);

FreeStyleBuild buildOne = buildsOne.get(0);
assertSame(Result.SUCCESS, buildOne.getResult());
assertEquals(1, projectOne.getBuilds().size());
assertSame(gerritServerOneName, buildOne.getCause(GerritCause.class).getEvent().getProvider().getName());
}

}

0 comments on commit 0e735d6

Please sign in to comment.
You can’t perform that action at this time.