Skip to content

Commit a57f614

Browse files
refactor: implement _get_status_code, adjust _check_repo_exists, and update tests
- Implemented function `_get_status_code` in repository_clone.py to extract the status code from an HTTP response - Adjusted `_check_repo_exists` in repository_clone.py to utilize the new `_get_status_code` function - Modified `_check_repo_exists` to return True for status codes 200 and 301, and False for 404 and 302 - Updated `test_check_repo_exists_with_redirect` in test_repository_clone.py to verify that `_check_repo_exists` returns False for status code 302 - Implemented test `test_check_repo_exists_with_permanent_redirect` in test_repository_clone.py to verify that `_check_repo_exists` returns True for status code 301
1 parent 9a19c92 commit a57f614

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

src/gitingest/repository_clone.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ async def _check_repo_exists(url: str) -> bool:
111111
-------
112112
bool
113113
True if the repository exists, False otherwise.
114+
115+
Raises
116+
------
117+
RuntimeError
118+
If the curl command returns an unexpected status code.
114119
"""
115120
proc = await asyncio.create_subprocess_exec(
116121
"curl",
@@ -120,11 +125,20 @@ async def _check_repo_exists(url: str) -> bool:
120125
stderr=asyncio.subprocess.PIPE,
121126
)
122127
stdout, _ = await proc.communicate()
128+
123129
if proc.returncode != 0:
124130
return False
125-
# Check if stdout contains "404" status code
126-
stdout_str = stdout.decode()
127-
return "HTTP/1.1 404" not in stdout_str and "HTTP/2 404" not in stdout_str
131+
132+
response = stdout.decode()
133+
status_code = _get_status_code(response)
134+
135+
if status_code in (200, 301):
136+
return True
137+
138+
if status_code in (404, 302):
139+
return False
140+
141+
raise RuntimeError(f"Unexpected status code: {status_code}")
128142

129143

130144
async def _run_git_command(*args: str) -> tuple[bytes, bytes]:
@@ -157,3 +171,22 @@ async def _run_git_command(*args: str) -> tuple[bytes, bytes]:
157171
raise RuntimeError(f"Git command failed: {' '.join(args)}\nError: {error_message}")
158172

159173
return stdout, stderr
174+
175+
176+
def _get_status_code(response: str) -> int:
177+
"""
178+
Extract the status code from an HTTP response.
179+
180+
Parameters
181+
----------
182+
response : str
183+
The HTTP response string.
184+
185+
Returns
186+
-------
187+
int
188+
The status code of the response
189+
"""
190+
status_line = response.splitlines()[0].strip()
191+
status_code = int(status_line.split(" ", 2)[1])
192+
return status_code

tests/test_repository_clone.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ async def test_clone_repo_commit_without_branch() -> None:
204204
@pytest.mark.asyncio
205205
async def test_check_repo_exists_with_redirect() -> None:
206206
"""
207-
Test the `_check_repo_exists` function for handling HTTP redirects (302 Found).
208-
Verifies that it correctly identifies the repository's existence.
207+
Test the `_check_repo_exists` function when the repository URL returns a redirect response.
208+
209+
Verifies that the function returns False when a 302 Found response is received.
209210
"""
210211
url = "https://github.com/user/repo"
211212
with patch("asyncio.create_subprocess_exec", new_callable=AsyncMock) as mock_exec:
@@ -214,4 +215,21 @@ async def test_check_repo_exists_with_redirect() -> None:
214215
mock_process.returncode = 0 # Simulate successful request
215216
mock_exec.return_value = mock_process
216217

218+
assert await _check_repo_exists(url) is False
219+
220+
221+
@pytest.mark.asyncio
222+
async def test_check_repo_exists_with_permanent_redirect() -> None:
223+
"""
224+
Test the `_check_repo_exists` function when the repository URL returns a redirect response.
225+
226+
Verifies that the function returns True when a 301 Found response is received.
227+
"""
228+
url = "https://github.com/user/repo"
229+
with patch("asyncio.create_subprocess_exec", new_callable=AsyncMock) as mock_exec:
230+
mock_process = AsyncMock()
231+
mock_process.communicate.return_value = (b"HTTP/1.1 301 Found\n", b"")
232+
mock_process.returncode = 0 # Simulate successful request
233+
mock_exec.return_value = mock_process
234+
217235
assert await _check_repo_exists(url)

0 commit comments

Comments
 (0)