Skip to content

Commit 9e21f9f

Browse files
authored
fix: supports directories for link annotation (#486)
1 parent dbed11c commit 9e21f9f

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

src/codegen/extensions/tools/link_annotation.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def add_links_to_message(message: str, codebase: Codebase, channel: MessageChann
9595
9696
This function:
9797
1. Links code snippets that match symbol names
98-
2. Links anything that looks like a filepath
98+
2. Links anything that looks like a filepath (files or directories)
9999
100100
Args:
101101
message: The message to process
@@ -109,10 +109,34 @@ def add_links_to_message(message: str, codebase: Codebase, channel: MessageChann
109109
for snippet in snippets:
110110
# Filepaths
111111
if is_likely_filepath(snippet):
112-
file = codebase.get_file(snippet, optional=True)
113-
if file:
114-
link = format_link(snippet, file.github_url, channel)
115-
message = message.replace(f"`{snippet}`", link)
112+
# Try as file first
113+
try:
114+
file = codebase.get_file(snippet, optional=True)
115+
if file:
116+
link = format_link(snippet, file.github_url, channel)
117+
message = message.replace(f"`{snippet}`", link)
118+
continue
119+
except (IsADirectoryError, OSError):
120+
# Skip if there are any filesystem errors with file access
121+
pass
122+
123+
# If not a file, try as directory
124+
try:
125+
directory = codebase.get_directory(snippet, optional=True)
126+
if directory:
127+
# TODO: implement `Directory.github_url`
128+
github_url = codebase.ctx.base_url
129+
github_url = github_url or "https://github.com/your/repo/tree/develop/" # Fallback URL
130+
if github_url.endswith(".git"):
131+
github_url = github_url.replace(".git", "/tree/develop/") + str(directory.dirpath)
132+
else:
133+
github_url = github_url + str(directory.dirpath)
134+
print(github_url)
135+
link = format_link(snippet, github_url, channel)
136+
message = message.replace(f"`{snippet}`", link)
137+
except (IsADirectoryError, OSError):
138+
# Skip if there are any filesystem errors with directory access
139+
pass
116140

117141
# Symbols
118142
else:

src/codegen/sdk/core/file.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ def owners(self) -> set[str]:
224224
def github_url(self) -> str | None:
225225
if self.ctx.base_url:
226226
if self.ctx.base_url.endswith(".git"):
227-
print("HERE")
228227
return self.ctx.base_url.replace(".git", "/blob/develop/") + self.file_path
229228
else:
230229
return self.ctx.base_url + "/" + self.file_path

tests/unit/codegen/extensions/test_message_annotation.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Greeter_duplicate:
3737
files = {
3838
"src/main.py": content,
3939
"src/utils/helpers.py": "# Helper functions",
40+
"src/utils/more/nested.py": "# Nested module",
4041
"docs/README.md": "# Documentation",
4142
"tsconfig.json": "{}",
4243
}
@@ -112,6 +113,14 @@ def test_add_links_filepath(codebase):
112113
assert "|src/utils/helpers.py>" in result
113114

114115

116+
def test_add_links_directory(codebase):
117+
"""Test adding links for directories."""
118+
message = "Look in the `src/utils` directory and `src/utils/more`"
119+
result = add_links_to_message(message, codebase)
120+
assert "|src/utils>" in result
121+
assert "|src/utils/more>" in result
122+
123+
115124
def test_add_links_filepath_with_extension(codebase):
116125
"""Test adding links for files with common extensions."""
117126
message = "See `tsconfig.json` and `docs/README.md`"
@@ -127,6 +136,13 @@ def test_nonexistent_filepath(codebase):
127136
assert result == message # Message should remain unchanged
128137

129138

139+
def test_nonexistent_directory(codebase):
140+
"""Test handling of nonexistent directories."""
141+
message = "This `src/nonexistent/dir` should not be linked"
142+
result = add_links_to_message(message, codebase)
143+
assert result == message # Message should remain unchanged
144+
145+
130146
def test_ignore_code_blocks(codebase):
131147
"""Test that code blocks are ignored."""
132148
message = """Here's a code block:
@@ -163,8 +179,10 @@ def test_mixed_content(codebase):
163179
message = """Here's a complex message:
164180
- Valid symbol: `hello`
165181
- Valid file: `src/main.py`
182+
- Valid directory: `src/utils`
166183
- Invalid symbol: `nonexistent`
167184
- Invalid file: `src/nonexistent.py`
185+
- Invalid directory: `src/nonexistent/dir`
168186
- Code block:
169187
```python
170188
def hello():
@@ -184,9 +202,13 @@ def hello():
184202
assert "|src/main.py>" in result
185203
assert "|docs/README.md>" in result
186204

205+
# Valid directories should be linked
206+
assert "|src/utils>" in result
207+
187208
# Invalid symbols and files should remain as-is
188209
assert "`nonexistent`" in result
189210
assert "`src/nonexistent.py`" in result
211+
assert "`src/nonexistent/dir`" in result
190212
assert "`hello_duplicate`" in result
191213

192214
# Code block should be preserved

0 commit comments

Comments
 (0)