Exemplo n.º 1
0
    def NotifyReviewEvent(self, request, context):
        print("got review request {}".format(request))

        # client connection to DataServe
        channel = grpc.insecure_channel(data_srv_addr, options=[
                ("grpc.max_send_message_length", grpc_max_msg_size),
                ("grpc.max_receive_message_length", grpc_max_msg_size),
            ])
        stub = service_data_pb2_grpc.DataStub(channel)
        changes = stub.GetChanges(
            service_data_pb2.ChangesRequest(
                head=request.commit_revision.head,
                base=request.commit_revision.base,
                want_contents=False,
                want_uast=True,
                exclude_vendored=True))

        comments = []
        for change in changes:
            print("analyzing '{}' in {}".format(change.head.path, change.head.language))
            fns = list(filter_uast(change.head.uast, "//*[@roleFunction]"))
            comments.append(
                service_analyzer_pb2.Comment(
                    file=change.head.path,
                    line=0,
                    text="language: {}, functions: {}".format(change.head.language, len(fns))))
        return service_analyzer_pb2.EventResponse(analyzer_version=version, comments=comments)
Exemplo n.º 2
0
    def NotifyReviewEvent(self, request, context):
        logger.info("got review request {}".format(request))

        # client connection to DataServe
        channel = grpc.insecure_channel(self.data_srv_addr,
                                        options=[
                                            ("grpc.max_send_message_length",
                                             grpc_max_msg_size),
                                            ("grpc.max_receive_message_length",
                                             grpc_max_msg_size),
                                        ])
        stub = service_data_pb2_grpc.DataStub(channel)
        changes = stub.GetChanges(
            service_data_pb2.ChangesRequest(
                head=request.commit_revision.head,
                base=request.commit_revision.base,
                include_pattern='.*\\.go$',  #FIXME: allow more languages
                want_contents=True,
                want_uast=True,
                want_language=True,
                exclude_vendored=True))

        comments = []
        for change in changes:
            logger.debug("analyzing '{}' in {}".format(change.head.path,
                                                       change.head.language))
            if change.head.uast is None:
                continue

            #TODO(smola): better handling of change.added_lines
            seqm = difflib.SequenceMatcher(
                None,
                change.base.content.splitlines(),
                change.head.content.splitlines(),
            )
            opcodes = seqm.get_opcodes()
            lines = set()
            for opcode in opcodes:
                if opcode[0] in ('insert', 'replace'):
                    lines |= set(range(opcode[3] + 1, opcode[4] + 1))

            for line, uast, snippet in self.tree_extractor.get_snippets(
                    file=change.head, lines=lines):
                #TODO(smola): speed up matching
                matched = self.stats.match(uast)
                if matched:
                    comment = service_analyzer_pb2.Comment(
                        file=change.head.path,
                        line=line,
                        text="Something looks wrong here:\n```\n%s\n```" %
                        matched[0])
                    comments.append(comment)
        logging.info("{} comments produced".format(len(comments)))
        return service_analyzer_pb2.EventResponse(analyzer_version=version,
                                                  comments=comments)
Exemplo n.º 3
0
    def NotifyReviewEvent(self, request, context):
        print("got review request {}".format(request))

        # client connection to DataServe
        channel = grpc.insecure_channel(data_srv_addr, options=[
                ("grpc.max_send_message_length", grpc_max_msg_size),
                ("grpc.max_receive_message_length", grpc_max_msg_size),
            ])
        stub = service_data_pb2_grpc.DataStub(channel)
        changes = stub.GetChanges(
            service_data_pb2.ChangesRequest(
                head=request.commit_revision.head,
                base=request.commit_revision.base,
                want_contents=False,
                want_uast=True,
                exclude_vendored=True))

        comments = []
        for change in changes:
            print("analyzing '{}' in {}".format(change.head.path, change.head.language))
            check_results = run_checks(
                list_checks(change.head.language.lower()),
                change.head.language.lower(),
                change.head.uast
            )
            n = 0
            for check in check_results:
                for res in check_results[check]:
                    comments.append(
                        service_analyzer_pb2.Comment(
                            file=change.head.path,
                            line=res["pos"]["line"] if res and "pos" in res and res["pos"] and "line" in res["pos"] else 0,
                            text="{}: {}".format(check, res["msg"])))
                    n += 1
        print("{} comments produced".format(n))
        return service_analyzer_pb2.EventResponse(analyzer_version=version, comments=comments)
    def NotifyReviewEvent(self, request, context):
        print("got review request {}".format(request))

        # client connection to DataServe
        channel = grpc.insecure_channel(data_srv_addr,
                                        options=[
                                            ("grpc.max_send_message_length",
                                             grpc_max_msg_size),
                                            ("grpc.max_receive_message_length",
                                             grpc_max_msg_size),
                                        ])
        stub = service_data_pb2_grpc.DataStub(channel)
        changes = stub.GetChanges(
            service_data_pb2.ChangesRequest(head=request.commit_revision.head,
                                            base=request.commit_revision.base,
                                            want_language=True,
                                            want_contents=True,
                                            want_uast=False,
                                            exclude_vendored=True))
        comments = []

        temp_dir = tempfile.mkdtemp()
        app = application.Application()
        n = 0

        try:
            app.initialize([])

            # FIXME: probably will need the full repo to give good results
            for change in changes:
                if change.head.language.lower() != "python":
                    continue

                file_path = os.path.join(temp_dir, change.head.path)

                try:
                    with open(file_path, "wb") as test_file:
                        test_file.write(change.head.content)
                        test_file.flush()
                        app.file_checker_manager.start([file_path])

                        for checker in app.file_checker_manager.checkers:
                            _, results, _ = checker.run_checks()

                            for r in results:
                                c = service_analyzer_pb2.Comment(
                                    file=change.head.path,
                                    line=r[1],
                                    text="{}: {}".format(r[0], r[3]))
                                if c:
                                    comments.append(c)
                                    n += 1
                finally:
                    if os.path.exists(file_path):
                        os.unlink(file_path)
        finally:
            shutil.rmtree(temp_dir)

        print("{} comments produced".format(n))
        return service_analyzer_pb2.EventResponse(analyzer_version=version,
                                                  comments=comments)