]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6732 IssueIndex compatible with CE's container (no UserSession)
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 14 Mar 2016 09:28:33 +0000 (10:28 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 21 Mar 2016 15:44:04 +0000 (16:44 +0100)
server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java

index ae7d8934a4912b53d5d6026ff747f6f928301c72..a0123c174fc9d46b190349e08e3a9470ec66cd40 100644 (file)
@@ -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<IssueDoc> 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<IssueDoc> 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<IssueDoc> 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");
+  }
 }