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.

LinesAction.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.source.ws;
  21. import com.google.common.base.MoreObjects;
  22. import com.google.common.io.Resources;
  23. import java.util.Optional;
  24. import java.util.function.Supplier;
  25. import org.sonar.api.server.ws.Change;
  26. import org.sonar.api.server.ws.Request;
  27. import org.sonar.api.server.ws.Response;
  28. import org.sonar.api.server.ws.WebService;
  29. import org.sonar.api.utils.text.JsonWriter;
  30. import org.sonar.api.web.UserRole;
  31. import org.sonar.db.DbClient;
  32. import org.sonar.db.DbSession;
  33. import org.sonar.db.component.ComponentDto;
  34. import org.sonar.db.component.SnapshotDto;
  35. import org.sonar.db.protobuf.DbFileSources;
  36. import org.sonar.server.component.ComponentFinder;
  37. import org.sonar.server.source.SourceService;
  38. import org.sonar.server.user.UserSession;
  39. import static com.google.common.base.Preconditions.checkArgument;
  40. import static org.sonar.server.component.ComponentFinder.ParamNames.UUID_AND_KEY;
  41. import static org.sonar.server.exceptions.BadRequestException.checkRequest;
  42. import static org.sonar.server.exceptions.NotFoundException.checkFoundWithOptional;
  43. import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
  44. import static org.sonar.server.ws.KeyExamples.KEY_FILE_EXAMPLE_001;
  45. import static org.sonar.server.ws.KeyExamples.KEY_PULL_REQUEST_EXAMPLE_001;
  46. public class LinesAction implements SourcesWsAction {
  47. private static final String PARAM_UUID = "uuid";
  48. private static final String PARAM_KEY = "key";
  49. private static final String PARAM_FROM = "from";
  50. private static final String PARAM_TO = "to";
  51. private static final String PARAM_BRANCH = "branch";
  52. private static final String PARAM_PULL_REQUEST = "pullRequest";
  53. private final ComponentFinder componentFinder;
  54. private final SourceService sourceService;
  55. private final LinesJsonWriter linesJsonWriter;
  56. private final DbClient dbClient;
  57. private final UserSession userSession;
  58. public LinesAction(ComponentFinder componentFinder, DbClient dbClient, SourceService sourceService,
  59. LinesJsonWriter linesJsonWriter, UserSession userSession) {
  60. this.componentFinder = componentFinder;
  61. this.sourceService = sourceService;
  62. this.linesJsonWriter = linesJsonWriter;
  63. this.dbClient = dbClient;
  64. this.userSession = userSession;
  65. }
  66. @Override
  67. public void define(WebService.NewController controller) {
  68. WebService.NewAction action = controller.createAction("lines")
  69. .setDescription("Show source code with line oriented info. Requires See Source Code permission on file's project<br/>" +
  70. "Each element of the result array is an object which contains:" +
  71. "<ol>" +
  72. "<li>Line number</li>" +
  73. "<li>Content of the line</li>" +
  74. "<li>Author of the line (from SCM information)</li>" +
  75. "<li>Revision of the line (from SCM information)</li>" +
  76. "<li>Last commit date of the line (from SCM information)</li>" +
  77. "<li>Line hits from coverage</li>" +
  78. "<li>Number of conditions to cover in tests</li>" +
  79. "<li>Number of conditions covered by tests</li>" +
  80. "<li>Whether the line is new</li>" +
  81. "</ol>")
  82. .setSince("5.0")
  83. .setInternal(true)
  84. .setResponseExample(Resources.getResource(getClass(), "lines-example.json"))
  85. .setChangelog(
  86. new Change("6.2", "fields \"utLineHits\", \"utConditions\" and \"utCoveredConditions\" " +
  87. "has been renamed \"lineHits\", \"conditions\" and \"coveredConditions\""),
  88. new Change("6.2", "fields \"itLineHits\", \"itConditions\" and \"itCoveredConditions\" " +
  89. "are no more returned"),
  90. new Change("6.6", "field \"branch\" added"),
  91. new Change("7.4", "field \"isNew\" added"))
  92. .setHandler(this);
  93. action
  94. .createParam(PARAM_UUID)
  95. .setDescription("File uuid. Mandatory if param 'key' is not set")
  96. .setExampleValue("f333aab4-7e3a-4d70-87e1-f4c491f05e5c");
  97. action
  98. .createParam(PARAM_KEY)
  99. .setDescription("File key. Mandatory if param 'uuid' is not set. Available since 5.2")
  100. .setExampleValue(KEY_FILE_EXAMPLE_001);
  101. action
  102. .createParam(PARAM_BRANCH)
  103. .setDescription("Branch key")
  104. .setInternal(true)
  105. .setExampleValue(KEY_BRANCH_EXAMPLE_001);
  106. action
  107. .createParam(PARAM_PULL_REQUEST)
  108. .setDescription("Pull request id")
  109. .setInternal(true)
  110. .setExampleValue(KEY_PULL_REQUEST_EXAMPLE_001);
  111. action
  112. .createParam(PARAM_FROM)
  113. .setDescription("First line to return. Starts from 1")
  114. .setExampleValue("10")
  115. .setDefaultValue("1");
  116. action
  117. .createParam(PARAM_TO)
  118. .setDescription("Optional last line to return (inclusive). It must be greater than " +
  119. "or equal to parameter 'from'. If unset, then all the lines greater than or equal to 'from' " +
  120. "are returned.")
  121. .setExampleValue("20");
  122. }
  123. @Override
  124. public void handle(Request request, Response response) {
  125. try (DbSession dbSession = dbClient.openSession(false)) {
  126. ComponentDto file = loadComponent(dbSession, request);
  127. Supplier<Optional<Long>> periodDateSupplier = () -> dbClient.snapshotDao()
  128. .selectLastAnalysisByComponentUuid(dbSession, file.projectUuid())
  129. .map(SnapshotDto::getPeriodDate);
  130. userSession.checkComponentPermission(UserRole.CODEVIEWER, file);
  131. int from = request.mandatoryParamAsInt(PARAM_FROM);
  132. int to = MoreObjects.firstNonNull(request.paramAsInt(PARAM_TO), Integer.MAX_VALUE);
  133. Iterable<DbFileSources.Line> lines = checkFoundWithOptional(sourceService.getLines(dbSession, file.uuid(), from, to), "No source found for file '%s'", file.getDbKey());
  134. try (JsonWriter json = response.newJsonWriter()) {
  135. json.beginObject();
  136. linesJsonWriter.writeSource(lines, json, periodDateSupplier);
  137. json.endObject();
  138. }
  139. }
  140. }
  141. private ComponentDto loadComponent(DbSession dbSession, Request wsRequest) {
  142. String componentKey = wsRequest.param(PARAM_KEY);
  143. String componentId = wsRequest.param(PARAM_UUID);
  144. String branch = wsRequest.param(PARAM_BRANCH);
  145. String pullRequest = wsRequest.param(PARAM_PULL_REQUEST);
  146. checkArgument(componentId == null || (branch == null && pullRequest == null), "Parameter '%s' cannot be used at the same time as '%s' or '%s'",
  147. PARAM_UUID, PARAM_BRANCH, PARAM_PULL_REQUEST);
  148. if (branch == null && pullRequest == null) {
  149. return componentFinder.getByUuidOrKey(dbSession, componentId, componentKey, UUID_AND_KEY);
  150. }
  151. checkRequest(componentKey != null, "The '%s' parameter is missing", PARAM_KEY);
  152. return componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, componentKey, branch, pullRequest);
  153. }
  154. }