def process_pull_requests(repository, installation, warn_seconds=None, close_seconds=None): now = time.time() # Find app name bot_name = get_app_name() # Get issues labeled as 'Close?' repo = RepoHandler(repository, 'master', installation) pull_requests = repo.open_pull_requests() for n in pull_requests: print(f'Checking {n}') pr = PullRequestHandler(repository, n, installation) if 'keep-open' in pr.labels: print('-> PROTECTED by label, skipping') continue commit_time = pr.last_commit_date time_since_last_commit = now - commit_time # Note: if warning time is before commit time, it's as if the warning # didn't exist since it's no longer relevant. warning_time = pr.last_comment_date(f'{bot_name}[bot]', filter_keep=is_close_warning) if warning_time is None or warning_time < commit_time: time_since_last_warning = -1. else: # We use max() here to make sure that the value is positive time_since_last_warning = max(0, now - warning_time) # We only close pull requests if there has been a warning before, and # the time since the warning exceeds the threshold specified by # close_seconds. if time_since_last_warning > close_seconds: comment_ids = pr.find_comments(f'{bot_name}[bot]', filter_keep=is_close_epilogue) if len(comment_ids) == 0: print(f'-> CLOSING pull request {n}') pr.set_labels(['closed-by-bot']) pr.submit_comment(PULL_REQUESTS_CLOSE_EPILOGUE) pr.close() else: print(f'-> Skipping pull request {n} (already closed)') elif time_since_last_commit > warn_seconds: # A negative time_since_last_warning means no warning since last commit. if time_since_last_warning < 0.: print(f'-> WARNING pull request {n}') pr.submit_comment(PULL_REQUESTS_CLOSE_WARNING.format(pasttime=naturaldelta(time_since_last_commit), futuretime=naturaldelta(close_seconds))) else: print(f'-> Skipping pull request {n} (already warned)') else: print(f'-> OK pull request {n}') print('Finished checking for stale pull requests')
def test_get_app_name(app): with app.app_context(): with patch('requests.get') as post: post.return_value.ok = True post.return_value.json.return_value = {'name': 'testbot'} name = get_app_name() assert name == 'testbot'
def process_issues(repository, installation, warn_seconds=None, close_seconds=None): now = time.time() # Find app name bot_name = get_app_name() # Get issues labeled as 'Close?' repo = RepoHandler(repository, 'master', installation) issuelist = repo.get_issues('open', 'Close?') for n in issuelist: print(f'Checking {n}') issue = IssueHandler(repository, n, installation) labeled_time = issue.get_label_added_date('Close?') if labeled_time is None: continue dt = now - labeled_time if dt > close_seconds: comment_ids = issue.find_comments(f'{bot_name}[bot]', filter_keep=is_close_epilogue) if len(comment_ids) == 0: print(f'-> CLOSING issue {n}') issue.set_labels(['closed-by-bot']) issue.submit_comment(ISSUE_CLOSE_EPILOGUE) issue.close() else: print(f'-> Skipping issue {n} (already closed)') elif dt > warn_seconds: comment_ids = issue.find_comments(f'{bot_name}[bot]', filter_keep=is_close_warning) if len(comment_ids) == 0: print(f'-> WARNING issue {n}') issue.submit_comment( ISSUE_CLOSE_WARNING.format( pasttime=naturaltime(dt), futuretime=naturaldelta(close_seconds - warn_seconds))) else: print(f'-> Skipping issue {n} (already warned)') else: print(f'-> OK issue {n}') print('Finished checking for stale issues')
def process_issues(repository, installation, warn_seconds=None, close_seconds=None): now = time.time() # Find app name bot_name = get_app_name() # Get issues labeled as 'Close?' repo = RepoHandler(repository, 'master', installation) issuelist = repo.get_issues('open', 'Close?') for n in issuelist: print(f'Checking {n}') issue = IssueHandler(repository, n, installation) labeled_time = issue.get_label_added_date('Close?') if labeled_time is None: continue time_since_close_label = now - labeled_time # Note: if warning time is before label time, it's as if the warning # didn't exist since it's no longer relevant. warning_time = issue.last_comment_date(f'{bot_name}[bot]', filter_keep=is_close_warning) if warning_time is None or warning_time < labeled_time: time_since_last_warning = -1. else: # We use max() here to make sure that the value is positive time_since_last_warning = max(0, now - warning_time) # We only close issues if there has been a warning before, and # the time since the warning exceeds the threshold specified by # close_seconds. if time_since_last_warning > close_seconds: comment_ids = issue.find_comments(f'{bot_name}[bot]', filter_keep=is_close_epilogue) if len(comment_ids) == 0: print(f'-> CLOSING issue {n}') issue.set_labels(['closed-by-bot']) issue.submit_comment(ISSUE_CLOSE_EPILOGUE) issue.close() else: print(f'-> Skipping issue {n} (already closed)') elif time_since_close_label > warn_seconds: comment_ids = issue.find_comments(f'{bot_name}[bot]', filter_keep=is_close_warning) if len(comment_ids) == 0: print(f'-> WARNING issue {n}') issue.submit_comment( ISSUE_CLOSE_WARNING.format( pasttime=naturaltime(time_since_close_label), futuretime=naturaldelta(close_seconds))) else: print(f'-> Skipping issue {n} (already warned)') else: print(f'-> OK issue {n}') print('Finished checking for stale issues')