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.logging;
26
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.MapperFeature;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.SerializationFeature;
31 import com.fasterxml.jackson.databind.json.JsonMapper;
32
33 import org.apache.maven.plugin.logging.Log;
34 import org.eluder.coveralls.maven.plugin.domain.Job;
35
36
37
38
39 public class JobLogger implements Logger {
40
41
42 private static final int ABBREV = 7;
43
44
45 private final Job job;
46
47
48 private final ObjectMapper jsonMapper;
49
50
51
52
53
54
55
56 public JobLogger(final Job job) {
57 this(job, null);
58 }
59
60
61
62
63
64
65
66
67
68 public JobLogger(final Job job, final ObjectMapper jsonMapper) {
69 if (job == null) {
70 throw new IllegalArgumentException("job must be defined");
71 }
72 this.job = job;
73 this.jsonMapper = jsonMapper != null ? jsonMapper : this.createDefaultJsonMapper();
74 }
75
76 @Override
77 public Position getPosition() {
78 return Position.BEFORE;
79 }
80
81 @Override
82 public void log(final Log log) {
83 final var starting = new StringBuilder("Starting Coveralls job");
84 if (this.job.getServiceName() != null) {
85 starting.append(" for ").append(this.job.getServiceName());
86 if (this.job.getServiceJobId() != null) {
87 starting.append(" (").append(this.job.getServiceJobId()).append(")");
88 } else if (this.job.getServiceBuildNumber() != null) {
89 starting.append(" (").append(this.job.getServiceBuildNumber());
90 if (this.job.getServiceBuildUrl() != null) {
91 starting.append(" / ").append(this.job.getServiceBuildUrl());
92 }
93 starting.append(")");
94 }
95 }
96 if (this.job.isDryRun()) {
97 starting.append(" in dry run mode");
98 }
99 if (this.job.isParallel()) {
100 starting.append(" with parallel option enabled");
101 }
102 log.info(starting.toString());
103
104 if (this.job.getRepoToken() != null) {
105 log.info("Using repository token <secret>");
106 }
107
108 if (this.job.getGit() != null) {
109 final var commit = this.job.getGit().getHead().getId();
110 final var branch = this.job.getBranch() != null ? this.job.getBranch() : this.job.getGit().getBranch();
111 log.info("Git commit " + commit.substring(0, JobLogger.ABBREV) + " in " + branch);
112 }
113
114 if (log.isDebugEnabled()) {
115 try {
116 log.debug("Complete Job description:\n" + this.jsonMapper.writeValueAsString(this.job));
117 } catch (final JsonProcessingException e) {
118 throw new IllegalStateException("FAiled to serialize job to JSON", e);
119 }
120 }
121 }
122
123
124
125
126
127
128 private ObjectMapper createDefaultJsonMapper() {
129 return JsonMapper.builder().configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
130 .configure(SerializationFeature.INDENT_OUTPUT, true)
131 .configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true).build();
132 }
133
134 }