From: Sébastien Lesaint Date: Mon, 14 Mar 2016 09:28:33 +0000 (+0100) Subject: SONAR-6732 IssueIndex compatible with CE's container (no UserSession) X-Git-Tag: 5.5-M11~77 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b57514cd55b1c785edf55f0603f00f544a87dd75;p=sonarqube.git SONAR-6732 IssueIndex compatible with CE's container (no UserSession) --- 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"); + } }