From 2fccc872686dc34ce71a45692a04ad4060a14417 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Tue, 19 Jan 2016 14:50:57 +0300 Subject: [PATCH] ZD-611794 Add missing API access key in request for single issue in Redmine --- .../tasks/redmine/RedmineRepository.java | 26 +++++++++---------- .../integration/RedmineIntegrationTest.java | 14 ++++++++++ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/plugins/tasks/tasks-core/src/com/intellij/tasks/redmine/RedmineRepository.java b/plugins/tasks/tasks-core/src/com/intellij/tasks/redmine/RedmineRepository.java index c74e76285896..0a4d5273f37d 100644 --- a/plugins/tasks/tasks-core/src/com/intellij/tasks/redmine/RedmineRepository.java +++ b/plugins/tasks/tasks-core/src/com/intellij/tasks/redmine/RedmineRepository.java @@ -125,10 +125,7 @@ public class RedmineRepository extends NewBaseRepositoryImpl { // /users/current.json. Unfortunately this endpoint may be unavailable on some old servers (see IDEA-122845) // and in this case we have to come back to requesting issues in this case to test anything at all. - URIBuilder uriBuilder = new URIBuilder(getRestApiUrl("users", "current.json")); - if (isUseApiKeyAuthentication()) { - uriBuilder.addParameter("key", getAPIKey()); - } + URIBuilder uriBuilder = createUriBuilderWithApiKey("users", "current.json"); myCurrentRequest.setURI(uriBuilder.build()); HttpClient client = getHttpClient(); @@ -182,7 +179,7 @@ public class RedmineRepository extends NewBaseRepositoryImpl { } private URI getIssuesUrl(int offset, int limit, boolean withClosed) throws URISyntaxException { - URIBuilder builder = new URIBuilder(getRestApiUrl("issues.json")) + URIBuilder builder = createUriBuilderWithApiKey("issues.json") .addParameter("offset", String.valueOf(offset)) .addParameter("limit", String.valueOf(limit)) .addParameter("sort", "updated_on:desc") @@ -194,9 +191,6 @@ public class RedmineRepository extends NewBaseRepositoryImpl { if (myCurrentProject != null && myCurrentProject != UNSPECIFIED_PROJECT) { builder.addParameter("project_id", String.valueOf(myCurrentProject.getId())); } - if (isUseApiKeyAuthentication()) { - builder.addParameter("key", myAPIKey); - } return builder.build(); } @@ -221,12 +215,9 @@ public class RedmineRepository extends NewBaseRepositoryImpl { @NotNull private URI getProjectsUrl(int offset, int limit) throws URISyntaxException { - URIBuilder builder = new URIBuilder(getRestApiUrl("projects.json")); + URIBuilder builder = createUriBuilderWithApiKey("projects.json"); builder.addParameter("offset", String.valueOf(offset)); builder.addParameter("limit", String.valueOf(limit)); - if (isUseApiKeyAuthentication()) { - builder.addParameter("key", myAPIKey); - } return builder.build(); } @@ -234,7 +225,7 @@ public class RedmineRepository extends NewBaseRepositoryImpl { @Override public Task findTask(@NotNull String id) throws Exception { ensureProjectsDiscovered(); - HttpGet method = new HttpGet(getRestApiUrl("issues", id + ".json")); + HttpGet method = new HttpGet(createUriBuilderWithApiKey("issues", id + ".json").build()); IssueWrapper wrapper = getHttpClient().execute(method, new GsonSingleObjectDeserializer(GSON, IssueWrapper.class, true)); if (wrapper == null) { return null; @@ -262,6 +253,15 @@ public class RedmineRepository extends NewBaseRepositoryImpl { return !isUseHttpAuthentication() && StringUtil.isNotEmpty(myAPIKey); } + @NotNull + private URIBuilder createUriBuilderWithApiKey(@NotNull Object... pathParts) throws URISyntaxException { + final URIBuilder builder = new URIBuilder(getRestApiUrl(pathParts)); + if (isUseApiKeyAuthentication()) { + builder.addParameter("key", myAPIKey); + } + return builder; + } + @Override public String getPresentableName() { String name = super.getPresentableName(); diff --git a/plugins/tasks/tasks-tests/test/com/intellij/tasks/integration/RedmineIntegrationTest.java b/plugins/tasks/tasks-tests/test/com/intellij/tasks/integration/RedmineIntegrationTest.java index a918c9bd3154..2cfcffb30e4d 100644 --- a/plugins/tasks/tasks-tests/test/com/intellij/tasks/integration/RedmineIntegrationTest.java +++ b/plugins/tasks/tasks-tests/test/com/intellij/tasks/integration/RedmineIntegrationTest.java @@ -18,6 +18,7 @@ import java.util.List; */ public class RedmineIntegrationTest extends TaskManagerTestCase { private static final String REDMINE_2_0_TEST_SERVER_URL = "http://trackers-tests.labs.intellij.net:8072"; + private static final String API_ACCESS_KEY = "b60d03b2449869ee1a4ba331011a32e50475f820"; private RedmineRepository myRepository; @@ -125,6 +126,19 @@ public class RedmineIntegrationTest extends TaskManagerTestCase { assertEquals(issues[0].getSummary(), "This issue was created for project filtering tests. Do not change it."); } + // ZD-611794 + public void testSingleIssueRequestedWithApiKey() throws Exception { + myRepository.setUseHttpAuthentication(false); + myRepository.setAPIKey(API_ACCESS_KEY); + try { + assertNotNull(myRepository.findTask("1")); + } + finally { + myRepository.setAPIKey(""); + myRepository.setUseHttpAuthentication(true); + } + } + @Override public void setUp() throws Exception { super.setUp();