Skip to content

Commit

Permalink
Merge pull request #89 from halkeye/add-new-exception-for-jira-compon…
Browse files Browse the repository at this point in the history
…ent-mismatch

Add a new exception type specifically for jira component name not found exceptions
  • Loading branch information
halkeye committed Jun 13, 2020
2 parents 25512da + 5d25c07 commit 290e611
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Expand Up @@ -166,11 +166,6 @@
<artifactId>sentry-logback</artifactId>
<version>${sentry.version}</version>
</dependency>
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-spring</artifactId>
<version>${sentry.version}</version>
</dependency>
</dependencies>

<repositories>
Expand Down
@@ -0,0 +1,36 @@
package io.jenkins.plugins.models;

import java.util.Objects;

public class MissingJiraComponentException extends Exception {
private final String pluginName;
private final String componentName;

public MissingJiraComponentException(String pluginName, String componentName) {
super(String.format("ComponentName(%s) and PluginName(%s) do not match.", componentName, pluginName));
this.pluginName = pluginName;
this.componentName = componentName;
}

public String getPluginName() {
return pluginName;
}

public String getComponentName() {
return componentName;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MissingJiraComponentException)) return false;
MissingJiraComponentException that = (MissingJiraComponentException) o;
return Objects.equals(pluginName, that.pluginName) && Objects.equals(componentName, that.componentName);
}

@Override
public int hashCode() {
return Objects.hash(pluginName, componentName);
}

}
@@ -1,9 +1,9 @@
package io.jenkins.plugins.services.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import io.jenkins.plugins.models.JiraIssue;
import io.jenkins.plugins.models.JiraIssues;
import io.jenkins.plugins.models.MissingJiraComponentException;
import io.jenkins.plugins.services.ConfigurationService;
import io.sentry.Sentry;
import org.apache.http.Header;
Expand Down Expand Up @@ -57,6 +57,7 @@ public JiraIssues getIssues(String pluginName) throws IOException {

public JiraIssues getIssues(String pluginName, int startAt) throws IOException {
int maxResults = 100;
Sentry.getContext().addExtra("plugin-name", pluginName);
String component = pluginName.replaceAll("-plugin$", "") + "-plugin";
JiraIssues jiraIssues = new JiraIssues();

Expand All @@ -73,7 +74,12 @@ public JiraIssues getIssues(String pluginName, int startAt) throws IOException {
JSONObject obj = new JSONObject(jsonInput);
if (obj.has("errorMessages")) {
logger.warn("[" + pluginName + "] JSON Response with error: " + jsonInput);
Sentry.capture(new Error(obj.getJSONArray("errorMessages").join("|")));
if (obj.getJSONArray("errorMessages").join("|").contains("\"The value '" + component + "' does not exist for the field 'component'.\"")) {
Sentry.capture(new MissingJiraComponentException(pluginName, component));
} else {
Sentry.capture(new Error(obj.getJSONArray("errorMessages").join("|")));
}

return jiraIssues;
}

Expand Down
@@ -0,0 +1,27 @@
package io.jenkins.plugins.models;

import org.junit.Test;

import static org.junit.Assert.*;

public class MissingJiraComponentExceptionTest {
@Test
public void testMessage() {
MissingJiraComponentException sot = new MissingJiraComponentException("plugin-git", "component-git");
assertEquals(
"ComponentName(component-git) and PluginName(plugin-git) do not match.",
sot.getMessage()
);
}

@Test
public void testEquals() {
MissingJiraComponentException sot1 = new MissingJiraComponentException("plugin-git", "component-git");
MissingJiraComponentException sot2 = new MissingJiraComponentException("plugin-git", "component-git");
assertEquals(
sot1,
sot2
);
}

}

0 comments on commit 290e611

Please sign in to comment.