From b57514cd55b1c785edf55f0603f00f544a87dd75 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 14 Mar 2016 10:28:33 +0100 Subject: [PATCH] SONAR-6732 IssueIndex compatible with CE's container (no UserSession) --- .../sonar/server/issue/index/IssueIndex.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java index ae7d8934a49..a0123c174fc 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java @@ -82,6 +82,7 @@ import org.sonar.server.user.UserSession; import org.sonar.server.view.index.ViewIndexDefinition; import static com.google.common.collect.Lists.newArrayList; +import static java.util.Objects.requireNonNull; import static org.sonarqube.ws.client.issue.IssueFilterParameters.ACTION_PLANS; import static org.sonarqube.ws.client.issue.IssueFilterParameters.ASSIGNEES; import static org.sonarqube.ws.client.issue.IssueFilterParameters.AUTHORS; @@ -156,9 +157,17 @@ public class IssueIndex extends BaseIndex { private final Sorting sorting; private final System2 system; + @CheckForNull private final UserSession userSession; - public IssueIndex(EsClient client, System2 system, UserSession userSession) { + /** + * Constructor for Pico when no UserSession is available such as in the Compute Engine. + */ + public IssueIndex(EsClient client, System2 system) { + this(client, system, null); + } + + public IssueIndex(EsClient client, System2 system, @Nullable UserSession userSession) { super(client); this.system = system; @@ -187,6 +196,8 @@ public class IssueIndex extends BaseIndex { */ @CheckForNull public IssueDoc getNullableByKey(String key) { + checkNotCalledFromCE(); + SearchResult result = search(IssueQuery.builder(userSession).issueKeys(newArrayList(key)).build(), new SearchOptions()); if (result.getTotal() == 1) { return result.getDocs().get(0); @@ -207,6 +218,8 @@ public class IssueIndex extends BaseIndex { } public SearchResult search(IssueQuery query, SearchOptions options) { + checkNotCalledFromCE(); + SearchRequestBuilder requestBuilder = getClient() .prepareSearch(IssueIndexDefinition.INDEX) .setTypes(IssueIndexDefinition.TYPE_ISSUE); @@ -756,6 +769,8 @@ public class IssueIndex extends BaseIndex { * Only fields needed for the batch are returned. */ public Iterator selectIssuesForBatch(ComponentDto component) { + checkNotCalledFromCE(); + BoolFilterBuilder filter = FilterBuilders.boolFilter() .must(createAuthorizationFilter(true, userSession.getLogin(), userSession.getUserGroups())) .mustNot(FilterBuilders.termsFilter(IssueIndexDefinition.FIELD_ISSUE_STATUS, Issue.STATUS_CLOSED)); @@ -789,4 +804,8 @@ public class IssueIndex extends BaseIndex { return EsUtils.scroll(getClient(), response.getScrollId(), DOC_CONVERTER); } + + private void checkNotCalledFromCE() { + requireNonNull(userSession, "getNullableByKey can not be used from the Compute Engine"); + } } -- 2.39.5