Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/nvidia_nat_core/src/nat/tool/github_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ async def get(url_text: str) -> str:
- https://github.com/org/repo/blob/main/README.md#L409-L417 -> Returns lines 409-417 of the README.md file
"""

pattern = r"https://github\.com/(?P<repo_path>[^/]*/[^/]*)/blob/(?P<file_path>[^?#]*)(?:#L(?P<start>\d+)(?:-L(?P<end>\d+))?)?"
pattern = r"https://github\.com/(?P<repo_path>[^/]*/[^/]*)/blob/(?P<file_path>[^?#]*)(?:\?[^#]*)?(?:#L(?P<start>\d+)(?:-L(?P<end>\d+))?)?"
match = re.match(pattern, url_text)
if not match:
return ("Invalid github url. Please provide a valid github url. "
Expand All @@ -507,7 +507,7 @@ async def get(url_text: str) -> str:

lines = response.text.splitlines()

if file_metadata.start_line > len(lines):
if file_metadata.start_line < 1 or file_metadata.start_line > len(lines):
return f"Error: Line {file_metadata.start_line} is out of range for the file {file_metadata.file_path}"

if file_metadata.end_line is None:
Expand All @@ -516,6 +516,9 @@ async def get(url_text: str) -> str:
if file_metadata.end_line > len(lines):
return f"Error: Line {file_metadata.end_line} is out of range for the file {file_metadata.file_path}"

if file_metadata.end_line < file_metadata.start_line:
return f"Error: Line range {file_metadata.start_line}-{file_metadata.end_line} is reversed"

selected_lines = lines[file_metadata.start_line - 1:file_metadata.end_line]
response_text = "\n".join(selected_lines)
return f"```{response_text}\n```"
Expand Down
28 changes: 28 additions & 0 deletions packages/nvidia_nat_core/tests/nat/tools/test_github_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,34 @@ def async_client_with_mock_transport(*args: Any, **kwargs: Any) -> httpx.AsyncCl
assert str(requests[0].url) == f"https://raw.githubusercontent.com/owner/repo/{ref}/docs/README.md"


@pytest.mark.parametrize(("link_suffix", "expected"),
[
("?plain=1#L2", "```second line\n```"),
("?plain=1#L2-L3", "```second line\nthird line\n```"),
("?plain=1", "```first line\nsecond line\nthird line\n\n```"),
("#L0", "Error: Line 0 is out of range for the file main/docs/README.md"),
("#L3-L2", "Error: Line range 3-2 is reversed"),
])
async def test_get_file_handles_line_references(link_suffix: str, expected: str, monkeypatch: pytest.MonkeyPatch):
from nat.tool.github_tools import GithubFilesGroupConfig
from nat.tool.github_tools import github_files_tool

def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(200, text="first line\nsecond line\nthird line\n")

real_async_client = httpx.AsyncClient

def async_client_with_mock_transport(*args: Any, **kwargs: Any) -> httpx.AsyncClient:
return real_async_client(*args, transport=httpx.MockTransport(handler), **kwargs)

monkeypatch.setattr(httpx, "AsyncClient", async_client_with_mock_transport)

async with github_files_tool(GithubFilesGroupConfig(), MagicMock()) as function_info:
result = await function_info.single_fn(f"https://github.com/owner/repo/blob/main/docs/README.md{link_suffix}")

assert result == expected


@pytest.mark.parametrize("since", ["yesterday", "2026-10-01", "2026-10-01 00:00:00", "2026-1-1T0:0:0Z", 20261001])
def test_get_issue_rejects_since_values_that_are_not_iso_8601_timestamps(since: str | int):
from nat.tool.github_tools import GithubGetIssueModel
Expand Down
Loading