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.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 }