-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from TouK/tslint
Tslint support
- Loading branch information
Showing
24 changed files
with
534 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package pl.touk.sputnik.exec; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.zeroturnaround.exec.ProcessExecutor; | ||
import org.zeroturnaround.exec.stream.slf4j.Slf4jStream; | ||
|
||
import java.util.Arrays; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Slf4j | ||
public final class ExternalProcess { | ||
|
||
public ProcessExecutor executor() { | ||
return new ProcessExecutor(); | ||
} | ||
|
||
public String executeCommand(String... args) { | ||
try { | ||
log.debug("Executing command " + Arrays.asList(args)); | ||
return executor().command(args) | ||
.timeout(60, TimeUnit.SECONDS) | ||
.redirectError(Slf4jStream.of(getClass()).asInfo()) | ||
.readOutput(true).execute() | ||
.outputUTF8(); | ||
} catch (Exception e) { | ||
log.warn("Exception while calling command " + Arrays.asList(args) + ": " + e); | ||
throw new ExternalProcessException(e); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/pl/touk/sputnik/exec/ExternalProcessException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package pl.touk.sputnik.exec; | ||
|
||
public class ExternalProcessException extends RuntimeException { | ||
|
||
public ExternalProcessException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/pl/touk/sputnik/processor/tslint/TSLintProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package pl.touk.sputnik.processor.tslint; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.jetbrains.annotations.NotNull; | ||
import pl.touk.sputnik.configuration.Configuration; | ||
import pl.touk.sputnik.configuration.GeneralOption; | ||
import pl.touk.sputnik.review.Review; | ||
import pl.touk.sputnik.review.ReviewProcessor; | ||
import pl.touk.sputnik.review.ReviewResult; | ||
import pl.touk.sputnik.review.Violation; | ||
import pl.touk.sputnik.review.filter.TypeScriptFilter; | ||
import pl.touk.sputnik.review.transformer.IOFileTransformer; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
public class TSLintProcessor implements ReviewProcessor { | ||
|
||
private static final String SOURCE_NAME = "TSLint"; | ||
|
||
private final TSLintScript tsLintScript; | ||
private final TSLintResultParser resultParser; | ||
|
||
public TSLintProcessor(Configuration config) { | ||
String tsScript = config.getProperty(GeneralOption.TSLINT_SCRIPT); | ||
String configFile = config.getProperty(GeneralOption.TSLINT_CONFIGURATION_FILE); | ||
|
||
tsLintScript = new TSLintScript(tsScript, configFile); | ||
tsLintScript.validateConfiguration(); | ||
resultParser = new TSLintResultParser(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return SOURCE_NAME; | ||
} | ||
|
||
@Override | ||
@NotNull | ||
public ReviewResult process(Review review) { | ||
ReviewResult result = new ReviewResult(); | ||
|
||
List<File> files = review.getFiles(new TypeScriptFilter(), new IOFileTransformer()); | ||
for (File file : files) { | ||
for (Violation violation : resultParser.parse(tsLintScript.reviewFile(file.getAbsolutePath()))) { | ||
result.add(violation); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/pl/touk/sputnik/processor/tslint/TSLintProcessorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package pl.touk.sputnik.processor.tslint; | ||
|
||
import pl.touk.sputnik.configuration.Configuration; | ||
import pl.touk.sputnik.configuration.GeneralOption; | ||
import pl.touk.sputnik.processor.ReviewProcessorFactory; | ||
|
||
public class TSLintProcessorFactory implements ReviewProcessorFactory<TSLintProcessor> { | ||
|
||
@Override | ||
public boolean isEnabled(Configuration configuration) { | ||
return Boolean.valueOf(configuration.getProperty(GeneralOption.TSLINT_ENABLED)); | ||
} | ||
|
||
@Override | ||
public TSLintProcessor create(Configuration configuration) { | ||
return new TSLintProcessor(configuration); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/pl/touk/sputnik/processor/tslint/TSLintResultParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package pl.touk.sputnik.processor.tslint; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import pl.touk.sputnik.connector.gerrit.GerritException; | ||
import pl.touk.sputnik.processor.tslint.json.ListViolationsResponse; | ||
import pl.touk.sputnik.processor.tslint.json.TSLintFileInfo; | ||
import pl.touk.sputnik.review.Severity; | ||
import pl.touk.sputnik.review.Violation; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
public class TSLintResultParser { | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
public List<Violation> parse(String jsonViolations) { | ||
if (StringUtils.isEmpty(jsonViolations)) { | ||
return Collections.emptyList(); | ||
} | ||
try { | ||
List<Violation> result = new ArrayList<>(); | ||
ListViolationsResponse violations = objectMapper | ||
.readValue(jsonViolations, ListViolationsResponse.class); | ||
log.debug(String.format("Converted from json format to %d violations.", violations.size())); | ||
for (TSLintFileInfo fileInfo : violations) { | ||
Violation violation = new Violation(fileInfo.getName(), fileInfo.getStartPosition().getLine(), | ||
fileInfo.getFailure(), Severity.ERROR); | ||
result.add(violation); | ||
} | ||
return result; | ||
} catch (IOException e) { | ||
throw new GerritException("Error when converting from json format", e); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/pl/touk/sputnik/processor/tslint/TSLintScript.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package pl.touk.sputnik.processor.tslint; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import pl.touk.sputnik.exec.ExternalProcess; | ||
import pl.touk.sputnik.review.ReviewException; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* Represents instance of TSLint executable file, which is used for validating files. | ||
*/ | ||
@Slf4j | ||
public class TSLintScript { | ||
|
||
/** Name of the NodeJs process. */ | ||
private static final String NODE_JS = "node"; | ||
/** Argument for validating the file. */ | ||
private static final String TS_LINT_CONFIG_PARAM = "--config"; | ||
/** Determines the output format. */ | ||
private static final String TS_LINT_OUTPUT_KEY = "--format"; | ||
private static final String TS_LINT_OUTPUT_VALUE = "json"; | ||
|
||
/** TSLint script that validates files. */ | ||
private final String tsScript; | ||
|
||
/** File with rules. */ | ||
private final String configFile; | ||
|
||
public TSLintScript(String tsScript, String configFile) { | ||
this.tsScript = tsScript; | ||
this.configFile = configFile; | ||
} | ||
|
||
/** | ||
* Since this class needs to have setup correctly external configuration, we want to validate this configuration | ||
* before validation starts. | ||
* | ||
* @throws ReviewException | ||
* when configuration is not valid or completed | ||
*/ | ||
public void validateConfiguration() throws ReviewException { | ||
// check if config file exist | ||
if (!new File(configFile).exists()) { | ||
throw new ReviewException("Could not find tslint configuration file: " + configFile); | ||
} | ||
} | ||
|
||
/** | ||
* Executes TSLint to look for violations. | ||
* | ||
* @param filePath | ||
* file that will be examined | ||
* @return violations in JSON format | ||
*/ | ||
public String reviewFile(String filePath) { | ||
log.info("Reviewing file: " + filePath); | ||
// use this format to make sure that ' ' are parsed properly | ||
String[] args = new String[] {NODE_JS, tsScript, TS_LINT_OUTPUT_KEY, TS_LINT_OUTPUT_VALUE, | ||
TS_LINT_CONFIG_PARAM, configFile, filePath }; | ||
return new ExternalProcess().executeCommand(args); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/pl/touk/sputnik/processor/tslint/json/ChangePosition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package pl.touk.sputnik.processor.tslint.json; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* TSLint output with violations entry. | ||
* Used with JSON unmarshaller only. | ||
* | ||
"startPosition": | ||
{ | ||
"position":1144, | ||
"line":20, | ||
"character":0 | ||
}, | ||
"endPosition": | ||
{ | ||
"position":1275, | ||
"line":20, | ||
"character":131 | ||
}, | ||
*/ | ||
@Data | ||
@NoArgsConstructor | ||
public final class ChangePosition { | ||
private int position; | ||
private int line; | ||
private int character; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/pl/touk/sputnik/processor/tslint/json/ListViolationsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package pl.touk.sputnik.processor.tslint.json; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* TSLint response with violations. | ||
* Used with JSON unmarshaller only. | ||
*/ | ||
@SuppressWarnings("serial") | ||
public final class ListViolationsResponse extends ArrayList<TSLintFileInfo> { | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/pl/touk/sputnik/processor/tslint/json/TSLintFileInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package pl.touk.sputnik.processor.tslint.json; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* TSLint output with violations entry. | ||
* Used with JSON unmarshaller only. | ||
[ | ||
{ | ||
"name":"service.ts", | ||
"failure":"exceeds maximum line length of 120", | ||
"startPosition": | ||
{ | ||
"position":897, | ||
"line":18, | ||
"character":0 | ||
}, | ||
"endPosition": | ||
{ | ||
"position":1028, | ||
"line":18, | ||
"character":131 | ||
}, | ||
"ruleName":"max-line-length" | ||
}, | ||
{ | ||
"name":"administration.ts", | ||
"failure":"exceeds maximum line length of 120", | ||
"startPosition": | ||
{ | ||
"position":1144, | ||
"line":20, | ||
"character":0 | ||
}, | ||
"endPosition": | ||
{ | ||
"position":1275, | ||
"line":20, | ||
"character":131 | ||
}, | ||
"ruleName":"max-line-length" | ||
} | ||
] | ||
*/ | ||
@Data | ||
@NoArgsConstructor | ||
public final class TSLintFileInfo { | ||
private String name; | ||
private String failure; | ||
|
||
private ChangePosition startPosition; | ||
|
||
private ChangePosition endPosition; | ||
|
||
private String ruleName; | ||
} |
Oops, something went wrong.