test_resolve_issues.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. import os
  2. import tempfile
  3. from unittest.mock import AsyncMock, MagicMock, patch
  4. import pytest
  5. from openhands.core.config import LLMConfig
  6. from openhands.events.action import CmdRunAction
  7. from openhands.events.observation import CmdOutputObservation, NullObservation
  8. from openhands.resolver.github_issue import GithubIssue, ReviewThread
  9. from openhands.resolver.issue_definitions import IssueHandler, PRHandler
  10. from openhands.resolver.resolve_issue import (
  11. complete_runtime,
  12. initialize_runtime,
  13. process_issue,
  14. )
  15. from openhands.resolver.resolver_output import ResolverOutput
  16. @pytest.fixture
  17. def mock_output_dir():
  18. with tempfile.TemporaryDirectory() as temp_dir:
  19. repo_path = os.path.join(temp_dir, 'repo')
  20. # Initialize a GitHub repo in "repo" and add a commit with "README.md"
  21. os.makedirs(repo_path)
  22. os.system(f'git init {repo_path}')
  23. readme_path = os.path.join(repo_path, 'README.md')
  24. with open(readme_path, 'w') as f:
  25. f.write('hello world')
  26. os.system(f'git -C {repo_path} add README.md')
  27. os.system(f"git -C {repo_path} commit -m 'Initial commit'")
  28. yield temp_dir
  29. @pytest.fixture
  30. def mock_subprocess():
  31. with patch('subprocess.check_output') as mock_check_output:
  32. yield mock_check_output
  33. @pytest.fixture
  34. def mock_os():
  35. with patch('os.system') as mock_system, patch('os.path.join') as mock_join:
  36. yield mock_system, mock_join
  37. @pytest.fixture
  38. def mock_prompt_template():
  39. return 'Issue: {{ body }}\n\nPlease fix this issue.'
  40. @pytest.fixture
  41. def mock_followup_prompt_template():
  42. return 'Issue context: {{ issues }}\n\nReview comments: {{ review_comments }}\n\nReview threads: {{ review_threads }}\n\nFiles: {{ files }}\n\nThread comments: {{ thread_context }}\n\nPlease fix this issue.'
  43. def create_cmd_output(exit_code: int, content: str, command_id: int, command: str):
  44. return CmdOutputObservation(
  45. exit_code=exit_code, content=content, command_id=command_id, command=command
  46. )
  47. def test_initialize_runtime():
  48. mock_runtime = MagicMock()
  49. mock_runtime.run_action.side_effect = [
  50. create_cmd_output(
  51. exit_code=0, content='', command_id=1, command='cd /workspace'
  52. ),
  53. create_cmd_output(
  54. exit_code=0,
  55. content='',
  56. command_id=2,
  57. command='git config --global core.pager ""',
  58. ),
  59. ]
  60. initialize_runtime(mock_runtime)
  61. assert mock_runtime.run_action.call_count == 2
  62. mock_runtime.run_action.assert_any_call(CmdRunAction(command='cd /workspace'))
  63. mock_runtime.run_action.assert_any_call(
  64. CmdRunAction(command='git config --global core.pager ""')
  65. )
  66. def test_download_issues_from_github():
  67. handler = IssueHandler('owner', 'repo', 'token')
  68. mock_issues_response = MagicMock()
  69. mock_issues_response.json.side_effect = [
  70. [
  71. {'number': 1, 'title': 'Issue 1', 'body': 'This is an issue'},
  72. {
  73. 'number': 2,
  74. 'title': 'PR 1',
  75. 'body': 'This is a pull request',
  76. 'pull_request': {},
  77. },
  78. {'number': 3, 'title': 'Issue 2', 'body': 'This is another issue'},
  79. ],
  80. None,
  81. ]
  82. mock_issues_response.raise_for_status = MagicMock()
  83. mock_comments_response = MagicMock()
  84. mock_comments_response.json.return_value = []
  85. mock_comments_response.raise_for_status = MagicMock()
  86. def get_mock_response(url, *args, **kwargs):
  87. if '/comments' in url:
  88. return mock_comments_response
  89. return mock_issues_response
  90. with patch('requests.get', side_effect=get_mock_response):
  91. issues = handler.get_converted_issues(issue_numbers=[1, 3])
  92. assert len(issues) == 2
  93. assert handler.issue_type == 'issue'
  94. assert all(isinstance(issue, GithubIssue) for issue in issues)
  95. assert [issue.number for issue in issues] == [1, 3]
  96. assert [issue.title for issue in issues] == ['Issue 1', 'Issue 2']
  97. assert [issue.review_comments for issue in issues] == [None, None]
  98. assert [issue.closing_issues for issue in issues] == [None, None]
  99. assert [issue.thread_ids for issue in issues] == [None, None]
  100. def test_download_pr_from_github():
  101. handler = PRHandler('owner', 'repo', 'token')
  102. mock_pr_response = MagicMock()
  103. mock_pr_response.json.side_effect = [
  104. [
  105. {
  106. 'number': 1,
  107. 'title': 'PR 1',
  108. 'body': 'This is a pull request',
  109. 'head': {'ref': 'b1'},
  110. },
  111. {
  112. 'number': 2,
  113. 'title': 'My PR',
  114. 'body': 'This is another pull request',
  115. 'head': {'ref': 'b2'},
  116. },
  117. {'number': 3, 'title': 'PR 3', 'body': 'Final PR', 'head': {'ref': 'b3'}},
  118. ],
  119. None,
  120. ]
  121. mock_pr_response.raise_for_status = MagicMock()
  122. # Mock for PR comments response
  123. mock_comments_response = MagicMock()
  124. mock_comments_response.json.return_value = [] # No PR comments
  125. mock_comments_response.raise_for_status = MagicMock()
  126. # Mock for GraphQL request (for download_pr_metadata)
  127. mock_graphql_response = MagicMock()
  128. mock_graphql_response.json.side_effect = lambda: {
  129. 'data': {
  130. 'repository': {
  131. 'pullRequest': {
  132. 'closingIssuesReferences': {
  133. 'edges': [
  134. {'node': {'body': 'Issue 1 body', 'number': 1}},
  135. {'node': {'body': 'Issue 2 body', 'number': 2}},
  136. ]
  137. },
  138. 'reviewThreads': {
  139. 'edges': [
  140. {
  141. 'node': {
  142. 'isResolved': False,
  143. 'id': '1',
  144. 'comments': {
  145. 'nodes': [
  146. {
  147. 'body': 'Unresolved comment 1',
  148. 'path': '/frontend/header.tsx',
  149. },
  150. {'body': 'Follow up thread'},
  151. ]
  152. },
  153. }
  154. },
  155. {
  156. 'node': {
  157. 'isResolved': True,
  158. 'id': '2',
  159. 'comments': {
  160. 'nodes': [
  161. {
  162. 'body': 'Resolved comment 1',
  163. 'path': '/some/file.py',
  164. }
  165. ]
  166. },
  167. }
  168. },
  169. {
  170. 'node': {
  171. 'isResolved': False,
  172. 'id': '3',
  173. 'comments': {
  174. 'nodes': [
  175. {
  176. 'body': 'Unresolved comment 3',
  177. 'path': '/another/file.py',
  178. }
  179. ]
  180. },
  181. }
  182. },
  183. ]
  184. },
  185. }
  186. }
  187. }
  188. }
  189. mock_graphql_response.raise_for_status = MagicMock()
  190. def get_mock_response(url, *args, **kwargs):
  191. if '/comments' in url:
  192. return mock_comments_response
  193. return mock_pr_response
  194. with patch('requests.get', side_effect=get_mock_response):
  195. with patch('requests.post', return_value=mock_graphql_response):
  196. issues = handler.get_converted_issues(issue_numbers=[1, 2, 3])
  197. assert len(issues) == 3
  198. assert handler.issue_type == 'pr'
  199. assert all(isinstance(issue, GithubIssue) for issue in issues)
  200. assert [issue.number for issue in issues] == [1, 2, 3]
  201. assert [issue.title for issue in issues] == ['PR 1', 'My PR', 'PR 3']
  202. assert [issue.head_branch for issue in issues] == ['b1', 'b2', 'b3']
  203. assert len(issues[0].review_threads) == 2 # Only unresolved threads
  204. assert (
  205. issues[0].review_threads[0].comment
  206. == 'Unresolved comment 1\n---\nlatest feedback:\nFollow up thread\n'
  207. )
  208. assert issues[0].review_threads[0].files == ['/frontend/header.tsx']
  209. assert (
  210. issues[0].review_threads[1].comment
  211. == 'latest feedback:\nUnresolved comment 3\n'
  212. )
  213. assert issues[0].review_threads[1].files == ['/another/file.py']
  214. assert issues[0].closing_issues == ['Issue 1 body', 'Issue 2 body']
  215. assert issues[0].thread_ids == ['1', '3']
  216. @pytest.mark.asyncio
  217. async def test_complete_runtime():
  218. mock_runtime = MagicMock()
  219. mock_runtime.run_action.side_effect = [
  220. create_cmd_output(
  221. exit_code=0, content='', command_id=1, command='cd /workspace'
  222. ),
  223. create_cmd_output(
  224. exit_code=0,
  225. content='',
  226. command_id=2,
  227. command='git config --global core.pager ""',
  228. ),
  229. create_cmd_output(
  230. exit_code=0,
  231. content='',
  232. command_id=3,
  233. command='git config --global --add safe.directory /workspace',
  234. ),
  235. create_cmd_output(
  236. exit_code=0,
  237. content='',
  238. command_id=4,
  239. command='git diff base_commit_hash fix',
  240. ),
  241. create_cmd_output(
  242. exit_code=0, content='git diff content', command_id=5, command='git apply'
  243. ),
  244. ]
  245. result = await complete_runtime(mock_runtime, 'base_commit_hash')
  246. assert result == {'git_patch': 'git diff content'}
  247. assert mock_runtime.run_action.call_count == 5
  248. @pytest.mark.asyncio
  249. async def test_process_issue(mock_output_dir, mock_prompt_template):
  250. # Mock dependencies
  251. mock_create_runtime = MagicMock()
  252. mock_initialize_runtime = AsyncMock()
  253. mock_run_controller = AsyncMock()
  254. mock_complete_runtime = AsyncMock()
  255. handler_instance = MagicMock()
  256. # Set up test data
  257. issue = GithubIssue(
  258. owner='test_owner',
  259. repo='test_repo',
  260. number=1,
  261. title='Test Issue',
  262. body='This is a test issue',
  263. )
  264. base_commit = 'abcdef1234567890'
  265. repo_instruction = 'Resolve this repo'
  266. max_iterations = 5
  267. llm_config = LLMConfig(model='test_model', api_key='test_api_key')
  268. runtime_container_image = 'test_image:latest'
  269. # Test cases for different scenarios
  270. test_cases = [
  271. {
  272. 'name': 'successful_run',
  273. 'run_controller_return': MagicMock(
  274. history=[NullObservation(content='')],
  275. metrics=MagicMock(
  276. get=MagicMock(return_value={'test_result': 'passed'})
  277. ),
  278. last_error=None,
  279. ),
  280. 'run_controller_raises': None,
  281. 'expected_success': True,
  282. 'expected_error': None,
  283. 'expected_explanation': 'Issue resolved successfully',
  284. },
  285. {
  286. 'name': 'value_error',
  287. 'run_controller_return': None,
  288. 'run_controller_raises': ValueError('Test value error'),
  289. 'expected_success': False,
  290. 'expected_error': 'Agent failed to run or crashed',
  291. 'expected_explanation': 'Agent failed to run',
  292. },
  293. {
  294. 'name': 'runtime_error',
  295. 'run_controller_return': None,
  296. 'run_controller_raises': RuntimeError('Test runtime error'),
  297. 'expected_success': False,
  298. 'expected_error': 'Agent failed to run or crashed',
  299. 'expected_explanation': 'Agent failed to run',
  300. },
  301. {
  302. 'name': 'json_decode_error',
  303. 'run_controller_return': MagicMock(
  304. history=[NullObservation(content='')],
  305. metrics=MagicMock(
  306. get=MagicMock(return_value={'test_result': 'passed'})
  307. ),
  308. last_error=None,
  309. ),
  310. 'run_controller_raises': None,
  311. 'expected_success': True,
  312. 'expected_error': None,
  313. 'expected_explanation': 'Non-JSON explanation',
  314. 'is_pr': True,
  315. 'comment_success': [
  316. True,
  317. False,
  318. ], # To trigger the PR success logging code path
  319. },
  320. ]
  321. for test_case in test_cases:
  322. # Reset mocks
  323. mock_create_runtime.reset_mock()
  324. mock_initialize_runtime.reset_mock()
  325. mock_run_controller.reset_mock()
  326. mock_complete_runtime.reset_mock()
  327. handler_instance.reset_mock()
  328. # Mock return values
  329. mock_create_runtime.return_value = MagicMock(connect=AsyncMock())
  330. if test_case['run_controller_raises']:
  331. mock_run_controller.side_effect = test_case['run_controller_raises']
  332. else:
  333. mock_run_controller.return_value = test_case['run_controller_return']
  334. mock_run_controller.side_effect = None
  335. mock_complete_runtime.return_value = {'git_patch': 'test patch'}
  336. handler_instance.guess_success.return_value = (
  337. test_case['expected_success'],
  338. test_case.get('comment_success', None),
  339. test_case['expected_explanation'],
  340. )
  341. handler_instance.get_instruction.return_value = ('Test instruction', [])
  342. handler_instance.issue_type = 'pr' if test_case.get('is_pr', False) else 'issue'
  343. with patch(
  344. 'openhands.resolver.resolve_issue.create_runtime', mock_create_runtime
  345. ), patch(
  346. 'openhands.resolver.resolve_issue.initialize_runtime',
  347. mock_initialize_runtime,
  348. ), patch(
  349. 'openhands.resolver.resolve_issue.run_controller', mock_run_controller
  350. ), patch(
  351. 'openhands.resolver.resolve_issue.complete_runtime', mock_complete_runtime
  352. ), patch('openhands.resolver.resolve_issue.logger'):
  353. # Call the function
  354. result = await process_issue(
  355. issue,
  356. base_commit,
  357. max_iterations,
  358. llm_config,
  359. mock_output_dir,
  360. runtime_container_image,
  361. mock_prompt_template,
  362. handler_instance,
  363. repo_instruction,
  364. reset_logger=False,
  365. )
  366. # Assert the result
  367. expected_issue_type = 'pr' if test_case.get('is_pr', False) else 'issue'
  368. assert handler_instance.issue_type == expected_issue_type
  369. assert isinstance(result, ResolverOutput)
  370. assert result.issue == issue
  371. assert result.base_commit == base_commit
  372. assert result.git_patch == 'test patch'
  373. assert result.success == test_case['expected_success']
  374. assert result.success_explanation == test_case['expected_explanation']
  375. assert result.error == test_case['expected_error']
  376. # Assert that the mocked functions were called
  377. mock_create_runtime.assert_called_once()
  378. mock_initialize_runtime.assert_called_once()
  379. mock_run_controller.assert_called_once()
  380. mock_complete_runtime.assert_called_once()
  381. # Assert that guess_success was called only for successful runs
  382. if test_case['expected_success']:
  383. handler_instance.guess_success.assert_called_once()
  384. else:
  385. handler_instance.guess_success.assert_not_called()
  386. def test_get_instruction(mock_prompt_template, mock_followup_prompt_template):
  387. issue = GithubIssue(
  388. owner='test_owner',
  389. repo='test_repo',
  390. number=123,
  391. title='Test Issue',
  392. body='This is a test issue refer to image ![First Image](https://sampleimage.com/image1.png)',
  393. )
  394. issue_handler = IssueHandler('owner', 'repo', 'token')
  395. instruction, images_urls = issue_handler.get_instruction(
  396. issue, mock_prompt_template, None
  397. )
  398. expected_instruction = 'Issue: Test Issue\n\nThis is a test issue refer to image ![First Image](https://sampleimage.com/image1.png)\n\nPlease fix this issue.'
  399. assert images_urls == ['https://sampleimage.com/image1.png']
  400. assert issue_handler.issue_type == 'issue'
  401. assert instruction == expected_instruction
  402. issue = GithubIssue(
  403. owner='test_owner',
  404. repo='test_repo',
  405. number=123,
  406. title='Test Issue',
  407. body='This is a test issue',
  408. closing_issues=['Issue 1 fix the type'],
  409. review_threads=[
  410. ReviewThread(
  411. comment="There is still a typo 'pthon' instead of 'python'", files=[]
  412. )
  413. ],
  414. thread_comments=[
  415. "I've left review comments, please address them",
  416. 'This is a valid concern.',
  417. ],
  418. )
  419. pr_handler = PRHandler('owner', 'repo', 'token')
  420. instruction, images_urls = pr_handler.get_instruction(
  421. issue, mock_followup_prompt_template, None
  422. )
  423. expected_instruction = "Issue context: [\n \"Issue 1 fix the type\"\n]\n\nReview comments: None\n\nReview threads: [\n \"There is still a typo 'pthon' instead of 'python'\"\n]\n\nFiles: []\n\nThread comments: I've left review comments, please address them\n---\nThis is a valid concern.\n\nPlease fix this issue."
  424. assert images_urls == []
  425. assert pr_handler.issue_type == 'pr'
  426. assert instruction == expected_instruction
  427. def test_file_instruction():
  428. issue = GithubIssue(
  429. owner='test_owner',
  430. repo='test_repo',
  431. number=123,
  432. title='Test Issue',
  433. body='This is a test issue ![image](https://sampleimage.com/sample.png)',
  434. )
  435. # load prompt from openhands/resolver/prompts/resolve/basic.jinja
  436. with open('openhands/resolver/prompts/resolve/basic.jinja', 'r') as f:
  437. prompt = f.read()
  438. # Test without thread comments
  439. issue_handler = IssueHandler('owner', 'repo', 'token')
  440. instruction, images_urls = issue_handler.get_instruction(issue, prompt, None)
  441. expected_instruction = """Please fix the following issue for the repository in /workspace.
  442. An environment has been set up for you to start working. You may assume all necessary tools are installed.
  443. # Problem Statement
  444. Test Issue
  445. This is a test issue ![image](https://sampleimage.com/sample.png)
  446. IMPORTANT: You should ONLY interact with the environment provided to you AND NEVER ASK FOR HUMAN HELP.
  447. You SHOULD INCLUDE PROPER INDENTATION in your edit commands.
  448. When you think you have fixed the issue through code changes, please finish the interaction."""
  449. assert instruction == expected_instruction
  450. assert images_urls == ['https://sampleimage.com/sample.png']
  451. def test_file_instruction_with_repo_instruction():
  452. issue = GithubIssue(
  453. owner='test_owner',
  454. repo='test_repo',
  455. number=123,
  456. title='Test Issue',
  457. body='This is a test issue',
  458. )
  459. # load prompt from openhands/resolver/prompts/resolve/basic.jinja
  460. with open('openhands/resolver/prompts/resolve/basic.jinja', 'r') as f:
  461. prompt = f.read()
  462. # load repo instruction from openhands/resolver/prompts/repo_instructions/all-hands-ai___openhands-resolver.txt
  463. with open(
  464. 'openhands/resolver/prompts/repo_instructions/all-hands-ai___openhands-resolver.txt',
  465. 'r',
  466. ) as f:
  467. repo_instruction = f.read()
  468. issue_handler = IssueHandler('owner', 'repo', 'token')
  469. instruction, image_urls = issue_handler.get_instruction(
  470. issue, prompt, repo_instruction
  471. )
  472. expected_instruction = """Please fix the following issue for the repository in /workspace.
  473. An environment has been set up for you to start working. You may assume all necessary tools are installed.
  474. # Problem Statement
  475. Test Issue
  476. This is a test issue
  477. IMPORTANT: You should ONLY interact with the environment provided to you AND NEVER ASK FOR HUMAN HELP.
  478. You SHOULD INCLUDE PROPER INDENTATION in your edit commands.
  479. Some basic information about this repository:
  480. This is a Python repo for openhands-resolver, a library that attempts to resolve github issues with the AI agent OpenHands.
  481. - Setup: `poetry install --with test --with dev`
  482. - Testing: `poetry run pytest tests/test_*.py`
  483. When you think you have fixed the issue through code changes, please finish the interaction."""
  484. assert instruction == expected_instruction
  485. assert issue_handler.issue_type == 'issue'
  486. assert image_urls == []
  487. def test_guess_success():
  488. mock_issue = GithubIssue(
  489. owner='test_owner',
  490. repo='test_repo',
  491. number=1,
  492. title='Test Issue',
  493. body='This is a test issue',
  494. )
  495. mock_history = [
  496. create_cmd_output(
  497. exit_code=0, content='', command_id=1, command='cd /workspace'
  498. )
  499. ]
  500. mock_llm_config = LLMConfig(model='test_model', api_key='test_api_key')
  501. mock_completion_response = MagicMock()
  502. mock_completion_response.choices = [
  503. MagicMock(
  504. message=MagicMock(
  505. content='--- success\ntrue\n--- explanation\nIssue resolved successfully'
  506. )
  507. )
  508. ]
  509. issue_handler = IssueHandler('owner', 'repo', 'token')
  510. with patch('litellm.completion', MagicMock(return_value=mock_completion_response)):
  511. success, comment_success, explanation = issue_handler.guess_success(
  512. mock_issue, mock_history, mock_llm_config
  513. )
  514. assert issue_handler.issue_type == 'issue'
  515. assert comment_success is None
  516. assert success
  517. assert explanation == 'Issue resolved successfully'
  518. def test_guess_success_with_thread_comments():
  519. mock_issue = GithubIssue(
  520. owner='test_owner',
  521. repo='test_repo',
  522. number=1,
  523. title='Test Issue',
  524. body='This is a test issue',
  525. thread_comments=[
  526. 'First comment',
  527. 'Second comment',
  528. 'latest feedback:\nPlease add tests',
  529. ],
  530. )
  531. mock_history = [MagicMock(message='I have added tests for this case')]
  532. mock_llm_config = LLMConfig(model='test_model', api_key='test_api_key')
  533. mock_completion_response = MagicMock()
  534. mock_completion_response.choices = [
  535. MagicMock(
  536. message=MagicMock(
  537. content='--- success\ntrue\n--- explanation\nTests have been added to verify thread comments handling'
  538. )
  539. )
  540. ]
  541. issue_handler = IssueHandler('owner', 'repo', 'token')
  542. with patch('litellm.completion', MagicMock(return_value=mock_completion_response)):
  543. success, comment_success, explanation = issue_handler.guess_success(
  544. mock_issue, mock_history, mock_llm_config
  545. )
  546. assert issue_handler.issue_type == 'issue'
  547. assert comment_success is None
  548. assert success
  549. assert 'Tests have been added' in explanation
  550. def test_instruction_with_thread_comments():
  551. # Create an issue with thread comments
  552. issue = GithubIssue(
  553. owner='test_owner',
  554. repo='test_repo',
  555. number=123,
  556. title='Test Issue',
  557. body='This is a test issue',
  558. thread_comments=[
  559. 'First comment',
  560. 'Second comment',
  561. 'latest feedback:\nPlease add tests',
  562. ],
  563. )
  564. # Load the basic prompt template
  565. with open('openhands/resolver/prompts/resolve/basic.jinja', 'r') as f:
  566. prompt = f.read()
  567. issue_handler = IssueHandler('owner', 'repo', 'token')
  568. instruction, images_urls = issue_handler.get_instruction(issue, prompt, None)
  569. # Verify that thread comments are included in the instruction
  570. assert 'First comment' in instruction
  571. assert 'Second comment' in instruction
  572. assert 'Please add tests' in instruction
  573. assert 'Issue Thread Comments:' in instruction
  574. assert images_urls == []
  575. def test_guess_success_failure():
  576. mock_issue = GithubIssue(
  577. owner='test_owner',
  578. repo='test_repo',
  579. number=1,
  580. title='Test Issue',
  581. body='This is a test issue',
  582. thread_comments=[
  583. 'First comment',
  584. 'Second comment',
  585. 'latest feedback:\nPlease add tests',
  586. ],
  587. )
  588. mock_history = [MagicMock(message='I have added tests for this case')]
  589. mock_llm_config = LLMConfig(model='test_model', api_key='test_api_key')
  590. mock_completion_response = MagicMock()
  591. mock_completion_response.choices = [
  592. MagicMock(
  593. message=MagicMock(
  594. content='--- success\ntrue\n--- explanation\nTests have been added to verify thread comments handling'
  595. )
  596. )
  597. ]
  598. issue_handler = IssueHandler('owner', 'repo', 'token')
  599. with patch('litellm.completion', MagicMock(return_value=mock_completion_response)):
  600. success, comment_success, explanation = issue_handler.guess_success(
  601. mock_issue, mock_history, mock_llm_config
  602. )
  603. assert issue_handler.issue_type == 'issue'
  604. assert comment_success is None
  605. assert success
  606. assert 'Tests have been added' in explanation
  607. def test_guess_success_negative_case():
  608. mock_issue = GithubIssue(
  609. owner='test_owner',
  610. repo='test_repo',
  611. number=1,
  612. title='Test Issue',
  613. body='This is a test issue',
  614. )
  615. mock_history = [
  616. create_cmd_output(
  617. exit_code=0, content='', command_id=1, command='cd /workspace'
  618. )
  619. ]
  620. mock_llm_config = LLMConfig(model='test_model', api_key='test_api_key')
  621. mock_completion_response = MagicMock()
  622. mock_completion_response.choices = [
  623. MagicMock(
  624. message=MagicMock(
  625. content='--- success\nfalse\n--- explanation\nIssue not resolved'
  626. )
  627. )
  628. ]
  629. issue_handler = IssueHandler('owner', 'repo', 'token')
  630. with patch('litellm.completion', MagicMock(return_value=mock_completion_response)):
  631. success, comment_success, explanation = issue_handler.guess_success(
  632. mock_issue, mock_history, mock_llm_config
  633. )
  634. assert issue_handler.issue_type == 'issue'
  635. assert comment_success is None
  636. assert not success
  637. assert explanation == 'Issue not resolved'
  638. def test_guess_success_invalid_output():
  639. mock_issue = GithubIssue(
  640. owner='test_owner',
  641. repo='test_repo',
  642. number=1,
  643. title='Test Issue',
  644. body='This is a test issue',
  645. )
  646. mock_history = [
  647. create_cmd_output(
  648. exit_code=0, content='', command_id=1, command='cd /workspace'
  649. )
  650. ]
  651. mock_llm_config = LLMConfig(model='test_model', api_key='test_api_key')
  652. mock_completion_response = MagicMock()
  653. mock_completion_response.choices = [
  654. MagicMock(message=MagicMock(content='This is not a valid output'))
  655. ]
  656. issue_handler = IssueHandler('owner', 'repo', 'token')
  657. with patch('litellm.completion', MagicMock(return_value=mock_completion_response)):
  658. success, comment_success, explanation = issue_handler.guess_success(
  659. mock_issue, mock_history, mock_llm_config
  660. )
  661. assert issue_handler.issue_type == 'issue'
  662. assert comment_success is None
  663. assert not success
  664. assert (
  665. explanation
  666. == 'Failed to decode answer from LLM response: This is not a valid output'
  667. )
  668. def test_download_pr_with_review_comments():
  669. handler = PRHandler('owner', 'repo', 'token')
  670. mock_pr_response = MagicMock()
  671. mock_pr_response.json.side_effect = [
  672. [
  673. {
  674. 'number': 1,
  675. 'title': 'PR 1',
  676. 'body': 'This is a pull request',
  677. 'head': {'ref': 'b1'},
  678. },
  679. ],
  680. None,
  681. ]
  682. mock_pr_response.raise_for_status = MagicMock()
  683. # Mock for PR comments response
  684. mock_comments_response = MagicMock()
  685. mock_comments_response.json.return_value = [] # No PR comments
  686. mock_comments_response.raise_for_status = MagicMock()
  687. # Mock for GraphQL request with review comments but no threads
  688. mock_graphql_response = MagicMock()
  689. mock_graphql_response.json.side_effect = lambda: {
  690. 'data': {
  691. 'repository': {
  692. 'pullRequest': {
  693. 'closingIssuesReferences': {'edges': []},
  694. 'reviews': {
  695. 'nodes': [
  696. {'body': 'Please fix this typo'},
  697. {'body': 'Add more tests'},
  698. ]
  699. },
  700. }
  701. }
  702. }
  703. }
  704. mock_graphql_response.raise_for_status = MagicMock()
  705. def get_mock_response(url, *args, **kwargs):
  706. if '/comments' in url:
  707. return mock_comments_response
  708. return mock_pr_response
  709. with patch('requests.get', side_effect=get_mock_response):
  710. with patch('requests.post', return_value=mock_graphql_response):
  711. issues = handler.get_converted_issues(issue_numbers=[1])
  712. assert len(issues) == 1
  713. assert handler.issue_type == 'pr'
  714. assert isinstance(issues[0], GithubIssue)
  715. assert issues[0].number == 1
  716. assert issues[0].title == 'PR 1'
  717. assert issues[0].head_branch == 'b1'
  718. # Verify review comments are set but threads are empty
  719. assert len(issues[0].review_comments) == 2
  720. assert issues[0].review_comments[0] == 'Please fix this typo'
  721. assert issues[0].review_comments[1] == 'Add more tests'
  722. assert not issues[0].review_threads
  723. assert not issues[0].closing_issues
  724. assert not issues[0].thread_ids
  725. def test_download_issue_with_specific_comment():
  726. handler = IssueHandler('owner', 'repo', 'token')
  727. # Define the specific comment_id to filter
  728. specific_comment_id = 101
  729. # Mock issue and comment responses
  730. mock_issue_response = MagicMock()
  731. mock_issue_response.json.side_effect = [
  732. [
  733. {'number': 1, 'title': 'Issue 1', 'body': 'This is an issue'},
  734. ],
  735. None,
  736. ]
  737. mock_issue_response.raise_for_status = MagicMock()
  738. mock_comments_response = MagicMock()
  739. mock_comments_response.json.return_value = [
  740. {
  741. 'id': specific_comment_id,
  742. 'body': 'Specific comment body',
  743. 'issue_url': 'https://api.github.com/repos/owner/repo/issues/1',
  744. },
  745. {
  746. 'id': 102,
  747. 'body': 'Another comment body',
  748. 'issue_url': 'https://api.github.com/repos/owner/repo/issues/2',
  749. },
  750. ]
  751. mock_comments_response.raise_for_status = MagicMock()
  752. def get_mock_response(url, *args, **kwargs):
  753. if '/comments' in url:
  754. return mock_comments_response
  755. return mock_issue_response
  756. with patch('requests.get', side_effect=get_mock_response):
  757. issues = handler.get_converted_issues(
  758. issue_numbers=[1], comment_id=specific_comment_id
  759. )
  760. assert len(issues) == 1
  761. assert issues[0].number == 1
  762. assert issues[0].title == 'Issue 1'
  763. assert issues[0].thread_comments == ['Specific comment body']
  764. if __name__ == '__main__':
  765. pytest.main()