All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.server.source.ws.LinesAction Maven / Gradle / Ivy

There is a newer version: 7.2.1
Show newest version
/*
 * SonarQube
 * Copyright (C) 2009-2018 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.server.source.ws;

import com.google.common.base.MoreObjects;
import com.google.common.io.Resources;
import java.util.Date;
import java.util.Optional;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.protobuf.DbFileSources;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.source.HtmlSourceDecorator;
import org.sonar.server.source.SourceService;
import org.sonar.server.user.UserSession;

import static com.google.common.base.Preconditions.checkArgument;
import static org.sonar.server.component.ComponentFinder.ParamNames.UUID_AND_KEY;
import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
import static org.sonar.server.ws.KeyExamples.KEY_FILE_EXAMPLE_001;
import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;

public class LinesAction implements SourcesWsAction {

  private static final String PARAM_UUID = "uuid";
  private static final String PARAM_KEY = "key";
  private static final String PARAM_FROM = "from";
  private static final String PARAM_TO = "to";
  private static final String PARAM_BRANCH = "branch";

  private final ComponentFinder componentFinder;
  private final SourceService sourceService;
  private final HtmlSourceDecorator htmlSourceDecorator;
  private final DbClient dbClient;
  private final UserSession userSession;

  public LinesAction(ComponentFinder componentFinder, DbClient dbClient, SourceService sourceService,
    HtmlSourceDecorator htmlSourceDecorator, UserSession userSession) {
    this.componentFinder = componentFinder;
    this.sourceService = sourceService;
    this.htmlSourceDecorator = htmlSourceDecorator;
    this.dbClient = dbClient;
    this.userSession = userSession;
  }

  @Override
  public void define(WebService.NewController controller) {
    WebService.NewAction action = controller.createAction("lines")
      .setDescription("Show source code with line oriented info. Require See Source Code permission on file's project
" + "Each element of the result array is an object which contains:" + "
    " + "
  1. Line number
  2. " + "
  3. Content of the line
  4. " + "
  5. Author of the line (from SCM information)
  6. " + "
  7. Revision of the line (from SCM information)
  8. " + "
  9. Last commit date of the line (from SCM information)
  10. " + "
  11. Line hits from coverage
  12. " + "
  13. Number of conditions to cover in tests
  14. " + "
  15. Number of conditions covered by tests
  16. " + "
") .setSince("5.0") .setInternal(true) .setResponseExample(Resources.getResource(getClass(), "lines-example.json")) .setChangelog( new Change("6.2", "fields \"utLineHits\", \"utConditions\" and \"utCoveredConditions\" " + "has been renamed \"lineHits\", \"conditions\" and \"coveredConditions\""), new Change("6.2", "fields \"itLineHits\", \"itConditions\" and \"itCoveredConditions\" " + "are no more returned"), new Change("6.6", "fields \"branch\" added")) .setHandler(this); action .createParam(PARAM_UUID) .setDescription("File uuid. Mandatory if param 'key' is not set") .setExampleValue("f333aab4-7e3a-4d70-87e1-f4c491f05e5c"); action .createParam(PARAM_KEY) .setDescription("File key. Mandatory if param 'uuid' is not set. Available since 5.2") .setExampleValue(KEY_FILE_EXAMPLE_001); action .createParam(PARAM_BRANCH) .setDescription("Branch key") .setInternal(true) .setExampleValue(KEY_BRANCH_EXAMPLE_001); action .createParam(PARAM_FROM) .setDescription("First line to return. Starts from 1") .setExampleValue("10") .setDefaultValue("1"); action .createParam(PARAM_TO) .setDescription("Optional last line to return (inclusive). It must be greater than " + "or equal to parameter 'from'. If unset, then all the lines greater than or equal to 'from' " + "are returned.") .setExampleValue("20"); } @Override public void handle(Request request, Response response) { try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto file = loadComponent(dbSession, request); userSession.checkComponentPermission(UserRole.CODEVIEWER, file); int from = request.mandatoryParamAsInt(PARAM_FROM); int to = MoreObjects.firstNonNull(request.paramAsInt(PARAM_TO), Integer.MAX_VALUE); Iterable lines = checkFoundWithOptional(sourceService.getLines(dbSession, file.uuid(), from, to), "No source found for file '%s'", file.getDbKey()); try (JsonWriter json = response.newJsonWriter()) { json.beginObject(); writeSource(lines, json); json.endObject(); } } } private ComponentDto loadComponent(DbSession dbSession, Request wsRequest) { String componentKey = wsRequest.param(PARAM_KEY); String componentId = wsRequest.param(PARAM_UUID); String branch = wsRequest.param(PARAM_BRANCH); checkArgument(componentId == null || branch == null, "'%s' and '%s' parameters cannot be used at the same time", PARAM_UUID, PARAM_BRANCH); return branch == null ? componentFinder.getByUuidOrKey(dbSession, componentId, componentKey, UUID_AND_KEY) : componentFinder.getByKeyAndBranch(dbSession, componentKey, branch); } private void writeSource(Iterable lines, JsonWriter json) { json.name("sources").beginArray(); for (DbFileSources.Line line : lines) { json.beginObject() .prop("line", line.getLine()) .prop("code", htmlSourceDecorator.getDecoratedSourceAsHtml(line.getSource(), line.getHighlighting(), line.getSymbols())) .prop("scmAuthor", line.getScmAuthor()) .prop("scmRevision", line.getScmRevision()); if (line.hasScmDate()) { json.prop("scmDate", DateUtils.formatDateTime(new Date(line.getScmDate()))); } Optional lineHits = getLineHits(line); if (lineHits.isPresent()) { json.prop("utLineHits", lineHits.get()); json.prop("lineHits", lineHits.get()); } Optional conditions = getConditions(line); if (conditions.isPresent()) { json.prop("utConditions", conditions.get()); json.prop("conditions", conditions.get()); } Optional coveredConditions = getCoveredConditions(line); if (coveredConditions.isPresent()) { json.prop("utCoveredConditions", coveredConditions.get()); json.prop("coveredConditions", coveredConditions.get()); } json.prop("duplicated", line.getDuplicationCount() > 0); json.endObject(); } json.endArray(); } private static Optional getLineHits(DbFileSources.Line line) { if (line.hasLineHits()) { return Optional.of(line.getLineHits()); } else if (line.hasDeprecatedOverallLineHits()) { return Optional.of(line.getDeprecatedOverallLineHits()); } else if (line.hasDeprecatedUtLineHits()) { return Optional.of(line.getDeprecatedUtLineHits()); } else if (line.hasDeprecatedItLineHits()) { return Optional.of(line.getDeprecatedItLineHits()); } return Optional.empty(); } private static Optional getConditions(DbFileSources.Line line) { if (line.hasConditions()) { return Optional.of(line.getConditions()); } else if (line.hasDeprecatedOverallConditions()) { return Optional.of(line.getDeprecatedOverallConditions()); } else if (line.hasDeprecatedUtConditions()) { return Optional.of(line.getDeprecatedUtConditions()); } else if (line.hasDeprecatedItConditions()) { return Optional.of(line.getDeprecatedItConditions()); } return Optional.empty(); } private static Optional getCoveredConditions(DbFileSources.Line line) { if (line.hasCoveredConditions()) { return Optional.of(line.getCoveredConditions()); } else if (line.hasDeprecatedOverallCoveredConditions()) { return Optional.of(line.getDeprecatedOverallCoveredConditions()); } else if (line.hasDeprecatedUtCoveredConditions()) { return Optional.of(line.getDeprecatedUtCoveredConditions()); } else if (line.hasDeprecatedItCoveredConditions()) { return Optional.of(line.getDeprecatedItCoveredConditions()); } return Optional.empty(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy