예제 #1
0
    def test_publish_checkrun(self):
        self.repo.create_checkrun = Mock()
        tst_config = build_review_config(fixer_ini,
                                         {'PULLREQUEST_STATUS': True})
        problems = Problems()
        filename_1 = 'Console/Command/Task/AssetBuildTask.php'
        errors = (
            Comment(filename_1, 117, 8, 'Something bad'),
            Comment(filename_1, 119, 9, 'Something worse'),
        )
        problems.add_many(errors)
        run_id = 42

        review = Review(self.repo, self.pr, tst_config)
        review.publish_checkrun(problems, run_id)

        assert self.repo.update_checkrun.called
        self.assertEqual(1, self.repo.update_checkrun.call_count)

        assert_checkrun(
            self,
            self.repo.update_checkrun.call_args,
            errors,
            run_id)
        assert self.repo.create_status.called is False, 'no status required'
예제 #2
0
    def test_publish_checkrun(self):
        self.repo.create_checkrun = Mock()
        config = build_review_config(fixer_ini,
                                     {'PULLREQUEST_STATUS': True})
        problems = Problems()

        filename_1 = 'Console/Command/Task/AssetBuildTask.php'
        errors = (
            Comment(filename_1, 117, 8, 'Something bad'),
            Comment(filename_1, 119, 9, 'Something worse'),
        )
        problems.add_many(errors)
        run_id = 42

        review = Review(self.repo, self.pr, config)
        review.publish_checkrun(problems, run_id)

        assert self.repo.update_checkrun.called
        eq_(1, self.repo.update_checkrun.call_count)

        assert_checkrun(
            self.repo.update_checkrun.call_args,
            errors,
            run_id)
        assert self.repo.create_status.called is False, 'no status required'
예제 #3
0
    def test_publish_checks_api__no_problems(self):
        self.repo.create_checkrun = Mock()
        config = build_review_config(checks_ini, {'PULLREQUEST_STATUS': True})
        problems = Problems()
        sha = 'abc123'

        review = Review(self.repo, self.pr, config)
        review.publish_checkrun(problems, False, sha)

        assert self.repo.create_checkrun.called
        eq_(1, self.repo.create_checkrun.call_count)

        assert_checkrun(self.repo.create_checkrun.call_args, [], sha)
예제 #4
0
    def test_publish_checkrun__no_problems(self):
        tst_config = build_review_config(fixer_ini,
                                         {'PULLREQUEST_STATUS': True})
        problems = Problems()
        run_id = 42

        review = Review(self.repo, self.pr, tst_config)
        review.publish_checkrun(problems, run_id)

        assert self.repo.update_checkrun.called
        self.assertEqual(1, self.repo.update_checkrun.call_count)

        assert_checkrun(self.repo.update_checkrun.call_args, [], run_id)
        assert self.repo.create_status.called is False, 'no status required'
예제 #5
0
    def test_publish_checkrun__multiple_chunks(self):
        self.repo.create_checkrun = Mock()
        tst_config = build_review_config(fixer_ini,
                                         {'PULLREQUEST_STATUS': True})
        problems = Problems()
        filename_1 = 'Console/Command/Task/AssetBuildTask.php'
        errors = [
            Comment(filename_1, i, i, 'Something worse') for i in range(0, 70)
        ]
        problems.add_many(errors)
        problems.add(IssueComment('In the body'))
        run_id = 42

        review = Review(self.repo, self.pr, tst_config)
        review.publish_checkrun(problems, run_id)

        assert self.repo.update_checkrun.call_count == 2
        assert self.repo.create_status.called is False, 'no status required'

        first_call = self.repo.update_checkrun.call_args_list[0]
        assert run_id == first_call[0][0]

        first_payload = first_call[0][1]
        assert 'failure' == first_payload['conclusion']

        assert 'completed_at' in first_payload
        assert 'title' in first_payload['output']
        assert 'summary' in first_payload['output']
        assert 'annotations' in first_payload['output']

        assert 'In the body' == first_payload['output']['summary']
        assert 50 == len(first_payload['output']['annotations'])

        second_call = self.repo.update_checkrun.call_args_list[1]
        assert run_id == second_call[0][0]

        # The second payload should only contain additional annotations.
        second_payload = second_call[0][1]
        assert 'completed_at' not in second_payload
        assert 'title' in second_payload['output']
        assert 'summary' in second_payload['output']
        assert 'annotations' in second_payload['output']
        assert 'In the body' == second_payload['output']['summary']

        assert 20 == len(second_payload['output']['annotations'])
예제 #6
0
    def test_publish_checkrun__no_problems(self):
        self.repo.create_checkrun = Mock()
        tst_config = build_review_config(fixer_ini,
                                         {'PULLREQUEST_STATUS': True})
        problems = Problems()
        run_id = 42

        review = Review(self.repo, self.pr, tst_config)
        review.publish_checkrun(problems, run_id)

        assert self.repo.update_checkrun.called
        self.assertEqual(1, self.repo.update_checkrun.call_count)

        assert_checkrun(
            self,
            self.repo.update_checkrun.call_args,
            [],
            run_id)
        assert self.repo.create_status.called is False, 'no status required'
예제 #7
0
    def test_publish_checks_api(self):
        self.repo.create_checkrun = Mock()
        config = build_review_config(checks_ini, {'PULLREQUEST_STATUS': True})
        problems = Problems()

        filename_1 = 'Console/Command/Task/AssetBuildTask.php'
        errors = (
            Comment(filename_1, 117, 8, 'Something bad'),
            Comment(filename_1, 119, 9, 'Something worse'),
        )
        problems.add_many(errors)
        sha = 'abc123'

        review = Review(self.repo, self.pr, config)
        review.publish_checkrun(problems, True, sha)

        assert self.repo.create_checkrun.called
        eq_(1, self.repo.create_checkrun.call_count)

        assert_checkrun(self.repo.create_checkrun.call_args, errors, sha)
예제 #8
0
    def test_publish_checkrun__has_errors_force_success_status(self):
        self.repo.create_checkrun = Mock()
        config = build_review_config(fixer_ini, {'PULLREQUEST_STATUS': False})
        eq_('success', config.failed_review_status(), 'config object changed')

        review = Review(self.repo, self.pr, config)

        problems = Problems()
        filename_1 = 'Console/Command/Task/AssetBuildTask.php'
        errors = (
            Comment(filename_1, 117, 8, 'Something bad'),
            Comment(filename_1, 119, 9, 'Something worse'),
        )
        problems.add_many(errors)
        run_id = 42
        review.publish_checkrun(problems, run_id)

        assert self.repo.create_status.called is False, 'no status required'

        checkrun = self.repo.update_checkrun.call_args[0][1]
        eq_('success', checkrun['conclusion'])
        assert len(checkrun['output']['annotations']) > 0
예제 #9
0
    def test_publish_checkrun__has_errors_force_success_status(self):
        self.repo.create_checkrun = Mock()
        tst_config = build_review_config(fixer_ini,
                                         {'PULLREQUEST_STATUS': False})
        self.assertEqual('success', tst_config.failed_review_status(),
                         'config object changed')

        review = Review(self.repo, self.pr, tst_config)

        problems = Problems()
        filename_1 = 'Console/Command/Task/AssetBuildTask.php'
        errors = (
            Comment(filename_1, 117, 8, 'Something bad'),
            Comment(filename_1, 119, 9, 'Something worse'),
        )
        problems.add_many(errors)
        run_id = 42
        review.publish_checkrun(problems, run_id)

        assert self.repo.create_status.called is False, 'no status required'

        checkrun = self.repo.update_checkrun.call_args[0][1]
        self.assertEqual('success', checkrun['conclusion'])
        assert len(checkrun['output']['annotations']) > 0
예제 #10
0
class Processor(object):

    _repository = None
    _pull_request = None
    _target_path = None
    _changes = None
    _review = None
    _config = None
    problems = None

    def __init__(self, repository, pull_request, target_path, config):
        self._config = config
        self._repository = repository
        self._pull_request = pull_request
        self._target_path = target_path
        self.problems = Problems()
        self._review = Review(repository, pull_request, config)

    def load_changes(self):
        log.info('Loading pull request patches from github.')
        files = self._pull_request.files()
        self._changes = DiffCollection(files)
        self.problems.set_changes(self._changes)

    def run_tools(self):
        if self._changes is None:
            raise RuntimeError('No loaded changes, cannot run tools. '
                               'Try calling load_changes first.')
        config = self._config

        files_to_check = self._changes.get_files(
            ignore_patterns=config.ignore_patterns())
        commits_to_check = self._pull_request.commits()

        tool_list = tools.factory(config, self.problems, self._target_path)

        if config.fixers_enabled():
            self.apply_fixers(tool_list, files_to_check)

        tools.run(tool_list, files_to_check, commits_to_check)

    def apply_fixers(self, tool_list, files_to_check):
        try:
            fixer_context = fixers.create_context(
                self._config,
                self._target_path,
                self._repository,
                self._pull_request,
            )
            fixer_diff = fixers.run_fixers(tool_list, self._target_path,
                                           files_to_check)
            fixers.apply_fixer_diff(self._changes, fixer_diff, fixer_context)
        except (ConfigurationError, WorkflowError) as e:
            log.info('Fixer application failed. Got %s', e)
            message = u'Unable to apply fixers. {}'.format(e)
            self.problems.add(InfoComment(message))
            fixers.rollback_changes(self._target_path, self._pull_request.head)
        except Exception as e:
            log.info(
                'Fixer application failed, '
                'rolling back working tree. Got %s', e)
            fixers.rollback_changes(self._target_path, self._pull_request.head)

    def publish(self, check_run_id=None):
        self.problems.limit_to_changes()
        if check_run_id:
            self._review.publish_checkrun(self.problems, check_run_id)
        else:
            self._review.publish_review(self.problems, self._pull_request.head)
예제 #11
0
class Processor(object):

    _repository = None
    _pull_request = None
    _target_path = None
    _changes = None
    _review = None
    _config = None
    problems = None

    def __init__(self, repository, pull_request, target_path, config):
        self._config = config
        self._repository = repository
        self._pull_request = pull_request
        self._target_path = target_path
        self.problems = Problems()
        self._review = Review(repository, pull_request, config)

    def load_changes(self):
        log.info('Loading pull request patches from github.')
        files = self._pull_request.files()
        self._changes = DiffCollection(files)
        self.problems.set_changes(self._changes)

    def run_tools(self):
        if self._changes is None:
            raise RuntimeError('No loaded changes, cannot run tools. '
                               'Try calling load_changes first.')
        config = self._config

        files_to_check = self._changes.get_files(
            ignore_patterns=config.ignore_patterns()
        )
        commits_to_check = self._pull_request.commits()

        tool_list = tools.factory(
            config,
            self.problems,
            self._target_path)

        if config.fixers_enabled():
            self.apply_fixers(tool_list, files_to_check)

        tools.run(tool_list, files_to_check, commits_to_check)

    def apply_fixers(self, tool_list, files_to_check):
        try:
            fixer_context = fixers.create_context(
                self._config,
                self._target_path,
                self._repository,
                self._pull_request,
            )
            fixer_diff = fixers.run_fixers(
                tool_list,
                self._target_path,
                files_to_check)
            fixers.apply_fixer_diff(
                self._changes,
                fixer_diff,
                fixer_context)
        except (ConfigurationError, WorkflowError) as e:
            log.info('Fixer application failed. Got %s', e)
            message = u'Unable to apply fixers. {}'.format(e)
            self.problems.add(InfoComment(message))
        except Exception as e:
            log.info('Fixer application failed, '
                     'rolling back working tree. Got %s', e)
            fixers.rollback_changes(self._target_path)

    def publish(self, check_run_id=None):
        self.problems.limit_to_changes()
        if check_run_id:
            self._review.publish_checkrun(
                self.problems,
                check_run_id)
        else:
            self._review.publish_review(
                self.problems,
                self._pull_request.head)