Environment.java

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
 *     https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.eluder.coveralls.maven.plugin;

import org.apache.commons.lang3.StringUtils;
import org.eluder.coveralls.maven.plugin.service.ServiceSetup;

/**
 * Constructs and setups the project environment and continuous integration service.
 */
public final class Environment {

    /** The mojo. */
    private final CoverallsReportMojo mojo;

    /** The services. */
    private final Iterable<ServiceSetup> services;

    /**
     * Instantiates a new environment.
     *
     * @param mojo
     *            the mojo
     * @param services
     *            the services
     */
    public Environment(final CoverallsReportMojo mojo, final Iterable<ServiceSetup> services) {
        if (mojo == null) {
            throw new IllegalArgumentException("mojo must be defined");
        }
        if (services == null) {
            throw new IllegalArgumentException("services must be defined");
        }
        this.mojo = mojo;
        this.services = services;
    }

    /**
     * Setup.
     */
    public void setup() {
        this.verify();
        this.setupService();
    }

    /**
     * Verify.
     */
    private void verify() {
        if (this.mojo.sourceEncoding == null) {
            throw new IllegalArgumentException(
                    "Source encoding not set, use <sourceEncoding> configuration option or set project wide property <project.build.sourceEncoding>");
        }
    }

    /**
     * Setup service.
     */
    private void setupService() {
        for (final ServiceSetup service : this.services) {
            if (service.isSelected()) {
                this.setupEnvironment(service);
                break;
            }
        }
    }

    /**
     * Sets the up environment.
     *
     * @param service
     *            the new up environment
     */
    private void setupEnvironment(final ServiceSetup service) {
        final var name = service.getName();
        if (StringUtils.isBlank(this.mojo.serviceName) && StringUtils.isNotBlank(name)) {
            this.mojo.serviceName = name;
        }

        final var jobId = service.getJobId();
        if (StringUtils.isBlank(this.mojo.serviceJobId) && StringUtils.isNotBlank(jobId)) {
            this.mojo.serviceJobId = jobId;
        }

        final var buildNumber = service.getBuildNumber();
        if (StringUtils.isBlank(this.mojo.serviceBuildNumber) && StringUtils.isNotBlank(buildNumber)) {
            this.mojo.serviceBuildNumber = buildNumber;
        }

        final var buildUrl = service.getBuildUrl();
        if (StringUtils.isBlank(this.mojo.serviceBuildUrl) && StringUtils.isNotBlank(buildUrl)) {
            this.mojo.serviceBuildUrl = buildUrl;
        }

        final var branch = service.getBranch();
        if (StringUtils.isBlank(this.mojo.branch) && StringUtils.isNotBlank(branch)) {
            this.mojo.branch = branch;
        }

        final var pullRequest = service.getPullRequest();
        if (StringUtils.isBlank(this.mojo.pullRequest) && StringUtils.isNotBlank(pullRequest)) {
            this.mojo.pullRequest = pullRequest;
        }

        final var environment = service.getEnvironment();
        if ((this.mojo.serviceEnvironment == null || this.mojo.serviceEnvironment.isEmpty()) && environment != null
                && !environment.isEmpty()) {
            this.mojo.serviceEnvironment = environment;
        }
    }
}