Esempio n. 1
0
class TestIssueHandler:
    def setup_class(self):
        self.issue = IssueHandler('fakerepo/doesnotexist', 1234)

    def test_urls(self):
        assert self.issue._url_issue == 'https://api.github.com/repos/fakerepo/doesnotexist/issues/1234'
        assert self.issue._url_issue_nonapi == 'https://github.com/fakerepo/doesnotexist/issues/1234'
        assert self.issue._url_labels == 'https://api.github.com/repos/fakerepo/doesnotexist/issues/1234/labels'
        assert self.issue._url_issue_comment == 'https://api.github.com/repos/fakerepo/doesnotexist/issues/1234/comments'
        assert self.issue._url_timeline == 'https://api.github.com/repos/fakerepo/doesnotexist/issues/1234/timeline'

    @pytest.mark.parametrize(('state', 'answer'), [('open', False),
                                                   ('closed', True)])
    def test_is_closed(self, state, answer):
        with patch('baldrick.github.github_api.IssueHandler.json',
                   new_callable=PropertyMock) as mock_json:  # noqa
            mock_json.return_value = {'state': state}
            assert self.issue.is_closed is answer

    def test_missing_labels(self):
        with patch('baldrick.github.github_api.IssueHandler.labels',
                   new_callable=PropertyMock) as mock_issue_labels:  # noqa
            mock_issue_labels.return_value = ['io.fits']
            with patch('baldrick.github.github_api.RepoHandler.get_all_labels'
                       ) as mock_repo_labels:  # noqa
                mock_repo_labels.return_value = ['io.fits', 'closed-by-bot']

                # closed-by-bot label will be added to issue in POST
                missing_labels = self.issue._get_missing_labels(
                    'closed-by-bot')
                assert missing_labels == ['closed-by-bot']

                # Desired labels do not exist in repo
                missing_labels = self.issue._get_missing_labels(
                    ['dummy', 'foo'])
                assert missing_labels is None

                # Desired label already set on issue
                missing_labels = self.issue._get_missing_labels(['io.fits'])
                assert missing_labels is None

                # A mix
                missing_labels = self.issue._get_missing_labels(
                    ['io.fits', 'closed-by-bot', 'foo'])
                assert missing_labels == ['closed-by-bot']
Esempio n. 2
0
 def setup_class(self):
     self.issue = IssueHandler('fakerepo/doesnotexist', 1234)
Esempio n. 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')
Esempio n. 4
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')