com.clarolab.bamboo.client.BambooPlanClient Maven / Gradle / Ivy
package com.clarolab.bamboo.client;
import com.clarolab.bamboo.entities.BambooMain;
import com.clarolab.bamboo.entities.BambooPlan;
import com.clarolab.bamboo.entities.BambooProject;
import com.clarolab.bamboo.utils.Constants;
import com.google.common.collect.Lists;
import lombok.Builder;
import lombok.Data;
import java.util.List;
import java.util.stream.Collectors;
@Data
public class BambooPlanClient extends BambooClient {
private BambooProjectClient bambooProjectClient;
@Builder
public BambooPlanClient(BambooApiClient bambooApiClient) {
super(bambooApiClient, Constants.AMOUNT_OF_ELEMENTS_TO_GET);
bambooProjectClient = BambooProjectClient.builder().bambooApiClient(bambooApiClient).build();
}
public int getPlansSize() throws Exception {
return perform(Constants.BAMBOO_PLAN_SIZE_ENDPOINT, BambooMain.class).getPlansSize();
}
/**
*
* @param planName should have the format "{bamboo_project_name} - {bamboo_plan_name}"
* @return BambooPlan
*
*/
public BambooPlan getPlan(String planName) throws Exception {
return getPlanByExhaustiveSearch(planName);
}
public List getPlansForProject(String projectName) throws Exception {
//this is the total of plans for current project
BambooProject bambooProject = bambooProjectClient.getProjectFromName(projectName);
if (bambooProject == null)
return null;
int plans = bambooProject.getTotalOfPlans();
List bambooPlanList = Lists.newArrayList();
int start = 0;
while (bambooPlanList.size() != plans) {
bambooPlanList.addAll(perform(String.format(Constants.BAMBOO_PLAN_ENDPOINT_LIMITED, start, getLimitResults()), BambooMain.class)
.getPlans().stream().filter(plan -> plan.getProjectName().equals(projectName)).collect(Collectors.toList()));
start += getLimitResults();
}
return bambooPlanList;
}
private BambooPlan getPlanByExhaustiveSearch(String planName) throws Exception {
int plansSize = getPlansSize();
int start = 0;
while (start < plansSize) {
BambooPlan bambooPlan = perform(String.format(Constants.BAMBOO_PLAN_ENDPOINT_LIMITED, start, getLimitResults()), BambooMain.class).getPlans().stream().filter(plan -> plan.getName().equals(planName)).findFirst().orElse(null);
if(bambooPlan != null)
return bambooPlan;
else
start += getLimitResults();
}
return null;
}
}