Beispiel #1
0
class TestRepoHandler:
    def setup_class(self):
        self.repo = RepoHandler('fakerepo/doesnotexist', branch='awesomebot')

    @patch('requests.get')
    def test_get_issues(self, mock_get):
        # http://engineroom.trackmaven.com/blog/real-life-mocking/
        mock_response = Mock()
        mock_response.json.return_value = [{
            'number': 42,
            'state': 'open'
        }, {
            'number': 55,
            'state': 'open',
            'pull_request': {
                'diff_url': 'blah'
            }
        }]
        mock_get.return_value = mock_response

        assert self.repo.get_issues('open', 'Close?') == [42]
        assert self.repo.get_issues('open', 'Close?',
                                    exclude_pr=False) == [42, 55]

    @patch('requests.get')
    def test_get_all_labels(self, mock_get):
        mock_response = Mock()
        mock_response.json.return_value = [{
            'name': 'io.fits'
        }, {
            'name': 'Documentation'
        }]
        mock_response.headers = {}
        mock_get.return_value = mock_response

        assert self.repo.get_all_labels() == ['io.fits', 'Documentation']

    def test_urls(self):
        assert self.repo._url_contents == 'https://api.github.com/repos/fakerepo/doesnotexist/contents/'
        assert self.repo._url_pull_requests == 'https://api.github.com/repos/fakerepo/doesnotexist/pulls'
        assert self.repo._headers == {}
Beispiel #2
0
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')
Beispiel #3
0
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')