]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8061 speed up add issue tag removing duplicate SQ 2148/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 5 Jun 2017 15:30:35 +0000 (17:30 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 9 Jun 2017 08:09:44 +0000 (10:09 +0200)
server/sonar-server/src/main/java/org/sonar/server/issue/ws/OperationResponseWriter.java
server/sonar-server/src/main/java/org/sonar/server/issue/ws/SetTagsAction.java
server/sonar-server/src/test/java/org/sonar/server/issue/ws/SetTagsActionTest.java

index 0365afabd47a9e0ee047c198af8fe49d21b60f91..87792a8248cd4e7ca1b79093a62905d008e81616 100644 (file)
@@ -24,7 +24,7 @@ import org.sonar.api.server.ws.Response;
 import org.sonar.server.ws.WsUtils;
 import org.sonarqube.ws.Issues;
 
-import static java.util.Collections.singleton;
+import static java.util.Collections.singletonList;
 import static org.sonar.server.issue.ws.SearchAdditionalField.ALL_ADDITIONAL_FIELDS;
 
 public class OperationResponseWriter {
@@ -37,17 +37,8 @@ public class OperationResponseWriter {
     this.format = format;
   }
 
-  public void write(String issueKey, Request request, Response response) {
-    SearchResponseLoader.Collector collector = new SearchResponseLoader.Collector(ALL_ADDITIONAL_FIELDS, singleton(issueKey));
-    SearchResponseData data = loader.load(collector, null);
-
-    Issues.Operation responseBody = format.formatOperation(data);
-
-    WsUtils.writeProtobuf(responseBody, request, response);
-  }
-
   public void write(String issueKey, SearchResponseData preloadedResponseData, Request request, Response response) {
-    SearchResponseLoader.Collector collector = new SearchResponseLoader.Collector(ALL_ADDITIONAL_FIELDS, singleton(issueKey));
+    SearchResponseLoader.Collector collector = new SearchResponseLoader.Collector(ALL_ADDITIONAL_FIELDS, singletonList(issueKey));
     SearchResponseData data = loader.load(preloadedResponseData, collector, null);
 
     Issues.Operation responseBody = format.formatOperation(data);
index 0df9128e171ea7164c9915d3b15caddf5b7f515d..79b08f695d4b55d0089efb9193dccccc31dc674e 100644 (file)
@@ -34,6 +34,7 @@ import org.sonar.core.issue.IssueChangeContext;
 import org.sonar.core.util.Uuids;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.issue.IssueDto;
 import org.sonar.server.issue.IssueFieldsSetter;
 import org.sonar.server.issue.IssueFinder;
 import org.sonar.server.issue.IssueUpdater;
@@ -93,18 +94,20 @@ public class SetTagsAction implements IssuesWsAction {
   public void handle(Request request, Response response) throws Exception {
     String key = request.mandatoryParam(PARAM_ISSUE);
     List<String> tags = MoreObjects.firstNonNull(request.paramAsStrings(PARAM_TAGS), Collections.<String>emptyList());
-    setTags(key, tags);
-    responseWriter.write(key, request, response);
+    SearchResponseData preloadedSearchResponseData = setTags(key, tags);
+    responseWriter.write(key, preloadedSearchResponseData, request, response);
   }
 
-  private void setTags(String issueKey, List<String> tags) {
+  private SearchResponseData setTags(String issueKey, List<String> tags) {
     userSession.checkLoggedIn();
     try (DbSession session = dbClient.openSession(false)) {
-      DefaultIssue issue = issueFinder.getByKey(session, issueKey).toDefaultIssue();
+      IssueDto issueDto = issueFinder.getByKey(session, issueKey);
+      DefaultIssue issue = issueDto.toDefaultIssue();
       IssueChangeContext context = IssueChangeContext.createUser(new Date(), userSession.getLogin());
       if (issueFieldsSetter.setTags(issue, tags, context)) {
-        issueUpdater.saveIssue(session, issue, context, null);
+        return issueUpdater.saveIssueAndPreloadSearchResponseData(session, issue, context, null);
       }
+      return new SearchResponseData(issueDto);
     }
   }
 
index 957b757fccf87c8c2586b825449c3a1aba00e256..12220f3320343253ef7d29edb50910bb49b42abb 100644 (file)
@@ -26,6 +26,7 @@ import javax.annotation.Nullable;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.mockito.ArgumentCaptor;
 import org.sonar.api.config.MapSettings;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
@@ -85,6 +86,7 @@ public class SetTagsActionTest {
   private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
   private OperationResponseWriter responseWriter = mock(OperationResponseWriter.class);
   private IssueIndexer issueIndexer = new IssueIndexer(esTester.client(), new IssueIteratorFactory(dbClient));
+  private ArgumentCaptor<SearchResponseData> preloadedSearchResponseDataCaptor = ArgumentCaptor.forClass(SearchResponseData.class);
 
   private WsActionTester ws = new WsActionTester(new SetTagsAction(userSession, dbClient, new IssueFinder(dbClient, userSession), new IssueFieldsSetter(),
     new IssueUpdater(dbClient,
@@ -98,7 +100,8 @@ public class SetTagsActionTest {
 
     call(issueDto.getKey(), "bug", "todo");
 
-    verify(responseWriter).write(eq(issueDto.getKey()), any(Request.class), any(Response.class));
+    verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
+    verifyContentOfPreloadedSearchResponseData(issueDto);
     IssueDto issueReloaded = dbClient.issueDao().selectByKey(db.getSession(), issueDto.getKey()).get();
     assertThat(issueReloaded.getTags()).containsOnly("bug", "todo");
   }
@@ -246,4 +249,17 @@ public class SetTagsActionTest {
     userSession.logIn("john").addProjectPermission(permission, dbClient.componentDao().selectByUuid(db.getSession(), issueDto.getProjectUuid()).get());
   }
 
+  private void verifyContentOfPreloadedSearchResponseData(IssueDto issue) {
+    SearchResponseData preloadedSearchResponseData = preloadedSearchResponseDataCaptor.getValue();
+    assertThat(preloadedSearchResponseData.getIssues())
+      .extracting(IssueDto::getKey)
+      .containsOnly(issue.getKey());
+    assertThat(preloadedSearchResponseData.getRules())
+      .extracting(RuleDefinitionDto::getKey)
+      .containsOnly(issue.getRuleKey());
+    assertThat(preloadedSearchResponseData.getComponents())
+      .extracting(ComponentDto::uuid)
+      .containsOnly(issue.getComponentUuid(), issue.getProjectUuid());
+  }
+
 }