diff --git a/src/managers/issue.py b/src/managers/issue.py index 632d4e6..e37a6d7 100644 --- a/src/managers/issue.py +++ b/src/managers/issue.py @@ -24,7 +24,9 @@ def handle_tasklist(event: IssuesEvent): repository = event.repository issue = event.issue issue_body = issue.body + all_checked = [] for checked, task in get_tasklist(issue_body): + all_checked.append(checked) if task_issue := get_issue(gh, repository, task): handle_issue_state(checked, task_issue) @@ -47,3 +49,5 @@ def handle_tasklist(event: IssuesEvent): issue_body = issue_body.replace(task, issue_ref(created_issue)) if issue_body != issue.body: issue.edit(body=issue_body) + if all_checked and all(all_checked): + issue.edit(state="closed") diff --git a/tests/managers/test_issue.py b/tests/managers/test_issue.py index ebd11f8..e5d138c 100644 --- a/tests/managers/test_issue.py +++ b/tests/managers/test_issue.py @@ -1,4 +1,4 @@ -from unittest.mock import Mock, patch +from unittest.mock import Mock, call, patch import pytest @@ -76,11 +76,33 @@ def test_handle_tasklist_with_repository_name_and_title( def test_handle_tasklist_with_issue_in_task_list(event, issue, repository): - issue.body = "- [x] #123" + issue.body = "- [ ] #123" repository.get_issue.return_value = issue with patch("src.managers.issue.handle_issue_state") as handle_issue_state: handle_tasklist(event) - handle_issue_state.assert_called_once_with(True, issue) + handle_issue_state.assert_called_once_with(False, issue) repository.get_issue.assert_called_once_with(123) repository.create_issue.assert_not_called() issue.edit.assert_not_called() + + +def test_handle_tasklist_when_not_all_tasks_are_done(event, issue, repository): + issue.body = "- [x] #123\r\n- [ ] #321" + repository.get_issue.return_value = issue + with patch("src.managers.issue.handle_issue_state") as handle_issue_state: + handle_tasklist(event) + handle_issue_state.assert_has_calls([call(True, issue), call(False, issue)]) + repository.get_issue.assert_has_calls([call(123), call(321)]) + repository.create_issue.assert_not_called() + issue.edit.assert_not_called() + + +def test_handle_tasklist_when_all_tasks_are_done(event, issue, repository): + issue.body = "- [x] #123\r\n- [x] #321" + repository.get_issue.return_value = issue + with patch("src.managers.issue.handle_issue_state") as handle_issue_state: + handle_tasklist(event) + handle_issue_state.assert_has_calls([call(True, issue), call(True, issue)]) + repository.get_issue.assert_has_calls([call(123), call(321)]) + repository.create_issue.assert_not_called() + issue.edit.assert_called_once_with(state="closed")