com.blackducksoftware.integration.hub.detect.workflow.project.ProjectNameVersionDecider.java Source code

Java tutorial

Introduction

Here is the source code for com.blackducksoftware.integration.hub.detect.workflow.project.ProjectNameVersionDecider.java

Source

/**
 * hub-detect
 *
 * Copyright (C) 2019 Black Duck Software, Inc.
 * http://www.blackducksoftware.com/
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.blackducksoftware.integration.hub.detect.workflow.project;

import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.blackducksoftware.integration.hub.detect.DetectTool;
import com.blackducksoftware.integration.hub.detect.configuration.DetectProperty;
import com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException;
import com.blackducksoftware.integration.hub.detect.exitcode.ExitCodeType;
import com.synopsys.integration.util.NameVersion;

public class ProjectNameVersionDecider {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final ProjectNameVersionOptions projectVersionOptions;

    public ProjectNameVersionDecider(final ProjectNameVersionOptions projectVersionOptions) {
        this.projectVersionOptions = projectVersionOptions;
    }

    public NameVersion decideProjectNameVersion(String preferredDetectTools,
            final List<DetectToolProjectInfo> detectToolProjectInfo) throws DetectUserFriendlyException {

        Optional<String> decidedProjectName = Optional.empty();
        Optional<String> decidedProjectVersion = Optional.empty();

        if (StringUtils.isNotBlank(projectVersionOptions.overrideProjectName)) {
            decidedProjectName = Optional.of(projectVersionOptions.overrideProjectName);
        }

        if (StringUtils.isNotBlank(projectVersionOptions.overrideProjectVersionName)) {
            decidedProjectVersion = Optional.of(projectVersionOptions.overrideProjectVersionName);
        }

        Optional<DetectToolProjectInfo> chosenTool = decideToolProjectInfo(preferredDetectTools,
                detectToolProjectInfo);
        if (chosenTool.isPresent()) {
            if (!decidedProjectName.isPresent()) {
                String suggestedName = chosenTool.get().getSuggestedNameVersion().getName();
                if (StringUtils.isNotBlank(suggestedName)) {
                    decidedProjectName = Optional.of(suggestedName);
                }
            }
            if (!decidedProjectVersion.isPresent()) {
                String suggestedVersion = chosenTool.get().getSuggestedNameVersion().getVersion();
                if (StringUtils.isNotBlank(suggestedVersion)) {
                    decidedProjectVersion = Optional.of(suggestedVersion);
                }
            }
        }

        if (!decidedProjectName.isPresent()) {
            logger.info("A project name could not be decided. Using the name of the source path.");
            decidedProjectName = Optional.of(projectVersionOptions.sourcePathName);
        }

        if (!decidedProjectVersion.isPresent()) {
            if ("timestamp".equals(projectVersionOptions.defaultProjectVersionScheme)) {
                logger.info("A project version name could not be decided. Using the current timestamp.");
                final String timeformat = projectVersionOptions.defaultProjectVersionFormat;
                final String timeString = DateTimeFormatter.ofPattern(timeformat).withZone(ZoneOffset.UTC)
                        .format(Instant.now().atZone(ZoneOffset.UTC));
                decidedProjectVersion = Optional.of(timeString);
            } else {
                logger.info("A project version name could not be decided. Using the default version text.");
                decidedProjectVersion = Optional.of(projectVersionOptions.defaultProjectVersionText);
            }
        }

        return new NameVersion(decidedProjectName.get(), decidedProjectVersion.get());
    }

    private Optional<DetectToolProjectInfo> findProjectInfoForTool(DetectTool tool,
            List<DetectToolProjectInfo> detectToolProjectInfo) {
        return detectToolProjectInfo.stream().filter(it -> it.getDetectTool().equals(tool)).findFirst();
    }

    private Optional<DetectToolProjectInfo> decideToolProjectInfo(String preferredDetectTools,
            List<DetectToolProjectInfo> detectToolProjectInfo) throws DetectUserFriendlyException {
        Optional<DetectToolProjectInfo> chosenTool = Optional.empty();

        List<DetectTool> toolOrder = null;
        if (StringUtils.isNotBlank(preferredDetectTools)) {
            String[] tools = preferredDetectTools.split(",");
            toolOrder = Arrays.asList(tools).stream().map(it -> DetectTool.valueOf(it))
                    .collect(Collectors.toList());
        }
        if (toolOrder == null) {
            throw new DetectUserFriendlyException(
                    "Could not determine project tool order. Please specify a tool order using "
                            + DetectProperty.DETECT_PROJECT_TOOL.getPropertyName(),
                    ExitCodeType.FAILURE_CONFIGURATION);
        }

        if (toolOrder == null) {
            throw new DetectUserFriendlyException(
                    "Could not determine project tool order. Please specify a tool order using "
                            + DetectProperty.DETECT_PROJECT_TOOL.getPropertyName(),
                    ExitCodeType.FAILURE_CONFIGURATION);
        }

        for (DetectTool tool : toolOrder) {
            chosenTool = findProjectInfoForTool(tool, detectToolProjectInfo);

            if (chosenTool.isPresent()) {
                logger.info("Using the first ordered tool with project info: " + tool.toString());
                break;
            }
        }

        return chosenTool;
    }
}