Search in sources :

Example 1 with Repo

use of com.google.startupos.tools.reviewer.ReviewerProtos.Repo in project startup-os by google.

the class CiTask method processRequest.

private void processRequest(CiRequest request) {
    log.atInfo().log("Processing request:\n%s", request);
    CiResponse.Builder responseBuilder = CiResponse.newBuilder().setRequest(request);
    if (!fileUtils.folderExists("ci")) {
        fileUtils.mkdirs("ci");
    }
    for (CiRequest.Target target : request.getTargetList()) {
        log.atInfo().log("Processing target:\n%s", target);
        Repo repo = target.getRepo();
        String repoPath = fileUtils.joinPaths(fileUtils.getCurrentWorkingDirectory(), "ci", repo.getId());
        GitRepo gitRepo = this.gitRepoFactory.create(repoPath);
        try {
            if (fileUtils.folderEmptyOrNotExists(repoPath)) {
                gitRepo.cloneRepo(repo.getUrl(), repoPath);
            } else {
                gitRepo.switchBranch("master");
                gitRepo.pull();
            }
        } catch (Exception e) {
            log.atSevere().withCause(e).log("Failed to process target");
            responseBuilder = responseBuilder.addResult(CiResponse.TargetResult.newBuilder().setTarget(target).setStatus(Status.FAIL).setLog(e.toString()).build());
            saveCiResponse(responseBuilder.build(), request.getDiffId());
            return;
        }
        gitRepo.switchBranch(target.getCommitId());
        log.atInfo().log("Running reviewer-ci.sh in %s", repoPath);
        CommandLine.CommandResult result = CommandLine.runCommandForError("/usr/bin/env bash " + fileUtils.joinPaths(repoPath, "reviewer-ci.sh"), repoPath);
        log.atInfo().log("Done running reviewer-ci.sh");
        String ciLog = result.stderr;
        if (ciLog.length() > MAX_LOG_LENGTH) {
            ciLog = ciLog.substring(0, MAX_LOG_LENGTH) + "[log truncated from length " + ciLog.length() + "]\n";
        }
        Status status = result.exitValue == 0 ? Status.SUCCESS : Status.FAIL;
        responseBuilder.addResult(CiResponse.TargetResult.newBuilder().setTarget(target).setStatus(status).setLog(ciLog).build());
        saveCiResponse(responseBuilder.build(), request.getDiffId());
        firestoreClient.deleteDocument(ReviewerConstants.CI_REQUESTS_PATH, Long.toString(request.getDiffId()));
        log.atInfo().log("Done processing CiRequest. Status=" + status);
    }
}
Also used : Status(com.google.startupos.tools.reviewer.ReviewerProtos.CiResponse.TargetResult.Status) CiRequest(com.google.startupos.tools.reviewer.ReviewerProtos.CiRequest) CommandLine(com.google.startupos.common.CommandLine) Repo(com.google.startupos.tools.reviewer.ReviewerProtos.Repo) GitRepo(com.google.startupos.common.repo.GitRepo) GitRepo(com.google.startupos.common.repo.GitRepo) CiResponse(com.google.startupos.tools.reviewer.ReviewerProtos.CiResponse)

Example 2 with Repo

use of com.google.startupos.tools.reviewer.ReviewerProtos.Repo in project startup-os by google.

the class SubmitterTask method trySubmitDiff.

private void trySubmitDiff(Diff diff) {
    log.atInfo().log("Attempting to submit diff %d", diff.getId());
    CiResponse response = diff.getCiResponse(diff.getCiResponseCount() - 1);
    boolean shouldPush = true;
    List<GitRepo> gitRepos = new ArrayList<>();
    CiRequest.Builder newRequestBuilder = CiRequest.newBuilder().setForSubmission(true);
    for (CiResponse.TargetResult targetResult : response.getResultList()) {
        CiRequest.Target target = targetResult.getTarget();
        Repo repo = target.getRepo();
        String repoPath = fileUtils.joinPaths(fileUtils.getCurrentWorkingDirectory(), "submitter", repo.getId());
        GitRepo gitRepo = gitRepoFactory.create(repoPath);
        gitRepos.add(gitRepo);
        try {
            if (fileUtils.folderEmptyOrNotExists(repoPath)) {
                gitRepo.cloneRepo(repo.getUrl(), repoPath);
            } else {
                gitRepo.pull();
            }
        } catch (Exception e) {
            log.atSevere().withCause(e).log("Failed to clone or pull repo at %s", repoPath);
            return;
        }
        String branch = String.format("origin/D%d", diff.getId());
        gitRepo.switchBranch(branch);
        newRequestBuilder.addTarget(CiRequest.Target.newBuilder().setRepo(repo).setCommitId(gitRepo.getHeadCommitId()).build());
        if (gitRepo.getHeadCommitId().equals(target.getCommitId())) {
            // latest result for CI matches last commit on the branch
            gitRepo.switchBranch("master");
            boolean mergingResult = gitRepo.merge("D%d", true);
            if (!mergingResult || gitRepo.hasChanges("master")) {
                shouldPush = false;
            }
        } else {
            // commit on the branch is newer than results in CI
            // it means we'll need to later push a new CiRequest
            shouldPush = false;
        }
    }
    if (shouldPush) {
        boolean allPushesSuccessful = gitRepos.stream().allMatch(repo -> repo.push("master"));
        if (allPushesSuccessful) {
            log.atInfo().log("All repos pushed successfully");
            diff = diff.toBuilder().setStatus(Diff.Status.SUBMITTED).build();
            firestoreClient.setProtoDocument(ReviewerConstants.DIFF_COLLECTION, String.valueOf(diff.getId()), diff);
        // TODO: store `SubmitterMergeResult`
        } else {
            // TODO: find out which one.
            log.atSevere().log("Some repo pushes failed");
        }
    } else {
        // TODO: Find out which repo caused it.
        log.atInfo().log("Latest CiResult is outdated.");
        CiRequest newRequest = newRequestBuilder.build();
        if (!ciSubmittedDiffIds.contains(newRequest)) {
            log.atInfo().log("New CiRequest sent.");
            firestoreClient.setProtoDocument(ReviewerConstants.CI_REQUESTS_PATH, String.valueOf(diff.getId()), newRequest);
            ciSubmittedDiffIds.add(newRequest);
        } else {
            log.atInfo().log("CiRequest was already sent before. Doing nothing.");
        }
    }
}
Also used : CiRequest(com.google.startupos.tools.reviewer.ReviewerProtos.CiRequest) GitRepo(com.google.startupos.common.repo.GitRepo) Repo(com.google.startupos.tools.reviewer.ReviewerProtos.Repo) GitRepo(com.google.startupos.common.repo.GitRepo) ArrayList(java.util.ArrayList) CiResponse(com.google.startupos.tools.reviewer.ReviewerProtos.CiResponse)

Example 3 with Repo

use of com.google.startupos.tools.reviewer.ReviewerProtos.Repo in project startup-os by google.

the class ReviewerMetadataUpdaterTask method mergeReviewerConfigData.

public ReviewerConfig mergeReviewerConfigData(ReviewerConfig reviewerConfig1, ReviewerConfig reviewerConfig2) {
    final String displayName = reviewerConfig1.getDisplayName();
    LinkedHashSet<Repo> repoList = new LinkedHashSet<>();
    repoList.addAll(reviewerConfig1.getRepoList());
    repoList.addAll(reviewerConfig2.getRepoList());
    LinkedHashSet<Project> projectList = new LinkedHashSet<>();
    projectList.addAll(reviewerConfig1.getProjectList());
    projectList.addAll(reviewerConfig2.getProjectList());
    LinkedHashSet<User> mergedUsersList = new LinkedHashSet<>();
    for (User user1 : reviewerConfig1.getUserList()) {
        User user2 = getUser(reviewerConfig2, user1.getId());
        if (user2.getId().isEmpty()) {
            mergedUsersList.add(user1);
        } else {
            // Users defined in both repos. Merging data.
            if (!user1.getEmail().isEmpty()) {
                if (!user1.getEmail().equals(user2.getEmail())) {
                    log.atInfo().log("***Emails for user %s differ between files.", user1.getId());
                }
            }
            if (!user1.getImageUrl().isEmpty()) {
                if (!user1.getImageUrl().equals(user2.getImageUrl())) {
                    log.atInfo().log("***Image Urls for user %s differ between files.", user1.getId());
                }
            }
            if (user1.getCrystals() != user2.getCrystals()) {
                log.atInfo().log("***Crystals amount for user  %s differ between files.", user1.getId());
            }
            LinkedHashSet<SocialNetwork> mergedUserSocialNetworks = new LinkedHashSet<>();
            mergedUserSocialNetworks.addAll(user1.getSocialNetworkList());
            LinkedHashSet<String> mergedUserSkillList = new LinkedHashSet<>();
            mergedUserSkillList.addAll(user1.getSkillList());
            LinkedHashSet<String> mergedUserProjectIdList = new LinkedHashSet<>();
            mergedUserProjectIdList.addAll(user1.getProjectIdList());
            mergedUserProjectIdList.addAll(user2.getProjectIdList());
            LinkedHashSet<Contribution> mergedUserContributions = new LinkedHashSet<>();
            mergedUserContributions.addAll(user1.getTopContributionList());
            mergedUserContributions.addAll(user2.getTopContributionList());
            User.Builder mergedUserBuilder = User.newBuilder().setId(user1.getId()).setFirstName(user1.getFirstName()).setLastName(user1.getLastName()).setEmail(user1.getEmail()).setImageUrl(user1.getImageUrl()).setCrystals(user1.getCrystals()).addAllSocialNetwork(user1.getSocialNetworkList()).addAllSkill(user1.getSkillList()).addAllProjectId(mergedUserProjectIdList).addAllTopContribution(mergedUserContributions);
            mergedUsersList.add(mergedUserBuilder.build());
        }
    }
    List<User> usersDefinedOnlyInReviewerConfig2 = reviewerConfig2.getUserList().stream().filter(user2 -> {
        boolean isUser2NotContainedInMergedUsers = true;
        for (User mergedUser : mergedUsersList) {
            if (mergedUser.getId().equals(user2.getId())) {
                isUser2NotContainedInMergedUsers = false;
                break;
            }
        }
        return isUser2NotContainedInMergedUsers;
    }).collect(Collectors.toList());
    mergedUsersList.addAll(usersDefinedOnlyInReviewerConfig2);
    int totalCrystals = reviewerConfig1.getTotalCrystal();
    ReviewerConfig.Builder mergedReviewerConfig = ReviewerConfig.newBuilder().setDisplayName(displayName).addAllRepo(repoList).addAllProject(projectList).addAllUser(mergedUsersList).setTotalCrystal(totalCrystals);
    return mergedReviewerConfig.build();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IntStream(java.util.stream.IntStream) Repo(com.google.startupos.tools.reviewer.ReviewerProtos.Repo) MessageDigest(java.security.MessageDigest) FileUtils(com.google.startupos.common.FileUtils) Contribution(com.google.startupos.tools.reviewer.ReviewerProtos.Contribution) SocialNetwork(com.google.startupos.tools.reviewer.ReviewerProtos.SocialNetwork) HashMap(java.util.HashMap) Project(com.google.startupos.tools.reviewer.ReviewerProtos.Project) Inject(javax.inject.Inject) GitRepoFactory(com.google.startupos.common.repo.GitRepoFactory) ReviewerRegistry(com.google.startupos.tools.reviewer.RegistryProtos.ReviewerRegistry) Map(java.util.Map) GitRepo(com.google.startupos.common.repo.GitRepo) FirestoreProtoClient(com.google.startupos.common.firestore.FirestoreProtoClient) FlagDesc(com.google.startupos.common.flags.FlagDesc) LinkedHashSet(java.util.LinkedHashSet) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ReviewerConfig(com.google.startupos.tools.reviewer.ReviewerProtos.ReviewerConfig) Files(java.nio.file.Files) User(com.google.startupos.tools.reviewer.ReviewerProtos.User) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) FluentLogger(com.google.common.flogger.FluentLogger) Flag(com.google.startupos.common.flags.Flag) User(com.google.startupos.tools.reviewer.ReviewerProtos.User) Project(com.google.startupos.tools.reviewer.ReviewerProtos.Project) ReviewerConfig(com.google.startupos.tools.reviewer.ReviewerProtos.ReviewerConfig) Repo(com.google.startupos.tools.reviewer.ReviewerProtos.Repo) GitRepo(com.google.startupos.common.repo.GitRepo) SocialNetwork(com.google.startupos.tools.reviewer.ReviewerProtos.SocialNetwork) Contribution(com.google.startupos.tools.reviewer.ReviewerProtos.Contribution)

Aggregations

GitRepo (com.google.startupos.common.repo.GitRepo)3 Repo (com.google.startupos.tools.reviewer.ReviewerProtos.Repo)3 CiRequest (com.google.startupos.tools.reviewer.ReviewerProtos.CiRequest)2 CiResponse (com.google.startupos.tools.reviewer.ReviewerProtos.CiResponse)2 FluentLogger (com.google.common.flogger.FluentLogger)1 CommandLine (com.google.startupos.common.CommandLine)1 FileUtils (com.google.startupos.common.FileUtils)1 FirestoreProtoClient (com.google.startupos.common.firestore.FirestoreProtoClient)1 Flag (com.google.startupos.common.flags.Flag)1 FlagDesc (com.google.startupos.common.flags.FlagDesc)1 GitRepoFactory (com.google.startupos.common.repo.GitRepoFactory)1 ReviewerRegistry (com.google.startupos.tools.reviewer.RegistryProtos.ReviewerRegistry)1 Status (com.google.startupos.tools.reviewer.ReviewerProtos.CiResponse.TargetResult.Status)1 Contribution (com.google.startupos.tools.reviewer.ReviewerProtos.Contribution)1 Project (com.google.startupos.tools.reviewer.ReviewerProtos.Project)1 ReviewerConfig (com.google.startupos.tools.reviewer.ReviewerProtos.ReviewerConfig)1 SocialNetwork (com.google.startupos.tools.reviewer.ReviewerProtos.SocialNetwork)1 User (com.google.startupos.tools.reviewer.ReviewerProtos.User)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1