use of com.google.startupos.common.repo.GitRepo 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.");
}
}
}
Aggregations