Search in sources :

Example 1 with ViolationConfig

use of org.jenkinsci.plugins.jvctgl.config.ViolationConfig in project violation-comments-to-gitlab-plugin by jenkinsci.

the class JvctglPerformer method logConfiguration.

private static void logConfiguration(final ViolationsToGitLabConfig config, final Run<?, ?> build, final TaskListener listener) {
    final PrintStream logger = listener.getLogger();
    logger.println(FIELD_GITLABURL + ": " + config.getGitLabUrl());
    logger.println(FIELD_PROJECTID + ": " + config.getProjectId());
    logger.println(FIELD_MERGEREQUESTIID + ": " + config.getMergeRequestIid());
    logger.println(FIELD_APITOKENCREDENTIALSID + ": " + !isNullOrEmpty(config.getApiTokenCredentialsId()));
    logger.println(FIELD_IGNORECERTIFICATEERRORS + ": " + config.getIgnoreCertificateErrors());
    logger.println(FIELD_APITOKENPRIVATE + ": " + config.getApiTokenPrivate());
    logger.println(FIELD_AUTHMETHODHEADER + ": " + config.getAuthMethodHeader());
    logger.println(FIELD_CREATECOMMENTWITHALLSINGLEFILECOMMENTS + ": " + config.getCreateCommentWithAllSingleFileComments());
    logger.println("createSingleFileComments" + ": " + config.getCreateSingleFileComments());
    logger.println(FIELD_COMMENTONLYCHANGEDCONTENT + ": " + config.getCommentOnlyChangedContent());
    logger.println(FIELD_COMMENTONLYCHANGEDCONTENTCONTEXT + ": " + config.getCommentOnlyChangedContentContext());
    logger.println("commentOnlyChangedFiles: " + config.getCommentOnlyChangedFiles());
    logger.println("maxNumberOfViolations:" + config.getMaxNumberOfViolations());
    logger.println(FIELD_MINSEVERITY + ": " + config.getMinSeverity());
    logger.println(FIELD_KEEP_OLD_COMMENTS + ": " + config.getKeepOldComments());
    logger.println(FIELD_SHOULD_SET_WIP + ": " + config.getShouldSetWip());
    logger.println("commentTemplate: " + config.getCommentTemplate());
    logger.println("proxyUri: " + config.getProxyUri());
    logger.println("proxyCredentialsId: " + (nullToEmpty(config.getProxyCredentialsId()).isEmpty() ? "no" : "yes"));
    for (final ViolationConfig violationConfig : config.getViolationConfigs()) {
        logger.println(violationConfig.getParser() + " with pattern " + violationConfig.getPattern());
    }
}
Also used : PrintStream(java.io.PrintStream) ViolationConfig(org.jenkinsci.plugins.jvctgl.config.ViolationConfig)

Example 2 with ViolationConfig

use of org.jenkinsci.plugins.jvctgl.config.ViolationConfig in project violation-comments-to-gitlab-plugin by jenkinsci.

the class JvctglPerformer method doPerform.

@VisibleForTesting
public static void doPerform(final ViolationsToGitLabConfig config, final String apiToken, final File workspace, final TaskListener listener, final String proxyUser, final String proxyPassword) throws MalformedURLException {
    if (config.getMergeRequestIid() == null) {
        listener.getLogger().println("No merge request id defined, will not send violation comments. " + "\n\nPossible cause might be: mergeRequestId changed in version 2.0 and is now mergeRequestIid. " + "If you just updated the plugin, mergeRequestIid should have the same value that you previously used for mergeRequestId. " + "It is named wrong in the Java GitLab API and that wrong name has spread to this API." + "\n\n");
        return;
    }
    final ViolationsLogger violationsLogger = new ViolationsLogger() {

        @Override
        public void log(final Level level, final String string) {
            if (!config.getEnableLogging()) {
                return;
            }
            Logger.getLogger(JvctglPerformer.class.getName()).log(level, string);
            if (level != Level.FINE) {
                listener.getLogger().println(level + " " + string);
            }
        }

        @Override
        public void log(final Level level, final String string, final Throwable t) {
            if (!config.getEnableLogging()) {
                return;
            }
            Logger.getLogger(JvctglPerformer.class.getName()).log(level, string);
            final StringWriter sw = new StringWriter();
            t.printStackTrace(new PrintWriter(sw));
            listener.getLogger().println(level + " " + string + "\n" + sw.toString());
        }
    };
    final Set<Violation> allParsedViolations = new TreeSet<>();
    for (final ViolationConfig violationConfig : config.getViolationConfigs()) {
        if (!isNullOrEmpty(violationConfig.getPattern())) {
            Set<Violation> parsedViolations = violationsApi().withViolationsLogger(// 
            violationsLogger).findAll(// 
            violationConfig.getParser()).withReporter(// 
            violationConfig.getReporter()).inFolder(// 
            workspace.getAbsolutePath()).withPattern(// 
            violationConfig.getPattern()).violations();
            final SEVERITY minSeverity = config.getMinSeverity();
            if (minSeverity != null) {
                parsedViolations = Filtering.withAtLEastSeverity(parsedViolations, minSeverity);
            }
            allParsedViolations.addAll(parsedViolations);
            listener.getLogger().println("Found " + parsedViolations.size() + " violations from " + violationConfig + ".");
        }
    }
    final String hostUrl = config.getGitLabUrl();
    final String projectId = config.getProjectId();
    final String mergeRequestIid = config.getMergeRequestIid();
    listener.getLogger().println("Will comment PR " + hostUrl + " " + projectId + " " + mergeRequestIid);
    try {
        final TokenType tokenType = config.getApiTokenPrivate() ? TokenType.PRIVATE : TokenType.ACCESS;
        final Integer mergeRequestIidInteger = Integer.parseInt(mergeRequestIid);
        final boolean shouldKeepOldComments = config.getKeepOldComments();
        final boolean shouldSetWIP = config.getShouldSetWip();
        final String commentTemplate = config.getCommentTemplate();
        violationCommentsToGitLabApi().setProxyServer(config.getProxyUri()).setProxyUser(proxyUser).setProxyPassword(proxyPassword).setHostUrl(hostUrl).setProjectId(projectId).setMergeRequestIid(mergeRequestIidInteger).setApiToken(apiToken).setTokenType(tokenType).setCommentOnlyChangedContent(config.getCommentOnlyChangedContent()).setCommentOnlyChangedContentContext(config.getCommentOnlyChangedContentContext()).withShouldCommentOnlyChangedFiles(config.getCommentOnlyChangedFiles()).setCreateCommentWithAllSingleFileComments(config.getCreateCommentWithAllSingleFileComments()).setCreateSingleFileComments(config.getCreateSingleFileComments()).setIgnoreCertificateErrors(// 
        config.getIgnoreCertificateErrors()).setViolations(// 
        allParsedViolations).setShouldKeepOldComments(// 
        shouldKeepOldComments).setShouldSetWIP(// 
        shouldSetWIP).setCommentTemplate(// 
        commentTemplate).setViolationsLogger(// 
        violationsLogger).setMaxNumberOfViolations(// 
        config.getMaxNumberOfViolations()).toPullRequest();
    } catch (final Exception e) {
        Logger.getLogger(JvctglPerformer.class.getName()).log(SEVERE, "", e);
        final StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        listener.getLogger().println(sw.toString());
    }
}
Also used : Violation(se.bjurr.violations.lib.model.Violation) ViolationConfig(org.jenkinsci.plugins.jvctgl.config.ViolationConfig) ViolationsLogger(se.bjurr.violations.lib.ViolationsLogger) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) TokenType(org.gitlab4j.api.Constants.TokenType) StringWriter(java.io.StringWriter) TreeSet(java.util.TreeSet) Level(java.util.logging.Level) SEVERITY(se.bjurr.violations.lib.model.SEVERITY) PrintWriter(java.io.PrintWriter) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with ViolationConfig

use of org.jenkinsci.plugins.jvctgl.config.ViolationConfig in project violation-comments-to-gitlab-plugin by jenkinsci.

the class JvctglPerformer method expand.

/**
 * Makes sure any Jenkins variable, used in the configuration fields, are evaluated.
 */
@VisibleForTesting
static ViolationsToGitLabConfig expand(final ViolationsToGitLabConfig config, final EnvVars environment) {
    final ViolationsToGitLabConfig expanded = new ViolationsToGitLabConfig();
    expanded.setGitLabUrl(environment.expand(config.getGitLabUrl()));
    expanded.setProjectId(environment.expand(config.getProjectId()));
    expanded.setMergeRequestIid(environment.expand(config.getMergeRequestIid()));
    expanded.setApiTokenCredentialsId(config.getApiTokenCredentialsId());
    expanded.setAuthMethodHeader(config.getAuthMethodHeader());
    expanded.setApiTokenPrivate(config.getApiTokenPrivate());
    expanded.setIgnoreCertificateErrors(config.getIgnoreCertificateErrors());
    expanded.setCommentOnlyChangedContent(config.getCommentOnlyChangedContent());
    expanded.setCommentOnlyChangedContentContext(config.getCommentOnlyChangedContentContext());
    expanded.setCommentOnlyChangedFiles(config.getCommentOnlyChangedFiles());
    expanded.setCreateCommentWithAllSingleFileComments(config.getCreateCommentWithAllSingleFileComments());
    expanded.setCreateSingleFileComments(config.getCreateSingleFileComments());
    expanded.setMinSeverity(config.getMinSeverity());
    expanded.setShouldSetWip(config.getShouldSetWip());
    expanded.setKeepOldComments(config.getKeepOldComments());
    expanded.setCommentTemplate(config.getCommentTemplate());
    expanded.setProxyUri(config.getProxyUri());
    expanded.setProxyCredentialsId(config.getProxyCredentialsId());
    expanded.setEnableLogging(config.getEnableLogging());
    expanded.setMaxNumberOfViolations(config.getMaxNumberOfViolations());
    for (final ViolationConfig violationConfig : config.getViolationConfigs()) {
        final String pattern = environment.expand(violationConfig.getPattern());
        final String reporter = violationConfig.getReporter();
        final Parser parser = violationConfig.getParser();
        if (isNullOrEmpty(pattern) || parser == null) {
            LOG.fine("Ignoring violationConfig because of null/empty -values: " + violationConfig);
            continue;
        }
        final ViolationConfig p = new ViolationConfig();
        p.setPattern(pattern);
        p.setReporter(reporter);
        p.setParser(parser);
        expanded.getViolationConfigs().add(p);
    }
    return expanded;
}
Also used : ViolationConfig(org.jenkinsci.plugins.jvctgl.config.ViolationConfig) ViolationsToGitLabConfig(org.jenkinsci.plugins.jvctgl.config.ViolationsToGitLabConfig) Parser(se.bjurr.violations.lib.reports.Parser) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

ViolationConfig (org.jenkinsci.plugins.jvctgl.config.ViolationConfig)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 MalformedURLException (java.net.MalformedURLException)1 TreeSet (java.util.TreeSet)1 Level (java.util.logging.Level)1 TokenType (org.gitlab4j.api.Constants.TokenType)1 ViolationsToGitLabConfig (org.jenkinsci.plugins.jvctgl.config.ViolationsToGitLabConfig)1 ViolationsLogger (se.bjurr.violations.lib.ViolationsLogger)1 SEVERITY (se.bjurr.violations.lib.model.SEVERITY)1 Violation (se.bjurr.violations.lib.model.Violation)1 Parser (se.bjurr.violations.lib.reports.Parser)1