You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CoveredFilesAction.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.test.ws;
  21. import com.google.common.base.Function;
  22. import com.google.common.collect.Lists;
  23. import com.google.common.collect.Maps;
  24. import com.google.common.io.Resources;
  25. import java.util.List;
  26. import java.util.Map;
  27. import javax.annotation.Nonnull;
  28. import org.sonar.api.server.ws.Change;
  29. import org.sonar.api.server.ws.Request;
  30. import org.sonar.api.server.ws.Response;
  31. import org.sonar.api.server.ws.WebService;
  32. import org.sonar.api.web.UserRole;
  33. import org.sonar.core.util.Uuids;
  34. import org.sonar.db.DbClient;
  35. import org.sonar.db.DbSession;
  36. import org.sonar.db.component.ComponentDto;
  37. import org.sonar.server.test.index.CoveredFileDoc;
  38. import org.sonar.server.test.index.TestDoc;
  39. import org.sonar.server.test.index.TestIndex;
  40. import org.sonar.server.user.UserSession;
  41. import org.sonarqube.ws.Tests;
  42. import static org.sonar.core.util.Protobuf.setNullable;
  43. import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
  44. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  45. public class CoveredFilesAction implements TestsWsAction {
  46. public static final String TEST_ID = "testId";
  47. private final DbClient dbClient;
  48. private final TestIndex index;
  49. private final UserSession userSession;
  50. public CoveredFilesAction(DbClient dbClient, TestIndex index, UserSession userSession) {
  51. this.dbClient = dbClient;
  52. this.index = index;
  53. this.userSession = userSession;
  54. }
  55. @Override
  56. public void define(WebService.NewController controller) {
  57. WebService.NewAction action = controller.createAction("covered_files")
  58. .setDescription("Get the list of source files covered by a test. Require Browse permission on test file's project")
  59. .setSince("4.4")
  60. .setResponseExample(Resources.getResource(getClass(), "tests-example-covered-files.json"))
  61. .setDeprecatedSince("5.6")
  62. .setHandler(this)
  63. .setChangelog(new Change("6.6", "\"branch\" field is now returned"))
  64. .addPagingParams(100);
  65. action
  66. .createParam(TEST_ID)
  67. .setRequired(true)
  68. .setDescription("Test ID")
  69. .setExampleValue(Uuids.UUID_EXAMPLE_01);
  70. }
  71. @Override
  72. public void handle(Request request, Response response) throws Exception {
  73. String testId = request.mandatoryParam(TEST_ID);
  74. TestDoc testDoc = checkFoundWithOptional(index.getNullableByTestUuid(testId), "Test with id '%s' is not found", testId);
  75. userSession.checkComponentUuidPermission(UserRole.CODEVIEWER, testDoc.fileUuid());
  76. List<CoveredFileDoc> coveredFiles = index.coveredFiles(testId);
  77. Map<String, ComponentDto> componentsByUuid = buildComponentsByUuid(coveredFiles);
  78. Tests.CoveredFilesResponse.Builder responseBuilder = Tests.CoveredFilesResponse.newBuilder();
  79. if (!coveredFiles.isEmpty()) {
  80. for (CoveredFileDoc doc : coveredFiles) {
  81. Tests.CoveredFilesResponse.CoveredFile.Builder fileBuilder = Tests.CoveredFilesResponse.CoveredFile.newBuilder();
  82. fileBuilder.setId(doc.fileUuid());
  83. fileBuilder.setCoveredLines(doc.coveredLines().size());
  84. ComponentDto component = componentsByUuid.get(doc.fileUuid());
  85. if (component != null) {
  86. fileBuilder.setKey(component.getKey());
  87. fileBuilder.setLongName(component.longName());
  88. setNullable(component.getBranch(), fileBuilder::setBranch);
  89. }
  90. responseBuilder.addFiles(fileBuilder);
  91. }
  92. }
  93. writeProtobuf(responseBuilder.build(), request, response);
  94. }
  95. private Map<String, ComponentDto> buildComponentsByUuid(List<CoveredFileDoc> coveredFiles) {
  96. List<String> sourceFileUuids = Lists.transform(coveredFiles, new CoveredFileToFileUuidFunction());
  97. List<ComponentDto> components;
  98. try (DbSession dbSession = dbClient.openSession(false)) {
  99. components = dbClient.componentDao().selectByUuids(dbSession, sourceFileUuids);
  100. }
  101. return Maps.uniqueIndex(components, ComponentDto::uuid);
  102. }
  103. private static class CoveredFileToFileUuidFunction implements Function<CoveredFileDoc, String> {
  104. @Override
  105. public String apply(@Nonnull CoveredFileDoc coveredFile) {
  106. return coveredFile.fileUuid();
  107. }
  108. }
  109. }