forked from sscpac/statick
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding groovy discovery plugin and npm-groovy-lint tool plugins. (ssc…
…pac#370) Signed-off-by: Alexander Xydes <[email protected]>
- Loading branch information
Showing
27 changed files
with
530 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,7 @@ jobs: | |
sudo npm install -g n | ||
sudo n stable | ||
sudo npm install -g [email protected] | ||
sudo npm install -g npm-groovy-lint | ||
rm nodesource_setup.sh | ||
- name: Test with mypy | ||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#!/bin/bash | ||
|
||
rm -rf build/ dist/ htmlcov/ output-py* .pytest_cache statick.egg-info/ statick_output/* .tox/ | ||
rm -rf build/ dist/ htmlcov/ output-py* .pytest_cache statick.egg-info/ statick_output/* .tox/ ./*.log | ||
find . -type d -name .mypy_cache -exec rm -rf {} \; | ||
find . -type d -name __pycache__ -exec rm -rf {} \; |
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,47 @@ | ||
"""Discover Groovy files to analyze.""" | ||
|
||
import logging | ||
from collections import OrderedDict | ||
from typing import List, Optional | ||
|
||
from statick_tool.discovery_plugin import DiscoveryPlugin | ||
from statick_tool.exceptions import Exceptions | ||
from statick_tool.package import Package | ||
|
||
|
||
class GroovyDiscoveryPlugin(DiscoveryPlugin): | ||
"""Discover Groovy files to analyze.""" | ||
|
||
def get_name(self) -> str: | ||
"""Get name of discovery type.""" | ||
return "groovy" | ||
|
||
def scan( | ||
self, package: Package, level: str, exceptions: Optional[Exceptions] = None | ||
) -> None: | ||
"""Scan package looking for Groovy files.""" | ||
src_files: List[str] = [] | ||
|
||
self.find_files(package) | ||
|
||
for file_dict in package.files.values(): | ||
if ( | ||
file_dict["name"].endswith(".groovy") | ||
or file_dict["name"].endswith(".gradle") | ||
or file_dict["name"].startswith("jenkinsfile") | ||
): | ||
src_files.append(file_dict["path"]) | ||
|
||
src_files = list(OrderedDict.fromkeys(src_files)) | ||
|
||
logging.info(" %d Groovy source files found.", len(src_files)) | ||
if exceptions: | ||
original_file_count = len(src_files) | ||
src_files = exceptions.filter_file_exceptions_early(package, src_files) | ||
if original_file_count > len(src_files): | ||
logging.info( | ||
" After filtering, %d Groovy files will be scanned.", | ||
len(src_files), | ||
) | ||
|
||
package["groovy_src"] = src_files |
3 changes: 3 additions & 0 deletions
3
statick_tool/plugins/discovery/groovy_discovery_plugin.yapsy-plugin
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,3 @@ | ||
[Core] | ||
Name = Groovy Discovery Plugin | ||
Module = groovy_discovery_plugin |
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,123 @@ | ||
"""Apply GroovyLint tool and gather results.""" | ||
|
||
import json | ||
import logging | ||
import subprocess | ||
from typing import List, Optional | ||
|
||
from statick_tool.issue import Issue | ||
from statick_tool.package import Package | ||
from statick_tool.tool_plugin import ToolPlugin | ||
|
||
|
||
class GroovyLintToolPlugin(ToolPlugin): | ||
"""Apply GroovyLint tool and gather results.""" | ||
|
||
def get_name(self) -> str: | ||
"""Get name of tool.""" | ||
return "groovylint" | ||
|
||
# pylint: disable=too-many-locals | ||
def scan(self, package: Package, level: str) -> Optional[List[Issue]]: | ||
"""Run tool and gather output.""" | ||
tool_bin = "npm-groovy-lint" | ||
|
||
tool_config = ".groovylintrc.json" | ||
if self.plugin_context: | ||
user_config = self.plugin_context.config.get_tool_config( | ||
self.get_name(), level, "config" | ||
) | ||
if user_config is not None: | ||
tool_config = user_config | ||
if self.plugin_context: | ||
format_file_name = self.plugin_context.resources.get_file(tool_config) | ||
|
||
flags: List[str] = [] | ||
if format_file_name is not None: | ||
flags += ["--config", format_file_name] | ||
flags += ["--output", "json"] | ||
user_flags = self.get_user_flags(level) | ||
flags += user_flags | ||
|
||
files: List[str] = [] | ||
if "groovy_src" in package: | ||
files += package["groovy_src"] | ||
|
||
total_output: List[str] = [] | ||
|
||
for src in files: | ||
try: | ||
exe = [tool_bin] + flags + ["-f", src] | ||
output = subprocess.check_output( | ||
exe, | ||
stderr=subprocess.STDOUT, | ||
universal_newlines=True, | ||
cwd=package.path, | ||
) | ||
total_output.append(output) | ||
|
||
except subprocess.CalledProcessError as ex: | ||
# npm-groovy-lint returns 1 on some errors but still has valid output | ||
if ex.returncode == 1: | ||
total_output.append(ex.output) | ||
else: | ||
logging.warning( | ||
"%s failed! Returncode = %d", tool_bin, ex.returncode | ||
) | ||
logging.warning("%s exception: %s", self.get_name(), ex.output) | ||
return None | ||
|
||
except OSError as ex: | ||
logging.warning("Couldn't find %s! (%s)", tool_bin, ex) | ||
return None | ||
|
||
with open(self.get_name() + ".log", "w") as fid: | ||
for output in total_output: | ||
fid.write(output) | ||
logging.debug("%s", output) | ||
|
||
issues: List[Issue] = self.parse_output(total_output) | ||
return issues | ||
|
||
# pylint: enable=too-many-locals | ||
|
||
def parse_output(self, total_output: List[str]) -> List[Issue]: | ||
"""Parse tool output and report issues.""" | ||
issues: List[Issue] = [] | ||
|
||
# pylint: disable=too-many-nested-blocks | ||
for output in total_output: | ||
lines = output.split("\n") | ||
for line in lines: | ||
try: | ||
err_dict = json.loads(line) | ||
if "files" in err_dict: | ||
all_files = err_dict["files"] | ||
for file_name in all_files: | ||
file_errs = all_files[file_name] | ||
if "errors" in file_errs: | ||
for issue in file_errs["errors"]: | ||
severity_str = issue["severity"] | ||
severity = "3" | ||
if severity_str == "info": | ||
severity = "1" | ||
elif severity_str == "warning": | ||
severity = "3" | ||
elif severity_str == "error": | ||
severity = "5" | ||
issues.append( | ||
Issue( | ||
file_name, | ||
str(issue["line"]), | ||
self.get_name(), | ||
issue["rule"], | ||
severity, | ||
issue["msg"], | ||
None, | ||
) | ||
) | ||
|
||
except ValueError as ex: | ||
logging.warning("ValueError: %s", ex) | ||
# pylint: enable=too-many-nested-blocks | ||
return issues |
3 changes: 3 additions & 0 deletions
3
statick_tool/plugins/tool/groovylint_tool_plugin.yapsy-plugin
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,3 @@ | ||
[Core] | ||
Name = NPM Groovy Lint Tool Plugin | ||
Module = groovylint_tool_plugin |
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,5 @@ | ||
{ | ||
"extends": "recommended", | ||
"rules": { | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/plugins/discovery/groovy_discovery_plugin/exceptions.yaml
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,5 @@ | ||
global: | ||
exceptions: | ||
file: | ||
- tools: all | ||
globs: ['*/ignore_this/*'] |
Empty file.
Empty file.
Empty file.
Oops, something went wrong.