Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #61 from edef/JENKINS-30408
Browse files Browse the repository at this point in the history
[JENKINS-30408] Fixed URI composition for REST API calls
  • Loading branch information
Radosław Antoniuk committed Sep 14, 2015
2 parents 00e439d + 89f80ba commit 2aefe93
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/main/java/hudson/plugins/jira/JiraRestService.java
Expand Up @@ -50,6 +50,12 @@ public class JiraRestService {

public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormat.forPattern("yyyy-MM-dd");

/**
* Base URI path for a REST API call. It must be relative to site's base
* URI.
*/
public static final String BASE_API_PATH = "rest/api/2";

private final URI uri;

private final JiraRestClient jiraRestClient;
Expand All @@ -58,6 +64,8 @@ public class JiraRestService {

private final String authHeader;

private final String baseApiPath;

public JiraRestService(URI uri, JiraRestClient jiraRestClient, String username, String password) {
this.uri = uri;
this.objectMapper = new ObjectMapper();
Expand All @@ -70,12 +78,24 @@ public JiraRestService(URI uri, JiraRestClient jiraRestClient, String username,
throw new RuntimeException("failed to encode username:password using Base64");
}
this.jiraRestClient = jiraRestClient;

final StringBuilder builder = new StringBuilder();
if (uri.getPath() != null) {
builder.append(uri.getPath());
if (!uri.getPath().endsWith("/")) {
builder.append('/');
}
} else {
builder.append('/');
}
builder.append(BASE_API_PATH);
baseApiPath = builder.toString();
}

public void addComment(String issueId, String commentBody,
String groupVisibility, String roleVisibility) {
final URIBuilder builder = new URIBuilder(uri)
.setPath(String.format("/rest/api/2/issue/%s/comment", issueId));
.setPath(String.format("%s/issue/%s/comment", baseApiPath, issueId));

final Comment comment;
if (StringUtils.isNotBlank(groupVisibility)) {
Expand Down Expand Up @@ -139,7 +159,7 @@ public List<Issue> getIssuesFromJqlSearch(String jqlSearch, Integer maxResults)

public List<Version> getVersions(String projectKey) {
final URIBuilder builder = new URIBuilder(uri)
.setPath(String.format("/rest/api/2/project/%s/versions", projectKey));
.setPath(String.format("%s/project/%s/versions", baseApiPath, projectKey));

List<Map<String, Object>> decoded = Collections.emptyList();
try {
Expand Down Expand Up @@ -179,7 +199,7 @@ public Version addVersion(String projectKey, String versionName) {

public void releaseVersion(String projectKey, Version version) {
final URIBuilder builder = new URIBuilder(uri)
.setPath(String.format("/rest/api/2/version/%s", version.getId()));
.setPath(String.format("%s/version/%s", baseApiPath, version.getId()));

final VersionInput versionInput = new VersionInput(projectKey, version.getName(), version.getDescription(), version
.getReleaseDate(), version.isArchived(), version.isReleased());
Expand Down Expand Up @@ -266,7 +286,7 @@ public List<Status> getStatuses() {

public List<Component> getComponents(String projectKey) {
final URIBuilder builder = new URIBuilder(uri)
.setPath(String.format("/rest/api/2/project/%s/components", projectKey));
.setPath(String.format("%s/project/%s/components", baseApiPath, projectKey));

try {
final Content content = buildGetRequest(builder.build()).execute().returnContent();
Expand Down Expand Up @@ -306,4 +326,7 @@ private Request buildGetRequest(URI uri) {
.addHeader("Content-Type", "application/json");
}

public String getBaseApiPath() {
return baseApiPath;
}
}
28 changes: 28 additions & 0 deletions src/test/java/hudson/plugins/jira/JiraRestServiceTest.java
@@ -0,0 +1,28 @@
package hudson.plugins.jira;

import static org.junit.Assert.*;

import java.net.URI;

import org.junit.Test;

public class JiraRestServiceTest {

@Test
public void testBaseApiPath01() {
URI uri = URI.create("http://example.com:8080/");
JiraRestService service = new JiraRestService(uri, null, "user",
"password");
assertEquals("/" + JiraRestService.BASE_API_PATH,
service.getBaseApiPath());
}

@Test
public void testBaseApiPath02() {
URI uri = URI.create("https://example.com/path/to/jira");
JiraRestService service = new JiraRestService(uri, null, "user",
"password");
assertEquals("/path/to/jira/" + JiraRestService.BASE_API_PATH,
service.getBaseApiPath());
}
}

0 comments on commit 2aefe93

Please sign in to comment.