cleargate 0.1.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.
Files changed (76) hide show
  1. cleargate/__init__.py +7 -0
  2. cleargate/__main__.py +12 -0
  3. cleargate/catalogue/SCHEMA.md +35 -0
  4. cleargate/catalogue/__init__.py +153 -0
  5. cleargate/catalogue/rules/frontend.toml +205 -0
  6. cleargate/catalogue/rules/maintainability.toml +686 -0
  7. cleargate/catalogue/rules/reliability.toml +163 -0
  8. cleargate/catalogue/rules/security.toml +392 -0
  9. cleargate/catalogue/rules/shell_docker.toml +62 -0
  10. cleargate/cli.py +117 -0
  11. cleargate/commands/__init__.py +5 -0
  12. cleargate/commands/explain.py +80 -0
  13. cleargate/commands/fix.py +269 -0
  14. cleargate/commands/gate.py +175 -0
  15. cleargate/commands/init_config.py +46 -0
  16. cleargate/commands/loc.py +197 -0
  17. cleargate/commands/plan.py +335 -0
  18. cleargate/commands/pr.py +168 -0
  19. cleargate/commands/rules.py +97 -0
  20. cleargate/commands/scan.py +79 -0
  21. cleargate/config.py +121 -0
  22. cleargate/detectors/__init__.py +5 -0
  23. cleargate/detectors/base.py +522 -0
  24. cleargate/detectors/css.py +198 -0
  25. cleargate/detectors/docker.py +176 -0
  26. cleargate/detectors/js.py +182 -0
  27. cleargate/detectors/python/__init__.py +34 -0
  28. cleargate/detectors/python/_common.py +307 -0
  29. cleargate/detectors/python/clear_text_http.py +101 -0
  30. cleargate/detectors/python/complexity.py +74 -0
  31. cleargate/detectors/python/composite_assert.py +176 -0
  32. cleargate/detectors/python/duplicate_literal.py +124 -0
  33. cleargate/detectors/python/fastapi_status.py +239 -0
  34. cleargate/detectors/python/float_equality.py +246 -0
  35. cleargate/detectors/python/hardcoded_ip.py +89 -0
  36. cleargate/detectors/python/log_injection.py +297 -0
  37. cleargate/detectors/python/regex.py +424 -0
  38. cleargate/detectors/python/suppression.py +210 -0
  39. cleargate/detectors/shell.py +189 -0
  40. cleargate/detectors/text/__init__.py +12 -0
  41. cleargate/detectors/text/common.py +199 -0
  42. cleargate/detectors/text/unicode_tag.py +105 -0
  43. cleargate/exit_codes.py +19 -0
  44. cleargate/explain/__init__.py +5 -0
  45. cleargate/explain/ai.py +150 -0
  46. cleargate/explain/offline.py +87 -0
  47. cleargate/fixers/__init__.py +5 -0
  48. cleargate/gate/__init__.py +5 -0
  49. cleargate/gate/newcode.py +87 -0
  50. cleargate/gate/publish.py +133 -0
  51. cleargate/gate/rating.py +130 -0
  52. cleargate/loc/__init__.py +5 -0
  53. cleargate/loc/advisor.py +217 -0
  54. cleargate/loc/ncloc.py +345 -0
  55. cleargate/loc/props.py +181 -0
  56. cleargate/loc/sonarglob.py +101 -0
  57. cleargate/model.py +132 -0
  58. cleargate/pr/__init__.py +5 -0
  59. cleargate/pr/annotations.py +89 -0
  60. cleargate/pr/github.py +234 -0
  61. cleargate/pr/link.py +130 -0
  62. cleargate/pr/mapper.py +215 -0
  63. cleargate/pr/status.py +89 -0
  64. cleargate/py.typed +0 -0
  65. cleargate/report/__init__.py +43 -0
  66. cleargate/report/json_report.py +38 -0
  67. cleargate/report/markdown.py +90 -0
  68. cleargate/report/sarif.py +193 -0
  69. cleargate/report/text.py +45 -0
  70. cleargate/ruffcfg.py +63 -0
  71. cleargate/rule_ids.py +90 -0
  72. cleargate-0.1.0.dist-info/METADATA +329 -0
  73. cleargate-0.1.0.dist-info/RECORD +76 -0
  74. cleargate-0.1.0.dist-info/WHEEL +4 -0
  75. cleargate-0.1.0.dist-info/entry_points.txt +2 -0
  76. cleargate-0.1.0.dist-info/licenses/LICENSE +21 -0
cleargate/__init__.py ADDED
@@ -0,0 +1,7 @@
1
+ """cleargate: find, explain and fix SonarCloud-style issues locally.
2
+
3
+ Author: Pandiyaraj Karuppasamy
4
+ Date: Nov-23-2026
5
+ """
6
+
7
+ __version__ = "0.1.0"
cleargate/__main__.py ADDED
@@ -0,0 +1,12 @@
1
+ """Allow ``python -m cleargate``.
2
+
3
+ Author: Pandiyaraj Karuppasamy
4
+ Date: Nov-23-2026
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from cleargate.cli import main
10
+
11
+ if __name__ == "__main__": # pragma: no cover
12
+ raise SystemExit(main())
@@ -0,0 +1,35 @@
1
+ <!-- Schema of the cleargate rule catalogue. Author: Pandiyaraj Karuppasamy | Date: Nov-23-2026 -->
2
+ # Catalogue schema
3
+
4
+ Rules live in `rules/<stem>.toml`, one `[[rule]]` table per rule. The stem for
5
+ each id is fixed by `cleargate.rule_ids.RULE_FILES`:
6
+
7
+ | Stem | Holds |
8
+ |---|---|
9
+ | `security` | vulnerabilities and security hotspots |
10
+ | `reliability` | bugs |
11
+ | `maintainability` | code smells, plus `CG-UNKNOWN` |
12
+ | `shell_docker` | Bash and Dockerfile rules |
13
+ | `frontend` | CSS and JavaScript rules |
14
+
15
+ Every key is required (`tests/test_catalogue_schema.py` checks this). Use an
16
+ empty string for "none".
17
+
18
+ | Key | Type | Meaning |
19
+ |---|---|---|
20
+ | `id` | string | cleargate id from `RULE_IDS`, e.g. `CG-S1244`. Never renamed. |
21
+ | `sonar_key` | string | Sonar key with repository prefix (`python:S1244`), or `""` |
22
+ | `title` | string | one line, Sonar's wording where there is one |
23
+ | `severity` | string | `blocker`, `critical`, `major`, `minor` or `info` |
24
+ | `category` | string | `security`, `reliability`, `maintainability`, `shell`, `docker`, `css`, `js` or `text` |
25
+ | `languages` | list of strings | from `python`, `shell`, `dockerfile`, `css`, `js`, `html`, `yaml`, `text` |
26
+ | `why` | string | why it matters, in plain words |
27
+ | `bad` | string | a short noncompliant example |
28
+ | `good` | string | the same example, fixed |
29
+ | `fix` | string | how to fix it, including when the automatic fix applies |
30
+ | `local_equivalent` | string | a free local check that finds it (`ruff PT018`), or `""` |
31
+ | `match_phrases` | list of strings | phrases from Sonar annotation text that map a PR finding to this rule |
32
+ | `detector` | bool | true when cleargate detects it locally |
33
+
34
+ Content rules: generic wording only (no company, repository or pull request
35
+ names, no counts from any one project), and no em or en dashes.
@@ -0,0 +1,153 @@
1
+ """Load the built-in rule catalogue from ``catalogue/rules/*.toml``.
2
+
3
+ Author: Pandiyaraj Karuppasamy
4
+ Date: Nov-23-2026
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import functools
10
+ import tomllib
11
+ from importlib import resources
12
+ from typing import Any
13
+
14
+ from cleargate.model import Category, Rule, Severity
15
+
16
+ CATALOGUE_FILES: tuple[str, ...] = (
17
+ "security",
18
+ "reliability",
19
+ "maintainability",
20
+ "shell_docker",
21
+ "frontend",
22
+ )
23
+ """Catalogue file stems, in display order."""
24
+
25
+ REQUIRED_FIELDS: tuple[str, ...] = (
26
+ "id",
27
+ "sonar_key",
28
+ "title",
29
+ "severity",
30
+ "category",
31
+ "languages",
32
+ "why",
33
+ "bad",
34
+ "good",
35
+ "fix",
36
+ "local_equivalent",
37
+ "match_phrases",
38
+ "detector",
39
+ )
40
+ """Every key a ``[[rule]]`` table must carry (empty string means None)."""
41
+
42
+
43
+ def _optional(value: str) -> str | None:
44
+ """Map an empty string to None.
45
+
46
+ Args:
47
+ value: A TOML string.
48
+
49
+ Returns:
50
+ The string, or None when it is empty.
51
+ """
52
+ return value or None
53
+
54
+
55
+ def rule_from_table(table: dict[str, Any]) -> Rule:
56
+ """Build a Rule from one ``[[rule]]`` TOML table.
57
+
58
+ Args:
59
+ table: The parsed table.
60
+
61
+ Returns:
62
+ The rule.
63
+
64
+ Raises:
65
+ KeyError: If a required field is missing.
66
+ ValueError: If the severity or category is unknown.
67
+ """
68
+ missing = [name for name in REQUIRED_FIELDS if name not in table]
69
+ if missing:
70
+ raise KeyError(f"rule {table.get('id', '?')} is missing {missing}")
71
+ return Rule(
72
+ id=table["id"],
73
+ sonar_key=_optional(table["sonar_key"]),
74
+ title=table["title"],
75
+ severity=Severity.parse(table["severity"]),
76
+ category=Category(table["category"]),
77
+ languages=tuple(table["languages"]),
78
+ why=table["why"].strip(),
79
+ bad=table["bad"].strip("\n"),
80
+ good=table["good"].strip("\n"),
81
+ fix=table["fix"].strip(),
82
+ local_equivalent=_optional(table["local_equivalent"]),
83
+ match_phrases=tuple(table["match_phrases"]),
84
+ detector=bool(table["detector"]),
85
+ )
86
+
87
+
88
+ def load_tables(stem: str) -> list[dict[str, Any]]:
89
+ """Read the raw ``[[rule]]`` tables of one catalogue file.
90
+
91
+ Args:
92
+ stem: The file stem, for example ``"security"``.
93
+
94
+ Returns:
95
+ The tables in file order.
96
+ """
97
+ source = resources.files("cleargate.catalogue").joinpath("rules", f"{stem}.toml")
98
+ data = tomllib.loads(source.read_text(encoding="utf-8"))
99
+ tables: list[dict[str, Any]] = data.get("rule", [])
100
+ return tables
101
+
102
+
103
+ @functools.cache
104
+ def load_catalogue() -> dict[str, Rule]:
105
+ """Load every catalogue rule.
106
+
107
+ Returns:
108
+ Rules keyed by cleargate id, in catalogue file order.
109
+
110
+ Raises:
111
+ ValueError: If two rules share an id.
112
+ """
113
+ rules: dict[str, Rule] = {}
114
+ for stem in CATALOGUE_FILES:
115
+ for table in load_tables(stem):
116
+ rule = rule_from_table(table)
117
+ if rule.id in rules:
118
+ raise ValueError(f"duplicate rule id {rule.id}")
119
+ rules[rule.id] = rule
120
+ return rules
121
+
122
+
123
+ def _normalise(term: str) -> str:
124
+ """Reduce a rule reference to its bare upper-case key.
125
+
126
+ Args:
127
+ term: ``CG-S1244``, ``S1244``, ``python:S1244`` and similar.
128
+
129
+ Returns:
130
+ The key without a ``CG-`` or ``language:`` prefix.
131
+ """
132
+ key = term.strip().upper()
133
+ if ":" in key:
134
+ key = key.rsplit(":", 1)[1]
135
+ return key.removeprefix("CG-")
136
+
137
+
138
+ def lookup(term: str) -> Rule | None:
139
+ """Find a rule by cleargate id or Sonar key, ignoring case.
140
+
141
+ Args:
142
+ term: ``CG-S1244``, ``S1244``, ``python:S1244`` or ``cg-pt018``.
143
+
144
+ Returns:
145
+ The rule, or None when nothing matches.
146
+ """
147
+ key = _normalise(term)
148
+ for rule in load_catalogue().values():
149
+ if _normalise(rule.id) == key:
150
+ return rule
151
+ if rule.sonar_key and _normalise(rule.sonar_key) == key:
152
+ return rule
153
+ return None
@@ -0,0 +1,205 @@
1
+ # cleargate catalogue: CSS and JavaScript rules. Schema: ../SCHEMA.md
2
+ # Author: Pandiyaraj Karuppasamy
3
+ # Date: Nov-23-2026
4
+
5
+ [[rule]]
6
+ id = "CG-CSS-DUP-SELECTOR"
7
+ sonar_key = "css:S4666"
8
+ title = "Selectors should not be duplicated"
9
+ severity = "major"
10
+ category = "css"
11
+ languages = ["css", "html"]
12
+ why = '''
13
+ The same selector defined twice splits one component's styles across the file,
14
+ and the later block silently overrides the earlier one.
15
+ '''
16
+ bad = '''
17
+ .card { padding: 8px; }
18
+ .title { font-weight: 600; }
19
+ .card { margin: 4px; }
20
+ '''
21
+ good = '''
22
+ .card { padding: 8px; margin: 4px; }
23
+ .title { font-weight: 600; }
24
+ '''
25
+ fix = '''
26
+ Merge the declarations into the first block and delete the duplicate.
27
+ '''
28
+ local_equivalent = "stylelint no-duplicate-selectors"
29
+ match_phrases = ["Duplicate selector", "first used at line"]
30
+ detector = true
31
+
32
+ [[rule]]
33
+ id = "CG-CSS-CONTRAST"
34
+ sonar_key = ""
35
+ title = "Text should meet the minimal contrast requirement with its background"
36
+ severity = "major"
37
+ category = "css"
38
+ languages = ["css", "html"]
39
+ why = '''
40
+ Low-contrast text is hard or impossible to read for many users. WCAG AA asks
41
+ for a contrast ratio of at least 4.5:1 for normal text.
42
+ '''
43
+ bad = '''
44
+ .hint { color: #999999; background: #ffffff; }
45
+ '''
46
+ good = '''
47
+ .hint { color: #595959; background: #ffffff; }
48
+ '''
49
+ fix = '''
50
+ Darken the text or lighten the background until the ratio is 4.5:1 or more
51
+ (3:1 for large text). Check both light and dark themes.
52
+ '''
53
+ local_equivalent = "axe, Lighthouse"
54
+ match_phrases = ["Text does not meet the minimal contrast requirement with its background"]
55
+ detector = false
56
+
57
+ [[rule]]
58
+ id = "CG-CSS-WORD-BREAK"
59
+ sonar_key = ""
60
+ title = "Deprecated keyword break-word for property word-break"
61
+ severity = "minor"
62
+ category = "css"
63
+ languages = ["css", "html"]
64
+ why = '''
65
+ word-break: break-word is deprecated and only kept for compatibility. The
66
+ standard way to let long words wrap is overflow-wrap.
67
+ '''
68
+ bad = '''
69
+ .cell { word-break: break-word; }
70
+ '''
71
+ good = '''
72
+ .cell { overflow-wrap: anywhere; }
73
+ '''
74
+ fix = '''
75
+ Replace the declaration with overflow-wrap: anywhere. cleargate fixes it
76
+ automatically.
77
+ '''
78
+ local_equivalent = "stylelint declaration-property-value-keyword-no-deprecated"
79
+ match_phrases = ["Deprecated keyword \"break-word\" for property \"word-break\""]
80
+ detector = true
81
+
82
+ [[rule]]
83
+ id = "CG-CSS-UNKNOWN-PROP"
84
+ sonar_key = "css:S4654"
85
+ title = "Properties should be valid"
86
+ severity = "major"
87
+ category = "css"
88
+ languages = ["css", "html"]
89
+ why = '''
90
+ Browsers ignore a property they do not know, so a typo or an invented property
91
+ silently does nothing.
92
+ '''
93
+ bad = '''
94
+ .panel { colr: #333333; }
95
+ '''
96
+ good = '''
97
+ .panel { color: #333333; }
98
+ '''
99
+ fix = '''
100
+ Correct the property name or remove the declaration.
101
+ '''
102
+ local_equivalent = "stylelint property-no-unknown"
103
+ match_phrases = ["Unknown property"]
104
+ detector = false
105
+
106
+ [[rule]]
107
+ id = "CG-JS-VAR"
108
+ sonar_key = "javascript:S3504"
109
+ title = "Variables should be declared with let or const"
110
+ severity = "major"
111
+ category = "js"
112
+ languages = ["js", "html"]
113
+ why = '''
114
+ var is function-scoped and hoisted, so a variable declared in a block leaks out
115
+ of it and can be used before it is assigned.
116
+ '''
117
+ bad = '''
118
+ var count = 0;
119
+ '''
120
+ good = '''
121
+ let count = 0;
122
+ '''
123
+ fix = '''
124
+ Use const for values that are never reassigned and let for the rest.
125
+ cleargate replaces var with let automatically.
126
+ '''
127
+ local_equivalent = "eslint no-var"
128
+ match_phrases = ["Replace \"var\" with \"let\" or \"const\""]
129
+ detector = true
130
+
131
+ [[rule]]
132
+ id = "CG-JS-NUMBER-GLOBALS"
133
+ sonar_key = ""
134
+ title = "Prefer Number static methods over global functions"
135
+ severity = "minor"
136
+ category = "js"
137
+ languages = ["js", "html"]
138
+ why = '''
139
+ The global parseInt, parseFloat, isNaN and isFinite behave differently from the
140
+ Number versions (global isNaN coerces its argument first). Number.* makes the
141
+ intent explicit and consistent.
142
+ '''
143
+ bad = '''
144
+ const size = parseInt(value, 10);
145
+ if (isNaN(size)) { return; }
146
+ '''
147
+ good = '''
148
+ const size = Number.parseInt(value, 10);
149
+ if (Number.isNaN(size)) { return; }
150
+ '''
151
+ fix = '''
152
+ Prefix the call with Number. Note that Number.isNaN does not coerce, so pass it
153
+ a number.
154
+ '''
155
+ local_equivalent = "eslint unicorn/prefer-number-properties"
156
+ match_phrases = ["Prefer `Number.parseInt` over `parseInt`", "Prefer `Number.isNaN` over `isNaN`", "Prefer `Number."]
157
+ detector = true
158
+
159
+ [[rule]]
160
+ id = "CG-JS-DATASET"
161
+ sonar_key = ""
162
+ title = "Prefer .dataset over getAttribute for data attributes"
163
+ severity = "minor"
164
+ category = "js"
165
+ languages = ["js", "html"]
166
+ why = '''
167
+ element.dataset reads data-* attributes directly and reads more clearly than
168
+ building the attribute name as a string.
169
+ '''
170
+ bad = '''
171
+ const id = row.getAttribute("data-id");
172
+ '''
173
+ good = '''
174
+ const id = row.dataset.id;
175
+ '''
176
+ fix = '''
177
+ Use element.dataset.name (camelCase for hyphenated names).
178
+ '''
179
+ local_equivalent = "eslint unicorn/prefer-dom-node-dataset"
180
+ match_phrases = ["Prefer `.dataset` over"]
181
+ detector = false
182
+
183
+ [[rule]]
184
+ id = "CG-JS-GETTIME"
185
+ sonar_key = ""
186
+ title = "Dates can be compared without calling getTime"
187
+ severity = "minor"
188
+ category = "js"
189
+ languages = ["js", "html"]
190
+ why = '''
191
+ Subtracting or comparing Date objects already converts them to numbers, so an
192
+ explicit getTime() adds noise.
193
+ '''
194
+ bad = '''
195
+ const age = now.getTime() - created.getTime();
196
+ '''
197
+ good = '''
198
+ const age = now - created;
199
+ '''
200
+ fix = '''
201
+ Drop the getTime() calls in arithmetic and comparisons.
202
+ '''
203
+ local_equivalent = "eslint unicorn"
204
+ match_phrases = ["getTime()"]
205
+ detector = false