Skip to content

Commit

Permalink
fix: supports directories for link annotation (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayhack authored Feb 13, 2025
1 parent dbed11c commit 9e21f9f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
34 changes: 29 additions & 5 deletions src/codegen/extensions/tools/link_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def add_links_to_message(message: str, codebase: Codebase, channel: MessageChann
This function:
1. Links code snippets that match symbol names
2. Links anything that looks like a filepath
2. Links anything that looks like a filepath (files or directories)
Args:
message: The message to process
Expand All @@ -109,10 +109,34 @@ def add_links_to_message(message: str, codebase: Codebase, channel: MessageChann
for snippet in snippets:
# Filepaths
if is_likely_filepath(snippet):
file = codebase.get_file(snippet, optional=True)
if file:
link = format_link(snippet, file.github_url, channel)
message = message.replace(f"`{snippet}`", link)
# Try as file first
try:
file = codebase.get_file(snippet, optional=True)
if file:
link = format_link(snippet, file.github_url, channel)
message = message.replace(f"`{snippet}`", link)
continue
except (IsADirectoryError, OSError):
# Skip if there are any filesystem errors with file access
pass

# If not a file, try as directory
try:
directory = codebase.get_directory(snippet, optional=True)
if directory:
# TODO: implement `Directory.github_url`
github_url = codebase.ctx.base_url
github_url = github_url or "https://github.com/your/repo/tree/develop/" # Fallback URL
if github_url.endswith(".git"):
github_url = github_url.replace(".git", "/tree/develop/") + str(directory.dirpath)
else:
github_url = github_url + str(directory.dirpath)
print(github_url)
link = format_link(snippet, github_url, channel)
message = message.replace(f"`{snippet}`", link)
except (IsADirectoryError, OSError):
# Skip if there are any filesystem errors with directory access
pass

# Symbols
else:
Expand Down
1 change: 0 additions & 1 deletion src/codegen/sdk/core/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ def owners(self) -> set[str]:
def github_url(self) -> str | None:
if self.ctx.base_url:
if self.ctx.base_url.endswith(".git"):
print("HERE")
return self.ctx.base_url.replace(".git", "/blob/develop/") + self.file_path
else:
return self.ctx.base_url + "/" + self.file_path
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/codegen/extensions/test_message_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Greeter_duplicate:
files = {
"src/main.py": content,
"src/utils/helpers.py": "# Helper functions",
"src/utils/more/nested.py": "# Nested module",
"docs/README.md": "# Documentation",
"tsconfig.json": "{}",
}
Expand Down Expand Up @@ -112,6 +113,14 @@ def test_add_links_filepath(codebase):
assert "|src/utils/helpers.py>" in result


def test_add_links_directory(codebase):
"""Test adding links for directories."""
message = "Look in the `src/utils` directory and `src/utils/more`"
result = add_links_to_message(message, codebase)
assert "|src/utils>" in result
assert "|src/utils/more>" in result


def test_add_links_filepath_with_extension(codebase):
"""Test adding links for files with common extensions."""
message = "See `tsconfig.json` and `docs/README.md`"
Expand All @@ -127,6 +136,13 @@ def test_nonexistent_filepath(codebase):
assert result == message # Message should remain unchanged


def test_nonexistent_directory(codebase):
"""Test handling of nonexistent directories."""
message = "This `src/nonexistent/dir` should not be linked"
result = add_links_to_message(message, codebase)
assert result == message # Message should remain unchanged


def test_ignore_code_blocks(codebase):
"""Test that code blocks are ignored."""
message = """Here's a code block:
Expand Down Expand Up @@ -163,8 +179,10 @@ def test_mixed_content(codebase):
message = """Here's a complex message:
- Valid symbol: `hello`
- Valid file: `src/main.py`
- Valid directory: `src/utils`
- Invalid symbol: `nonexistent`
- Invalid file: `src/nonexistent.py`
- Invalid directory: `src/nonexistent/dir`
- Code block:
```python
def hello():
Expand All @@ -184,9 +202,13 @@ def hello():
assert "|src/main.py>" in result
assert "|docs/README.md>" in result

# Valid directories should be linked
assert "|src/utils>" in result

# Invalid symbols and files should remain as-is
assert "`nonexistent`" in result
assert "`src/nonexistent.py`" in result
assert "`src/nonexistent/dir`" in result
assert "`hello_duplicate`" in result

# Code block should be preserved
Expand Down

0 comments on commit 9e21f9f

Please sign in to comment.