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();
}
}
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;
}
Aggregations