codefox 0.4.5__tar.gz → 0.4.7__tar.gz
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.
- {codefox-0.4.5 → codefox-0.4.7}/.github/PULL_REQUEST_TEMPLATE.md +1 -0
- {codefox-0.4.5 → codefox-0.4.7}/.github/workflows/ci.yml +1 -1
- {codefox-0.4.5 → codefox-0.4.7}/.gitignore +2 -0
- {codefox-0.4.5 → codefox-0.4.7}/PKG-INFO +2 -2
- {codefox-0.4.5 → codefox-0.4.7}/action.yml +24 -2
- codefox-0.4.7/assets//320/241/320/275/320/270/320/274/320/276/320/272 /321/215/320/272/321/200/320/260/320/275/320/260 2026-03-21 /320/262 09.08.23.png +0 -0
- codefox-0.4.7/assets//320/241/320/275/320/270/320/274/320/276/320/272 /321/215/320/272/321/200/320/260/320/275/320/260 2026-03-21 /320/262 09.10.30.png +0 -0
- codefox-0.4.7/assets//320/241/320/275/320/270/320/274/320/276/320/272 /321/215/320/272/321/200/320/260/320/275/320/260 2026-03-21 /320/262 09.16.17.png +0 -0
- codefox-0.4.7/assets//320/241/320/275/320/270/320/274/320/276/320/272 /321/215/320/272/321/200/320/260/320/275/320/260 2026-03-21 /320/262 09.18.00.png +0 -0
- codefox-0.4.7/assets//320/241/320/275/320/270/320/274/320/276/320/272 /321/215/320/272/321/200/320/260/320/275/320/260 2026-03-21 /320/262 09.21.38.png +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/api/base_api.py +4 -1
- {codefox-0.4.5 → codefox-0.4.7}/codefox/api/ollama.py +9 -2
- {codefox-0.4.5 → codefox-0.4.7}/codefox/bots/github_bot.py +3 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/cli/scan.py +49 -6
- {codefox-0.4.5 → codefox-0.4.7}/codefox/utils/helper.py +72 -17
- {codefox-0.4.5 → codefox-0.4.7}/codefox/utils/local_rag.py +41 -2
- {codefox-0.4.5 → codefox-0.4.7}/codefox/utils/parser.py +101 -18
- {codefox-0.4.5 → codefox-0.4.7}/pyproject.toml +1 -1
- {codefox-0.4.5 → codefox-0.4.7}/setup.py +1 -1
- {codefox-0.4.5 → codefox-0.4.7}/tests/test_base_api_config.py +18 -0
- codefox-0.4.7/tests/test_github_bot.py +39 -0
- {codefox-0.4.5 → codefox-0.4.7}/tests/test_helper.py +62 -1
- {codefox-0.4.5 → codefox-0.4.7}/tests/test_local_rag.py +68 -0
- codefox-0.4.7/tests/test_ollama.py +41 -0
- {codefox-0.4.5 → codefox-0.4.7}/tests/test_scan.py +61 -1
- {codefox-0.4.5 → codefox-0.4.7}/uv.lock +10 -2
- {codefox-0.4.5 → codefox-0.4.7}/.github/ISSUE_TEMPLATE/bug-report.md +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/.github/workflows/publish.yml +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/.python-version +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/CODE_OF_CONDUCT.md +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/CONTRIBUTING.md +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/LICENSE +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/README.md +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/SECURITY.md +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/WIKI.md +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/assets/logo.png +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/assets/work_review.gif +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/__init__.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/__main__.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/api/__init__.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/api/gemini.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/api/model_enum.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/api/openrouter.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/bots/__init__.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/bots/gitlab_bot.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/cli/__init__.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/cli/base_cli.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/cli/clean.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/cli/index.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/cli/init.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/cli/list.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/cli_manager.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/main.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/prompts/__init__.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/prompts/audit/__init__.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/prompts/audit/audit_content.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/prompts/audit/audit_system.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/prompts/prompt_template.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/prompts/template.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/tools/__init__.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/tools/base_tool.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/tools/rag_tool.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox/utils/__init__.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/codefox-template.yml +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/requirements-dev.txt +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/requirements.in +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/requirements.txt +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/tests/conftest.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/tests/test_clean.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/tests/test_cli_manager.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/tests/test_gitlab_bot.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/tests/test_init.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/tests/test_model_enum.py +0 -0
- {codefox-0.4.5 → codefox-0.4.7}/tests/test_prompt_template.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: codefox
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.7
|
|
4
4
|
Summary: CodeFox CLI - code auditing and code review tool
|
|
5
5
|
Project-URL: Documentation, https://github.com/codefox-lab/CodeFox-CLI/wiki
|
|
6
6
|
Project-URL: Homepage, https://github.com/URLbug/CodeFox-CLI
|
|
@@ -31,7 +31,7 @@ runs:
|
|
|
31
31
|
- name: Restore CodeFox cache
|
|
32
32
|
uses: actions/cache@v4
|
|
33
33
|
with:
|
|
34
|
-
path: ${{
|
|
34
|
+
path: ${{ github.workspace }}/.codefox/
|
|
35
35
|
key: ${{ runner.os }}-codefox-${{ github.repository }}-${{ github.base_ref }}-${{ inputs.codefox-branch }}
|
|
36
36
|
restore-keys: |
|
|
37
37
|
${{ runner.os }}-codefox-${{ github.repository }}-${{ github.base_ref }}-
|
|
@@ -62,7 +62,29 @@ runs:
|
|
|
62
62
|
- name: Run CodeFox Scan
|
|
63
63
|
shell: bash
|
|
64
64
|
run: |
|
|
65
|
+
# Without .codefox.yml the scan aborts before anything is posted,
|
|
66
|
+
# which leaves the PR with no review comment at all.
|
|
67
|
+
if [ ! -f .codefox.yml ]; then
|
|
68
|
+
codefox init --provider "${CODEFOX_PROVIDER:-gemini}" \
|
|
69
|
+
--token "codefox-action-placeholder-token" </dev/null >/dev/null 2>&1 || true
|
|
70
|
+
fi
|
|
71
|
+
|
|
72
|
+
if [ ! -f .codefox.yml ]; then
|
|
73
|
+
echo "::error::CodeFox could not create .codefox.yml in the repository root."
|
|
74
|
+
exit 1
|
|
75
|
+
fi
|
|
76
|
+
|
|
77
|
+
# Embedding the whole checkout is what stalls CI: every chunk costs
|
|
78
|
+
# a CPU embedding on a 2-4 core runner. Unless the repo opted in,
|
|
79
|
+
# cap the index size so the review starts in a bounded time.
|
|
80
|
+
if [ "${CODEFOX_FULL_REPO_RAG:-false}" != "true" ]; then
|
|
81
|
+
CODEFOX_RAG_MAX_CHUNKS="${CODEFOX_RAG_MAX_CHUNKS:-600}"
|
|
82
|
+
CODEFOX_RAG_MAX_FILES="${CODEFOX_RAG_MAX_FILES:-300}"
|
|
83
|
+
export CODEFOX_RAG_MAX_CHUNKS CODEFOX_RAG_MAX_FILES
|
|
84
|
+
python -c "import os, yaml; p = '.codefox.yml'; c = yaml.safe_load(open(p)) or {}; m = c.setdefault('model', {}); m.setdefault('rag_chunk_size', 1000); m.setdefault('rag_chunk_overlap', 200); m.setdefault('rag_max_chunks', int(os.environ['CODEFOX_RAG_MAX_CHUNKS'])); m.setdefault('rag_max_files', int(os.environ['CODEFOX_RAG_MAX_FILES'])); yaml.safe_dump(c, open(p, 'w'), sort_keys=False); print('CodeFox RAG capped at', m['rag_max_chunks'], 'chunks /', m['rag_max_files'], 'files')"
|
|
85
|
+
fi
|
|
86
|
+
|
|
65
87
|
codefox scan \
|
|
66
88
|
--target-branch ${{ github.base_ref }} \
|
|
67
89
|
--source-branch ${{ github.head_ref }} \
|
|
68
|
-
--ci
|
|
90
|
+
--ci
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -125,7 +125,10 @@ class BaseAPI(abc.ABC):
|
|
|
125
125
|
self.rag.save_index()
|
|
126
126
|
return True, None
|
|
127
127
|
except Exception as e:
|
|
128
|
-
|
|
128
|
+
# RAG only enriches the review with project context. Losing it
|
|
129
|
+
# must not abort the scan and leave the PR without a comment.
|
|
130
|
+
self.rag = None
|
|
131
|
+
return True, f"LocalRAG unavailable, reviewing diff only: {e}"
|
|
129
132
|
|
|
130
133
|
@abc.abstractmethod
|
|
131
134
|
def remove_files(self) -> None:
|
|
@@ -55,7 +55,11 @@ class Ollama(BaseAPI):
|
|
|
55
55
|
|
|
56
56
|
def check_connection(self) -> tuple[bool, Any]:
|
|
57
57
|
try:
|
|
58
|
-
|
|
58
|
+
response = requests.get(
|
|
59
|
+
f"{self.base_url}/api/tags",
|
|
60
|
+
timeout=self.model_config.get("timeout", 600),
|
|
61
|
+
)
|
|
62
|
+
response.raise_for_status()
|
|
59
63
|
return True, None
|
|
60
64
|
except Exception as e:
|
|
61
65
|
return False, e
|
|
@@ -154,7 +158,10 @@ class Ollama(BaseAPI):
|
|
|
154
158
|
return response
|
|
155
159
|
|
|
156
160
|
def get_tag_models(self) -> list[str]:
|
|
157
|
-
response = requests.get(
|
|
161
|
+
response = requests.get(
|
|
162
|
+
f"{self.base_url}/api/tags",
|
|
163
|
+
timeout=self.model_config.get("timeout", 600),
|
|
164
|
+
)
|
|
158
165
|
|
|
159
166
|
if response.status_code == 200:
|
|
160
167
|
data = response.json()
|
|
@@ -24,6 +24,9 @@ class GitHubBot:
|
|
|
24
24
|
self.github = Github(self.github_token)
|
|
25
25
|
|
|
26
26
|
def send(self, message: str) -> None:
|
|
27
|
+
if not message or not message.strip():
|
|
28
|
+
raise ValueError("Message must not be empty.")
|
|
29
|
+
|
|
27
30
|
if not self.pr_number or not self.pr_number.isdigit():
|
|
28
31
|
raise RuntimeError(f"Invalid PR_NUMBER: {self.pr_number}")
|
|
29
32
|
|
|
@@ -12,17 +12,30 @@ from codefox.bots.github_bot import GitHubBot
|
|
|
12
12
|
from codefox.bots.gitlab_bot import GitLabBot
|
|
13
13
|
from codefox.cli.base_cli import BaseCLI
|
|
14
14
|
from codefox.cli.list import List
|
|
15
|
-
from codefox.utils.helper import
|
|
15
|
+
from codefox.utils.helper import (
|
|
16
|
+
GitBranchNotFoundError,
|
|
17
|
+
GitRepositoryError,
|
|
18
|
+
Helper,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class EmptyReviewError(RuntimeError):
|
|
23
|
+
"""Raised when the model returns nothing that can be posted as a review."""
|
|
16
24
|
|
|
17
25
|
|
|
18
26
|
class Scan(BaseCLI):
|
|
27
|
+
# Fail loudly instead of posting a blank (or "TypeError"/"None") comment.
|
|
28
|
+
placeholder_texts = frozenset(
|
|
29
|
+
{"", "none", "null", "n/a", "undefined", "no response", "empty"}
|
|
30
|
+
)
|
|
31
|
+
|
|
19
32
|
def __init__(self, model: type[BaseAPI], args: dict[str, Any]):
|
|
20
33
|
self.model = model()
|
|
21
34
|
self.args = args
|
|
22
35
|
|
|
23
36
|
self.github_bot = None
|
|
24
37
|
self.gitlab_bot = None
|
|
25
|
-
if self.args.get("ci", False) and os.getenv("
|
|
38
|
+
if self.args.get("ci", False) and os.getenv("GITHUB_TOKEN"):
|
|
26
39
|
self.github_bot = GitHubBot()
|
|
27
40
|
elif self.args.get("ci", False) and os.getenv("GITLAB_TOKEN"):
|
|
28
41
|
self.gitlab_bot = GitLabBot()
|
|
@@ -30,10 +43,15 @@ class Scan(BaseCLI):
|
|
|
30
43
|
def execute(self) -> None:
|
|
31
44
|
source_branch, target_branch = self._get_branchs()
|
|
32
45
|
|
|
33
|
-
|
|
46
|
+
try:
|
|
47
|
+
diff_text = Helper.get_diff(source_branch, target_branch)
|
|
48
|
+
except (GitRepositoryError, GitBranchNotFoundError) as e:
|
|
49
|
+
print(f"[red]{escape(str(e))}[/red]")
|
|
50
|
+
return
|
|
34
51
|
if not diff_text:
|
|
35
52
|
print(
|
|
36
|
-
"[yellow]
|
|
53
|
+
"[yellow]No changes found between the selected "
|
|
54
|
+
"branches.[/yellow]"
|
|
37
55
|
)
|
|
38
56
|
return
|
|
39
57
|
|
|
@@ -71,6 +89,9 @@ class Scan(BaseCLI):
|
|
|
71
89
|
)
|
|
72
90
|
return
|
|
73
91
|
|
|
92
|
+
if error:
|
|
93
|
+
print("[yellow]Warning: " + escape(str(error)) + "[/yellow]")
|
|
94
|
+
|
|
74
95
|
print("[yellow]Waiting for model response...[/yellow]")
|
|
75
96
|
|
|
76
97
|
if not self.args.get("ci", False):
|
|
@@ -81,14 +102,36 @@ class Scan(BaseCLI):
|
|
|
81
102
|
|
|
82
103
|
def _ci_response_answer(self, diff_text: str) -> None:
|
|
83
104
|
response = self.model.execute(diff_text)
|
|
105
|
+
message = self._clean_review_text(getattr(response, "text", None))
|
|
106
|
+
if not message:
|
|
107
|
+
self.model.remove_files()
|
|
108
|
+
raise EmptyReviewError(
|
|
109
|
+
"The model returned an empty review, so nothing was posted "
|
|
110
|
+
"to the pull request. Reported raw output: "
|
|
111
|
+
f"{str(getattr(response, 'text', None))[:200]!r}. "
|
|
112
|
+
"Check the model name, API key and token limits in "
|
|
113
|
+
".codefox.yml / .codefoxenv."
|
|
114
|
+
)
|
|
115
|
+
|
|
84
116
|
if self.github_bot is not None:
|
|
85
|
-
self.github_bot.send(
|
|
117
|
+
self.github_bot.send(message)
|
|
86
118
|
|
|
87
119
|
if self.gitlab_bot is not None:
|
|
88
|
-
self.gitlab_bot.send(
|
|
120
|
+
self.gitlab_bot.send(message)
|
|
89
121
|
|
|
90
122
|
self.model.remove_files()
|
|
91
123
|
|
|
124
|
+
def _clean_review_text(self, text: Any) -> str:
|
|
125
|
+
"""Normalize model output into either a real comment or ''."""
|
|
126
|
+
if not isinstance(text, str):
|
|
127
|
+
return ""
|
|
128
|
+
|
|
129
|
+
message = text.strip()
|
|
130
|
+
if message.lower() in self.placeholder_texts:
|
|
131
|
+
return ""
|
|
132
|
+
|
|
133
|
+
return message
|
|
134
|
+
|
|
92
135
|
def _classic_response_answer(self, diff_text: str) -> None:
|
|
93
136
|
try:
|
|
94
137
|
response = self.model.execute(diff_text)
|
|
@@ -7,6 +7,21 @@ import yaml
|
|
|
7
7
|
|
|
8
8
|
from codefox.utils.parser import Parser, TreeSitterParser
|
|
9
9
|
|
|
10
|
+
|
|
11
|
+
class GitRepositoryError(RuntimeError):
|
|
12
|
+
"""Raised when the current directory is not inside a Git repository."""
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class GitBranchNotFoundError(RuntimeError):
|
|
16
|
+
"""Raised when a requested source or target branch cannot be resolved."""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
SKIP_DIRS = frozenset({".git", "__pycache__", "node_modules"})
|
|
20
|
+
RAG_ARTIFACT_DIRS = frozenset(
|
|
21
|
+
{"qdrant", "rag_index", "embedding_cache", "codefox_rag"}
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
10
25
|
if TYPE_CHECKING:
|
|
11
26
|
import codefox.utils.local_rag as local_rag
|
|
12
27
|
|
|
@@ -46,11 +61,20 @@ class Helper:
|
|
|
46
61
|
ignored_paths = Helper.read_codefoxignore()
|
|
47
62
|
|
|
48
63
|
all_files_to_upload = []
|
|
49
|
-
for root,
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
64
|
+
for root, dirs, files in os.walk(path_files):
|
|
65
|
+
# Skip vendored dependencies, virtualenvs, caches and generated
|
|
66
|
+
# indexes. Indexing them is what makes real RAG builds crawl:
|
|
67
|
+
# a .venv alone can add thousands of files to embed.
|
|
68
|
+
dirs[:] = [
|
|
69
|
+
d
|
|
70
|
+
for d in dirs
|
|
71
|
+
if not Helper._is_ignored_dir(d)
|
|
72
|
+
and not any(
|
|
73
|
+
ignored in os.path.join(root, d)
|
|
74
|
+
for ignored in ignored_paths
|
|
75
|
+
)
|
|
76
|
+
]
|
|
77
|
+
if any(ignored in root for ignored in ignored_paths):
|
|
54
78
|
continue
|
|
55
79
|
|
|
56
80
|
for filename in files:
|
|
@@ -60,27 +84,58 @@ class Helper:
|
|
|
60
84
|
|
|
61
85
|
return all_files_to_upload
|
|
62
86
|
|
|
87
|
+
@staticmethod
|
|
88
|
+
def _is_ignored_dir(name: str) -> bool:
|
|
89
|
+
return (
|
|
90
|
+
name.startswith(".")
|
|
91
|
+
or name in SKIP_DIRS
|
|
92
|
+
or name in RAG_ARTIFACT_DIRS
|
|
93
|
+
)
|
|
94
|
+
|
|
63
95
|
@staticmethod
|
|
64
96
|
def get_diff(
|
|
65
97
|
source_branch: str | None = None, target_branch: str | None = None
|
|
66
98
|
) -> str | None:
|
|
67
99
|
try:
|
|
68
100
|
repo = git.Repo(".", search_parent_directories=True)
|
|
69
|
-
|
|
70
|
-
if source_branch and target_branch:
|
|
71
|
-
diff_text = repo.git.diff(
|
|
72
|
-
f"origin/{target_branch}...origin/{source_branch}"
|
|
73
|
-
)
|
|
74
|
-
else:
|
|
75
|
-
diff_text = repo.git.diff()
|
|
76
|
-
|
|
77
|
-
return cast(str | None, diff_text)
|
|
78
101
|
except (
|
|
79
102
|
git.exc.InvalidGitRepositoryError,
|
|
80
103
|
git.exc.NoSuchPathError,
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
104
|
+
) as e:
|
|
105
|
+
raise GitRepositoryError(
|
|
106
|
+
"The current directory is not a Git repository. "
|
|
107
|
+
"Run this command inside a checkout containing .git."
|
|
108
|
+
) from e
|
|
109
|
+
|
|
110
|
+
if source_branch and target_branch:
|
|
111
|
+
refs: dict[str, str] = {}
|
|
112
|
+
for branch in (source_branch, target_branch):
|
|
113
|
+
for ref in (f"origin/{branch}", branch):
|
|
114
|
+
try:
|
|
115
|
+
refs[branch] = str(repo.commit(ref))
|
|
116
|
+
break
|
|
117
|
+
except (git.exc.BadName, ValueError):
|
|
118
|
+
continue
|
|
119
|
+
if branch not in refs:
|
|
120
|
+
raise GitBranchNotFoundError(
|
|
121
|
+
f"Branch '{branch}' does not exist locally or on "
|
|
122
|
+
"the configured origin remote."
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
try:
|
|
126
|
+
return cast(
|
|
127
|
+
str | None,
|
|
128
|
+
repo.git.diff(
|
|
129
|
+
f"{refs[target_branch]}...{refs[source_branch]}"
|
|
130
|
+
),
|
|
131
|
+
)
|
|
132
|
+
except git.exc.GitCommandError as e:
|
|
133
|
+
raise RuntimeError(f"Unable to compare branches: {e}") from e
|
|
134
|
+
|
|
135
|
+
try:
|
|
136
|
+
return cast(str | None, repo.git.diff())
|
|
137
|
+
except git.exc.GitCommandError as e:
|
|
138
|
+
raise RuntimeError(f"Unable to read the Git diff: {e}") from e
|
|
84
139
|
|
|
85
140
|
# ------------------------------------------------------------------
|
|
86
141
|
# Backward‑compatible wrappers - delegate to Parser
|
|
@@ -30,7 +30,15 @@ class LocalRAG:
|
|
|
30
30
|
self.console = Console()
|
|
31
31
|
self.console.print("[bold cyan]Initializing LocalRAG...[/bold cyan]")
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
# NLTK 3.9 sentence tokenization needs punkt_tab, not just punkt.
|
|
34
|
+
for package in ("punkt", "punkt_tab"):
|
|
35
|
+
try:
|
|
36
|
+
nltk.download(package, quiet=True)
|
|
37
|
+
except Exception as e:
|
|
38
|
+
self.console.print(
|
|
39
|
+
f"[yellow]Warning: could not download NLTK data "
|
|
40
|
+
f"'{package}': {e}[/yellow]"
|
|
41
|
+
)
|
|
34
42
|
|
|
35
43
|
ram_gb = psutil.virtual_memory().total / math.pow(1024, 3)
|
|
36
44
|
if ram_gb < 8:
|
|
@@ -135,6 +143,7 @@ class LocalRAG:
|
|
|
135
143
|
step = chunk_size
|
|
136
144
|
|
|
137
145
|
files_read = 0
|
|
146
|
+
failed_files: list[tuple[str, str]] = []
|
|
138
147
|
for file in track(
|
|
139
148
|
self.all_files,
|
|
140
149
|
description="[cyan]Reading & chunking files...[/cyan]",
|
|
@@ -154,13 +163,20 @@ class LocalRAG:
|
|
|
154
163
|
path, content, chunk_size, chunk_overlap
|
|
155
164
|
)
|
|
156
165
|
|
|
166
|
+
if not chunks:
|
|
167
|
+
failed_files.append((file, "no chunks produced"))
|
|
168
|
+
|
|
157
169
|
for chunk_text in chunks:
|
|
158
170
|
texts.append(chunk_text)
|
|
159
171
|
self.files.append({"path": file, "text": chunk_text})
|
|
160
172
|
|
|
161
173
|
if max_chunks is not None and len(texts) >= max_chunks:
|
|
162
174
|
break
|
|
163
|
-
except Exception:
|
|
175
|
+
except Exception as e:
|
|
176
|
+
# Keep going, but never lose the reason silently: an empty
|
|
177
|
+
# message here used to surface as a bare bm25s crash later.
|
|
178
|
+
reason = str(e).strip() or f"{type(e).__name__} (no message)"
|
|
179
|
+
failed_files.append((file, reason))
|
|
164
180
|
continue
|
|
165
181
|
|
|
166
182
|
self.console.print(
|
|
@@ -168,6 +184,29 @@ class LocalRAG:
|
|
|
168
184
|
f"{files_read} files."
|
|
169
185
|
)
|
|
170
186
|
|
|
187
|
+
if failed_files:
|
|
188
|
+
self.console.print(
|
|
189
|
+
f"[yellow]⚠ {len(failed_files)} file(s) could not be "
|
|
190
|
+
"chunked and were skipped:[/yellow]"
|
|
191
|
+
)
|
|
192
|
+
for failed_path, reason in failed_files[:5]:
|
|
193
|
+
self.console.print(f" [dim]{failed_path}: {reason}[/dim]")
|
|
194
|
+
if len(failed_files) > 5:
|
|
195
|
+
self.console.print(
|
|
196
|
+
f" [dim]... and {len(failed_files) - 5} more[/dim]"
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
if not texts:
|
|
200
|
+
first_error = (
|
|
201
|
+
f"{failed_files[0][0]}: {failed_files[0][1]}"
|
|
202
|
+
if failed_files
|
|
203
|
+
else "no readable files"
|
|
204
|
+
)
|
|
205
|
+
raise ValueError(
|
|
206
|
+
f"No chunks could be built from {files_read} file(s) in "
|
|
207
|
+
f"'{self.files_path}'. First error: {first_error}"
|
|
208
|
+
)
|
|
209
|
+
|
|
171
210
|
with self.console.status(
|
|
172
211
|
"[yellow]Tokenizing and building BM25 index...[/yellow]"
|
|
173
212
|
):
|
|
@@ -131,30 +131,103 @@ class Parser:
|
|
|
131
131
|
return chunks
|
|
132
132
|
|
|
133
133
|
@classmethod
|
|
134
|
-
def
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
134
|
+
def _split_sentences(cls, text: str) -> list[str]:
|
|
135
|
+
"""Split text into sentences without depending on NLTK data files.
|
|
136
|
+
|
|
137
|
+
NLTK 3.9 `sent_tokenize` needs the `punkt_tab` package and its
|
|
138
|
+
English pickled model is large; splitting plain code with it is slow
|
|
139
|
+
and pointless, so code-like text uses a cheap line-aware split.
|
|
140
|
+
"""
|
|
141
|
+
if cls._looks_like_code(text):
|
|
142
|
+
return [
|
|
143
|
+
line for line in text.splitlines(keepends=True) if line.strip()
|
|
144
|
+
]
|
|
140
145
|
|
|
141
|
-
|
|
146
|
+
try:
|
|
147
|
+
sentences = sent_tokenize(text)
|
|
148
|
+
except Exception:
|
|
149
|
+
sentences = re.split(r"(?<=[.!?])\s+", text)
|
|
142
150
|
|
|
143
|
-
for
|
|
144
|
-
current.append(sent)
|
|
145
|
-
current_len += len(sent)
|
|
151
|
+
return [s for s in sentences if s.strip()]
|
|
146
152
|
|
|
147
|
-
|
|
148
|
-
|
|
153
|
+
@classmethod
|
|
154
|
+
def _looks_like_code(cls, text: str) -> bool:
|
|
155
|
+
code_lines = 0
|
|
156
|
+
total = 0
|
|
157
|
+
for line in text.splitlines():
|
|
158
|
+
stripped = line.strip()
|
|
159
|
+
if not stripped:
|
|
160
|
+
continue
|
|
161
|
+
total += 1
|
|
162
|
+
code_like = stripped.endswith(
|
|
163
|
+
(";", "{", "}", ":", "->", ",", "(")
|
|
164
|
+
) or bool(
|
|
165
|
+
re.match(
|
|
166
|
+
r"^(def |class |import |from |return|if |for |while "
|
|
167
|
+
r"|#|@|\w+\()",
|
|
168
|
+
stripped,
|
|
169
|
+
)
|
|
170
|
+
)
|
|
171
|
+
if code_like:
|
|
172
|
+
code_lines += 1
|
|
173
|
+
if total >= 40:
|
|
174
|
+
break
|
|
149
175
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
176
|
+
return total > 0 and code_lines / total >= 0.5
|
|
177
|
+
|
|
178
|
+
@classmethod
|
|
179
|
+
def _pack(
|
|
180
|
+
cls, units: list[str], chunk_size: int, overlap: int
|
|
181
|
+
) -> list[str]:
|
|
182
|
+
"""Pack units into chunks of at most `chunk_size` characters.
|
|
183
|
+
|
|
184
|
+
Long units are hard-sliced and the overlap tail is only added when
|
|
185
|
+
it still fits, so no chunk exceeds the requested size. The budget
|
|
186
|
+
keeps room for the single separator inserted when joining.
|
|
187
|
+
"""
|
|
188
|
+
budget = max(chunk_size - 1, 1)
|
|
189
|
+
overlap = min(overlap, budget)
|
|
190
|
+
step = max(budget - overlap, 1)
|
|
191
|
+
|
|
192
|
+
flattened: list[str] = []
|
|
193
|
+
for unit in units:
|
|
194
|
+
if len(unit) <= budget:
|
|
195
|
+
flattened.append(unit)
|
|
196
|
+
continue
|
|
197
|
+
flattened.extend(
|
|
198
|
+
unit[i : i + budget] for i in range(0, len(unit), step)
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
chunks: list[str] = []
|
|
202
|
+
current: list[str] = []
|
|
203
|
+
current_len = 0
|
|
204
|
+
|
|
205
|
+
for unit in flattened:
|
|
206
|
+
if current and current_len + 1 + len(unit) > budget:
|
|
207
|
+
text = " ".join(current)
|
|
208
|
+
chunks.append(text)
|
|
209
|
+
tail = text[-overlap:] if overlap else ""
|
|
210
|
+
while tail and len(tail) + 1 + len(unit) > budget:
|
|
211
|
+
tail = tail[1:]
|
|
212
|
+
current = [tail] if tail else []
|
|
213
|
+
current_len = len(tail)
|
|
214
|
+
|
|
215
|
+
current_len += (1 if current else 0) + len(unit)
|
|
216
|
+
current.append(unit)
|
|
153
217
|
|
|
154
218
|
if current:
|
|
155
219
|
chunks.append(" ".join(current))
|
|
156
220
|
|
|
157
|
-
return chunks
|
|
221
|
+
return [c for c in chunks if c.strip()]
|
|
222
|
+
|
|
223
|
+
@classmethod
|
|
224
|
+
def chunk_text_sentences(
|
|
225
|
+
cls, text: str, chunk_size: int, overlap: int
|
|
226
|
+
) -> list[str]:
|
|
227
|
+
if not text.strip():
|
|
228
|
+
return []
|
|
229
|
+
|
|
230
|
+
return cls._pack(cls._split_sentences(text), chunk_size, overlap)
|
|
158
231
|
|
|
159
232
|
@classmethod
|
|
160
233
|
def smart_chunk(
|
|
@@ -162,11 +235,21 @@ class Parser:
|
|
|
162
235
|
) -> list:
|
|
163
236
|
ext = path.suffix.lower()
|
|
164
237
|
|
|
165
|
-
|
|
238
|
+
try:
|
|
239
|
+
parser = cls.get_ts_parser_by_extension(ext)
|
|
240
|
+
except Exception:
|
|
241
|
+
parser = None
|
|
166
242
|
|
|
167
243
|
if parser:
|
|
168
|
-
|
|
244
|
+
try:
|
|
245
|
+
chunks = cls.chunk_code_with_ts(parser, content)
|
|
246
|
+
except Exception:
|
|
247
|
+
chunks = []
|
|
169
248
|
if chunks:
|
|
249
|
+
# Whole functions/classes can exceed chunk_size; pack them
|
|
250
|
+
# so the embedding cost per file stays bounded.
|
|
251
|
+
if max(len(c) for c in chunks) > chunk_size:
|
|
252
|
+
return cls._pack(chunks, chunk_size, overlap)
|
|
170
253
|
return chunks
|
|
171
254
|
|
|
172
255
|
return cls.chunk_text_sentences(content, chunk_size, overlap)
|
|
@@ -7,7 +7,7 @@ README = (HERE / "README.md").read_text(encoding="utf-8") if (HERE / "README.md"
|
|
|
7
7
|
|
|
8
8
|
setup(
|
|
9
9
|
name="codefox",
|
|
10
|
-
version="0.4.
|
|
10
|
+
version="0.4.7",
|
|
11
11
|
description="CodeFox CLI - code auditing and code review tool",
|
|
12
12
|
long_description=README,
|
|
13
13
|
long_description_content_type="text/markdown",
|
|
@@ -104,3 +104,21 @@ def test_timeout_default_and_validation() -> None:
|
|
|
104
104
|
"review": {},
|
|
105
105
|
}
|
|
106
106
|
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def test_upload_files_falls_back_to_diff_only_when_rag_fails(
|
|
110
|
+
sample_config: dict,
|
|
111
|
+
) -> None:
|
|
112
|
+
with patch("codefox.api.gemini.genai.Client"):
|
|
113
|
+
api = Gemini(sample_config)
|
|
114
|
+
|
|
115
|
+
with patch(
|
|
116
|
+
"codefox.api.base_api.LocalRAG",
|
|
117
|
+
side_effect=ValueError("No chunks could be built"),
|
|
118
|
+
):
|
|
119
|
+
ok, message = api.upload_files(".")
|
|
120
|
+
|
|
121
|
+
assert ok is True
|
|
122
|
+
assert api.rag is None
|
|
123
|
+
assert "reviewing diff only" in message
|
|
124
|
+
assert "No chunks could be built" in message
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""Tests for GitHub bot."""
|
|
2
|
+
|
|
3
|
+
from unittest.mock import MagicMock, patch
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from codefox.bots.github_bot import GitHubBot
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _bot(monkeypatch: pytest.MonkeyPatch) -> GitHubBot:
|
|
11
|
+
monkeypatch.setenv("GITHUB_TOKEN", "token")
|
|
12
|
+
monkeypatch.setenv("GITHUB_REPOSITORY", "owner/repo")
|
|
13
|
+
monkeypatch.setenv("PR_NUMBER", "12")
|
|
14
|
+
|
|
15
|
+
with patch("codefox.bots.github_bot.Github"):
|
|
16
|
+
return GitHubBot()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_send_creates_issue_comment(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
20
|
+
with patch("codefox.bots.github_bot.Github") as mock_github_class:
|
|
21
|
+
bot = _bot(monkeypatch)
|
|
22
|
+
bot.github = mock_github_class.return_value
|
|
23
|
+
mock_pull = bot.github.get_repo.return_value.get_pull.return_value
|
|
24
|
+
|
|
25
|
+
bot.send("hello")
|
|
26
|
+
|
|
27
|
+
bot.github.get_repo.assert_called_once_with("owner/repo")
|
|
28
|
+
bot.github.get_repo.return_value.get_pull.assert_called_once_with(12)
|
|
29
|
+
mock_pull.create_issue_comment.assert_called_once_with("hello")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_send_rejects_empty_message(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
33
|
+
bot = _bot(monkeypatch)
|
|
34
|
+
bot.github = MagicMock()
|
|
35
|
+
|
|
36
|
+
with pytest.raises(ValueError, match="Message must not be empty"):
|
|
37
|
+
bot.send(" \n ")
|
|
38
|
+
|
|
39
|
+
bot.github.get_repo.assert_not_called()
|
|
@@ -5,7 +5,11 @@ from pathlib import Path
|
|
|
5
5
|
|
|
6
6
|
import pytest
|
|
7
7
|
|
|
8
|
-
from codefox.utils.helper import
|
|
8
|
+
from codefox.utils.helper import (
|
|
9
|
+
GitBranchNotFoundError,
|
|
10
|
+
GitRepositoryError,
|
|
11
|
+
Helper,
|
|
12
|
+
)
|
|
9
13
|
|
|
10
14
|
|
|
11
15
|
def test_read_yml_missing_file_raises() -> None:
|
|
@@ -26,6 +30,32 @@ def test_read_yml_valid(tmp_path: Path, sample_config: dict) -> None:
|
|
|
26
30
|
assert result["model"]["temperature"] == 0.2
|
|
27
31
|
|
|
28
32
|
|
|
33
|
+
def test_get_diff_outside_git_repository_is_explicit(tmp_path: Path) -> None:
|
|
34
|
+
previous = os.getcwd()
|
|
35
|
+
try:
|
|
36
|
+
os.chdir(tmp_path)
|
|
37
|
+
with pytest.raises(GitRepositoryError, match="not a Git repository"):
|
|
38
|
+
Helper.get_diff()
|
|
39
|
+
finally:
|
|
40
|
+
os.chdir(previous)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def test_get_diff_missing_branch_is_explicit(tmp_path: Path) -> None:
|
|
44
|
+
import git
|
|
45
|
+
|
|
46
|
+
repo = git.Repo.init(tmp_path)
|
|
47
|
+
(tmp_path / "file.py").write_text("print('ok')\n", encoding="utf-8")
|
|
48
|
+
repo.index.add(["file.py"])
|
|
49
|
+
repo.index.commit("initial")
|
|
50
|
+
previous = os.getcwd()
|
|
51
|
+
try:
|
|
52
|
+
os.chdir(tmp_path)
|
|
53
|
+
with pytest.raises(GitBranchNotFoundError, match="missing"):
|
|
54
|
+
Helper.get_diff("missing", "main")
|
|
55
|
+
finally:
|
|
56
|
+
os.chdir(previous)
|
|
57
|
+
|
|
58
|
+
|
|
29
59
|
def test_read_codefoxignore_missing_returns_empty(tmp_path: Path) -> None:
|
|
30
60
|
prev = os.getcwd()
|
|
31
61
|
try:
|
|
@@ -164,3 +194,34 @@ def test_smart_chunk_fallback_to_sentences() -> None:
|
|
|
164
194
|
chunks = Helper.smart_chunk(path, content, chunk_size=25, overlap=5)
|
|
165
195
|
assert len(chunks) >= 1
|
|
166
196
|
assert "First" in chunks[0] or "First" in " ".join(chunks)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def test_get_all_files_skips_rag_artifacts(tmp_path) -> None:
|
|
200
|
+
from codefox.utils.helper import Helper
|
|
201
|
+
|
|
202
|
+
(tmp_path / "app.py").write_text("x = 1\n", encoding="utf-8")
|
|
203
|
+
for name in ("rag_index", "qdrant", "embedding_cache"):
|
|
204
|
+
d = tmp_path / name
|
|
205
|
+
d.mkdir()
|
|
206
|
+
(d / "leak.py").write_text("y = 2\n", encoding="utf-8")
|
|
207
|
+
|
|
208
|
+
found = Helper.get_all_files(str(tmp_path))
|
|
209
|
+
names = {f.rsplit("/", 1)[-1] for f in found}
|
|
210
|
+
|
|
211
|
+
assert "app.py" in names
|
|
212
|
+
assert "leak.py" not in names
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def test_get_all_files_skips_hidden_and_vendored_dirs(tmp_path) -> None:
|
|
216
|
+
from codefox.utils.helper import Helper
|
|
217
|
+
|
|
218
|
+
(tmp_path / "app.py").write_text("x = 1\n", encoding="utf-8")
|
|
219
|
+
for name in (".venv", ".mypy_cache", "node_modules", ".git"):
|
|
220
|
+
d = tmp_path / name / "pkg"
|
|
221
|
+
d.mkdir(parents=True)
|
|
222
|
+
(d / "vendored.py").write_text("y = 2\n", encoding="utf-8")
|
|
223
|
+
|
|
224
|
+
names = {f.rsplit("/", 1)[-1] for f in Helper.get_all_files(str(tmp_path))}
|
|
225
|
+
|
|
226
|
+
assert "app.py" in names
|
|
227
|
+
assert "vendored.py" not in names
|
|
@@ -105,3 +105,71 @@ def test_get_model_tag_returns_list() -> None:
|
|
|
105
105
|
assert isinstance(tags, list)
|
|
106
106
|
assert "BAAI/bge-small-en-v1.5" in tags
|
|
107
107
|
assert "other" in tags
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def test_build_reports_error_and_skipped_files() -> None:
|
|
111
|
+
import bm25s
|
|
112
|
+
|
|
113
|
+
rag = LocalRAG.__new__(LocalRAG)
|
|
114
|
+
from rich.console import Console
|
|
115
|
+
|
|
116
|
+
rag.console = Console()
|
|
117
|
+
rag.all_files = ["bad.py", "good.py"]
|
|
118
|
+
rag.kwargs = rag._get_kwargs()
|
|
119
|
+
rag.retriever = bm25s.BM25()
|
|
120
|
+
rag.files = []
|
|
121
|
+
rag.client = None
|
|
122
|
+
rag.chunks = []
|
|
123
|
+
rag.embedding_name = "BAAI/bge-small-en-v1.5"
|
|
124
|
+
rag.files_path = "."
|
|
125
|
+
|
|
126
|
+
with patch(
|
|
127
|
+
"codefox.utils.parser.Parser.smart_chunk",
|
|
128
|
+
side_effect=LookupError(""),
|
|
129
|
+
):
|
|
130
|
+
with pytest.raises(ValueError, match="No chunks could be built"):
|
|
131
|
+
rag.build()
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def test_smart_chunk_survives_missing_nltk_data() -> None:
|
|
135
|
+
from codefox.utils.parser import Parser
|
|
136
|
+
|
|
137
|
+
content = "def f():\n return 1\n\ndef g():\n return 2\n"
|
|
138
|
+
with patch("codefox.utils.parser.sent_tokenize", side_effect=LookupError):
|
|
139
|
+
with patch.object(
|
|
140
|
+
Parser, "get_ts_parser_by_extension", return_value=None
|
|
141
|
+
):
|
|
142
|
+
chunks = Parser.smart_chunk(Path("x.py"), content, 300, 50)
|
|
143
|
+
|
|
144
|
+
assert chunks
|
|
145
|
+
assert "".join(chunks).replace(" ", "").count("return") == 2
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def test_chunk_size_is_honored() -> None:
|
|
149
|
+
from codefox.utils.parser import Parser
|
|
150
|
+
|
|
151
|
+
text = "\n".join(
|
|
152
|
+
f'def f{i}():\n return "value {i}"\n' for i in range(400)
|
|
153
|
+
)
|
|
154
|
+
chunks = Parser.chunk_text_sentences(text, 300, 50)
|
|
155
|
+
|
|
156
|
+
assert chunks
|
|
157
|
+
assert max(len(c) for c in chunks) <= 300
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def test_code_is_not_tokenized_as_prose() -> None:
|
|
161
|
+
from codefox.utils.parser import Parser
|
|
162
|
+
|
|
163
|
+
code = "def a():\n return 1\n\nclass B:\n pass\n"
|
|
164
|
+
assert Parser._looks_like_code(code) is True
|
|
165
|
+
assert Parser._looks_like_code("Hello world. This is prose.") is False
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def test_regex_fallback_chunks_remain_bounded() -> None:
|
|
169
|
+
from codefox.utils.parser import Parser
|
|
170
|
+
|
|
171
|
+
text = "line of code;\n" * 2000
|
|
172
|
+
chunks = Parser.chunk_text_sentences(text, 1000, 200)
|
|
173
|
+
|
|
174
|
+
assert chunks
|
|
175
|
+
assert max(len(c) for c in chunks) <= 1000
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""Tests for Ollama provider connectivity and model listing."""
|
|
2
|
+
|
|
3
|
+
from unittest.mock import MagicMock, patch
|
|
4
|
+
|
|
5
|
+
from codefox.api.ollama import Ollama
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def test_check_connection_does_not_require_default_model() -> None:
|
|
9
|
+
with patch("codefox.api.base_api.Helper.read_yml") as read_yml:
|
|
10
|
+
read_yml.return_value = {
|
|
11
|
+
"model": {"name": "llama3.1:8b"},
|
|
12
|
+
"review": {},
|
|
13
|
+
}
|
|
14
|
+
with patch("codefox.api.ollama.Client"):
|
|
15
|
+
provider = Ollama()
|
|
16
|
+
|
|
17
|
+
response = MagicMock(status_code=200)
|
|
18
|
+
with patch(
|
|
19
|
+
"codefox.api.ollama.requests.get", return_value=response
|
|
20
|
+
) as get:
|
|
21
|
+
connected, error = provider.check_connection()
|
|
22
|
+
|
|
23
|
+
assert connected is True
|
|
24
|
+
assert error is None
|
|
25
|
+
get.assert_called_once()
|
|
26
|
+
response.raise_for_status.assert_called_once()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_get_tag_models_returns_empty_list_when_no_models() -> None:
|
|
30
|
+
with patch("codefox.api.base_api.Helper.read_yml") as read_yml:
|
|
31
|
+
read_yml.return_value = {
|
|
32
|
+
"model": {"name": "llama3.1:8b"},
|
|
33
|
+
"review": {},
|
|
34
|
+
}
|
|
35
|
+
with patch("codefox.api.ollama.Client"):
|
|
36
|
+
provider = Ollama()
|
|
37
|
+
|
|
38
|
+
response = MagicMock(status_code=200)
|
|
39
|
+
response.json.return_value = {"models": []}
|
|
40
|
+
with patch("codefox.api.ollama.requests.get", return_value=response):
|
|
41
|
+
assert provider.get_tag_models() == []
|
|
@@ -2,8 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
from unittest.mock import MagicMock, patch
|
|
4
4
|
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
5
7
|
from codefox.api.gemini import Gemini
|
|
6
|
-
from codefox.cli.scan import Scan
|
|
8
|
+
from codefox.cli.scan import EmptyReviewError, Scan
|
|
9
|
+
from codefox.utils.helper import GitBranchNotFoundError, GitRepositoryError
|
|
7
10
|
|
|
8
11
|
|
|
9
12
|
def test_get_branchs_from_args() -> None:
|
|
@@ -30,6 +33,33 @@ def test_get_branchs_from_args() -> None:
|
|
|
30
33
|
assert tgt == "main"
|
|
31
34
|
|
|
32
35
|
|
|
36
|
+
def test_execute_reports_missing_git_repository() -> None:
|
|
37
|
+
scan = Scan.__new__(Scan)
|
|
38
|
+
scan.args = {}
|
|
39
|
+
scan.model = MagicMock()
|
|
40
|
+
scan.model.review_config = {"sourceBranch": None, "targetBranch": None}
|
|
41
|
+
with patch("codefox.cli.scan.Helper.get_diff") as get_diff:
|
|
42
|
+
get_diff.side_effect = GitRepositoryError("not a Git repository")
|
|
43
|
+
with patch("codefox.cli.scan.print") as output:
|
|
44
|
+
Scan.execute(scan)
|
|
45
|
+
output.assert_called_once()
|
|
46
|
+
assert "not a Git repository" in output.call_args.args[0]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_execute_reports_missing_branch() -> None:
|
|
50
|
+
scan = Scan.__new__(Scan)
|
|
51
|
+
scan.args = {}
|
|
52
|
+
scan.model = MagicMock()
|
|
53
|
+
scan.model.review_config = {"sourceBranch": None, "targetBranch": None}
|
|
54
|
+
with patch("codefox.cli.scan.Helper.get_diff") as get_diff:
|
|
55
|
+
get_diff.side_effect = GitBranchNotFoundError(
|
|
56
|
+
"Branch 'missing' does not exist"
|
|
57
|
+
)
|
|
58
|
+
with patch("codefox.cli.scan.print") as output:
|
|
59
|
+
Scan.execute(scan)
|
|
60
|
+
assert "missing" in output.call_args.args[0]
|
|
61
|
+
|
|
62
|
+
|
|
33
63
|
def test_get_branchs_from_config_when_args_empty() -> None:
|
|
34
64
|
with patch("codefox.api.gemini.genai.Client"):
|
|
35
65
|
with patch("codefox.api.base_api.Helper.read_yml") as m:
|
|
@@ -73,3 +103,33 @@ def test_get_branchs_prefers_args_over_config() -> None:
|
|
|
73
103
|
src, tgt = scan._get_branchs()
|
|
74
104
|
assert src == "feat"
|
|
75
105
|
assert tgt == "master"
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _ci_scan(response_text: object) -> Scan:
|
|
109
|
+
scan = Scan.__new__(Scan)
|
|
110
|
+
scan.args = {"ci": True}
|
|
111
|
+
scan.model = MagicMock()
|
|
112
|
+
scan.model.execute.return_value = MagicMock(text=response_text)
|
|
113
|
+
scan.github_bot = MagicMock()
|
|
114
|
+
scan.gitlab_bot = None
|
|
115
|
+
return scan
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def test_ci_response_posts_model_text() -> None:
|
|
119
|
+
scan = _ci_scan(" ### CodeFox Audit Report\nfine ")
|
|
120
|
+
scan._ci_response_answer("diff")
|
|
121
|
+
scan.github_bot.send.assert_called_once_with(
|
|
122
|
+
"### CodeFox Audit Report\nfine"
|
|
123
|
+
)
|
|
124
|
+
scan.model.remove_files.assert_called_once()
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@pytest.mark.parametrize("text", ["", " \n\t", None, "None", "null"])
|
|
128
|
+
def test_ci_response_raises_instead_of_posting_empty_comment(
|
|
129
|
+
text: object,
|
|
130
|
+
) -> None:
|
|
131
|
+
scan = _ci_scan(text)
|
|
132
|
+
with pytest.raises(EmptyReviewError, match="empty review"):
|
|
133
|
+
scan._ci_response_answer("diff")
|
|
134
|
+
scan.github_bot.send.assert_not_called()
|
|
135
|
+
scan.model.remove_files.assert_called_once()
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
version = 1
|
|
2
|
-
revision =
|
|
2
|
+
revision = 5
|
|
3
3
|
requires-python = ">=3.11"
|
|
4
4
|
resolution-markers = [
|
|
5
5
|
"python_full_version >= '3.14'",
|
|
@@ -217,7 +217,7 @@ wheels = [
|
|
|
217
217
|
|
|
218
218
|
[[package]]
|
|
219
219
|
name = "codefox"
|
|
220
|
-
version = "0.4.
|
|
220
|
+
version = "0.4.6"
|
|
221
221
|
source = { editable = "." }
|
|
222
222
|
dependencies = [
|
|
223
223
|
{ name = "bm25s" },
|
|
@@ -2212,6 +2212,14 @@ version = "0.23.1"
|
|
|
2212
2212
|
source = { registry = "https://pypi.org/simple" }
|
|
2213
2213
|
sdist = { url = "https://files.pythonhosted.org/packages/22/85/a61c782afbb706a47d990eaee6977e7c2bd013771c5bf5c81c617684f286/tree_sitter_c_sharp-0.23.1.tar.gz", hash = "sha256:322e2cfd3a547a840375276b2aea3335fa6458aeac082f6c60fec3f745c967eb", size = 1317728, upload-time = "2024-11-11T05:25:32.535Z" }
|
|
2214
2214
|
wheels = [
|
|
2215
|
+
{ url = "https://files.pythonhosted.org/packages/4c/dc/d4a0ad9e466263728f80f9dac399609473af01c1aba2ea3ea8879ce56276/tree_sitter_c_sharp-0.23.1-cp310-abi3-macosx_10_9_x86_64.whl", hash = "sha256:e87be7572991552606a3155d2f6c2045ded8bce94bfd9f74bf521d949c219a1c", size = 333661, upload-time = "2026-04-14T15:11:14.227Z" },
|
|
2216
|
+
{ url = "https://files.pythonhosted.org/packages/61/7a/5c862770460a2e27079e725585ad2718100373c09448c14e36934ef44414/tree_sitter_c_sharp-0.23.1-cp310-abi3-macosx_11_0_arm64.whl", hash = "sha256:86c2fdf178c66474a1be2965602818d30780e4e3ed890e3c206931f65d9a154c", size = 376295, upload-time = "2026-04-14T15:11:15.346Z" },
|
|
2217
|
+
{ url = "https://files.pythonhosted.org/packages/67/18/0571a3a34c0feda60a9c37cf6dd5edfdbc24f8fcb1e48b6b6eb0f324ad2a/tree_sitter_c_sharp-0.23.1-cp310-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:035d259e64c41d02cc45afc3b8b46388b232e7d16d84734d851cca7334761da5", size = 358331, upload-time = "2026-04-14T15:11:16.418Z" },
|
|
2218
|
+
{ url = "https://files.pythonhosted.org/packages/44/65/0f7e1f50f6365338eb700f01710da0adc49a49fa9a8443e5a90ea4f29491/tree_sitter_c_sharp-0.23.1-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:fa472cb9de7e14fee9408e144f29f68384cd8e9c677dff0002da19f361a59bdf", size = 359444, upload-time = "2026-04-14T15:11:17.509Z" },
|
|
2219
|
+
{ url = "https://files.pythonhosted.org/packages/98/60/129bd56d5ef22b4ae254940a09b6d3ed873093218868a3f9635d571d514e/tree_sitter_c_sharp-0.23.1-cp310-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:1a0ea86eccff74e85ab4a2cf77c813fad7c84162962ce242dff0c51601028832", size = 358143, upload-time = "2026-04-14T15:11:18.755Z" },
|
|
2220
|
+
{ url = "https://files.pythonhosted.org/packages/7c/cd/e12cdca47e0c56151cb4b156d48091b7bc1d968e072c1656cf6b73fe7218/tree_sitter_c_sharp-0.23.1-cp310-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:8ab26dc998bbd4b4287b129f67c10ca715deb402ed77d0645674490ea509097e", size = 357524, upload-time = "2026-04-14T15:11:19.717Z" },
|
|
2221
|
+
{ url = "https://files.pythonhosted.org/packages/6a/2c/f742d60f818cba83760f4975c7158d1c96c36b5807e95a843db7fb8c64b7/tree_sitter_c_sharp-0.23.1-cp310-abi3-win_amd64.whl", hash = "sha256:d4486653feaff3314ef45534dcb6f9ea8ab3aa160896287c6473788f88eb38be", size = 338755, upload-time = "2026-04-14T15:11:20.883Z" },
|
|
2222
|
+
{ url = "https://files.pythonhosted.org/packages/5b/e4/8a8642b9bba86248ac2facc81ffb187c06c6768efa56c79d61fab70d736b/tree_sitter_c_sharp-0.23.1-cp310-abi3-win_arm64.whl", hash = "sha256:e7a14b76ec23cc8386cf662d5ea602d81331376c93ca6299a97b174047790345", size = 337261, upload-time = "2026-04-14T15:11:22.111Z" },
|
|
2215
2223
|
{ url = "https://files.pythonhosted.org/packages/58/04/f6c2df4c53a588ccd88d50851155945cff8cd887bd70c175e00aaade7edf/tree_sitter_c_sharp-0.23.1-cp39-abi3-macosx_10_9_x86_64.whl", hash = "sha256:2b612a6e5bd17bb7fa2aab4bb6fc1fba45c94f09cb034ab332e45603b86e32fd", size = 372235, upload-time = "2024-11-11T05:25:19.424Z" },
|
|
2216
2224
|
{ url = "https://files.pythonhosted.org/packages/99/10/1aa9486f1e28fc22810fa92cbdc54e1051e7f5536a5e5b5e9695f609b31e/tree_sitter_c_sharp-0.23.1-cp39-abi3-macosx_11_0_arm64.whl", hash = "sha256:1a8b98f62bc53efcd4d971151950c9b9cd5cbe3bacdb0cd69fdccac63350d83e", size = 419046, upload-time = "2024-11-11T05:25:20.679Z" },
|
|
2217
2225
|
{ url = "https://files.pythonhosted.org/packages/0f/21/13df29f8fcb9ba9f209b7b413a4764b673dfd58989a0dd67e9c7e19e9c2e/tree_sitter_c_sharp-0.23.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:986e93d845a438ec3c4416401aa98e6a6f6631d644bbbc2e43fcb915c51d255d", size = 415999, upload-time = "2024-11-11T05:25:22.359Z" },
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|