public TargetDetail targetFrom(BuildType buildType) {
final BuildTypeDetail buildTypeDetail = communicator.detailsFor(buildType);
if (buildTypeDetail.paused) {
return new TargetDetail(communicator.endpoint() + buildType.href, buildType.webUrl(), buildType.name, Status.DISABLED, 0L);
}
final Set<Sponsor> sponsors = new HashSet<Sponsor>();
final List<RunningBuild> runningBuilds = new ArrayList<RunningBuild>();
long startTime = 0L;
for(Build build : communicator.runningBuildsFor(buildType)) {
final BuildDetail buildDetail = communicator.detailsOf(build);
startTime = Math.max(buildDetail.startDateTime(), startTime);
sponsors.addAll(sponsorsOf(buildDetail));
runningBuilds.add(new RunningBuild(percentageOf(build.percentageComplete), buildDetail.status()));
}
Status currentStatus = Status.UNKNOWN;
final Build lastCompletedBuild = communicator.lastCompletedBuildFor(buildTypeDetail);
if (null != lastCompletedBuild) {
currentStatus = lastCompletedBuild.status();
if (runningBuilds.isEmpty() || Status.BROKEN.equals(currentStatus)) {
final BuildDetail buildDetail = communicator.detailsOf(lastCompletedBuild);
startTime = Math.max(buildDetail.startDateTime(), startTime);
sponsors.addAll(sponsorsOf(buildDetail));
currentStatus = buildDetail.status();
}
}
if (Status.BROKEN.equals(currentStatus)) {
final List<Investigation> investigations = communicator.investigationsOf(buildType);
if (!investigations.isEmpty() && (investigations.get(0).startDateTime() > startTime) && investigations.get(0).underInvestigation()) {
currentStatus = Status.UNDER_INVESTIGATION;
}
}
return new TargetDetail(communicator.endpoint() + buildType.href, buildType.webUrl(), buildType.name, currentStatus, startTime, runningBuilds, sponsors);
}