diff --git a/src/managers/issue_manager.py b/src/managers/issue_manager.py index 449e8c9..74938a7 100644 --- a/src/managers/issue_manager.py +++ b/src/managers/issue_manager.py @@ -83,9 +83,7 @@ def handle_task_list(event: IssuesEvent) -> Optional[IssueJob]: continue # issue created in a previous run if created_issue := created_issues.get(task): - JobService.update( - created_issue, checked=checked, job_status=JobStatus.PENDING - ) + JobService.update(created_issue, checked=checked, job_status=JobStatus.PENDING) else: jobs.append( Job( @@ -105,9 +103,7 @@ def handle_task_list(event: IssuesEvent) -> Optional[IssueJob]: @lru_cache -def _cached_get_auth( - hook_installation_target_id: int, installation_id: int -) -> github.Auth: +def _cached_get_auth(hook_installation_target_id: int, installation_id: int) -> github.Auth: """Get the auth for the given installation, cached.""" return _get_auth(hook_installation_target_id, installation_id) @@ -115,9 +111,7 @@ def _cached_get_auth( @lru_cache def _get_gh(hook_installation_target_id: int, installation_id: int) -> github.Github: """Get the Github object for the given installation, cached""" - return github.Github( - auth=_cached_get_auth(hook_installation_target_id, installation_id) - ) + return github.Github(auth=_cached_get_auth(hook_installation_target_id, installation_id)) @lru_cache @@ -149,9 +143,7 @@ def _get_repository(issue_job: IssueJob, repository_name: str) -> Repository: @lru_cache -def _instantiate_github_class( - clazz: type[T], hook_installation_target_id: int, installation_id: int, url: str -) -> T: +def _instantiate_github_class(clazz: type[T], hook_installation_target_id: int, installation_id: int, url: str) -> T: """Instantiate a Github class, cached""" return clazz( requester=_get_requester(hook_installation_target_id, installation_id), @@ -214,9 +206,7 @@ def _get_repository_url_and_title(issue_job: IssueJob, task: str) -> tuple[str, def process_pending_jobs(issue_job: IssueJob) -> NoReturn: """Process the pending jobs separating what is a job to create an issue from a job to update an issue""" - for job in JobService.filter( - original_issue_url=issue_job.issue_url, job_status=JobStatus.PENDING - ): + for job in JobService.filter(original_issue_url=issue_job.issue_url, job_status=JobStatus.PENDING): task = job.task if job.issue_ref or is_issue_ref(task): issue_ref = job.issue_ref or task @@ -244,9 +234,7 @@ def process_pending_jobs(issue_job: IssueJob) -> NoReturn: def process_update_issue_status(issue_job: IssueJob) -> NoReturn: """Process the update issue status jobs.""" - for job in JobService.filter( - original_issue_url=issue_job.issue_url, job_status=JobStatus.UPDATE_ISSUE_STATUS - ): + for job in JobService.filter(original_issue_url=issue_job.issue_url, job_status=JobStatus.UPDATE_ISSUE_STATUS): issue = _instantiate_github_class( Issue, issue_job.hook_installation_target_id, @@ -254,31 +242,53 @@ def process_update_issue_status(issue_job: IssueJob) -> NoReturn: job.issue_url, ) try: - handle_issue_state(job.checked, issue) - set_jobs_to_done([job], issue_job) + _handle_checkbox(issue, job.checked) + JobService.update( + job, + job_status=JobStatus.DONE, + ) except UnknownObjectException: logger.warning("Issue %s not found", issue.url) JobService.update(job, job_status=JobStatus.ERROR) -@Config.call_if("issue_manager.create_issues_from_tasklist") +@Config.call_if("issue_manager.handle_checkbox") +def _handle_checkbox(issue: Issue, checked: bool) -> NoReturn: + """ + Handle the state of the issue. + If the issue is closed and the checkbox is checked, open the issue. + If the issue is open and the checkbox is unchecked, close the issue. + """ + handle_issue_state(checked, issue) + + def process_create_issue(issue_job: IssueJob) -> NoReturn: - """Process the create issue status jobs.""" - for job in JobService.filter( - original_issue_url=issue_job.issue_url, job_status=JobStatus.CREATE_ISSUE - ): - repository = _instantiate_github_class( - Repository, - issue_job.hook_installation_target_id, - issue_job.installation_id, - job.repository_url, - ) - created_issue = repository.create_issue(title=job.title) - JobService.update( - job, - job_status=JobStatus.UPDATE_ISSUE_BODY, - issue_ref=get_issue_ref(created_issue), - ) + """Process the create_issue status jobs.""" + for job in JobService.filter(original_issue_url=issue_job.issue_url, job_status=JobStatus.CREATE_ISSUE): + if created_issue := _create_issue(issue_job, job): + JobService.update( + job, + job_status=JobStatus.UPDATE_ISSUE_BODY, + issue_ref=get_issue_ref(created_issue), + ) + else: + JobService.update( + job, + job_status=JobStatus.DONE, + ) + + +@Config.call_if("issue_manager.create_issues_from_tasklist") +def _create_issue(issue_job: IssueJob, job: Job) -> Issue: + """Create a new issue.""" + repository = _instantiate_github_class( + Repository, + issue_job.hook_installation_target_id, + issue_job.installation_id, + job.repository_url, + ) + created_issue = repository.create_issue(title=job.title) + return created_issue def process_update_issue_body(issue_job: IssueJob) -> NoReturn: @@ -303,6 +313,7 @@ def process_update_issue_body(issue_job: IssueJob) -> NoReturn: set_jobs_to_done(update_issue_body_jobs, issue_job) +@Config.call_if("issue_manager.close_parent") def close_issue_if_all_checked(issue_job: IssueJob) -> NoReturn: """Close the issue if all the tasks are checked.""" issue = _instantiate_github_class( @@ -323,15 +334,9 @@ def process_update_progress(issue_job: IssueJob) -> NoReturn: if issue_job.issue_job_status == IssueJobStatus.DONE: comment = "Job's done" else: - done = len( - JobService.filter( - original_issue_url=issue_job.issue_url, job_status=JobStatus.DONE - ) - ) + done = len(JobService.filter(original_issue_url=issue_job.issue_url, job_status=JobStatus.DONE)) total = len(JobService.filter(original_issue_url=issue_job.issue_url)) - comment = ( - f"Analyzing the tasklist [{done}/{total}]\n{markdown_progress(done, total)}" - ) + comment = f"Analyzing the tasklist [{done}/{total}]\n{markdown_progress(done, total)}" issue = _instantiate_github_class( Issue, issue_job.hook_installation_target_id,