def _label_pr_based_on_status()

in services/lambda-pr-status-labeler/pr_status_bot/PRStatusBot.py [0:0]


    def _label_pr_based_on_status(self, full_build_status_state, pull_request_obj):
        """
        This method checks the CI status of the specific commit of the PR
        and it labels the PR accordingly
        :param full_build_status_state
        :param pull_request_obj
        """
        # pseudo-code
        # if WIP in title or PR is draft or CI failed:
        #   pr-work-in-progress
        # elif CI has not started yet or CI is in progress:
        #   pr-awaiting-testing
        # else: # CI passed checks
        #   if pr has at least one approval and no request changes:
        #       pr-awaiting-merge
        #   elif pr has no review or all reviews have been dismissed/re-requested:
        #       pr-awaiting-review
        #   else: # pr has a review that hasn't been dismissed yet no approval
        #       pr-awaiting-response

        # combined status of PR can be 1 of the 3 potential states
        # https://developer.github.com/v3/repos/statuses/#get-the-combined-status-for-a-specific-reference
        wip_in_title, ci_failed, ci_pending = False, False, False
        if full_build_status_state == FAILURE_STATE:
            ci_failed = True
        elif full_build_status_state == PENDING_STATE:
            ci_pending = True

        if WORK_IN_PROGRESS_TITLE_SUBSTRING in pull_request_obj.title:
            logging.info('WIP in PR Title')
            wip_in_title = True
        work_in_progress_conditions = wip_in_title or pull_request_obj.draft or ci_failed
        if work_in_progress_conditions:
            self._add_label(pull_request_obj, PR_WORK_IN_PROGRESS_LABEL)
        elif ci_pending:
            self._add_label(pull_request_obj, PR_AWAITING_TESTING_LABEL)
        else:  # CI passed since status=successful
            # parse reviews to assess count of approved/requested changes/commented reviews
            # make sure you take into account dismissed reviews
            approves, request_changes, comments = self._parse_reviews(pull_request_obj)
            if approves > 0 and request_changes == 0:
                self._add_label(pull_request_obj, PR_AWAITING_MERGE_LABEL)
            else:
                # decisive review means approve/request change
                # comment is a non-decisive review
                has_no_decisive_reviews = approves + request_changes == 0
                if has_no_decisive_reviews:
                    self._add_label(pull_request_obj, PR_AWAITING_REVIEW_LABEL)
                else:
                    self._add_label(pull_request_obj, PR_AWAITING_RESPONSE_LABEL)
        return