use of com.google.startupos.tools.reviewer.ReviewerProtos.CiRequest in project startup-os by google.
the class FirestoreClientTool method testFunctionality.
public void testFunctionality() {
WriteResult result = client.setProtoDocument("test/bla", Diff.newBuilder().setId(123).build());
System.out.println("Update time : " + result.getUpdateTime());
client.addCollectionListener("/reviewer/ci/requests", CiRequest.newBuilder(), new ProtoEventListener<ProtoQuerySnapshot<CiRequest>>() {
@Override
public void onEvent(@Nullable ProtoQuerySnapshot<CiRequest> snapshot, @Nullable RuntimeException e) {
if (e != null) {
System.err.println("Listen failed: " + e);
return;
}
ImmutableList<CiRequest> protos = snapshot.getProtos();
for (CiRequest ciRequest : protos) {
System.out.println(ciRequest);
}
ImmutableList<ProtoChange<CiRequest>> protoChanges = snapshot.getProtoChanges();
for (ProtoChange<CiRequest> protoChange : protoChanges) {
System.out.println(protoChange.getProto());
System.out.println(protoChange.getType());
System.out.println(protoChange.getOldIndex());
System.out.println(protoChange.getNewIndex());
}
}
});
}
use of com.google.startupos.tools.reviewer.ReviewerProtos.CiRequest 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);
}
}
use of com.google.startupos.tools.reviewer.ReviewerProtos.CiRequest in project startup-os by google.
the class CiTask method tryRegisterListener.
public void tryRegisterListener() {
if (!listenerRegistered) {
firestoreClient.addCollectionListener(ReviewerConstants.CI_REQUESTS_PATH, CiRequest.newBuilder(), new ProtoEventListener<ProtoQuerySnapshot<CiRequest>>() {
@Override
public void onEvent(@Nullable ProtoQuerySnapshot<CiRequest> snapshot, @Nullable RuntimeException e) {
if (e != null) {
System.err.println("Listen failed: " + e);
return;
}
ArrayList<CiRequest> updatedRequests = new ArrayList(requests);
for (ProtoChange<CiRequest> protoChange : snapshot.getProtoChanges()) {
switch(protoChange.getType()) {
case ADDED:
updatedRequests.add(protoChange.getProto());
break;
case MODIFIED:
// The indices are made so that this type of sequential remove & add works, see:
// https://googleapis.github.io/google-cloud-java/google-cloud-clients/apidocs/com/google/cloud/firestore/DocumentChange.html#getNewIndex--
updatedRequests.remove(protoChange.getOldIndex());
updatedRequests.add(protoChange.getNewIndex(), protoChange.getProto());
break;
case REMOVED:
updatedRequests.remove(protoChange.getOldIndex());
break;
default:
throw new IllegalStateException("Unknown enum " + protoChange.getType());
}
}
requests = ImmutableList.copyOf(updatedRequests);
run();
}
});
listenerRegistered = true;
}
}
use of com.google.startupos.tools.reviewer.ReviewerProtos.CiRequest 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