generated from konveyor-ecosystem/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ adding the intial analizer-lsp RPC client and validator
- Loading branch information
1 parent
564899a
commit 77ae54f
Showing
10 changed files
with
143 additions
and
3 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
File renamed without changes.
Empty file.
Empty file.
12 changes: 12 additions & 0 deletions
12
playpen/repo_level_awareness/task_runner/analyzer_lsp/api.py
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 @@ | ||
from dataclasses import dataclass, field | ||
from typing import Dict, List, Optional, Type | ||
from kai.models.report_types import AnalysisReport, Incident, RuleSet, Violation | ||
from playpen.repo_level_awareness.api import ValidationError, ValidationResult, ValidationStep | ||
|
||
@dataclass | ||
class AnalyzerRuleViolation(ValidationError): | ||
incident: Incident | ||
violation: Violation | ||
ruleset: RuleSet | ||
|
||
|
88 changes: 88 additions & 0 deletions
88
playpen/repo_level_awareness/task_runner/analyzer_lsp/validator.py
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,88 @@ | ||
from dataclasses import dataclass, field | ||
from typing import List, Dict, Optional, Type | ||
import subprocess | ||
|
||
from playpen.repo_level_awareness.api import RpcClientConfig, ValidationError, ValidationResult, ValidationStep | ||
from playpen.repo_level_awareness.task_runner.analyzer_lsp.api import AnalyzerRuleViolation | ||
from playpen.client import anlalyzer_rpc as analyzer_rpc | ||
|
||
from kai.models.report import Report | ||
|
||
@dataclass | ||
class RuleSet: | ||
pass | ||
|
||
class AnlayzerLSPStep(ValidationStep): | ||
|
||
rpc: analyzer_rpc.AnalyzerRpcServer | ||
|
||
def __init__(self, RpcClientConfig: RpcClientConfig) -> None: | ||
"""This will start and analyzer-lsp jsonrpc server""" | ||
|
||
rpc_server = subprocess.Popen( | ||
[RpcClientConfig.analyzer_lsp_server_binary, "-source-directory", RpcClientConfig.repo_directory, "-rules-directory", RpcClientConfig.rules_directory], | ||
stdin=subprocess.PIPE, | ||
stdout=subprocess.PIPE, | ||
text=True, | ||
) | ||
# trunk-ignore-end(bandit/B603) | ||
|
||
self.rpc = analyzer_rpc.AnalyzerRpcServer( | ||
json_rpc_endpoint=analyzer_rpc.AnlayzerRPCEndpoint( | ||
rpc_server.stdin, rpc_server.stdout | ||
), | ||
timeout=60, | ||
) | ||
self.rpc.start() | ||
|
||
super().__init__(RpcClientConfig) | ||
|
||
def run(self) -> ValidationResult: | ||
analyzer_output = self.__run_analyzer_lsp() | ||
print(type(analyzer_output)) | ||
errors = self.__parse_analyzer_lsp_output(analyzer_output) | ||
return ValidationResult(passed=not errors, errors=errors) | ||
|
||
def __run_analyzer_lsp(self) -> List[RuleSet]: | ||
|
||
request_params = { | ||
"label_selector": "konveyor.io/target=cloud-readiness", | ||
"included_paths": [], | ||
"incident_selector": "" | ||
} | ||
if self.config.label_selector is not None: | ||
request_params.LabelSelector = self.config.label_selector | ||
|
||
if self.config.included_paths is not None: | ||
request_params.IncludedPaths = self.config.included_paths | ||
|
||
if self.config.incident_selector is not None: | ||
request_params.IncidentSelector = self.config.incident_selector | ||
|
||
return self.rpc.call_method( | ||
"analysis_engine.Analyze", | ||
kwargs=request_params, | ||
) | ||
|
||
def __parse_analyzer_lsp_output(self, analyzer_output: Dict[str, any]) -> List[AnalyzerRuleViolation]: | ||
rulesets = analyzer_output.get("Rulesets") | ||
|
||
if not rulesets or not isinstance(rulesets, list): | ||
print(f"here rulesets: {rulesets}") | ||
return [] | ||
|
||
r = Report.load_report_from_object(rulesets, "analysis_run_task_runner") | ||
|
||
validation_errors: List[AnalyzerRuleViolation] = [] | ||
for k, v in r.rulesets.items(): | ||
print(k) | ||
|
||
for vk, vio in v.violations.items(): | ||
print(vk) | ||
for i in vio.incidents: | ||
validation_errors.append(AnalyzerRuleViolation(i, vio, v)) | ||
|
||
return validation_errors | ||
|
||
def stop(self): | ||
self.rpc.stop() |
Empty file.
File renamed without changes.