Search in sources :

Example 1 with Parser

use of se.bjurr.violations.lib.reports.Parser in project violation-comments-to-bitbucket-cloud-command-line by tomasbjerre.

the class Runner method main.

public void main(final String[] args) throws Exception {
    final Argument<?> helpArgument = helpArgument("-h", "--help");
    final String parsersString = Arrays.asList(Parser.values()).stream().map((it) -> it.toString()).collect(Collectors.joining(", "));
    final Argument<List<List<String>>> violationsArg = stringArgument("--violations", "-v").arity(4).repeated().description("The violations to look for. <PARSER> <FOLDER> <REGEXP PATTERN> <NAME> where PARSER is one of: " + parsersString + "\n Example: -v \"JSHINT\" \".\" \".*/jshint.xml$\" \"JSHint\"").build();
    final Argument<SEVERITY> minSeverityArg = enumArgument(SEVERITY.class, "-severity", "-s").defaultValue(INFO).description("Minimum severity level to report.").build();
    final Argument<Boolean> showDebugInfo = optionArgument("-show-debug-info").description("Please run your command with this parameter and supply output when reporting bugs.").build();
    final Argument<Boolean> createCommentWithAllSingleFileCommentsArg = booleanArgument("-create-comment-with-all-single-file-comments", "-ccwasfc").defaultValue(false).build();
    final Argument<Boolean> createSingleFileCommentsArg = booleanArgument("-create-single-file-comments", "-csfc").defaultValue(true).build();
    final Argument<Boolean> keepOldCommentsArg = booleanArgument("-keep-old-comments").defaultValue(false).build();
    final Argument<String> commentTemplateArg = stringArgument("-comment-template").defaultValue("").description("https://github.com/tomasbjerre/violation-comments-lib").build();
    final Argument<String> pullRequestIdArg = stringArgument("-pull-request-id", "-prid").required().build();
    final Argument<String> workspaceArg = stringArgument("-workspace", "-ws").required().description("The workspace is typically same as username.").build();
    final Argument<String> repositorySlugArg = stringArgument("-repository-slug", "-rs").required().build();
    final Argument<String> usernameArg = stringArgument("-username", "-u").defaultValue("").build();
    final Argument<String> passwordArg = stringArgument("-password", "-p").defaultValue("").description("You can create an 'application password' in Bitbucket to use here. See https://confluence.atlassian.com/bitbucket/app-passwords-828781300.html").build();
    final Argument<Boolean> shouldCommentOnlyChangedContentArg = booleanArgument("-comment-only-changed-content", "-cocc").defaultValue(true).description("True if only changed parts of the changed files should be commented. False if all findings on the changed files should be commented.").build();
    final Argument<Boolean> shouldCommentOnlyChangedFilesArg = booleanArgument("-comment-only-changed-files", "-cocf").defaultValue(true).description("True if only changed files should be commented. False if all findings should be commented.").build();
    final Argument<Integer> maxNumberOfViolationsArg = integerArgument("-max-number-of-violations", "-max").defaultValue(Integer.MAX_VALUE).build();
    try {
        final ParsedArguments parsed = withArguments(// 
        helpArgument, // 
        violationsArg, // 
        minSeverityArg, // 
        showDebugInfo, // 
        createCommentWithAllSingleFileCommentsArg, // 
        createSingleFileCommentsArg, // 
        keepOldCommentsArg, // 
        commentTemplateArg, // 
        pullRequestIdArg, // 
        workspaceArg, // 
        repositorySlugArg, // 
        usernameArg, // 
        passwordArg, // 
        shouldCommentOnlyChangedContentArg, // 
        shouldCommentOnlyChangedFilesArg, // 
        maxNumberOfViolationsArg).parse(args);
        this.violations = parsed.get(violationsArg);
        this.minSeverity = parsed.get(minSeverityArg);
        this.createCommentWithAllSingleFileComments = parsed.get(createCommentWithAllSingleFileCommentsArg);
        this.createSingleFileComments = parsed.get(createSingleFileCommentsArg);
        this.keepOldComments = parsed.get(keepOldCommentsArg);
        this.commentTemplate = parsed.get(commentTemplateArg);
        this.pullRequestId = parsed.get(pullRequestIdArg);
        this.workspace = parsed.get(workspaceArg);
        this.repositorySlug = parsed.get(repositorySlugArg);
        this.username = parsed.get(usernameArg);
        this.password = parsed.get(passwordArg);
        this.shouldCommentOnlyChangedContent = parsed.get(shouldCommentOnlyChangedContentArg);
        this.shouldCommentOnlyChangedFiles = parsed.get(shouldCommentOnlyChangedFilesArg);
        this.maxNumberOfViolations = parsed.get(maxNumberOfViolationsArg);
        this.showDebugInfo = parsed.wasGiven(showDebugInfo);
        if (this.showDebugInfo) {
            System.out.println("Given parameters:\n" + Arrays.asList(args).stream().map((it) -> it.toString()).collect(Collectors.joining(", ")) + "\n\nParsed parameters:\n" + this.toString());
        }
    } catch (final ArgumentException exception) {
        System.out.println(exception.getMessageAndUsage());
        System.exit(1);
    }
    ViolationsLogger violationsLogger = new ViolationsLogger() {

        @Override
        public void log(final Level level, final String string) {
            System.out.println(level + " " + string);
        }

        @Override
        public void log(final Level level, final String string, final Throwable t) {
            final StringWriter sw = new StringWriter();
            t.printStackTrace(new PrintWriter(sw));
            System.out.println(level + " " + string + "\n" + sw.toString());
        }
    };
    if (!this.showDebugInfo) {
        violationsLogger = FilteringViolationsLogger.filterLevel(violationsLogger);
    }
    Set<Violation> allParsedViolations = new TreeSet<>();
    for (final List<String> configuredViolation : this.violations) {
        final String reporter = configuredViolation.size() >= 4 ? configuredViolation.get(3) : null;
        final Set<Violation> parsedViolations = // 
        violationsApi().withViolationsLogger(// 
        violationsLogger).findAll(// 
        Parser.valueOf(configuredViolation.get(0))).inFolder(// 
        configuredViolation.get(1)).withPattern(// 
        configuredViolation.get(2)).withReporter(// 
        reporter).violations();
        if (this.minSeverity != null) {
            allParsedViolations = Filtering.withAtLEastSeverity(allParsedViolations, this.minSeverity);
        }
        allParsedViolations.addAll(parsedViolations);
    }
    System.out.println("PR: " + this.workspace + "/" + this.repositorySlug + "/" + this.pullRequestId);
    final ViolationCommentsToBitbucketCloudApi violationCommentsToBitbucketServerApi = new ViolationCommentsToBitbucketCloudApi();
    try {
        if (!this.username.isEmpty()) {
            // 
            violationCommentsToBitbucketServerApi.withUsername(// 
            this.username).withPassword(this.password);
        }
        // 
        violationCommentsToBitbucketServerApi.withPullRequestId(// 
        this.pullRequestId).withWorkspace(// 
        this.workspace).withRepositorySlug(// 
        this.repositorySlug).withViolations(// 
        allParsedViolations).withCreateCommentWithAllSingleFileComments(// 
        this.createCommentWithAllSingleFileComments).withCreateSingleFileComment(// 
        this.createSingleFileComments).withShouldCommentOnlyChangedContent(// 
        this.shouldCommentOnlyChangedContent).withShouldCommentOnlyChangedFiles(// 
        this.shouldCommentOnlyChangedFiles).withKeepOldComments(// 
        this.keepOldComments).withCommentTemplate(// 
        this.commentTemplate).withMaxNumberOfViolations(// 
        this.maxNumberOfViolations).withViolationsLogger(new ViolationsLogger() {

            @Override
            public void log(final Level level, final String string) {
                System.out.println(level + " " + string);
            }

            @Override
            public void log(final Level level, final String string, final Throwable t) {
                final StringWriter sw = new StringWriter();
                t.printStackTrace(new PrintWriter(sw));
                System.out.println(level + " " + string + "\n" + sw.toString());
            }
        }).toPullRequest();
    } catch (final Exception e) {
        e.printStackTrace();
    }
}
Also used : Arrays(java.util.Arrays) Parser(se.bjurr.violations.lib.reports.Parser) Arguments.helpArgument(se.softhouse.jargo.Arguments.helpArgument) Arguments.enumArgument(se.softhouse.jargo.Arguments.enumArgument) Arguments.stringArgument(se.softhouse.jargo.Arguments.stringArgument) ArgumentException(se.softhouse.jargo.ArgumentException) Violation(se.bjurr.violations.lib.model.Violation) ViolationsLogger(se.bjurr.violations.lib.ViolationsLogger) TreeSet(java.util.TreeSet) Level(java.util.logging.Level) Filtering(se.bjurr.violations.lib.util.Filtering) ParsedArguments(se.softhouse.jargo.ParsedArguments) CommandLineParser.withArguments(se.softhouse.jargo.CommandLineParser.withArguments) FilteringViolationsLogger(se.bjurr.violations.lib.FilteringViolationsLogger) PrintWriter(java.io.PrintWriter) ViolationsApi.violationsApi(se.bjurr.violations.lib.ViolationsApi.violationsApi) Arguments.booleanArgument(se.softhouse.jargo.Arguments.booleanArgument) Arguments.integerArgument(se.softhouse.jargo.Arguments.integerArgument) ViolationCommentsToBitbucketCloudApi(se.bjurr.violations.comments.bitbucketcloud.lib.ViolationCommentsToBitbucketCloudApi) INFO(se.bjurr.violations.lib.model.SEVERITY.INFO) StringWriter(java.io.StringWriter) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) Argument(se.softhouse.jargo.Argument) SEVERITY(se.bjurr.violations.lib.model.SEVERITY) Arguments.optionArgument(se.softhouse.jargo.Arguments.optionArgument) Violation(se.bjurr.violations.lib.model.Violation) StringWriter(java.io.StringWriter) TreeSet(java.util.TreeSet) List(java.util.List) ArgumentException(se.softhouse.jargo.ArgumentException) PrintWriter(java.io.PrintWriter) ViolationCommentsToBitbucketCloudApi(se.bjurr.violations.comments.bitbucketcloud.lib.ViolationCommentsToBitbucketCloudApi) ViolationsLogger(se.bjurr.violations.lib.ViolationsLogger) FilteringViolationsLogger(se.bjurr.violations.lib.FilteringViolationsLogger) ArgumentException(se.softhouse.jargo.ArgumentException) ParsedArguments(se.softhouse.jargo.ParsedArguments) Level(java.util.logging.Level) SEVERITY(se.bjurr.violations.lib.model.SEVERITY)

Example 2 with Parser

use of se.bjurr.violations.lib.reports.Parser 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

Parser (se.bjurr.violations.lib.reports.Parser)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Level (java.util.logging.Level)1 Collectors (java.util.stream.Collectors)1 ViolationConfig (org.jenkinsci.plugins.jvctgl.config.ViolationConfig)1 ViolationsToGitLabConfig (org.jenkinsci.plugins.jvctgl.config.ViolationsToGitLabConfig)1 ViolationCommentsToBitbucketCloudApi (se.bjurr.violations.comments.bitbucketcloud.lib.ViolationCommentsToBitbucketCloudApi)1 FilteringViolationsLogger (se.bjurr.violations.lib.FilteringViolationsLogger)1 ViolationsApi.violationsApi (se.bjurr.violations.lib.ViolationsApi.violationsApi)1 ViolationsLogger (se.bjurr.violations.lib.ViolationsLogger)1 SEVERITY (se.bjurr.violations.lib.model.SEVERITY)1 INFO (se.bjurr.violations.lib.model.SEVERITY.INFO)1 Violation (se.bjurr.violations.lib.model.Violation)1 Filtering (se.bjurr.violations.lib.util.Filtering)1