Skip to content

Commit

Permalink
✨ add support for excluding files / folders from analysis (#542)
Browse files Browse the repository at this point in the history
* ✨ add support for excluding files / folders from analysis

Signed-off-by: Pranav Gaikwad <[email protected]>

* 👻 update go.mod

Signed-off-by: Pranav Gaikwad <[email protected]>

* 🐛 address feedback

Signed-off-by: Pranav Gaikwad <[email protected]>

* 🐛 fix mypy issues

Signed-off-by: Pranav Gaikwad <[email protected]>

* 🐛 make trunk happy

Signed-off-by: Pranav Gaikwad <[email protected]>

* 👻 make included_paths a list of Path

Signed-off-by: Pranav Gaikwad <[email protected]>

---------

Signed-off-by: Pranav Gaikwad <[email protected]>
  • Loading branch information
pranavgaikwad authored Jan 14, 2025
1 parent 4df3174 commit f1483fb
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 17 deletions.
13 changes: 9 additions & 4 deletions kai/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import threading
from io import BufferedReader, BufferedWriter
from pathlib import Path
from typing import IO, Optional, cast
from typing import IO, Any, Optional, cast

from kai.constants import ENV, PATH_KAI
from kai.jsonrpc.core import JsonRpcServer
Expand Down Expand Up @@ -40,6 +40,7 @@ def __init__(
analyzer_lsp_path: Path,
analyzer_java_bundle_path: Path,
dep_open_source_labels_path: Optional[Path],
excluded_paths: Optional[list[Path]] = None,
) -> None:
"""This will start an analyzer-lsp jsonrpc server"""
# trunk-ignore-begin(bandit/B603)
Expand Down Expand Up @@ -69,6 +70,7 @@ def __init__(
env=ENV,
)
# trunk-ignore-end(bandit/B603)
self.excluded_paths = excluded_paths

self.stderr_logging_thread = threading.Thread(
target=log_stderr, args=(self.rpc_server.stderr,)
Expand All @@ -89,16 +91,19 @@ def __init__(
def run_analyzer_lsp(
self,
label_selector: str,
included_paths: list[str],
included_paths: list[Path],
incident_selector: str,
scoped_paths: Optional[list[Path]] = None,
) -> JsonRpcResponse | JsonRpcError | None:
request_params = {
request_params: dict[str, Any] = {
"label_selector": label_selector,
"included_paths": included_paths,
"incident_selector": incident_selector,
"excluded_paths": self.excluded_paths,
}

if included_paths is not None:
request_params["included_paths"] = [str(p) for p in included_paths]

if scoped_paths is not None:
request_params["included_paths"] = [str(p) for p in scoped_paths]

Expand Down
3 changes: 2 additions & 1 deletion kai/reactive_codeplanner/task_manager/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class RpcClientConfig:
analyzer_java_bundle_path: Path
label_selector: Optional[str]
incident_selector: Optional[str]
included_paths: Optional[list[str]]
included_paths: Optional[list[Path]] = None
excluded_paths: Optional[list[Path]] = None
dep_open_source_labels_path: Optional[Path] = None


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ def log_stderr(stderr: IO[bytes]) -> None:

class AnalyzerLSPStep(ValidationStep):
label_selector: str
included_paths: list[str]
included_paths: list[Path]
excluded_paths: list[Path]
incident_selector: str

def __init__(self, config: RpcClientConfig, analyzer: AnalyzerLSP) -> None:
self.analyzerLSP = analyzer
self.label_selector = config.label_selector or ""
self.included_paths = config.included_paths or []
self.excluded_paths = config.excluded_paths or []
self.incident_selector = config.incident_selector or ""
super().__init__(config)

Expand Down
3 changes: 3 additions & 0 deletions kai/rpc_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class KaiRpcApplicationConfig(CamelCaseBaseModel):
analyzer_lsp_rules_path: AutoAbsPathExists
analyzer_lsp_java_bundle_path: AutoAbsPathExists
analyzer_lsp_dep_labels_path: Optional[AutoAbsPathExists] = None
analyzer_lsp_excluded_paths: Optional[list[AutoAbsPathExists]] = None

@staticmethod
def model_validate_filepath(filepath: str | Path) -> "KaiRpcApplicationConfig":
Expand Down Expand Up @@ -195,6 +196,7 @@ def initialize(
analyzer_java_bundle_path=app.config.analyzer_lsp_java_bundle_path,
dep_open_source_labels_path=app.config.analyzer_lsp_dep_labels_path
or Path(),
excluded_paths=app.config.analyzer_lsp_excluded_paths,
)

internal_config = RpcClientConfig(
Expand All @@ -206,6 +208,7 @@ def initialize(
label_selector="konveyor.io/target=quarkus || konveyor.io/target=jakarta-ee",
incident_selector=None,
included_paths=None,
excluded_paths=app.config.analyzer_lsp_excluded_paths,
dep_open_source_labels_path=app.config.analyzer_lsp_dep_labels_path,
)

Expand Down
4 changes: 2 additions & 2 deletions kai_analyzer_rpc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.23.1

require (
github.com/go-logr/logr v1.4.2
github.com/konveyor/analyzer-lsp v0.6.0-alpha.2.0.20250109185820-bdda7bfa329c
github.com/konveyor/analyzer-lsp/external-providers/java-external-provider v0.0.0-20250109185820-bdda7bfa329c
github.com/konveyor/analyzer-lsp v0.6.0-alpha.2.0.20250113140148-5896cc079e63
github.com/konveyor/analyzer-lsp/external-providers/java-external-provider v0.0.0-20241216180535-51a951fe8a57

)

Expand Down
8 changes: 4 additions & 4 deletions kai_analyzer_rpc/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJ
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
github.com/jhump/protoreflect v1.16.0 h1:54fZg+49widqXYQ0b+usAFHbMkBGR4PpXrsHc8+TBDg=
github.com/jhump/protoreflect v1.16.0/go.mod h1:oYPd7nPvcBw/5wlDfm/AVmU9zH9BgqGCI469pGxfj/8=
github.com/konveyor/analyzer-lsp v0.6.0-alpha.2.0.20250109185820-bdda7bfa329c h1:5A0q+TV+SbQ6eePIITKjE444z+0/W0QdDZNJ6r1FuQk=
github.com/konveyor/analyzer-lsp v0.6.0-alpha.2.0.20250109185820-bdda7bfa329c/go.mod h1:l9XC3uazLba8yXoAFJWN7uBDju1s/g1Hc8TKBpE3B2U=
github.com/konveyor/analyzer-lsp/external-providers/java-external-provider v0.0.0-20250109185820-bdda7bfa329c h1:iHMXMqHyMH7xPIRhUcebWVvgKw/3oJatDkvL8e8K7b8=
github.com/konveyor/analyzer-lsp/external-providers/java-external-provider v0.0.0-20250109185820-bdda7bfa329c/go.mod h1:9hR5THTSExZSAPz8uDEVgm2gm47F8gqFj8cU9K/d32M=
github.com/konveyor/analyzer-lsp v0.6.0-alpha.2.0.20250113140148-5896cc079e63 h1:Bbl7H2RVhzTEuHbgb0Mxs1tF5Fxt8T690AB6b3K5FiM=
github.com/konveyor/analyzer-lsp v0.6.0-alpha.2.0.20250113140148-5896cc079e63/go.mod h1:l9XC3uazLba8yXoAFJWN7uBDju1s/g1Hc8TKBpE3B2U=
github.com/konveyor/analyzer-lsp/external-providers/java-external-provider v0.0.0-20241216180535-51a951fe8a57 h1:Arp20MUlvJtdTUQYlVOi1xY2onSypzXwzGURdzMZ0c4=
github.com/konveyor/analyzer-lsp/external-providers/java-external-provider v0.0.0-20241216180535-51a951fe8a57/go.mod h1:9hR5THTSExZSAPz8uDEVgm2gm47F8gqFj8cU9K/d32M=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down
1 change: 0 additions & 1 deletion kai_analyzer_rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
)

func main() {

sourceDirectory := flag.String("source-directory", ".", "This will be the absolute path to the source code directory that should be analyzed")
rulesDirectory := flag.String("rules-directory", ".", "This will be the absolute path to the rules directory")
logFile := flag.String("log-file", "", "This is the file where logs should be stored. By default they will just be written to stderr")
Expand Down
16 changes: 12 additions & 4 deletions kai_analyzer_rpc/pkg/service/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ type Args struct {
LabelSelector string `json:"label_selector,omitempty"`
IncidentSelector string `json:"incident_selector,omitempty"`
IncludedPaths []string `json:"included_paths,omitempty"`
ExcludedPaths []string `json:"excluded_paths,omitempty"`
// RulesFiles []string
}

Expand Down Expand Up @@ -140,14 +141,21 @@ func (a *Analyzer) Analyze(args Args, response *Response) error {
}
a.Logger.Info("Have selectors", "selectors", selectors)

var scopes engine.Scope = nil
scopes := []engine.Scope{}
if len(args.IncludedPaths) > 0 {
scopes = engine.IncludedPathsScope(args.IncludedPaths, a.Logger)
a.Logger.V(2).Info("Using Scopes", "scopes", scopes.Name())
currScope := engine.IncludedPathsScope(args.IncludedPaths, a.Logger)
scopes = append(scopes, currScope)
a.Logger.V(2).Info("Using inclusion scope", "scope", currScope.Name())
}

if len(args.ExcludedPaths) > 0 {
currScope := engine.ExcludedPathsScope(args.ExcludedPaths, a.Logger)
scopes = append(scopes, currScope)
a.Logger.V(2).Info("Using exclusion scope", "scope", currScope.Name())
}

// This will already wait
rulesets := a.engine.RunRulesScoped(context.Background(), a.ruleSets, scopes, selectors...)
rulesets := a.engine.RunRulesScoped(context.Background(), a.ruleSets, engine.NewScope(scopes...), selectors...)

sort.SliceStable(rulesets, func(i, j int) bool {
return rulesets[i].Name < rulesets[j].Name
Expand Down

0 comments on commit f1483fb

Please sign in to comment.