diff --git a/pom.xml b/pom.xml index b7daf71..edafdf7 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ com.github.unchai checkstyle-github-maven-plugin - 0.0.1 + 0.0.2-SNAPSHOT maven-plugin Checkstyle github pull request maven plugin @@ -69,12 +69,12 @@ org.apache.maven maven-plugin-api - 3.5.4 + 3.6.1 org.apache.maven.plugin-tools maven-plugin-annotations - 3.5.2 + 3.6.0 org.apache.maven @@ -85,12 +85,13 @@ org.kohsuke github-api - 1.93 + 1.95 com.puppycrawl.tools checkstyle - 8.2 + [8.29,) + provided com.sun @@ -98,6 +99,12 @@ + + org.projectlombok + lombok + 1.18.12 + provided + junit diff --git a/src/main/java/com/github/unchai/maven/checkstyle/ChangedFile.java b/src/main/java/com/github/unchai/maven/checkstyle/ChangedFile.java index 606f709..5114678 100644 --- a/src/main/java/com/github/unchai/maven/checkstyle/ChangedFile.java +++ b/src/main/java/com/github/unchai/maven/checkstyle/ChangedFile.java @@ -20,30 +20,10 @@ import java.util.Map; -import org.apache.commons.lang3.builder.ToStringBuilder; +import lombok.Data; +@Data public class ChangedFile { private String path; private Map linePositionMap; - - public String getPath() { - return path; - } - - public void setPath(String path) { - this.path = path; - } - - public Map getLinePositionMap() { - return linePositionMap; - } - - public void setLinePositionMap(Map linePositionMap) { - this.linePositionMap = linePositionMap; - } - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this); - } } diff --git a/src/main/java/com/github/unchai/maven/checkstyle/CheckstyleAuditListener.java b/src/main/java/com/github/unchai/maven/checkstyle/CheckstyleAuditListener.java index 894d32d..feef495 100644 --- a/src/main/java/com/github/unchai/maven/checkstyle/CheckstyleAuditListener.java +++ b/src/main/java/com/github/unchai/maven/checkstyle/CheckstyleAuditListener.java @@ -51,7 +51,7 @@ public void fileFinished(AuditEvent event) { public void addError(AuditEvent event) { final CheckstyleError error = new CheckstyleError(); error.setSeverityLevel(event.getSeverityLevel()); - error.setFilename(event.getFileName()); + error.setPath(event.getFileName()); error.setLine(event.getLine()); error.setMessage(event.getMessage()); diff --git a/src/main/java/com/github/unchai/maven/checkstyle/CheckstyleError.java b/src/main/java/com/github/unchai/maven/checkstyle/CheckstyleError.java index edf6eca..2cf0c76 100644 --- a/src/main/java/com/github/unchai/maven/checkstyle/CheckstyleError.java +++ b/src/main/java/com/github/unchai/maven/checkstyle/CheckstyleError.java @@ -18,50 +18,13 @@ */ package com.github.unchai.maven.checkstyle; -import org.apache.commons.lang3.builder.ToStringBuilder; - import com.puppycrawl.tools.checkstyle.api.SeverityLevel; +import lombok.Data; +@Data public class CheckstyleError { private SeverityLevel severityLevel; - private String filename; + private String path; private int line; private String message; - - public SeverityLevel getSeverityLevel() { - return severityLevel; - } - - public void setSeverityLevel(SeverityLevel severityLevel) { - this.severityLevel = severityLevel; - } - - public String getFilename() { - return filename; - } - - public void setFilename(String filename) { - this.filename = filename; - } - - public int getLine() { - return line; - } - - public void setLine(int line) { - this.line = line; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this); - } } diff --git a/src/main/java/com/github/unchai/maven/checkstyle/CheckstyleExecutor.java b/src/main/java/com/github/unchai/maven/checkstyle/CheckstyleExecutor.java index b553f16..4c31460 100644 --- a/src/main/java/com/github/unchai/maven/checkstyle/CheckstyleExecutor.java +++ b/src/main/java/com/github/unchai/maven/checkstyle/CheckstyleExecutor.java @@ -20,6 +20,7 @@ import java.io.File; import java.util.List; +import java.util.stream.Collectors; import org.apache.maven.plugin.MojoExecutionException; @@ -29,16 +30,17 @@ import com.puppycrawl.tools.checkstyle.api.Configuration; public class CheckstyleExecutor { - private Checker checker; + private final Checker checker = new Checker(); + private final String configLocation; - public CheckstyleExecutor() { - this.checker = new Checker(); + public CheckstyleExecutor(String configLocation) { + this.configLocation = configLocation; } - public List execute(String config, List files) throws MojoExecutionException { + public List execute(File baseDir, List files) throws MojoExecutionException { try { final Configuration configuration = ConfigurationLoader.loadConfiguration( - config, + configLocation, new PropertiesExpander(System.getProperties()) ); @@ -47,11 +49,19 @@ public List execute(String config, List files) throws Moj checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.configure(configuration); checker.addListener(listener); - checker.process(files); + checker.process(files.stream().map(file -> new File(baseDir, file)).collect(Collectors.toList())); - return listener.getErrors(); + return listener.getErrors() + .stream() + .map(error -> stripBaseDir(baseDir.getPath(), error)) + .collect(Collectors.toList()); } catch (Exception e) { throw new MojoExecutionException(e.getMessage(), e); } } + + private CheckstyleError stripBaseDir(String baseDir, CheckstyleError checkstyleError) { + checkstyleError.setPath(checkstyleError.getPath().replace(baseDir, "").substring(1)); + return checkstyleError; + } } diff --git a/src/main/java/com/github/unchai/maven/checkstyle/Comment.java b/src/main/java/com/github/unchai/maven/checkstyle/Comment.java index 68e7caf..5c864fd 100644 --- a/src/main/java/com/github/unchai/maven/checkstyle/Comment.java +++ b/src/main/java/com/github/unchai/maven/checkstyle/Comment.java @@ -20,39 +20,11 @@ import java.util.List; -import org.apache.commons.lang3.builder.ToStringBuilder; +import lombok.Data; +@Data public class Comment { private String path; private int position; private List checkstyleErrors; - - public String getPath() { - return path; - } - - public void setPath(String path) { - this.path = path; - } - - public int getPosition() { - return position; - } - - public void setPosition(int position) { - this.position = position; - } - - public List getCheckstyleErrors() { - return checkstyleErrors; - } - - public void setCheckstyleErrors(List checkstyleErrors) { - this.checkstyleErrors = checkstyleErrors; - } - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this); - } } diff --git a/src/main/java/com/github/unchai/maven/checkstyle/GithubHelper.java b/src/main/java/com/github/unchai/maven/checkstyle/GithubHelper.java index 45d5c05..d7e9036 100644 --- a/src/main/java/com/github/unchai/maven/checkstyle/GithubHelper.java +++ b/src/main/java/com/github/unchai/maven/checkstyle/GithubHelper.java @@ -33,16 +33,19 @@ import org.kohsuke.github.GHPullRequestReviewComment; import org.kohsuke.github.GHRepository; import org.kohsuke.github.GitHub; -import org.kohsuke.github.GitHubBuilder; class GithubHelper { private static final String CONTEXT = "coding-convention/checkstyle"; private static final String PREFIX = "#### :rotating_light: checkstyle defects"; + private GHRepository repo; private GHPullRequest pr; private String username; - GithubHelper() { + GithubHelper(GitHub github, String repository, int pullRequest) throws IOException { + this.username = github.getMyself().getLogin(); + this.repo = github.getRepository(repository); + this.pr = this.repo.getPullRequest(pullRequest); } Map parsePatch(String patch) { @@ -69,21 +72,6 @@ Map parsePatch(String patch) { return map; } - void connect(String endpoint, String token, String repository, int pullRequest) throws MojoExecutionException { - try { - final GitHub github = new GitHubBuilder() - .withEndpoint(endpoint) - .withOAuthToken(token) - .build(); - - this.username = github.getMyself().getLogin(); - this.repo = github.getRepository(repository); - this.pr = this.repo.getPullRequest(pullRequest); - } catch (IOException e) { - throw new MojoExecutionException(e.getMessage(), e); - } - } - List listChangedFile() { final List linePositionMap = new ArrayList<>(); diff --git a/src/main/java/com/github/unchai/maven/checkstyle/MainMojo.java b/src/main/java/com/github/unchai/maven/checkstyle/MainMojo.java index c611d32..c0c24e8 100644 --- a/src/main/java/com/github/unchai/maven/checkstyle/MainMojo.java +++ b/src/main/java/com/github/unchai/maven/checkstyle/MainMojo.java @@ -19,6 +19,7 @@ package com.github.unchai.maven.checkstyle; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.EnumMap; @@ -30,11 +31,11 @@ import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.kohsuke.github.GHCommitState; +import org.kohsuke.github.GitHub; +import org.kohsuke.github.GitHubBuilder; import com.puppycrawl.tools.checkstyle.api.SeverityLevel; @@ -58,32 +59,31 @@ public class MainMojo extends AbstractMojo { @Parameter(property = "checkstyle-github.configLocation", required = true) String configLocation; - @Component(role = GithubHelper.class) - GithubHelper githubHelper; - - @Component(role = CheckstyleExecutor.class) - CheckstyleExecutor checkstyleExecutor; - @Override - public void execute() throws MojoExecutionException, MojoFailureException { - githubHelper.connect(ghEndpoint, ghToken, ghRepository, ghPullRequest); + public void execute() throws MojoExecutionException { + final GithubHelper githubHelper; + + try { + final GitHub github = new GitHubBuilder().withEndpoint(ghEndpoint).withOAuthToken(ghToken).build(); + githubHelper = new GithubHelper(github, ghRepository, ghPullRequest); + } catch (IOException e) { + throw new MojoExecutionException("Cannot connect github."); + } + githubHelper.changeStatus(GHCommitState.PENDING, null); + final CheckstyleExecutor checkstyleExecutor = new CheckstyleExecutor(configLocation); + final List changedFiles = githubHelper.listChangedFile(); + final Map changedFileMap = changedFiles .stream() + .filter(changedFile -> changedFile.getPath().endsWith(".java")) .collect(Collectors.toMap(ChangedFile::getPath, Function.identity())); - final List files = - changedFiles - .stream() - .map(changedFile -> new File(projectBasedir, changedFile.getPath())) - .filter(file -> file.getName().endsWith(".java")) - .collect(Collectors.toList()); - final List checkstyleErrors = - checkstyleExecutor.execute(configLocation, files) + checkstyleExecutor.execute(projectBasedir, changedFiles.stream().map(ChangedFile::getPath).collect(Collectors.toList())) .stream() .filter(checkstyleError -> contains(checkstyleError, changedFileMap)) .collect(Collectors.toList()); @@ -114,14 +114,8 @@ public void execute() throws MojoExecutionException, MojoFailureException { } private boolean contains(CheckstyleError checkstyleError, Map changedFileMap) { - final String path = stripBasedir(checkstyleError.getFilename()); - - return changedFileMap.containsKey(path) - && changedFileMap.get(path).getLinePositionMap().containsKey(checkstyleError.getLine()); - } - - private String stripBasedir(String filepath) { - return filepath.replace(projectBasedir.getPath(), "").substring(1); + return changedFileMap.containsKey(checkstyleError.getPath()) + && changedFileMap.get(checkstyleError.getPath()).getLinePositionMap().containsKey(checkstyleError.getLine()); } private Map buildSeverityLevelCountMap(List errors) { @@ -145,7 +139,7 @@ private Collection buildComments( final Map commentMap = new HashMap<>(); for (CheckstyleError error : errors) { - final String path = stripBasedir(error.getFilename()); + final String path = error.getPath(); final String key = path + "|" + error.getLine(); if (commentMap.containsKey(key)) { diff --git a/src/test/java/com/github/unchai/maven/checkstyle/GithubHelperTest.java b/src/test/java/com/github/unchai/maven/checkstyle/GithubHelperTest.java index 1e1850d..69aa1ed 100644 --- a/src/test/java/com/github/unchai/maven/checkstyle/GithubHelperTest.java +++ b/src/test/java/com/github/unchai/maven/checkstyle/GithubHelperTest.java @@ -1,4 +1,4 @@ -/* +package com.github.unchai.maven.checkstyle;/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -16,10 +16,11 @@ * specific language governing permissions and limitations * under the License. */ -package com.github.unchai.maven.checkstyle; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; import java.io.IOException; import java.io.InputStream; @@ -29,6 +30,9 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.kohsuke.github.GHMyself; +import org.kohsuke.github.GHRepository; +import org.kohsuke.github.GitHub; import org.mockito.junit.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) @@ -36,8 +40,15 @@ public class GithubHelperTest { private GithubHelper githubHelper; @Before - public void setUp() { - githubHelper = new GithubHelper(); + public void setUp() throws IOException { + final GitHub github = mock(GitHub.class); + final GHMyself ghMyself = mock(GHMyself.class); + final GHRepository ghRepository = mock(GHRepository.class); + + when(github.getMyself()).thenReturn(ghMyself); + when(github.getRepository(anyString())).thenReturn(ghRepository); + + githubHelper = new GithubHelper(github, "repo", 1); } @Test