1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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 }