All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.bjurr.violations.lib.model.codeclimate.CodeClimateTransformer Maven / Gradle / Ivy

There is a newer version: 1.157.3
Show newest version
package se.bjurr.violations.lib.model.codeclimate;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;

public class CodeClimateTransformer {
  public static List fromViolations(final List from) {
    return from.stream()
        .map(
            violation -> {
              return toCodeClimate(violation);
            })
        .collect(Collectors.toList());
  }

  private static CodeClimate toCodeClimate(final Violation v) {
    final String description = v.getMessage();
    final String fingerprint = toHash(v);
    final CodeClimateLines lines = new CodeClimateLines(v.getStartLine());
    final CodeClimateLocation location = new CodeClimateLocation(v.getFile(), lines, null);
    final CodeClimateSeverity severity = toSeverity(v.getSeverity());
    final String check_name = v.getReporter();
    final List categories = new ArrayList();
    categories.add(CodeClimateCategory.BUGRISK);
    return new CodeClimate(description, fingerprint, location, severity, check_name, categories);
  }

  private static CodeClimateSeverity toSeverity(final SEVERITY severity) {
    if (severity == SEVERITY.ERROR) {
      return CodeClimateSeverity.critical;
    }
    if (severity == SEVERITY.WARN) {
      return CodeClimateSeverity.minor;
    }
    return CodeClimateSeverity.info;
  }

  private static String toHash(final Violation v) {
    MessageDigest digest;
    try {
      digest = MessageDigest.getInstance("SHA-256");
    } catch (final NoSuchAlgorithmException e) {
      return "No Hash: " + e.getMessage();
    }
    final byte[] encodedhash = digest.digest(v.toString().getBytes(StandardCharsets.UTF_8));
    final StringBuffer hexString = new StringBuffer();
    for (final byte element : encodedhash) {
      final String hex = Integer.toHexString(0xff & element);
      if (hex.length() == 1) {
        hexString.append('0');
      }
      hexString.append(hex);
    }
    return hexString.toString();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy