org.eluder.coveralls.maven.plugin.logging.JobLogger Maven / Gradle / Ivy
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 - 2023, Tapio Rautonen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.eluder.coveralls.maven.plugin.logging;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import org.apache.maven.plugin.logging.Log;
import org.eluder.coveralls.maven.plugin.domain.Job;
public class JobLogger implements Logger {
private static final int ABBREV = 7;
private final Job job;
private final ObjectMapper jsonMapper;
public JobLogger(final Job job) {
this(job, null);
}
public JobLogger(final Job job, final ObjectMapper jsonMapper) {
if (job == null) {
throw new IllegalArgumentException("job must be defined");
}
this.job = job;
this.jsonMapper = jsonMapper != null ? jsonMapper : createDefaultJsonMapper();
}
@Override
public Position getPosition() {
return Position.BEFORE;
}
@Override
public void log(final Log log) {
StringBuilder starting = new StringBuilder("Starting Coveralls job");
if (job.getServiceName() != null) {
starting.append(" for ").append(job.getServiceName());
if (job.getServiceJobId() != null) {
starting.append(" (").append(job.getServiceJobId()).append(")");
} else if (job.getServiceBuildNumber() != null) {
starting.append(" (").append(job.getServiceBuildNumber());
if (job.getServiceBuildUrl() != null) {
starting.append(" / ").append(job.getServiceBuildUrl());
}
starting.append(")");
}
}
if (job.isDryRun()) {
starting.append(" in dry run mode");
}
if (job.isParallel()) {
starting.append(" with parallel option enabled");
}
log.info(starting.toString());
if (job.getRepoToken() != null) {
log.info("Using repository token ");
}
if (job.getGit() != null) {
String commit = job.getGit().getHead().getId();
String branch = job.getBranch() != null ? job.getBranch() : job.getGit().getBranch();
log.info("Git commit " + commit.substring(0, ABBREV) + " in " + branch);
}
if (log.isDebugEnabled()) {
try {
log.debug("Complete Job description:\n" + jsonMapper.writeValueAsString(job));
} catch (JsonProcessingException ex) {
throw new RuntimeException(ex);
}
}
}
private ObjectMapper createDefaultJsonMapper() {
return JsonMapper.builder().configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
.configure(SerializationFeature.INDENT_OUTPUT, true)
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true).build();
}
}