def test_publish_review_no_count_change(self): fixture = load_fixture('comments_current.json') self.pr.review_comments.return_value = [ GhIssueComment(f) for f in json.loads(fixture) ] problems = Problems() # Match the line/positions in comments_current.json filename_1 = 'Console/Command/Task/AssetBuildTask.php' errors = ( Comment(filename_1, 40, 40, '2. Something bad'), Comment(filename_1, 87, 87, '1. Something bad'), Comment(filename_1, 89, 89, '2. Something bad'), ) problems.add_many(errors) problems.set_changes([1]) sha = 'abc123' config = build_review_config(fixer_ini, {'SUMMARY_THRESHOLD': 1}) review = Review(self.repo, self.pr, config) review.publish_summary = Mock() review.publish_status = Mock() review.publish_review(problems, sha) # Ensure publish_status(True) means the status=failed review.publish_status.assert_called_with(True)
def test_publish_review_remove_ok_label(self): problems = Problems() filename_1 = 'Console/Command/Task/AssetBuildTask.php' errors = ( Comment(filename_1, 117, 117, 'Something bad'), Comment(filename_1, 119, 119, 'Something bad'), ) problems.add_many(errors) sha = 'abc123' config = {'OK_LABEL': 'No lint'} review = Review(self.repo, self.pr, config) sha = 'abc123' review.publish_review(problems, sha) assert self.pr.remove_label.called, 'Label should be removed' assert self.pr.create_review.called, 'Review should be added' eq_(1, self.pr.create_review.call_count) self.pr.remove_label.assert_called_with(config['OK_LABEL']) assert_review( self.pr.create_review.call_args, errors, sha)
def test_publish_review__no_comments(self): problems = Problems() sha = 'abc123' review = Review(self.repo, self.pr, self.config) review.publish_review(problems, sha) assert self.pr.create_review.called is False
def test_publish_review__only_issue_comment(self): problems = Problems() problems.add(IssueComment('Very bad')) sha = 'abc123' review = Review(self.repo, self.pr, self.config) review.publish_review(problems, sha) assert self.pr.create_review.called assert_review(self.pr.create_review.call_args, [], sha, body='Very bad')
def test_publish_review_empty_comment(self): problems = Problems(changes=DiffCollection([])) review = Review(self.repo, self.pr, self.config) sha = 'abc123' review.publish_review(problems, sha) assert self.pr.create_comment.called, 'Should create a comment' msg = ('Could not review pull request. ' 'It may be too large, or contain no reviewable changes.') self.pr.create_comment.assert_called_with(msg)
def test_publish_review_empty_comment_add_ok_label(self): problems = Problems(changes=DiffCollection([])) tst_config = build_review_config(fixer_ini, {'OK_LABEL': 'No lint'}) review = Review(self.repo, self.pr, tst_config) sha = 'abc123' review.publish_review(problems, sha) assert self.pr.create_comment.called, 'ok comment should be added.' assert self.pr.remove_label.called, 'label should be removed.' self.pr.remove_label.assert_called_with(tst_config['OK_LABEL']) msg = ('Could not review pull request. ' 'It may be too large, or contain no reviewable changes.') self.pr.create_comment.assert_called_with(msg)
def test_publish_review_empty_comment_with_comment_status(self): config = build_review_config(fixer_ini, {'PULLREQUEST_STATUS': True}) problems = Problems(changes=[]) review = Review(self.repo, self.pr, config) sha = 'abc123' review.publish_review(problems, sha) assert self.pr.create_comment.called, 'Should create a comment' msg = ('Could not review pull request. ' 'It may be too large, or contain no reviewable changes.') self.repo.create_status.assert_called_with(self.pr.head, 'success', msg) self.pr.create_comment.assert_called_with(msg)
def test_publish_review(self): problems = Problems() filename_1 = 'Console/Command/Task/AssetBuildTask.php' errors = ( Comment(filename_1, 117, 117, 'Something bad'), Comment(filename_1, 119, 119, 'Something bad'), ) problems.add_many(errors) sha = 'abc123' review = Review(self.repo, self.pr, self.config) review.publish_review(problems, sha) assert self.pr.create_review.called eq_(1, self.pr.create_review.call_count) assert_review(self.pr.create_review.call_args, errors, sha)
def test_publish_review_empty_comment_with_comment_status(self): tst_config = build_review_config(fixer_ini, {'PULLREQUEST_STATUS': True}) problems = Problems(changes=DiffCollection([])) review = Review(self.repo, self.pr, tst_config) sha = 'abc123' review.publish_review(problems, sha) assert self.pr.create_comment.called, 'Should create a comment' msg = ('Could not review pull request. ' 'It may be too large, or contain no reviewable changes.') self.repo.create_status.assert_called_with( self.pr.head, 'success', msg) self.pr.create_comment.assert_called_with(msg)
def test_publish__join_issue_comments(self): problems = Problems() filename_1 = 'Console/Command/Task/AssetBuildTask.php' errors = ( IssueComment('First'), Comment(filename_1, 119, 119, 'Something bad'), IssueComment('Second'), ) problems.add_many(errors) sha = 'abc123' review = Review(self.repo, self.pr, self.config) review.publish_review(problems, sha) assert self.pr.create_review.called eq_(1, self.pr.create_review.call_count) assert_review(self.pr.create_review.call_args, [errors[1]], sha, body='First\n\nSecond')
def test_publish_review_comment_threshold_checks(self): fixture = load_fixture('comments_current.json') self.pr.review_comments.return_value = [ GhIssueComment(f) for f in json.loads(fixture)] problems = Problems() filename_1 = 'Console/Command/Task/AssetBuildTask.php' errors = ( Comment(filename_1, 117, 117, 'Something bad'), Comment(filename_1, 119, 119, 'Something bad'), ) problems.add_many(errors) problems.set_changes([1]) sha = 'abc123' config = build_review_config(fixer_ini, {'SUMMARY_THRESHOLD': 1}) review = Review(self.repo, self.pr, config) review.publish_summary = Mock() review.publish_review(problems, sha) assert review.publish_summary.called, 'Should have been called.'
def test_publish_review_comment_threshold_checks(self): fixture = load_fixture('comments_current.json') self.pr.review_comments.return_value = [ GhIssueComment(f, self.session) for f in json.loads(fixture) ] problems = Problems() filename_1 = 'Console/Command/Task/AssetBuildTask.php' errors = ( Comment(filename_1, 117, 117, 'Something bad'), Comment(filename_1, 119, 119, 'Something bad'), ) problems.add_many(errors) problems.set_changes([1]) sha = 'abc123' tst_config = build_review_config(fixer_ini, {'SUMMARY_THRESHOLD': 1}) review = Review(self.repo, self.pr, tst_config) with patch('lintreview.review.Review.publish_summary') as pub_sum_mock: review.publish_review(problems, sha) self.assertTrue(pub_sum_mock.called)
def test_publish_review_no_count_change(self, pub_status_mock, _): fixture = load_fixture('comments_current.json') self.pr.review_comments.return_value = [ GhIssueComment(f, self.session) for f in json.loads(fixture)] problems = Problems() # Match the line/positions in comments_current.json filename_1 = 'Console/Command/Task/AssetBuildTask.php' errors = ( Comment(filename_1, 40, 40, '2. Something bad'), Comment(filename_1, 87, 87, '1. Something bad'), Comment(filename_1, 89, 89, '2. Something bad'), ) problems.add_many(errors) problems.set_changes([1]) sha = 'abc123' tst_config = build_review_config(fixer_ini, {'SUMMARY_THRESHOLD': 1}) review = Review(self.repo, self.pr, tst_config) review.publish_review(problems, sha) # Ensure publish_status(True) means the status=failed pub_status_mock.assert_called_with(True)
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)
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)