コード例 #1
0
ファイル: github_reporter.py プロジェクト: twidi/linty_fresh
    def report(self, problems: List[Problem]) -> None:
        grouped_problems = Problem.group_by_path_and_line(problems)

        headers = {
            'Authorization': 'token {}'.format(self.auth_token),
        }
        with aiohttp.ClientSession(headers=headers) as client_session:
            (line_map, existing_messages) = yield from asyncio.gather(
                self.create_line_to_position_map(client_session),
                self.get_existing_messages(client_session))
            lint_errors = 0
            review_comment_awaitable = []
            pr_url = self._get_pr_url()
            for location, problems_for_line in grouped_problems:
                message_for_line = [':sparkles:Linty Fresh Says:sparkles::',
                                    '',
                                    '```']

                reported_problems_for_line = set()
                for problem in problems_for_line:
                    if problem.message not in reported_problems_for_line:
                        message_for_line.append(problem.message)
                        reported_problems_for_line.add(problem.message)
                message_for_line.append('```')

                path = location[0]
                line_number = location[1]
                position = line_map.get(path, {}).get(line_number, None)
                if position is not None:
                    message = '\n'.join(message_for_line)
                    if (path, position, message) not in existing_messages:
                        lint_errors += 1
                        if lint_errors <= MAX_LINT_ERROR_REPORTS:
                            data = json.dumps({
                                'body': message,
                                'commit_id': self.commit,
                                'path': path,
                                'position': position,
                            }, sort_keys=True)
                            review_comment_awaitable.append(
                                client_session.post(pr_url, data=data))
            if lint_errors > MAX_LINT_ERROR_REPORTS:
                message = ''':sparkles:Linty Fresh Says:sparkles::

Too many lint errors to report inline!  {0} lines have a problem.
Only reporting the first {1}.'''.format(
                    lint_errors, MAX_LINT_ERROR_REPORTS)
                data = json.dumps({
                    'body': message
                })
                review_comment_awaitable.append(
                    asyncio.async(client_session.post(pr_url, data=data)))

            responses = yield from asyncio.gather(
                *review_comment_awaitable
            )  # type: List[aiohttp.ClientResponse]

            for response in responses:
                response.close()
コード例 #2
0
    def report(self, problems: List[Problem]) -> None:
        grouped_problems = Problem.group_by_path_and_line(problems)

        headers = {
            'Authorization': 'token {}'.format(self.auth_token),
        }
        with aiohttp.ClientSession(headers=headers) as client_session:
            (line_map, existing_messages) = yield from asyncio.gather(
                self.create_line_to_position_map(client_session),
                self.get_existing_messages(client_session))
            lint_errors = 0
            review_comment_awaitable = []
            pr_url = self._get_pr_url()
            for location, problems_for_line in grouped_problems:
                message_for_line = [
                    ':sparkles:Linty Fresh Says:sparkles::', '', '```'
                ]

                reported_problems_for_line = set()
                for problem in problems_for_line:
                    if problem.message not in reported_problems_for_line:
                        message_for_line.append(problem.message)
                        reported_problems_for_line.add(problem.message)
                message_for_line.append('```')

                path = location[0]
                line_number = location[1]
                position = line_map.get(path, {}).get(line_number, None)
                if position is not None:
                    message = '\n'.join(message_for_line)
                    if (path, position, message) not in existing_messages:
                        lint_errors += 1
                        if lint_errors <= MAX_LINT_ERROR_REPORTS:
                            data = json.dumps(
                                {
                                    'body': message,
                                    'commit_id': self.commit,
                                    'path': path,
                                    'position': position,
                                },
                                sort_keys=True)
                            review_comment_awaitable.append(
                                client_session.post(pr_url, data=data))
            if lint_errors > MAX_LINT_ERROR_REPORTS:
                message = ''':sparkles:Linty Fresh Says:sparkles::

Too many lint errors to report inline!  {0} lines have a problem.
Only reporting the first {1}.'''.format(lint_errors, MAX_LINT_ERROR_REPORTS)
                data = json.dumps({'body': message})
                review_comment_awaitable.append(asyncio. async (
                    client_session.post(pr_url, data=data)))

            responses = yield from asyncio.gather(
                *review_comment_awaitable
            )  # type: List[aiohttp.ClientResponse]

            for response in responses:
                response.close()
コード例 #3
0
    async def report(self, linter_name: str,
                     problems: List[GenericProblem]) -> None:
        if not problems:
            grouped_problems = {}
        elif isinstance(list(problems)[0], TestProblem):
            grouped_problems = TestProblem.group_by_group(problems)
        else:
            grouped_problems = Problem.group_by_path_and_line(problems)

        headers = {
            'Authorization': 'token {}'.format(self.auth_token),
        }
        with aiohttp.ClientSession(headers=headers) as client_session:
            (line_map, existing_messages, message_ids) = await asyncio.gather(
                self.create_line_to_position_map(client_session),
                self.get_existing_pr_messages(client_session, linter_name),
                self.get_existing_issue_message_ids(client_session,
                                                    linter_name))
            lint_errors = 0
            review_comment_awaitable = []
            pr_url = self._get_pr_url()
            no_matching_line_number = []
            for location, problems_for_line in grouped_problems:
                message_for_line = ['{0} says:'.format(linter_name), '']

                reported_problems_for_line = set()

                path = location[0]
                line_number = location[1]
                position = line_map.get(path, {}).get(line_number, None)
                if position is None and path in line_map:
                    file_map = line_map[path]
                    closest_line = min(file_map.keys(),
                                       key=lambda x: abs(x - line_number))
                    position = file_map[closest_line]
                    message_for_line.append(
                        '(From line {})'.format(line_number))
                message_for_line.append('```')
                if position is not None:
                    for problem in problems_for_line:
                        if problem.message not in reported_problems_for_line:
                            message_for_line.append(problem.message)
                            reported_problems_for_line.add(problem.message)
                    message_for_line.append('```')
                    message = '\n'.join(message_for_line)
                    try:
                        existing_messages.remove(
                            ExistingGithubMessage(None, path, position,
                                                  message))
                    except KeyError:
                        lint_errors += 1
                        if lint_errors <= MAX_LINT_ERROR_REPORTS:
                            data = json.dumps(
                                {
                                    'body': message,
                                    'commit_id': self.commit,
                                    'path': path,
                                    'position': position,
                                },
                                sort_keys=True)
                            review_comment_awaitable.append(
                                client_session.post(pr_url, data=data))
                else:
                    no_matching_line_number.append(
                        (location, problems_for_line))

            if lint_errors > MAX_LINT_ERROR_REPORTS:
                message = """{0} says:

Too many lint errors to report inline!  {1} lines have a problem.
Only reporting the first {2}.""".format(linter_name, lint_errors,
                                        MAX_LINT_ERROR_REPORTS)
                data = json.dumps({'body': message})
                review_comment_awaitable.append(
                    asyncio.ensure_future(
                        client_session.post(self._get_issue_url(), data=data)))

            if self.delete_previous_comments:
                for message_id in message_ids:
                    review_comment_awaitable.append(
                        asyncio.ensure_future(
                            client_session.delete(
                                self._get_delete_issue_comment_url(
                                    message_id))))
                for message in existing_messages:
                    review_comment_awaitable.append(
                        asyncio.ensure_future(
                            client_session.delete(
                                self._get_delete_pr_comment_url(
                                    message.comment_id))))

            if no_matching_line_number:
                no_matching_line_messages = []
                for location, problems_for_line in no_matching_line_number:
                    lint_errors += 1
                    path = location[0]
                    line_number = location[1]
                    no_matching_line_messages.append('{0}:{1}:'.format(
                        path, line_number))
                    for problem in problems_for_line:
                        no_matching_line_messages.append('\t{0}'.format(
                            problem.message))
                message = ('{0} says: I found some problems with lines not '
                           'modified by this commit:\n```\n{1}\n```'.format(
                               linter_name,
                               '\n'.join(no_matching_line_messages)))
                data = json.dumps({'body': message})
                review_comment_awaitable.append(
                    asyncio.ensure_future(
                        client_session.post(self._get_issue_url(), data=data)))

            responses = await asyncio.gather(
                *review_comment_awaitable
            )  # type: List[aiohttp.ClientResponse]
            for response in responses:
                response.close()

            if lint_errors > 0:
                raise HadLintErrorsException()
コード例 #4
0
    async def report(self, problems: List[Problem]) -> None:
        grouped_problems = Problem.group_by_path_and_line(problems)

        headers = {
            'Authorization': 'token {}'.format(self.auth_token),
        }
        with aiohttp.ClientSession(headers=headers) as client_session:
            (line_map, existing_messages) = await asyncio.gather(
                self.create_line_to_position_map(client_session),
                self.get_existing_messages(client_session))
            lint_errors = 0
            review_comment_awaitable = []
            pr_url = self._get_pr_url()
            no_matching_line_number = []
            for location, problems_for_line in grouped_problems:
                message_for_line = [
                    ':sparkles:Linty Fresh Says:sparkles::', ''
                ]

                reported_problems_for_line = set()

                path = location[0]
                line_number = location[1]
                position = line_map.get(path, {}).get(line_number, None)
                if position is None and path in line_map:
                    file_map = line_map[path]
                    closest_line = min(file_map.keys(),
                                       key=lambda x: abs(x - line_number))
                    position = file_map[closest_line]
                    message_for_line.append(
                        '(From line {})'.format(line_number))
                message_for_line.append('```')
                if position is not None:
                    for problem in problems_for_line:
                        if problem.message not in reported_problems_for_line:
                            message_for_line.append(problem.message)
                            reported_problems_for_line.add(problem.message)
                    message_for_line.append('```')
                    message = '\n'.join(message_for_line)
                    if (path, position, message) not in existing_messages:
                        lint_errors += 1
                        if lint_errors <= MAX_LINT_ERROR_REPORTS:
                            data = json.dumps(
                                {
                                    'body': message,
                                    'commit_id': self.commit,
                                    'path': path,
                                    'position': position,
                                },
                                sort_keys=True)
                            review_comment_awaitable.append(
                                client_session.post(pr_url, data=data))
                else:
                    no_matching_line_number.append(
                        (location, problems_for_line))
            if lint_errors > MAX_LINT_ERROR_REPORTS:
                message = ''':sparkles:Linty Fresh Says:sparkles::

Too many lint errors to report inline!  {0} lines have a problem.
Only reporting the first {1}.'''.format(lint_errors, MAX_LINT_ERROR_REPORTS)
                data = json.dumps({'body': message})
                review_comment_awaitable.append(
                    asyncio.ensure_future(
                        client_session.post(self._get_issue_url(), data=data)))
            if no_matching_line_number:
                no_matching_line_messages = []
                for location, problems_for_line in no_matching_line_number:
                    path = location[0]
                    line_number = location[1]
                    no_matching_line_messages.append('{0}:{1}:'.format(
                        path, line_number))
                    for problem in problems_for_line:
                        no_matching_line_messages.append('\t{0}'.format(
                            problem.message))
                message = ('Linters found some problems with lines not '
                           'modified by this commit:\n```\n{0}\n```'.format(
                               '\n'.join(no_matching_line_messages)))
                data = json.dumps({'body': message})
                review_comment_awaitable.append(
                    asyncio.ensure_future(
                        client_session.post(self._get_issue_url(), data=data)))

            responses = await asyncio.gather(
                *review_comment_awaitable
            )  # type: List[aiohttp.ClientResponse]
            for response in responses:
                response.close()