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();
}
Aggregations