View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
5    *     https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   */
25  package org.eluder.coveralls.maven.plugin;
26  
27  import org.apache.commons.lang3.StringUtils;
28  import org.eluder.coveralls.maven.plugin.service.ServiceSetup;
29  
30  /**
31   * Constructs and setups the project environment and continuous integration service.
32   */
33  public final class Environment {
34  
35      /** The mojo. */
36      private final CoverallsReportMojo mojo;
37  
38      /** The services. */
39      private final Iterable<ServiceSetup> services;
40  
41      /**
42       * Instantiates a new environment.
43       *
44       * @param mojo
45       *            the mojo
46       * @param services
47       *            the services
48       */
49      public Environment(final CoverallsReportMojo mojo, final Iterable<ServiceSetup> services) {
50          if (mojo == null) {
51              throw new IllegalArgumentException("mojo must be defined");
52          }
53          if (services == null) {
54              throw new IllegalArgumentException("services must be defined");
55          }
56          this.mojo = mojo;
57          this.services = services;
58      }
59  
60      /**
61       * Setup.
62       */
63      public void setup() {
64          this.verify();
65          this.setupService();
66      }
67  
68      /**
69       * Verify.
70       */
71      private void verify() {
72          if (this.mojo.sourceEncoding == null) {
73              throw new IllegalArgumentException(
74                      "Source encoding not set, use <sourceEncoding> configuration option or set project wide property <project.build.sourceEncoding>");
75          }
76      }
77  
78      /**
79       * Setup service.
80       */
81      private void setupService() {
82          for (final ServiceSetup service : this.services) {
83              if (service.isSelected()) {
84                  this.setupEnvironment(service);
85                  break;
86              }
87          }
88      }
89  
90      /**
91       * Sets the up environment.
92       *
93       * @param service
94       *            the new up environment
95       */
96      private void setupEnvironment(final ServiceSetup service) {
97          final var name = service.getName();
98          if (StringUtils.isBlank(this.mojo.serviceName) && StringUtils.isNotBlank(name)) {
99              this.mojo.serviceName = name;
100         }
101 
102         final var jobId = service.getJobId();
103         if (StringUtils.isBlank(this.mojo.serviceJobId) && StringUtils.isNotBlank(jobId)) {
104             this.mojo.serviceJobId = jobId;
105         }
106 
107         final var buildNumber = service.getBuildNumber();
108         if (StringUtils.isBlank(this.mojo.serviceBuildNumber) && StringUtils.isNotBlank(buildNumber)) {
109             this.mojo.serviceBuildNumber = buildNumber;
110         }
111 
112         final var buildUrl = service.getBuildUrl();
113         if (StringUtils.isBlank(this.mojo.serviceBuildUrl) && StringUtils.isNotBlank(buildUrl)) {
114             this.mojo.serviceBuildUrl = buildUrl;
115         }
116 
117         final var branch = service.getBranch();
118         if (StringUtils.isBlank(this.mojo.branch) && StringUtils.isNotBlank(branch)) {
119             this.mojo.branch = branch;
120         }
121 
122         final var pullRequest = service.getPullRequest();
123         if (StringUtils.isBlank(this.mojo.pullRequest) && StringUtils.isNotBlank(pullRequest)) {
124             this.mojo.pullRequest = pullRequest;
125         }
126 
127         final var environment = service.getEnvironment();
128         if ((this.mojo.serviceEnvironment == null || this.mojo.serviceEnvironment.isEmpty()) && environment != null
129                 && !environment.isEmpty()) {
130             this.mojo.serviceEnvironment = environment;
131         }
132     }
133 }