1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
32
33 public final class Environment {
34
35
36 private final CoverallsReportMojo mojo;
37
38
39 private final Iterable<ServiceSetup> services;
40
41
42
43
44
45
46
47
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
62
63 public void setup() {
64 this.verify();
65 this.setupService();
66 }
67
68
69
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
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
92
93
94
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 }