def _task_status_from_pull_request(pull_request: PullRequest) -> str: if not pull_request.closed(): return "Open" elif pull_request.closed() and pull_request.merged(): return "Merged" elif pull_request.closed() and not pull_request.merged(): return "Closed" else: logger.error("Pull request is in an invalid state") return ""
def _is_pull_request_ready_for_automerge(pull_request: PullRequest) -> bool: # autofail if not enabled or pull request isn't open if ( not SGTM_FEATURE__AUTOMERGE_ENABLED or pull_request.closed() or pull_request.merged() ): return False # if there are multiple labels, we use the most permissive to define automerge behavior if pull_request_has_label(pull_request, AutomergeLabel.IMMEDIATELY.value): return pull_request.mergeable() in ( MergeableState.MERGEABLE, MergeableState.UNKNOWN, ) if pull_request_has_label(pull_request, AutomergeLabel.AFTER_TESTS.value): return pull_request.is_build_successful() and pull_request.is_mergeable() if pull_request_has_label( pull_request, AutomergeLabel.AFTER_TESTS_AND_APPROVAL.value ): return ( pull_request.is_build_successful() and pull_request.is_mergeable() and pull_request.is_approved() ) if pull_request_has_label(pull_request, AutomergeLabel.AFTER_APPROVAL.value): return pull_request.is_mergeable() and pull_request.is_approved() return False
def _task_completion_from_pull_request( pull_request: PullRequest) -> StatusReason: if not pull_request.closed(): return StatusReason(False, "the pull request is open.") elif not pull_request.merged(): return StatusReason( True, "the pull request was closed without merging code.") elif github_logic.pull_request_approved_before_merging(pull_request): return StatusReason(True, "the pull request was approved before merging.") elif github_logic.pull_request_approved_after_merging(pull_request): return StatusReason(True, "the pull request was approved after merging.") else: return StatusReason( False, "the pull request hasn't yet been approved by a reviewer after merging.", )
def should_autocomplete_tasks_on_merge(pull_request: PullRequest) -> bool: return (SGTM_FEATURE__AUTOCOMPLETE_ENABLED and pull_request.merged() and pull_request_has_label( pull_request, AutocompleteLabel.COMPLETE_ON_MERGE.value))