Skip to content

Commit

Permalink
Merge pull request #267 from MCMicS/bug/84
Browse files Browse the repository at this point in the history
Improve Connection Error handling
  • Loading branch information
MCMicS authored Feb 28, 2021
2 parents b95fb75 + 73afa5d commit 04b774e
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 52 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Version 0.13.11
*
* #84 Handle connection exceptions

Version 0.13.10
* #253 [Feature Request] Log in progress
Expand Down
6 changes: 5 additions & 1 deletion includes/pluginChanges.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<h3>0.13.11</h3>
<ul>
<li></li>
<li>#84 Handle connection exceptions<br>
RSS change: Use Notification system. First failed build will shown as warning in Notification Group 'Jenkins Notification'
instead as balloon over Build Status Summary widget.
</li>

</ul>
<h3>0.13.10</h3>
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

package org.codinjutsu.tools.jenkins.security;
package org.codinjutsu.tools.jenkins.exception;

public class AuthenticationException extends RuntimeException {
public class AuthenticationException extends JenkinsPluginRuntimeException {

private final String responseBody;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.jetbrains.annotations.NotNull;

public class ConfigurationException extends RuntimeException {
public class ConfigurationException extends JenkinsPluginRuntimeException {

public ConfigurationException(String message) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.codinjutsu.tools.jenkins.exception;

public class JenkinsPluginRuntimeException extends RuntimeException {

public JenkinsPluginRuntimeException(String message) {
super(message);
}

public JenkinsPluginRuntimeException(String s, Throwable throwable) {
super(s, throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import java.text.MessageFormat;

public class NoJobFoundException extends RuntimeException {
public class NoJobFoundException extends JenkinsPluginRuntimeException {

public NoJobFoundException(@NotNull Job job) {
super(createMessage(job));
Expand Down
39 changes: 15 additions & 24 deletions src/main/java/org/codinjutsu/tools/jenkins/logic/RssLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,15 @@
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.BalloonBuilder;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.ui.awt.RelativePoint;
import org.codinjutsu.tools.jenkins.JenkinsAppSettings;
import org.codinjutsu.tools.jenkins.JobTracker;
import org.codinjutsu.tools.jenkins.exception.ConfigurationException;
import org.codinjutsu.tools.jenkins.exception.JenkinsPluginRuntimeException;
import org.codinjutsu.tools.jenkins.model.Build;
import org.codinjutsu.tools.jenkins.model.BuildStatusEnum;
import org.codinjutsu.tools.jenkins.util.GuiUtil;
import org.codinjutsu.tools.jenkins.view.JenkinsWidget;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.*;
import java.util.List;
import java.util.*;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
Expand Down Expand Up @@ -85,6 +78,7 @@ public void initScheduledJobs() {
}
}

@SuppressWarnings({"java:S3824", "java:S3398"})
private Map<String, Build> loadAndReturnNewLatestBuilds() {
final Map<String, Build> latestBuildMap = requestManager.loadJenkinsRssLatestBuilds(jenkinsAppSettings);
final Map<String, Build> newBuildMap = new HashMap<>();
Expand All @@ -106,6 +100,7 @@ private Map<String, Build> loadAndReturnNewLatestBuilds() {
return newBuildMap;
}

@SuppressWarnings("java:S3398")
private void sendNotificationForEachBuild(List<Build> buildToSortByDateDescending) {
for (Build build : buildToSortByDateDescending) {
BuildStatusEnum status = build.getStatus();
Expand All @@ -118,35 +113,33 @@ private void sendNotificationForEachBuild(List<Build> buildToSortByDateDescendin
notificationType = NotificationType.WARNING;
}
NotificationGroupManager.getInstance().getNotificationGroup("Jenkins Rss")
.createNotification("", buildMessage(build), notificationType, NotificationListener.URL_OPENING_LISTENER)
.createNotification("", buildMessage(build), notificationType,
NotificationListener.URL_OPENING_LISTENER)
.notify(project);
}
}

@SuppressWarnings("java:S3398")
private List<Build> sortByDateDescending(Map<String, Build> finishedBuilds) {
final List<Build> buildToSortByDateDescending = new ArrayList<>(finishedBuilds.values());

buildToSortByDateDescending.sort(Comparator.comparing(Build::getBuildDate));
return buildToSortByDateDescending;
}

private void displayTheFirstFailedBuildInABalloon(Map.Entry<String, Build> firstFailedBuild) {
@SuppressWarnings("java:S3398")
private void notifyFirstFailedBuild(Map.Entry<String, Build> firstFailedBuild) {
if (firstFailedBuild != null) {
final String jobName = firstFailedBuild.getKey();
final Build build = firstFailedBuild.getValue();
final String message = Optional.ofNullable(build.getFullDisplayName())
.orElseGet(() -> jobName + build.getDisplayNumber()) + ": FAILED";
displayErrorMessageInABalloon(message);
JenkinsNotifier.getInstance(project).notify(message, NotificationType.WARNING);
}
}

private void displayErrorMessageInABalloon(String message) {
BalloonBuilder balloonBuilder = JBPopupFactory.getInstance().createHtmlTextBalloonBuilder(message, MessageType.ERROR, null);
final Balloon balloon = balloonBuilder.setFadeoutTime(TimeUnit.SECONDS.toMillis(1)).createBalloon();
GuiUtil.runInSwingThread(() -> balloon.show(new RelativePoint(JenkinsWidget.getInstance(project).getComponent(),
new Point(0, 0)), Balloon.Position.above));
}

@SuppressWarnings("java:S3398")
@Nullable
private Map.Entry<String, Build> getFirstFailedBuild(Map<String, Build> finishedBuilds) {
for (Map.Entry<String, Build> buildByJobName : finishedBuilds.entrySet()) {
Build build = buildByJobName.getValue();
Expand Down Expand Up @@ -186,19 +179,17 @@ public void run(@NotNull ProgressIndicator indicator) {
final Map<String, Build> finishedBuilds;
try {
finishedBuilds = loadAndReturnNewLatestBuilds();
} catch (ConfigurationException ex) {
displayErrorMessageInABalloon(ex.getMessage());
} catch (JenkinsPluginRuntimeException ex) {
JenkinsNotifier.getInstance(project).error(ex.getMessage());
return;
}
if (!shouldDisplayResult || finishedBuilds.isEmpty()) {
return;
}

JobTracker.getInstance().onNewFinishedBuilds(finishedBuilds);

sendNotificationForEachBuild(sortByDateDescending(finishedBuilds));

displayTheFirstFailedBuildInABalloon(getFirstFailedBuild(finishedBuilds));
notifyFirstFailedBuild(getFirstFailedBuild(finishedBuilds));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHeaders;
import org.codinjutsu.tools.jenkins.exception.AuthenticationException;
import org.codinjutsu.tools.jenkins.exception.ConfigurationException;
import org.codinjutsu.tools.jenkins.util.IOUtils;
import org.jetbrains.annotations.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHeaders;
import org.codinjutsu.tools.jenkins.exception.AuthenticationException;
import org.codinjutsu.tools.jenkins.exception.ConfigurationException;
import org.codinjutsu.tools.jenkins.model.FileParameter;
import org.codinjutsu.tools.jenkins.model.RequestData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.codinjutsu.tools.jenkins.JenkinsSettings;
import org.codinjutsu.tools.jenkins.JenkinsTree;
import org.codinjutsu.tools.jenkins.JenkinsTreeState;
import org.codinjutsu.tools.jenkins.exception.ConfigurationException;
import org.codinjutsu.tools.jenkins.exception.JenkinsPluginRuntimeException;
import org.codinjutsu.tools.jenkins.logic.*;
import org.codinjutsu.tools.jenkins.model.*;
import org.codinjutsu.tools.jenkins.util.CollectionUtil;
Expand Down Expand Up @@ -476,7 +476,7 @@ public void run(@NotNull ProgressIndicator indicator) {
}
currentSelectedView = viewToLoad;
loadJobs();
} catch (ConfigurationException ex) {
} catch (JenkinsPluginRuntimeException ex) {
notifyErrorJenkinsToolWindow(ex.getMessage());
} finally {
setTreeBusy(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import org.apache.commons.lang.StringUtils;
import org.codinjutsu.tools.jenkins.JenkinsAppSettings;
import org.codinjutsu.tools.jenkins.JenkinsSettings;
import org.codinjutsu.tools.jenkins.exception.AuthenticationException;
import org.codinjutsu.tools.jenkins.exception.ConfigurationException;
import org.codinjutsu.tools.jenkins.logic.ConfigurationValidator;
import org.codinjutsu.tools.jenkins.logic.RequestManager;
import org.codinjutsu.tools.jenkins.security.AuthenticationException;
import org.codinjutsu.tools.jenkins.security.JenkinsVersion;
import org.codinjutsu.tools.jenkins.util.GuiUtil;
import org.codinjutsu.tools.jenkins.view.annotation.FormValidator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.intellij.openapi.wm.ToolWindowManager;
import com.offbytwo.jenkins.helper.BuildConsoleStreamListener;
import lombok.Value;
import org.codinjutsu.tools.jenkins.exception.NoJobFoundException;
import org.codinjutsu.tools.jenkins.exception.JenkinsPluginRuntimeException;
import org.codinjutsu.tools.jenkins.logic.RequestManager;
import org.codinjutsu.tools.jenkins.model.Build;
import org.codinjutsu.tools.jenkins.model.BuildType;
Expand Down Expand Up @@ -60,7 +60,7 @@ public void run(@NotNull ProgressIndicator indicator) {
try {
final RequestManager requestManager = browserPanel.getJenkinsManager();
requestManager.loadConsoleTextFor(job, buildType, processHandler);
} catch (NoJobFoundException e) {
} catch (JenkinsPluginRuntimeException e) {
browserPanel.notifyErrorJenkinsToolWindow(e.getMessage());
}
}
Expand Down Expand Up @@ -91,9 +91,7 @@ private static JComponent createConsolePanel(ConsoleView view, ActionGroup actio
}

private static ActionToolbar createToolbar(ActionGroup actions) {
final ActionToolbar actionToolbar = ActionManager.getInstance().createActionToolbar(
ActionPlaces.UNKNOWN, actions, false);
return actionToolbar;
return ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, actions, false);
}

private void showInToolWindow(ShowLogConsoleView showLogConsoleView, String tabName) {
Expand Down Expand Up @@ -161,7 +159,6 @@ public ShowLogRunDescriptor(@Nullable ExecutionConsole executionConsole,
}

public void init() {
//setContentToolWindowId(TOOL_WINDOW_ID);
setActivateToolWindowWhenAdded(true);
setAutoFocusContent(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.offbytwo.jenkins.model.TestResult;
import com.offbytwo.jenkins.model.TestSuites;
import jetbrains.buildServer.messages.serviceMessages.TestFailed;
import org.codinjutsu.tools.jenkins.exception.NoJobFoundException;
import org.codinjutsu.tools.jenkins.exception.JenkinsPluginRuntimeException;
import org.codinjutsu.tools.jenkins.logic.RequestManager;
import org.codinjutsu.tools.jenkins.model.Job;
import org.codinjutsu.tools.jenkins.view.BrowserPanel;
Expand Down Expand Up @@ -43,7 +43,7 @@ class JobTestResultsHandler {
: Arrays.stream(clazz.getMethods())
.filter(m -> method.equals(m.getName()))
.findFirst()
.map(m -> (PsiElement) m)
.map(PsiElement.class::cast)
.orElse(clazz);

return clazz == null ? Collections.emptyList() : Collections.singletonList(new PsiLocation<>(element));
Expand All @@ -56,7 +56,7 @@ void handle() {
try {
List<TestResult> testResults = RequestManager.getInstance(project).loadTestResultsFor(job);
testResults.forEach(this::handleTestResult);
} catch (NoJobFoundException e) {
} catch (JenkinsPluginRuntimeException e) {
browserPanel.notifyErrorJenkinsToolWindow(e.getMessage());
}
testEventsProcessor.onFinishTesting();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@

import org.codinjutsu.tools.jenkins.JenkinsAppSettings;
import org.codinjutsu.tools.jenkins.JenkinsSettings;
import org.codinjutsu.tools.jenkins.exception.ConfigurationException;
import org.codinjutsu.tools.jenkins.logic.RequestManager;
import org.codinjutsu.tools.jenkins.security.AuthenticationException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.codinjutsu.tools.jenkins.JenkinsAppSettings.DEFAULT_BUILD_DELAY;
import static org.codinjutsu.tools.jenkins.JenkinsAppSettings.DUMMY_JENKINS_SERVER_URL;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doThrow;

public class ConfigurationPanelTest {

Expand Down

0 comments on commit 04b774e

Please sign in to comment.