use of com.google.startupos.common.repo.GitRepo in project startup-os by google.
the class PatchCommand method run.
@Override
public boolean run(String[] args) {
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-d")) {
args[i] = "--diff_number";
}
}
Flags.parseCurrentPackage(args);
String branchName = String.format("D%d", diffNumber.get());
try {
fileUtils.listContents(workspacePath).stream().map(path -> fileUtils.joinToAbsolutePath(workspacePath, path)).filter(path -> fileUtils.folderExists(path)).forEach(path -> {
GitRepo repo = this.gitRepoFactory.create(path);
String currentBranchName = repo.currentBranch();
if (!currentBranchName.startsWith("D")) {
System.err.println(String.format("Currently (%s) not on a diff branch, unable to apply changes from %s", ANSI_YELLOW + ANSI_BOLD + currentBranchName + ANSI_RESET, branchName));
} else {
repo.merge(branchName, true);
}
});
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
use of com.google.startupos.common.repo.GitRepo in project startup-os by google.
the class SyncCommand method revertChanges.
private void revertChanges(Map<String, String> repoToInitialBranch) {
if (!repoToInitialBranch.isEmpty()) {
repoToInitialBranch.forEach((repoName, initialBranch) -> {
GitRepo repo = repoFactory.create(fileUtils.joinToAbsolutePath(workspacePath, repoName));
String currentBranch = repo.currentBranch();
if (!currentBranch.equals(initialBranch)) {
repo.switchBranch(initialBranch);
}
});
}
}
use of com.google.startupos.common.repo.GitRepo in project startup-os by google.
the class SyncCommand method run.
@Override
public boolean run(String[] args) {
// Pull all repos in head
try {
fileUtils.listContents(headPath).stream().map(path -> fileUtils.joinToAbsolutePath(headPath, path)).filter(fileUtils::folderExists).forEach(path -> {
System.out.println(String.format("[HEAD]: Performing sync: %s", path));
repoFactory.create(path).pull();
});
} catch (IOException e) {
e.printStackTrace();
}
// `git am -3 < ~/patch-file-we-moved-out`
try {
fileUtils.listContents(workspacePath).stream().map(path -> fileUtils.joinToAbsolutePath(workspacePath, path)).filter(fileUtils::folderExists).forEach(path -> {
String repoName = Paths.get(path).getFileName().toString();
System.out.println(String.format("[%s/%s]: Performing sync", workspaceName, repoName));
GitRepo repo = repoFactory.create(path);
repoToInitialBranch.put(repoName, repo.currentBranch());
if (repo.branchExists(TEMP_BRANCH_FOR_SYNC)) {
repo.removeBranch(TEMP_BRANCH_FOR_SYNC);
}
System.out.println(String.format("[%s/%s]: switching to temp branch", workspaceName, repoName));
repo.switchBranch(TEMP_BRANCH_FOR_SYNC);
System.out.println(String.format("[%s/%s]: committing all changes", workspaceName, repoName));
repo.commit(repo.getUncommittedFiles(), "Sync: temporary commit");
System.out.println(String.format("[%s/%s]: switching to master", workspaceName, repoName));
repo.switchBranch("master");
System.out.println(String.format("[%s/%s]: pulling", workspaceName, repoName));
repo.pull();
System.out.println(String.format("[%s/%s]: merging changes", workspaceName, repoName));
boolean mergeResult = repo.merge("temp_branch_for_sync");
if (!mergeResult) {
System.out.println(String.format("[%s/%s]: manual merge required, check files for conflicts", workspaceName, repoName));
}
System.out.println(String.format("[%s/%s]: removing temp branch", workspaceName, repoName));
repo.removeBranch("temp_branch_for_sync");
});
} catch (Exception e) {
revertChanges(repoToInitialBranch);
e.printStackTrace();
}
return true;
}
use of com.google.startupos.common.repo.GitRepo in project startup-os by google.
the class AaModule method diffNumber.
@Provides
@Named("Diff number")
public static Integer diffNumber(FileUtils fileUtils, @Named("Workspace path") String workspacePath, GitRepoFactory gitRepoFactory) {
try {
String firstWorkspacePath = fileUtils.listContents(workspacePath).stream().map(path -> fileUtils.joinToAbsolutePath(workspacePath, path)).filter(fileUtils::folderExists).findFirst().orElse(null);
if (firstWorkspacePath == null) {
throw new RuntimeException(String.format("There are no repositories in workspace %s", workspacePath));
}
GitRepo repo = gitRepoFactory.create(firstWorkspacePath);
return repo.listBranches().stream().filter(branchName -> branchName.startsWith("D")).mapToInt(branchName -> safeParsePositiveInt(branchName.replace("D", ""))).filter(number -> number > 0).findFirst().orElse(-1);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
use of com.google.startupos.common.repo.GitRepo 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);
}
}
Aggregations