Search in sources :

Example 1 with DiffFilesResponse

use of com.google.startupos.tools.reviewer.local_server.service.Protos.DiffFilesResponse in project startup-os by google.

the class CodeReviewService method getDiffFiles.

@Override
public void getDiffFiles(DiffFilesRequest request, StreamObserver<DiffFilesResponse> responseObserver) {
    if (request.getWorkspace().isEmpty() || request.getDiffId() <= 0) {
        responseObserver.onError(Status.INVALID_ARGUMENT.withDescription("Workspace must be set and diff_id must be > 0.\nrequest:\n" + request).asRuntimeException());
        return;
    }
    logger.atInfo().log("DiffFiles request\n%s", request);
    String workspacePath;
    String workspace;
    String branch;
    boolean isHead;
    if (workspaceExists(request.getWorkspace())) {
        workspacePath = getWorkspacePath(request.getWorkspace());
        workspace = request.getWorkspace();
        branch = "D" + request.getDiffId();
        isHead = false;
    } else {
        logger.atInfo().log("Workspace %s does not exist. Fallbacking to head.", request.getWorkspace());
        workspacePath = fileUtils.joinToAbsolutePath(basePath, "head");
        workspace = "";
        branch = "remotes/origin/D" + request.getDiffId();
        isHead = true;
    }
    DiffFilesResponse.Builder response = DiffFilesResponse.newBuilder();
    try {
        fileUtils.listContents(workspacePath).stream().map(path -> fileUtils.joinToAbsolutePath(workspacePath, path)).filter(fileUtils::folderExists).forEach(path -> {
            String repoName = Paths.get(path).getFileName().toString();
            Repo repo = repoFactory.create(path);
            if (!isHead) {
                ImmutableList<Commit> commits = addWorkspaceAndRepoToCommits(repo.getCommits(branch), workspace, repoName);
                ImmutableList<File> uncommittedFiles = addWorkspaceAndRepoToFiles(repo.getUncommittedFiles(), workspace, repoName);
                response.addBranchInfo(BranchInfo.newBuilder().setDiffId(request.getDiffId()).setRepoId(repoName).addAllCommit(commits).addAllUncommittedFile(uncommittedFiles).build());
            } else {
                ImmutableList<Commit> commits = ImmutableList.of();
                if (repo.branchExists(branch)) {
                    commits = addWorkspaceAndRepoToCommits(repo.getCommits(branch), workspace, repoName);
                } else {
                    logger.atInfo().log("Branch %s does not exist in head", branch);
                }
                response.addBranchInfo(BranchInfo.newBuilder().setDiffId(request.getDiffId()).setRepoId(repoName).addAllCommit(commits).build());
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
    responseObserver.onNext(response.build());
    responseObserver.onCompleted();
}
Also used : DiffFilesResponse(com.google.startupos.tools.reviewer.local_server.service.Protos.DiffFilesResponse) Commit(com.google.startupos.common.repo.Protos.Commit) Repo(com.google.startupos.common.repo.Repo) IOException(java.io.IOException) File(com.google.startupos.common.repo.Protos.File)

Aggregations

Commit (com.google.startupos.common.repo.Protos.Commit)1 File (com.google.startupos.common.repo.Protos.File)1 Repo (com.google.startupos.common.repo.Repo)1 DiffFilesResponse (com.google.startupos.tools.reviewer.local_server.service.Protos.DiffFilesResponse)1 IOException (java.io.IOException)1