Search in sources :

Example 11 with GitRepo

use of com.google.startupos.common.repo.GitRepo in project startup-os by google.

the class SubmitCommand method run.

@Override
public boolean run(String[] args) {
    if (diffNumber == -1) {
        System.out.println(RED_ERROR + "Workspace has no diff to submit (git branch has no D# branch)");
        return false;
    }
    final Diff.Builder diffBuilder = codeReviewBlockingStub.getDiff(DiffRequest.newBuilder().setDiffId(diffNumber).build()).toBuilder();
    boolean hasApprovedReviews = diffBuilder.getReviewerList().stream().anyMatch(Reviewer::getApproved);
    if (!hasApprovedReviews) {
        System.out.println(RED_ERROR + String.format("D%d is not approved yet", diffNumber));
        return false;
    }
    System.out.println("Updating diff status: SUBMITTING");
    codeReviewBlockingStub.createDiff(CreateDiffRequest.newBuilder().setDiff(diffBuilder.setStatus(Status.SUBMITTING)).build());
    final String diffBranchName = String.format("D%s", diffBuilder.getId());
    try {
        fileUtils.listContents(workspacePath).stream().map(path -> fileUtils.joinToAbsolutePath(workspacePath, path)).filter(fileUtils::folderExists).forEach(path -> {
            String repoName = Paths.get(path).getFileName().toString();
            GitRepo repo = this.gitRepoFactory.create(path);
            boolean hasDiffBranch = repo.listBranches().stream().anyMatch(branchName -> branchName.equals(diffBranchName));
            if (!hasDiffBranch) {
                System.out.println(String.format("Repo %s has no branch named %s, skipping", repoName, diffBranchName));
                return;
            }
            System.out.println(String.format("[%s]: committing changes", repoName));
            repo.commit(repo.getUncommittedFiles(), String.format("%s: %s", diffBranchName, diffBuilder.getDescription()));
            System.out.println(String.format("[%s]: removing branch", repoName));
            repo.removeBranch(diffBranchName);
            System.out.println(String.format("[%s]: pushing to remote", repoName));
            repo.push(diffBranchName);
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Updating diff status: SUBMITTED");
    codeReviewBlockingStub.createDiff(CreateDiffRequest.newBuilder().setDiff(diffBuilder.setStatus(Status.SUBMITTED)).build());
    return true;
}
Also used : GitRepo(com.google.startupos.common.repo.GitRepo) Diff(com.google.startupos.tools.reviewer.local_server.service.Protos.Diff) IOException(java.io.IOException) Reviewer(com.google.startupos.tools.reviewer.local_server.service.Protos.Reviewer)

Example 12 with GitRepo

use of com.google.startupos.common.repo.GitRepo in project startup-os by google.

the class DiffConverter method getReviewCommentsByRepoName.

private ImmutableList<ReviewComment> getReviewCommentsByRepoName(List<Thread> codeThreads, String repoName, ImmutableMap<String, GitRepo> repoNameToGitRepos, long modifiedTimestamp, long diffId) {
    ImmutableList.Builder<ReviewComment> result = ImmutableList.builder();
    ImmutableList<Thread> codeThreadsByRepo = ImmutableList.copyOf(codeThreads.stream().filter(thread -> thread.getRepoId().equals(repoName)).collect(Collectors.toList()));
    for (Thread thread : codeThreadsByRepo) {
        for (Comment comment : thread.getCommentList()) {
            GitRepo gitRepo = getGitRepo(thread.getFile().getRepoId(), repoNameToGitRepos);
            int githubCommentPosition;
            String commitId;
            boolean isOutsideDiffComment = false;
            // `0` value means the comment isn't synced with GitHub
            if (thread.getGithubCommentPosition() == 0 && thread.getClosestGithubCommentPosition() == 0) {
                String filename = thread.getFile().getFilename();
                String baseBranchCommitId = gitRepo.getMostRecentCommitOfBranch("master");
                String patch;
                if (baseBranchCommitId.equals(thread.getFile().getCommitId())) {
                    // for the left file
                    commitId = gitRepo.getMostRecentCommitOfFile(filename);
                    patch = gitRepo.getPatch(baseBranchCommitId, commitId, filename);
                } else {
                    // for the right file
                    commitId = thread.getFile().getCommitId();
                    patch = gitRepo.getPatch(baseBranchCommitId, commitId, filename);
                }
                LineNumberConverter.LineNumberToGithubPositionCorrelation correlation = getGithubReviewCommentPosition(baseBranchCommitId, thread, patch);
                if (correlation.getExactGithubPosition() != 0) {
                    githubCommentPosition = correlation.getExactGithubPosition();
                } else {
                    githubCommentPosition = correlation.getClosestGithubPosition();
                    isOutsideDiffComment = true;
                }
                reviewerClient.addGithubReviewCommentPosition(diffId, thread.getId(), githubCommentPosition, comment.getId());
            } else {
                if (thread.getGithubCommentPosition() != 0) {
                    githubCommentPosition = thread.getGithubCommentPosition();
                } else {
                    githubCommentPosition = thread.getClosestGithubCommentPosition();
                    isOutsideDiffComment = true;
                }
                commitId = thread.getFile().getCommitId();
            }
            ReviewComment.Builder githubComment = ReviewComment.newBuilder();
            githubComment.setPath(thread.getFile().getFilename()).setId(comment.getGithubCommentId()).setPosition(githubCommentPosition).setCommitId(commitId).setUser(User.newBuilder().setEmail(comment.getCreatedBy()).build()).setBody(comment.getContent()).setCreatedAt(String.valueOf(Instant.ofEpochMilli(comment.getTimestamp()))).setUpdatedAt(String.valueOf(Instant.ofEpochMilli(modifiedTimestamp))).setReviewerThreadId(thread.getId()).setReviewerCommentId(comment.getId()).setIsOutsideDiffComment(isOutsideDiffComment).setReviewerLineNumber(thread.getLineNumber()).build();
            result.add(githubComment.build());
        }
    }
    return result.build();
}
Also used : Comment(com.google.startupos.tools.reviewer.local_server.service.Protos.Comment) ReviewComment(com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.ReviewComment) IssueComment(com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.IssueComment) ReviewComment(com.google.startupos.tools.reviewer.job.sync.GithubPullRequestProtos.ReviewComment) ImmutableList(com.google.common.collect.ImmutableList) Thread(com.google.startupos.tools.reviewer.local_server.service.Protos.Thread) GitRepo(com.google.startupos.common.repo.GitRepo)

Example 13 with GitRepo

use of com.google.startupos.common.repo.GitRepo in project startup-os by google.

the class HttpArchiveDepsGenerator method getHttpArchiveDeps.

public HttpArchiveDepsList getHttpArchiveDeps(WorkspaceFile workspaceFile, List<String> httpArchiveNames) throws IOException {
    HttpArchiveDepsList.Builder result = HttpArchiveDepsList.newBuilder();
    for (String httpArchiveName : httpArchiveNames) {
        WorkspaceFile.HttpArchive httpArchive = WorkspaceFile.HttpArchive.getDefaultInstance();
        for (WorkspaceFile.HttpArchive currentHttpArchive : workspaceFile.getHttpArchiveList()) {
            if (currentHttpArchive.getName().equals(httpArchiveName)) {
                httpArchive = currentHttpArchive;
            }
        }
        if (areCommitIdsTheSame(httpArchiveName, getCommitId(httpArchive.getStripPrefix()))) {
            log.atInfo().log("Commit id in WORKSPACE file and commit id in \'%s\' file for \'%s\' http_archive " + "are the same. Nothing to update.", HTTP_ARCHIVE_DEPS_FILENAME, httpArchiveName);
            continue;
        }
        if (httpArchive.getName().equals(httpArchiveName)) {
            HttpArchiveDeps.Builder builder = HttpArchiveDeps.newBuilder();
            String url = httpArchive.getUrls(0).split("/archive")[0] + ".git";
            String repoName = url.substring(url.lastIndexOf('/') + 1).replace(".git", "");
            GitRepo gitRepo = createRepo(url, repoName);
            switchToCommit(gitRepo, httpArchive.getStripPrefix());
            String absRepoPath = fileUtils.joinPaths(fileUtils.getCurrentWorkingDirectory(), BUILD_GENERATOR_TEMP_FOLDER, repoName);
            ImmutableList<String> buildFilesAbsPaths = getBuildFilesAbsPaths(absRepoPath);
            for (String path : buildFilesAbsPaths) {
                BuildFile buildFile = buildFileParser.getBuildFile(path);
                for (BuildFile.JavaLibrary javaLibrary : buildFile.getJavaLibraryList()) {
                    addDeps(absRepoPath, builder, path, javaLibrary.getSrcsList(), javaLibrary.getName());
                }
                for (BuildFile.JavaBinary javaBinary : buildFile.getJavaBinaryList()) {
                    addDeps(absRepoPath, builder, path, javaBinary.getSrcsList(), javaBinary.getName());
                }
                for (BuildFile.ProtoLibrary protoLibrary : buildFile.getProtoLibraryList()) {
                    String absProtoFilePath = path.replace("BUILD", protoLibrary.getSrcs(0));
                    for (BuildFile.JavaProtoLibrary javaProtoLibrary : buildFile.getJavaProtoLibraryList()) {
                        if (javaProtoLibrary.getDepsList().contains(":" + protoLibrary.getName())) {
                            ProtoFile protoFile = protoFileAnalyzer.getProtoFile(absProtoFilePath);
                            if ((!protoFile.getJavaPackage().isEmpty()) && (!protoFile.getJavaOuterClassname().isEmpty())) {
                                String fullJavaClassName = protoFile.getJavaPackage() + "." + protoFile.getJavaOuterClassname();
                                String target = path.replace(absRepoPath, "/").replace("/BUILD", ":") + javaProtoLibrary.getName();
                                builder.addHttpArchiveDep(HttpArchiveDep.newBuilder().setTarget(target).setJavaClass(fullJavaClassName).build());
                            }
                        }
                    }
                }
            }
            builder.setName(getCommitId(httpArchive.getName())).setCommitId(getCommitId(httpArchive.getStripPrefix()));
            result.addHttpArchiveDeps(builder.build());
            fileUtils.clearDirectoryUnchecked(fileUtils.joinPaths(fileUtils.getCurrentWorkingDirectory(), BUILD_GENERATOR_TEMP_FOLDER));
        } else {
            log.atWarning().log("Can't find http_archive with name: %s", httpArchiveName);
        }
    }
    fileUtils.deleteFileOrDirectoryIfExists(fileUtils.joinPaths(fileUtils.getCurrentWorkingDirectory(), BUILD_GENERATOR_TEMP_FOLDER));
    return result.build();
}
Also used : BuildFile(com.google.startupos.tools.build_file_generator.Protos.BuildFile) HttpArchiveDeps(com.google.startupos.tools.build_file_generator.Protos.HttpArchiveDeps) WorkspaceFile(com.google.startupos.tools.build_file_generator.Protos.WorkspaceFile) ProtoFile(com.google.startupos.tools.build_file_generator.Protos.ProtoFile) GitRepo(com.google.startupos.common.repo.GitRepo) HttpArchiveDepsList(com.google.startupos.tools.build_file_generator.Protos.HttpArchiveDepsList)

Example 14 with GitRepo

use of com.google.startupos.common.repo.GitRepo in project startup-os by google.

the class HttpArchiveDepsGenerator method createRepo.

private GitRepo createRepo(String url, String repoName) {
    String absRepoPath = fileUtils.joinPaths(fileUtils.getCurrentWorkingDirectory(), BUILD_GENERATOR_TEMP_FOLDER, repoName);
    GitRepo gitRepo = gitRepoFactory.create(absRepoPath);
    gitRepo.cloneRepo(url, absRepoPath);
    return gitRepo;
}
Also used : GitRepo(com.google.startupos.common.repo.GitRepo)

Example 15 with GitRepo

use of com.google.startupos.common.repo.GitRepo in project startup-os by google.

the class SnapshotCommand method run.

@Override
public boolean run(String[] args) {
    if (diffNumber == -1) {
        System.out.println(RED_ERROR + "Cannot find diff number");
        return false;
    }
    String branchName = String.format("D%d", diffNumber);
    try {
        fileUtils.listContents(workspacePath).stream().map(path -> fileUtils.joinToAbsolutePath(workspacePath, path)).filter(fileUtils::folderExists).forEach(path -> {
            String repoName = Paths.get(path).getFileName().toString();
            GitRepo repo = this.gitRepoFactory.create(path);
            repoToInitialBranch.put(repoName, repo.currentBranch());
            repo.switchBranch(branchName);
            ImmutableList<File> files = repo.getUncommittedFiles();
            if (files.isEmpty()) {
                System.out.println(String.format("[%s]: No files to update", repoName));
                // Only skips this iteration
                return;
            }
            String message = branchName + ":\n" + files.stream().map(File::getFilename).collect(Collectors.joining("\n"));
            repo.commit(files, String.format(message, branchName));
            System.out.println(String.format("[%s]: Committed changes", repoName));
        });
    } catch (Exception e) {
        revertChanges(repoToInitialBranch);
        e.printStackTrace();
    }
    return true;
}
Also used : GitRepo(com.google.startupos.common.repo.GitRepo) File(com.google.startupos.common.repo.Protos.File)

Aggregations

GitRepo (com.google.startupos.common.repo.GitRepo)21 IOException (java.io.IOException)6 GitRepoFactory (com.google.startupos.common.repo.GitRepoFactory)4 FileUtils (com.google.startupos.common.FileUtils)3 Diff (com.google.startupos.tools.reviewer.local_server.service.Protos.Diff)3 ImmutableList (com.google.common.collect.ImmutableList)2 Flag (com.google.startupos.common.flags.Flag)2 FlagDesc (com.google.startupos.common.flags.FlagDesc)2 Flags (com.google.startupos.common.flags.Flags)2 CiRequest (com.google.startupos.tools.reviewer.ReviewerProtos.CiRequest)2 CiResponse (com.google.startupos.tools.reviewer.ReviewerProtos.CiResponse)2 Repo (com.google.startupos.tools.reviewer.ReviewerProtos.Repo)2 Reviewer (com.google.startupos.tools.reviewer.local_server.service.Protos.Reviewer)2 Paths (java.nio.file.Paths)2 Named (javax.inject.Named)2 CommandLine (com.google.startupos.common.CommandLine)1 CommonModule (com.google.startupos.common.CommonModule)1 File (com.google.startupos.common.repo.Protos.File)1 BuildFile (com.google.startupos.tools.build_file_generator.Protos.BuildFile)1 HttpArchiveDeps (com.google.startupos.tools.build_file_generator.Protos.HttpArchiveDeps)1