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