def check_pr_for_mergability(pr: PullRequest) -> str: repo = pr.base.repo commit = repo.get_commit(pr.head.sha) checks: Dict[str, str] = {} for status in commit.get_statuses(): print(status) if status.context == PDM_CHECK_CONTEXT: continue if checks.get(status.context) is None: checks[status.context] = status.state if status.state != 'success': commit.create_status(state='pending', description=f'Waiting for {status.context}', context=PDM_CHECK_CONTEXT) return f'Merge blocked by {status.context}' travis_pr = 'continuous-integration/travis-ci/pr' if travis_pr not in checks.keys(): # There's a lovely race condition where, if: # 1. travis/push has completed before the PR was made # 2. And the label is applied on creation (or author is whitelisted) # The PR can be merged before travis is aware of the PR. # The solution to this is to hardcode a check for /pr commit.create_status(state='pending', description=f'Waiting for {travis_pr}', context=PDM_CHECK_CONTEXT) return f'Merge blocked by {travis_pr}' labels = [l.name for l in pr.as_issue().labels] if 'do not merge' in labels: commit.create_status(state='failure', description='Blocked by "do not merge"', context=PDM_CHECK_CONTEXT) return 'Do not Merge' whitelisted = pr.user in repo.get_collaborators() if not whitelisted and not 'merge when ready' in labels: commit.create_status(state='pending', description='Waiting for "merge when ready"', context=PDM_CHECK_CONTEXT) return 'Waiting for label' if 'beta test' in labels: trying = repo.get_git_ref('heads/trying') if trying.object.sha == commit.sha: commit.create_status(state='success', description='Deployed to test branch', context=PDM_CHECK_CONTEXT) return 'Already deployed' trying.edit(commit.sha, True) commit.create_status(state='success', description='Deployed to test branch', context=PDM_CHECK_CONTEXT) return 'beta test' commit.create_status(state='success', description='Ready to merge', context=PDM_CHECK_CONTEXT) pr.merge() return 'good to merge'
def _merge_pr(self, pr: ghp.PullRequest) -> None: assert self.repo_options.gh_auto_merge_pr is not None if pr.update() is True: if pr.state == 'closed' or not pr.mergeable: return if self.repo_options.gh_auto_merge_pr.required_label_name not in [l.name for l in pr.labels]: return status = pr.merge() if status.merged: print(f'GH:{self.repo_path}: PR#{pr.number} merged.') else: raise RuntimeError(f'GH:{self.repo_path}: PR#{pr.number} fail to merge - "{status.message}"!')