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

org.sonar.server.component.ws.ComponentDtoToWsComponent 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.component.ws;

import com.google.common.collect.ImmutableSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import org.sonar.api.resources.Qualifiers;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.project.Visibility;
import org.sonarqube.ws.Components;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.emptyToNull;
import static org.sonar.api.utils.DateUtils.formatDateTime;
import static org.sonar.core.util.Protobuf.setNullable;

class ComponentDtoToWsComponent {

  /**
   * The concept of "visibility" will only be configured for these qualifiers.
   */
  private static final Set QUALIFIERS_WITH_VISIBILITY = ImmutableSet.of(Qualifiers.PROJECT, Qualifiers.VIEW, Qualifiers.APP);

  private ComponentDtoToWsComponent() {
    // prevent instantiation
  }

  static Components.Component.Builder componentDtoToWsComponent(ComponentDto dto, OrganizationDto organizationDto, Optional lastAnalysis) {
    checkArgument(
      Objects.equals(dto.getOrganizationUuid(), organizationDto.getUuid()),
      "OrganizationUuid (%s) of ComponentDto to convert to Ws Component is not the same as the one (%s) of the specified OrganizationDto",
      dto.getOrganizationUuid(), organizationDto.getUuid());
    return componentDtoToWsComponent(dto, organizationDto.getKey(), lastAnalysis);
  }

  private static Components.Component.Builder componentDtoToWsComponent(ComponentDto dto, String organizationDtoKey, Optional lastAnalysis) {
    Components.Component.Builder wsComponent = Components.Component.newBuilder()
      .setOrganization(organizationDtoKey)
      .setId(dto.uuid())
      .setKey(dto.getKey())
      .setName(dto.name())
      .setQualifier(dto.qualifier());
    setNullable(emptyToNull(dto.getBranch()), wsComponent::setBranch);
    setNullable(emptyToNull(dto.path()), wsComponent::setPath);
    setNullable(emptyToNull(dto.description()), wsComponent::setDescription);
    setNullable(emptyToNull(dto.language()), wsComponent::setLanguage);
    setTags(dto, wsComponent);
    lastAnalysis.ifPresent(
      analysis -> {
        wsComponent.setAnalysisDate(formatDateTime(analysis.getCreatedAt()));
        setNullable(analysis.getPeriodDate(), leak -> wsComponent.setLeakPeriodDate(formatDateTime(leak)));
        setNullable(analysis.getVersion(), wsComponent::setVersion);
      });
    if (QUALIFIERS_WITH_VISIBILITY.contains(dto.qualifier())) {
      wsComponent.setVisibility(Visibility.getLabel(dto.isPrivate()));
    }
    return wsComponent;
  }

  private static void setTags(ComponentDto dto, Components.Component.Builder wsComponent) {
    if (Qualifiers.PROJECT.equals(dto.qualifier())) {
      wsComponent.getTagsBuilder().addAllTags(dto.getTags());
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy