Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagnostics pages #275

Merged
merged 11 commits into from
Nov 11, 2016
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@
<version>2.2</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>support-core</artifactId>
<version>2.31</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,28 @@
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger;

import com.sonymobile.tools.gerrit.gerritevents.GerritSendCommandQueue;
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.PluginConfig;
import com.sonyericsson.hudson.plugins.gerrit.trigger.diagnostics.Diagnostics;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritAdministrativeMonitor;

import com.sonymobile.tools.gerrit.gerritevents.GerritSendCommandQueue;
import hudson.DescriptorExtensionList;
import hudson.Extension;
import hudson.Functions;
import hudson.model.AdministrativeMonitor;
import hudson.model.AutoCompletionCandidates;
import hudson.model.Describable;
import hudson.model.Failure;
import hudson.model.Descriptor;
import hudson.model.Failure;
import hudson.model.Hudson;
import hudson.model.ManagementLink;
import hudson.model.Saveable;
import hudson.util.FormValidation;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.List;

import javax.annotation.CheckForNull;
import javax.servlet.ServletException;

import jenkins.model.Jenkins;

import jenkins.model.ModelObjectWithContextMenu;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.CharEncoding;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerProxy;
Expand All @@ -67,6 +55,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.CheckForNull;
import javax.servlet.ServletException;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.List;

import static com.sonyericsson.hudson.plugins.gerrit.trigger.utils.StringUtil.PLUGIN_IMAGES_URL;

/**
Expand All @@ -86,6 +82,8 @@ public class GerritManagement extends ManagementLink implements StaplerProxy, De

private static final Logger logger = LoggerFactory.getLogger(GerritManagement.class);

private static final Diagnostics DIAGNOSTICS = new Diagnostics();


@Override
public String getIconFileName() {
Expand Down Expand Up @@ -118,13 +116,28 @@ public ContextMenu doContextMenu(StaplerRequest request, StaplerResponse respons
assert jenkins != null;
ContextMenu menu = new ContextMenu();
menu.add("newServer", Functions.joinPath(jenkins.getRootUrl(), Functions.getResourcePath(),
"images", "24x24", "new-package.png"), Messages.AddNewServer());
"images", "24x24", "new-package.png"), Messages.AddNewServer());
for (GerritServer server : getServers()) {
menu.add(server);
}
MenuItem item = new MenuItem()
.withUrl("diagnostics")
.withDisplayName(DIAGNOSTICS)
.withStockIcon("folder.png");
item.subMenu = DIAGNOSTICS.getContextMenu("diagnostics");
menu.add(item);
return menu;
}

/**
* A sub page displaying some diagnostic views.
*
* @return the diagnostics page.
*/
public Diagnostics getDiagnostics() {
return DIAGNOSTICS;
}

/**
* Descriptor is only used for UI form bindings.
*/
Expand Down Expand Up @@ -170,10 +183,10 @@ public AutoCompletionCandidates doAutoCompleteCopyNewItemFrom(@QueryParameter fi
}

/**
* Gets the list of GerritServer.
*
* @return the list of GerritServer.
*/
* Gets the list of GerritServer.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Darn! I never asked for these to be changed. must have been a slip when I optimized the imports.

*
* @return the list of GerritServer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to go in and add a single additional space to all of these?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, see my comment above.

*/
@SuppressWarnings("unused") //Called from Jelly
public List<GerritServer> getServers() {
PluginImpl plugin = PluginImpl.getInstance();
Expand All @@ -184,10 +197,11 @@ public List<GerritServer> getServers() {
}

/**
* Used when redirected to a server.
* @param encodedServerName the server name encoded by URLEncoder.encode(name,"UTF-8").
* @return the GerritServer object.
*/
* Used when redirected to a server.
*
* @param encodedServerName the server name encoded by URLEncoder.encode(name,"UTF-8").
* @return the GerritServer object.
*/
@SuppressWarnings("unused") //Called from Jelly
public GerritServer getServer(String encodedServerName) {
String serverName;
Expand Down Expand Up @@ -242,8 +256,8 @@ public JSONObject getServerStatuses() {
*
* @param req the StaplerRequest
* @param rsp the StaplerResponse
* @throws IOException when error sending redirect back to the list of servers
* @return the new GerritServer
* @throws IOException when error sending redirect back to the list of servers
*/
public GerritServer doAddNewServer(StaplerRequest req, StaplerResponse rsp) throws IOException {
String serverName = req.getParameter("name");
Expand Down Expand Up @@ -281,7 +295,11 @@ public GerritServer doAddNewServer(StaplerRequest req, StaplerResponse rsp) thro

@Override
public Object getTarget() {
Hudson.getInstance().checkPermission(Jenkins.ADMINISTER);
Jenkins jenkins = Jenkins.getInstance();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we switch from Hudson instance to Jenkins here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inherital sin

if (jenkins == null) {
throw new IllegalStateException("Jenkins is not alive.");
}
jenkins.checkPermission(Jenkins.ADMINISTER);
return this;
}

Expand All @@ -292,6 +310,7 @@ public void save() throws IOException {

/**
* Returns this singleton.
*
* @return the single loaded instance if this class.
*/
public static GerritManagement get() {
Expand Down Expand Up @@ -381,13 +400,13 @@ public FormValidation doNameFreeCheck(@QueryParameter("value") final String valu
*
* @param req StaplerRequest
* @param rsp StaplerResponse
* @throws ServletException if something unfortunate happens.
* @throws IOException if something unfortunate happens.
* @throws ServletException if something unfortunate happens.
* @throws IOException if something unfortunate happens.
* @throws InterruptedException if something unfortunate happens.
*/
public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws ServletException,
IOException,
InterruptedException {
IOException,
InterruptedException {
if (logger.isDebugEnabled()) {
logger.debug("submit {}", req.toString());
}
Expand All @@ -402,4 +421,5 @@ public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws Servl

rsp.sendRedirect(".");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger;

import com.sonyericsson.hudson.plugins.gerrit.trigger.utils.StringUtil;
import com.sonymobile.tools.gerrit.gerritevents.ConnectionListener;
import com.sonymobile.tools.gerrit.gerritevents.GerritEventListener;
import com.sonymobile.tools.gerrit.gerritevents.dto.GerritEvent;
Expand All @@ -49,7 +50,7 @@
*
* @author Gustaf Lundh &lt;Gustaf.Lundh@sonyericsson.com&gt;
*/
public class GerritProjectListUpdater extends Thread implements ConnectionListener, GerritEventListener {
public class GerritProjectListUpdater extends Thread implements ConnectionListener, NamedGerritEventListener {
/**
* The command for fetching projects.
*/
Expand All @@ -73,6 +74,15 @@ public GerritProjectListUpdater(String serverName) {
addThisAsListener();
}

/**
* The name of the {@link GerritServer} this listener is working for.
*
* @return the {@link GerritServer#getName()}.
*/
public String getServerName() {
return serverName;
}

/**
* Add the current list updater as a listener to the GerritServer object.
*/
Expand Down Expand Up @@ -325,5 +335,10 @@ public synchronized List<String> getGerritProjects() {
}
return gerritProjects;
}

@Override
public String getDisplayName() {
return StringUtil.getDefaultDisplayNameForSpecificServer(this, getServerName());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,18 @@ public GerritServer(String name, boolean noConnectionOnStartup) {
config = new Config();
}

/**
* If the parameter represents {@link #ANY_SERVER}.
* I.e. if serverName is null or empty or equal to {@link #ANY_SERVER}.
*
* @param serverName the String to test
* @return true if so.
* @see GerritTrigger#isAnyServer()
*/
public static boolean isAnyServer(String serverName) {
return serverName == null || serverName.isEmpty() || ANY_SERVER.equals(serverName);
}

/**
* Gets the global config of this server.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 CloudBees Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.sonyericsson.hudson.plugins.gerrit.trigger;

import com.sonymobile.tools.gerrit.gerritevents.GerritEventListener;

/**
* A {@link GerritEventListener} that can provide a display name.
* Nice to have when displaying this on {@link com.sonyericsson.hudson.plugins.gerrit.trigger.diagnostics.Diagnostics}.
*
* @author Robert Sandell &lt;rsandell@cloudbees.com&gt;.
*/
public interface NamedGerritEventListener extends GerritEventListener {

/**
* A possible more descriptive display name than the class name.
*
* @return the name
*/
String getDisplayName(); //TODO Replace with a default method in GerritEventListener when moving to Java 8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this allowed/intended to return null? This one would be very helpful to add the appropriate Findbugs annotation to.

}
Loading