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