polycheck 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- polycheck/__init__.py +19 -0
- polycheck/cli.py +791 -0
- polycheck/config.py +123 -0
- polycheck/detect.py +151 -0
- polycheck/finding.py +131 -0
- polycheck/initializer.py +333 -0
- polycheck/languages/__init__.py +10 -0
- polycheck/mcp_server.py +994 -0
- polycheck/py.typed +0 -0
- polycheck/registry.py +119 -0
- polycheck/reporters/__init__.py +33 -0
- polycheck/reporters/json_reporter.py +58 -0
- polycheck/reporters/markdown_reporter.py +141 -0
- polycheck/reporters/sarif_reporter.py +114 -0
- polycheck/runner.py +279 -0
- polycheck/session.py +134 -0
- polycheck/tools/__init__.py +10 -0
- polycheck/tools/actionlint.py +66 -0
- polycheck/tools/base.py +449 -0
- polycheck/tools/depcheck.py +96 -0
- polycheck/tools/deptry.py +83 -0
- polycheck/tools/eslint.py +89 -0
- polycheck/tools/gitleaks.py +78 -0
- polycheck/tools/hadolint.py +75 -0
- polycheck/tools/knip.py +84 -0
- polycheck/tools/mypy.py +85 -0
- polycheck/tools/npm_audit.py +74 -0
- polycheck/tools/pip_audit.py +132 -0
- polycheck/tools/ruff.py +80 -0
- polycheck/tools/semgrep.py +80 -0
- polycheck/tools/shellcheck.py +80 -0
- polycheck/tools/sonarless.py +238 -0
- polycheck/tools/tsc.py +77 -0
- polycheck/tools/vulture.py +82 -0
- polycheck/verify.py +288 -0
- polycheck/workflows/__init__.py +76 -0
- polycheck-0.2.0.dist-info/METADATA +614 -0
- polycheck-0.2.0.dist-info/RECORD +42 -0
- polycheck-0.2.0.dist-info/WHEEL +5 -0
- polycheck-0.2.0.dist-info/entry_points.txt +2 -0
- polycheck-0.2.0.dist-info/licenses/LICENSE +21 -0
- polycheck-0.2.0.dist-info/top_level.txt +1 -0
polycheck/config.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""Configuration loader.
|
|
2
|
+
|
|
3
|
+
The user can drop a ``.polycheck.yml`` or ``.polycheck.toml`` at the
|
|
4
|
+
repo root. The file is optional — sensible defaults are used otherwise.
|
|
5
|
+
|
|
6
|
+
Example ``.polycheck.yml``:
|
|
7
|
+
|
|
8
|
+
enable:
|
|
9
|
+
- ruff
|
|
10
|
+
- mypy
|
|
11
|
+
- gitleaks
|
|
12
|
+
disable:
|
|
13
|
+
- pylint
|
|
14
|
+
severity_threshold: LOW
|
|
15
|
+
timeout: 600
|
|
16
|
+
output:
|
|
17
|
+
- markdown
|
|
18
|
+
- json
|
|
19
|
+
"""
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
import sys
|
|
23
|
+
from dataclasses import dataclass, field
|
|
24
|
+
from pathlib import Path
|
|
25
|
+
from typing import Any
|
|
26
|
+
|
|
27
|
+
import yaml
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class OutputConfig:
|
|
32
|
+
formats: list[str] = field(default_factory=lambda: ["markdown", "json"])
|
|
33
|
+
report_dir: str = "polycheck-reports"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass
|
|
37
|
+
class Config:
|
|
38
|
+
"""User-tunable configuration. Loaded from ``.polycheck.yml``.
|
|
39
|
+
|
|
40
|
+
All fields are optional; the defaults are sensible for most repos.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
enable: list[str] = field(default_factory=list)
|
|
44
|
+
disable: list[str] = field(default_factory=list)
|
|
45
|
+
# Durable triage verdicts: "rule" suppresses everywhere, "rule@file"
|
|
46
|
+
# in one file. See runner.apply_suppressions.
|
|
47
|
+
suppress: list[str] = field(default_factory=list)
|
|
48
|
+
severity_threshold: str = "INFO" # minimum severity to keep
|
|
49
|
+
timeout: int = 300 # per-tool seconds
|
|
50
|
+
output: OutputConfig = field(default_factory=OutputConfig)
|
|
51
|
+
extra_args: dict[str, list[str]] = field(default_factory=dict)
|
|
52
|
+
source: Path | None = None # path to the file the config was loaded from
|
|
53
|
+
|
|
54
|
+
@classmethod
|
|
55
|
+
def load(cls, repo: Path) -> Config:
|
|
56
|
+
"""Look for ``.polycheck.yml`` / ``.polycheck.toml`` at ``repo``;
|
|
57
|
+
return a Config. Missing file → defaults. Malformed file →
|
|
58
|
+
raise ``ConfigError`` with the parser error."""
|
|
59
|
+
repo = Path(repo).resolve()
|
|
60
|
+
for name in (".polycheck.yml", ".polycheck.yaml", ".polycheck.toml"):
|
|
61
|
+
path = repo / name
|
|
62
|
+
if path.exists():
|
|
63
|
+
return cls._parse(path)
|
|
64
|
+
return cls()
|
|
65
|
+
|
|
66
|
+
@classmethod
|
|
67
|
+
def _parse(cls, path: Path) -> Config:
|
|
68
|
+
data = cls._read_config_file(path)
|
|
69
|
+
cfg = cls()
|
|
70
|
+
cfg.source = path
|
|
71
|
+
cfg._apply_dict(data)
|
|
72
|
+
return cfg
|
|
73
|
+
|
|
74
|
+
@staticmethod
|
|
75
|
+
def _read_config_file(path: Path) -> dict[str, Any]:
|
|
76
|
+
"""Read and parse a config file (YAML or TOML)."""
|
|
77
|
+
try:
|
|
78
|
+
text = path.read_text(encoding="utf-8")
|
|
79
|
+
if path.suffix == ".toml":
|
|
80
|
+
return Config._parse_toml(text)
|
|
81
|
+
return yaml.safe_load(text) or {}
|
|
82
|
+
except (yaml.YAMLError, ValueError) as e:
|
|
83
|
+
raise ConfigError(f"Could not parse {path}: {e}") from e
|
|
84
|
+
|
|
85
|
+
@staticmethod
|
|
86
|
+
def _parse_toml(text: str) -> dict[str, Any]:
|
|
87
|
+
"""Parse TOML text, handling Python 3.10 compatibility."""
|
|
88
|
+
if sys.version_info >= (3, 11):
|
|
89
|
+
import tomllib
|
|
90
|
+
return tomllib.loads(text)
|
|
91
|
+
import tomli as tomllib
|
|
92
|
+
return tomllib.loads(text)
|
|
93
|
+
|
|
94
|
+
def _apply_dict(self, data: dict[str, Any]) -> None:
|
|
95
|
+
"""Apply config values from a parsed dictionary."""
|
|
96
|
+
if "enable" in data:
|
|
97
|
+
self.enable = list(data["enable"])
|
|
98
|
+
if "disable" in data:
|
|
99
|
+
self.disable = list(data["disable"])
|
|
100
|
+
if "suppress" in data:
|
|
101
|
+
self.suppress = [str(x) for x in data["suppress"]]
|
|
102
|
+
if "severity_threshold" in data:
|
|
103
|
+
self.severity_threshold = str(data["severity_threshold"]).upper()
|
|
104
|
+
if "timeout" in data:
|
|
105
|
+
self.timeout = int(data["timeout"])
|
|
106
|
+
if "extra_args" in data and isinstance(data["extra_args"], dict):
|
|
107
|
+
self.extra_args = {
|
|
108
|
+
str(k): list(v) if isinstance(v, list) else [str(v)]
|
|
109
|
+
for k, v in data["extra_args"].items()
|
|
110
|
+
}
|
|
111
|
+
if "output" in data and isinstance(data["output"], dict):
|
|
112
|
+
self._apply_output(data["output"])
|
|
113
|
+
|
|
114
|
+
def _apply_output(self, out: dict[str, Any]) -> None:
|
|
115
|
+
"""Apply output configuration from a dictionary."""
|
|
116
|
+
if "formats" in out:
|
|
117
|
+
self.output.formats = list(out["formats"])
|
|
118
|
+
if "report_dir" in out:
|
|
119
|
+
self.output.report_dir = str(out["report_dir"])
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class ConfigError(Exception):
|
|
123
|
+
"""Raised when a config file is malformed."""
|
polycheck/detect.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"""Language detection.
|
|
2
|
+
|
|
3
|
+
A polyglot repo can contain many languages. We detect each language
|
|
4
|
+
by looking for a marker file at the repo root (or anywhere, for some
|
|
5
|
+
languages). The detection is conservative — a missing marker means
|
|
6
|
+
"don't run language-specific tools", not "this language is absent".
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class Language:
|
|
16
|
+
"""A detected language in the repo."""
|
|
17
|
+
|
|
18
|
+
slug: str # e.g. "python", "javascript", "typescript"
|
|
19
|
+
display: str # e.g. "Python", "JavaScript", "TypeScript"
|
|
20
|
+
markers: tuple[str, ...] # files that indicate this language
|
|
21
|
+
note: str = "" # human note, e.g. "transpiled to JS"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# Marker-based detection. We deliberately use a *tuple* of file globs per
|
|
25
|
+
# language, because any one of them is sufficient. The matcher is
|
|
26
|
+
# conservative: missing markers → language not detected, not "absent".
|
|
27
|
+
LANGUAGES: tuple[Language, ...] = (
|
|
28
|
+
Language("python", "Python",
|
|
29
|
+
("pyproject.toml", "setup.py", "setup.cfg", "requirements.txt",
|
|
30
|
+
"Pipfile", "poetry.lock", "uv.lock")),
|
|
31
|
+
Language("javascript", "JavaScript",
|
|
32
|
+
("package.json", ".eslintrc.js", ".eslintrc.json", ".eslintrc.yml",
|
|
33
|
+
"jsconfig.json")),
|
|
34
|
+
Language("typescript", "TypeScript",
|
|
35
|
+
("tsconfig.json",)),
|
|
36
|
+
Language("go", "Go",
|
|
37
|
+
("go.mod", "go.sum")),
|
|
38
|
+
Language("rust", "Rust",
|
|
39
|
+
("Cargo.toml", "Cargo.lock")),
|
|
40
|
+
Language("java", "Java",
|
|
41
|
+
("pom.xml", "build.gradle", "build.gradle.kts")),
|
|
42
|
+
Language("ruby", "Ruby",
|
|
43
|
+
("Gemfile", ".ruby-version")),
|
|
44
|
+
Language("php", "PHP",
|
|
45
|
+
("composer.json",)),
|
|
46
|
+
Language("csharp", "C# / .NET",
|
|
47
|
+
()), # placeholder — no C#-specific tools yet; semgrep covers it
|
|
48
|
+
Language("kotlin", "Kotlin",
|
|
49
|
+
()), # placeholder — no Kotlin-specific tools yet; semgrep covers it
|
|
50
|
+
Language("swift", "Swift",
|
|
51
|
+
("Package.swift",)),
|
|
52
|
+
Language("scala", "Scala",
|
|
53
|
+
("build.sbt",)),
|
|
54
|
+
Language("elixir", "Elixir",
|
|
55
|
+
("mix.exs",)),
|
|
56
|
+
Language("haskell", "Haskell",
|
|
57
|
+
("stack.yaml", "cabal.project")),
|
|
58
|
+
Language("docker", "Docker",
|
|
59
|
+
("Dockerfile",)),
|
|
60
|
+
Language("github-actions", "GitHub Actions",
|
|
61
|
+
(".github/workflows",)),
|
|
62
|
+
Language("shell", "Shell",
|
|
63
|
+
()), # matched by file extension elsewhere
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def detect(repo: Path) -> list[Language]:
|
|
68
|
+
"""Return the languages detected in ``repo``. The order is stable
|
|
69
|
+
(LANGUAGES tuple order), so tests can rely on it.
|
|
70
|
+
|
|
71
|
+
Detection is shallow: any marker at the repo root (or under the
|
|
72
|
+
.github/workflows subtree for GitHub Actions) is enough. The
|
|
73
|
+
function does NOT walk the whole tree — that would be slow on
|
|
74
|
+
monorepos, and most tools work off the root anyway.
|
|
75
|
+
|
|
76
|
+
Also detects by file extensions in the root directory.
|
|
77
|
+
"""
|
|
78
|
+
repo = Path(repo).resolve()
|
|
79
|
+
if not repo.is_dir():
|
|
80
|
+
raise NotADirectoryError(f"{repo} is not a directory")
|
|
81
|
+
|
|
82
|
+
found = _detect_by_markers(repo)
|
|
83
|
+
_detect_shell_by_extension(repo, found)
|
|
84
|
+
_detect_by_file_extension(repo, found)
|
|
85
|
+
|
|
86
|
+
return found
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _detect_by_markers(repo: Path) -> list[Language]:
|
|
90
|
+
"""Detect languages by checking for marker files/directories."""
|
|
91
|
+
found: list[Language] = []
|
|
92
|
+
for lang in LANGUAGES:
|
|
93
|
+
if _has_marker(repo, lang):
|
|
94
|
+
found.append(lang)
|
|
95
|
+
return found
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _has_marker(repo: Path, lang: Language) -> bool:
|
|
99
|
+
"""Check if a language has any of its markers in the repo."""
|
|
100
|
+
for marker in lang.markers:
|
|
101
|
+
if marker.endswith("/"):
|
|
102
|
+
if (repo / marker).is_dir():
|
|
103
|
+
return True
|
|
104
|
+
elif (repo / marker).exists():
|
|
105
|
+
return True
|
|
106
|
+
return False
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _detect_shell_by_extension(repo: Path, found: list[Language]) -> None:
|
|
110
|
+
"""Also detect shell by file extension if any .sh files exist at root."""
|
|
111
|
+
if any(p.suffix == ".sh" for p in repo.iterdir() if p.is_file()):
|
|
112
|
+
if not any(lang.slug == "shell" for lang in found):
|
|
113
|
+
found.append(Language("shell", "Shell", ()))
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
# File extension to language slug mapping
|
|
117
|
+
_EXT_TO_LANG: dict[str, str] = {
|
|
118
|
+
".py": "python",
|
|
119
|
+
".js": "javascript",
|
|
120
|
+
".ts": "typescript",
|
|
121
|
+
".go": "go",
|
|
122
|
+
".rs": "rust",
|
|
123
|
+
".java": "java",
|
|
124
|
+
".rb": "ruby",
|
|
125
|
+
".php": "php",
|
|
126
|
+
".cs": "csharp",
|
|
127
|
+
".kt": "kotlin",
|
|
128
|
+
".swift": "swift",
|
|
129
|
+
".scala": "scala",
|
|
130
|
+
".ex": "elixir",
|
|
131
|
+
".hs": "haskell",
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _detect_by_file_extension(repo: Path, found: list[Language]) -> None:
|
|
136
|
+
"""Detect languages by file extensions in the root directory.
|
|
137
|
+
|
|
138
|
+
This catches single-file projects that lack marker files.
|
|
139
|
+
"""
|
|
140
|
+
found_slugs = {lang.slug for lang in found}
|
|
141
|
+
for p in repo.iterdir():
|
|
142
|
+
if not p.is_file():
|
|
143
|
+
continue
|
|
144
|
+
lang_slug = _EXT_TO_LANG.get(p.suffix)
|
|
145
|
+
if lang_slug and lang_slug not in found_slugs:
|
|
146
|
+
# Find the Language object for this slug
|
|
147
|
+
for lang in LANGUAGES:
|
|
148
|
+
if lang.slug == lang_slug:
|
|
149
|
+
found.append(lang)
|
|
150
|
+
found_slugs.add(lang_slug)
|
|
151
|
+
break
|
polycheck/finding.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"""Unified Finding schema — the lingua franca between tools, reporters, and LLMs.
|
|
2
|
+
|
|
3
|
+
Every tool adapter normalizes its raw output into ``Finding`` objects. This
|
|
4
|
+
means:
|
|
5
|
+
|
|
6
|
+
* Reporters (JSON, markdown, SARIF) only have to know one shape.
|
|
7
|
+
* Cross-tool deduplication can compare Findings on (file, line, message).
|
|
8
|
+
* An LLM triaging a report sees a consistent input regardless of which
|
|
9
|
+
tool produced each finding.
|
|
10
|
+
"""
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import enum
|
|
14
|
+
from dataclasses import asdict, dataclass, field
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Severity(enum.IntEnum):
|
|
19
|
+
"""Coarse severity ordering. Tools that emit their own severities are
|
|
20
|
+
mapped to one of these by the tool adapter."""
|
|
21
|
+
|
|
22
|
+
INFO = 1 # style, formatting, docstring
|
|
23
|
+
LOW = 2 # minor smell, dead code, missing dep
|
|
24
|
+
MEDIUM = 3 # type warning, complexity, duplication
|
|
25
|
+
HIGH = 4 # real bug, unused error, missing error handling
|
|
26
|
+
CRITICAL = 5 # security, secrets, CVE
|
|
27
|
+
|
|
28
|
+
@classmethod
|
|
29
|
+
def from_str(cls, value: str) -> Severity:
|
|
30
|
+
"""Best-effort parse for tool-specific severity strings."""
|
|
31
|
+
v = value.upper().strip()
|
|
32
|
+
aliases = {
|
|
33
|
+
"BLOCKER": "CRITICAL",
|
|
34
|
+
"CRITICAL": "CRITICAL",
|
|
35
|
+
"MAJOR": "HIGH",
|
|
36
|
+
"ERROR": "HIGH",
|
|
37
|
+
"HIGH": "HIGH",
|
|
38
|
+
"MINOR": "LOW",
|
|
39
|
+
"WARNING": "MEDIUM",
|
|
40
|
+
"WARN": "MEDIUM",
|
|
41
|
+
"MEDIUM": "MEDIUM",
|
|
42
|
+
"INFO": "INFO",
|
|
43
|
+
"INFORMATIONAL": "INFO",
|
|
44
|
+
"NOTE": "INFO",
|
|
45
|
+
"STYLE": "INFO",
|
|
46
|
+
}
|
|
47
|
+
return cls[aliases.get(v, "MEDIUM")]
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class Category(enum.Enum):
|
|
51
|
+
"""Top-level category. Used by reporters to group and color."""
|
|
52
|
+
|
|
53
|
+
LINT = "lint"
|
|
54
|
+
TYPE = "type"
|
|
55
|
+
SECURITY = "security"
|
|
56
|
+
SECRETS = "secrets"
|
|
57
|
+
DEAD_CODE = "dead-code"
|
|
58
|
+
COMPLEXITY = "complexity"
|
|
59
|
+
DUPLICATION = "duplication"
|
|
60
|
+
CVE = "cve"
|
|
61
|
+
DEPS = "deps"
|
|
62
|
+
FORMAT = "format"
|
|
63
|
+
OTHER = "other"
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@dataclass
|
|
67
|
+
class Finding:
|
|
68
|
+
"""A single reported issue, normalized across all tools."""
|
|
69
|
+
|
|
70
|
+
tool: str # e.g. "ruff", "eslint", "gitleaks"
|
|
71
|
+
rule: str # tool-specific rule id, e.g. "E501", "no-unused-vars"
|
|
72
|
+
severity: Severity
|
|
73
|
+
category: Category
|
|
74
|
+
message: str # human-readable, single line
|
|
75
|
+
file: str | None = None # repo-relative POSIX path
|
|
76
|
+
line: int | None = None
|
|
77
|
+
column: int | None = None
|
|
78
|
+
fixable: bool = False # tool has a --fix equivalent
|
|
79
|
+
fix_command: str | None = None # exact command the user can run
|
|
80
|
+
doc_url: str | None = None # tool-specific docs link, if known
|
|
81
|
+
raw: dict[str, Any] = field(default_factory=dict) # original tool output
|
|
82
|
+
|
|
83
|
+
def location(self) -> str:
|
|
84
|
+
"""``path:line:col`` short form for reporters. Path is bare if
|
|
85
|
+
no file; line/column elided if missing."""
|
|
86
|
+
if not self.file:
|
|
87
|
+
return ""
|
|
88
|
+
loc = self.file
|
|
89
|
+
if self.line is not None:
|
|
90
|
+
loc += f":{self.line}"
|
|
91
|
+
if self.column is not None:
|
|
92
|
+
loc += f":{self.column}"
|
|
93
|
+
return loc
|
|
94
|
+
|
|
95
|
+
def fingerprint(self) -> str:
|
|
96
|
+
"""Stable identity for cross-tool deduplication. Two findings with
|
|
97
|
+
the same fingerprint are likely the same defect seen by two tools.
|
|
98
|
+
"""
|
|
99
|
+
return f"{self.tool}|{self.rule}|{self.file or ''}|{self.line or 0}|{self.column or 0}"
|
|
100
|
+
|
|
101
|
+
def to_dict(self) -> dict[str, Any]:
|
|
102
|
+
d = asdict(self)
|
|
103
|
+
d["severity"] = self.severity.name
|
|
104
|
+
d["category"] = self.category.value
|
|
105
|
+
return d
|
|
106
|
+
|
|
107
|
+
@classmethod
|
|
108
|
+
def from_dict(cls, d: dict[str, Any]) -> Finding:
|
|
109
|
+
return cls(
|
|
110
|
+
tool=d["tool"],
|
|
111
|
+
rule=d["rule"],
|
|
112
|
+
severity=Severity[d["severity"]] if isinstance(d["severity"], str) else d["severity"],
|
|
113
|
+
category=Category(d["category"]) if isinstance(d["category"], str) else d["category"],
|
|
114
|
+
message=d["message"],
|
|
115
|
+
file=d.get("file"),
|
|
116
|
+
line=d.get("line"),
|
|
117
|
+
column=d.get("column"),
|
|
118
|
+
fixable=d.get("fixable", False),
|
|
119
|
+
fix_command=d.get("fix_command"),
|
|
120
|
+
doc_url=d.get("doc_url"),
|
|
121
|
+
raw=d.get("raw", {}),
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def fingerprint(tool: str, rule: str, file: str | None, line: int | None, column: int | None) -> str:
|
|
126
|
+
"""Module-level helper that matches ``Finding.fingerprint()``.
|
|
127
|
+
|
|
128
|
+
Useful for tool adapters that want to mint a fingerprint without
|
|
129
|
+
first constructing a full ``Finding``.
|
|
130
|
+
"""
|
|
131
|
+
return f"{tool}|{rule}|{file or ''}|{line or 0}|{column or 0}"
|