View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
5    *     https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
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   * The Class JobLogger.
38   */
39  public class JobLogger implements Logger {
40  
41      /** The Constant ABBREV. */
42      private static final int ABBREV = 7;
43  
44      /** The job. */
45      private final Job job;
46  
47      /** The json mapper. */
48      private final ObjectMapper jsonMapper;
49  
50      /**
51       * Instantiates a new job logger.
52       *
53       * @param job
54       *            the job
55       */
56      public JobLogger(final Job job) {
57          this(job, null);
58      }
59  
60      /**
61       * Instantiates a new job logger.
62       *
63       * @param job
64       *            the job
65       * @param jsonMapper
66       *            the json mapper
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      * Creates the default json mapper.
125      *
126      * @return the object mapper
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 }