private void writeFileRefs(DbSession session, Map<String, Reference> refByComponentKey, ShowResponse.Builder response, @Nullable String branch, @Nullable String pullRequest) { Map<String, ComponentDto> projectsByUuid = new HashMap<>(); Map<String, ComponentDto> parentModulesByUuid = new HashMap<>(); for (Map.Entry<String, Reference> entry : refByComponentKey.entrySet()) { Reference ref = entry.getValue(); ComponentDto file = ref.getDto(); if (file != null) { ComponentDto project = getProject(file.projectUuid(), projectsByUuid, session); ComponentDto parentModule = getParentProject(file.moduleUuid(), parentModulesByUuid, session); response.putFiles(ref.getId(), toWsFile(file, project, parentModule, branch, pullRequest)); } else { response.putFiles(ref.getId(), toWsFile(ref.getComponentKey(), branch, pullRequest)); } } }
private static Duplications.Duplication.Builder toWsDuplication(DuplicationsParser.Block block, Map<String, Reference> refByComponentKey) { Duplications.Duplication.Builder wsDuplication = Duplications.Duplication.newBuilder(); block.getDuplications().stream() .map(duplication -> toWsBlock(duplication, refByComponentKey)) .forEach(wsDuplication::addBlocks); return wsDuplication; }
ShowResponse build(DbSession session, List<DuplicationsParser.Block> blocks, @Nullable String branch, @Nullable String pullRequest) { Map<String, Reference> refByComponentKey = new LinkedHashMap<>(); ShowResponse.Builder response = ShowResponse.newBuilder(); blocks.stream() .map(block -> toWsDuplication(block, refByComponentKey)) .forEach(response::addDuplications); writeFileRefs(session, refByComponentKey, response, branch, pullRequest); return response.build(); }
private void test(List<DuplicationsParser.Block> blocks, @Nullable String branch, @Nullable String pullRequest, String expected) { StringWriter output = new StringWriter(); JsonWriter jsonWriter = JsonWriter.of(output); ProtobufJsonFormat.write(underTest.build(db.getSession(), blocks, branch, pullRequest), jsonWriter); JsonAssert.assertJson(output.toString()).isSimilarTo(expected); }
ShowResponse build(DbSession session, List<DuplicationsParser.Block> blocks, @Nullable String branch, @Nullable String pullRequest) { ShowResponse.Builder response = ShowResponse.newBuilder(); Map<String, String> refByComponentKey = newHashMap(); blocks.stream() .map(block -> toWsDuplication(block, refByComponentKey)) .forEach(response::addDuplications); writeFiles(session, response, refByComponentKey, branch, pullRequest); return response.build(); }
@Override public void handle(Request request, Response response) { try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto component = loadComponent(dbSession, request); userSession.checkComponentPermission(UserRole.CODEVIEWER, component); String duplications = findDataFromComponent(dbSession, component); String branch = component.getBranch(); String pullRequest = component.getPullRequest(); List<DuplicationsParser.Block> blocks = parser.parse(dbSession, component, branch, pullRequest, duplications); writeProtobuf(responseBuilder.build(dbSession, blocks, branch, pullRequest), request, response); } }
private void writeFiles(DbSession session, ShowResponse.Builder response, Map<String, String> refByComponentKey, @Nullable String branch, @Nullable String pullRequest) { Map<String, ComponentDto> projectsByUuid = newHashMap(); Map<String, ComponentDto> parentModulesByUuid = newHashMap(); Map<String, Duplications.File> filesByRef = response.getMutableFiles(); for (Map.Entry<String, String> entry : refByComponentKey.entrySet()) { String componentKey = entry.getKey(); String ref = entry.getValue(); Optional<ComponentDto> fileOptional = componentDao.selectByKey(session, componentKey); if (fileOptional.isPresent()) { ComponentDto file = fileOptional.get(); ComponentDto project = getProject(file.projectUuid(), projectsByUuid, session); ComponentDto parentModule = getParentProject(file.getRootUuid(), parentModulesByUuid, session); filesByRef.put(ref, toWsFile(file, project, parentModule, branch, pullRequest)); } } }
@Override public void handle(Request request, Response response) { try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto component = loadComponent(dbSession, request); userSession.checkComponentPermission(UserRole.CODEVIEWER, component); String duplications = findDataFromComponent(dbSession, component); String branch = component.getBranch(); String pullRequest = component.getPullRequest(); List<DuplicationsParser.Block> blocks = parser.parse(dbSession, component, branch, pullRequest, duplications); writeProtobuf(responseBuilder.build(dbSession, blocks, branch, pullRequest), request, response); } }
private static Duplications.Duplication.Builder toWsDuplication(DuplicationsParser.Block block, Map<String, String> refByComponentKey) { Duplications.Duplication.Builder wsDuplication = Duplications.Duplication.newBuilder(); block.getDuplications().stream() .map(d -> toWsBlock(refByComponentKey, d)) .forEach(wsDuplication::addBlocks); return wsDuplication; }