Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Handles bad data when accessing pipelines #116

Merged
merged 2 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.capitalone.dashboard.rest;

import com.capitalone.dashboard.misc.HygieiaException;
import com.capitalone.dashboard.model.PipelineResponse;
import com.capitalone.dashboard.request.PipelineSearchRequest;
import com.capitalone.dashboard.service.PipelineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -22,7 +24,7 @@ public PipelineController(PipelineService pipelineService) {
}

@RequestMapping(value = "/pipeline", method = GET, produces = APPLICATION_JSON_VALUE)
public Iterable<PipelineResponse> searchPipelines(@Valid PipelineSearchRequest searchRequest) {
public Iterable<PipelineResponse> searchPipelines(@Valid @RequestBody PipelineSearchRequest searchRequest) throws HygieiaException {
return pipelineService.search(searchRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import com.capitalone.dashboard.misc.HygieiaException;
import org.apache.commons.lang.ObjectUtils;
import org.apache.log4j.Logger;
import org.bson.types.ObjectId;
Expand Down Expand Up @@ -106,7 +107,7 @@ public DynamicPipelineServiceImpl(PipelineRepository pipelineRepository, Dashboa
}

@Override
public Iterable<PipelineResponse> search(PipelineSearchRequest searchRequest) {
public Iterable<PipelineResponse> search(PipelineSearchRequest searchRequest) throws HygieiaException {
//sets the lower and upper bound for the prod bucket's commits. uses constant for lower bound limit and today as default for upper bound
Long lowerBound = searchRequest.getBeginDate();
//if(lowerBound == null){
Expand Down Expand Up @@ -140,7 +141,7 @@ protected Pipeline getOrCreatePipeline(ObjectId collectorItemId) {
}

// Creates the response that is returned to the client
private PipelineResponse buildPipelineResponse(Pipeline pipeline, Long lowerBound, Long upperBound){
private PipelineResponse buildPipelineResponse(Pipeline pipeline, Long lowerBound, Long upperBound) throws HygieiaException {
/**
* get the collector item and dashboard
*/
Expand Down Expand Up @@ -870,7 +871,7 @@ private boolean isBetween(Long commitTimestamp, Long lowerBound, Long upperBound
* @param pipeline
* @return
*/
private PipelineResponseCommit applyStageTimestamps(PipelineResponseCommit commit, Dashboard dashboard, Pipeline pipeline,List<PipelineStage> pipelineStageList){
private PipelineResponseCommit applyStageTimestamps(PipelineResponseCommit commit, Dashboard dashboard, Pipeline pipeline,List<PipelineStage> pipelineStageList) throws HygieiaException {
PipelineResponseCommit returnCommit = new PipelineResponseCommit(commit);

for(PipelineStage systemStage : pipelineStageList) {
Expand All @@ -894,7 +895,7 @@ private PipelineResponseCommit applyStageTimestamps(PipelineResponseCommit commi
* @param stageType
* @return
*/
private Map<String, PipelineCommit> findCommitsForStage(Dashboard dashboard, Pipeline pipeline, PipelineStage stage) {
private Map<String, PipelineCommit> findCommitsForStage(Dashboard dashboard, Pipeline pipeline, PipelineStage stage) throws HygieiaException {
Map<String, PipelineCommit> commitMap = new HashMap<>();

// The environment name including the pseudo environments "Build" and "Commit"
Expand All @@ -915,7 +916,7 @@ private Map<String, PipelineCommit> findCommitsForStage(Dashboard dashboard, Pip
* @param stage current stage
* @return a list of all commits as pipeline response commits that havent moved past the current stage
*/
public List<PipelineResponseCommit> findNotPropagatedCommits(Dashboard dashboard, Pipeline pipeline, PipelineStage stage,List<PipelineStage> pipelineStageList){
public List<PipelineResponseCommit> findNotPropagatedCommits(Dashboard dashboard, Pipeline pipeline, PipelineStage stage,List<PipelineStage> pipelineStageList) throws HygieiaException {

Map<String, PipelineCommit> startingStage = findCommitsForStage(dashboard, pipeline, stage);
List<PipelineResponseCommit> notPropagatedCommits = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.capitalone.dashboard.service;

import com.capitalone.dashboard.misc.HygieiaException;
import com.capitalone.dashboard.model.PipelineResponse;
import com.capitalone.dashboard.request.PipelineSearchRequest;

Expand All @@ -11,5 +12,5 @@ public interface PipelineService {
* @param searchRequest search request
* @return all pipelines for team dashboards
*/
Iterable<PipelineResponse> search(PipelineSearchRequest searchRequest);
Iterable<PipelineResponse> search(PipelineSearchRequest searchRequest) throws HygieiaException;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.capitalone.dashboard.service;

import com.capitalone.dashboard.misc.HygieiaException;
import com.capitalone.dashboard.settings.ApiSettings;
import com.capitalone.dashboard.model.CollectorItem;
import com.capitalone.dashboard.model.Dashboard;
Expand Down Expand Up @@ -48,7 +49,7 @@ public PipelineServiceImpl(PipelineRepository pipelineRepository, DashboardRepos
}

@Override
public Iterable<PipelineResponse> search(PipelineSearchRequest searchRequest) {
public Iterable<PipelineResponse> search(PipelineSearchRequest searchRequest) throws HygieiaException {
List<PipelineResponse> pipelineResponses = new ArrayList<>();
for(ObjectId collectorItemId : searchRequest.getCollectorItemId()){
Pipeline pipeline = getOrCreatePipeline(collectorItemId);
Expand All @@ -67,7 +68,7 @@ protected Pipeline getOrCreatePipeline(ObjectId collectorItemId) {
return pipeline;
}

private PipelineResponse buildPipelineResponse(Pipeline pipeline, Long beginDate, Long endDate){
private PipelineResponse buildPipelineResponse(Pipeline pipeline, Long beginDate, Long endDate) throws HygieiaException {
Long lowerBound = beginDate;
if(beginDate == null){
Calendar cal = new GregorianCalendar();
Expand All @@ -80,7 +81,14 @@ private PipelineResponse buildPipelineResponse(Pipeline pipeline, Long beginDate
* get the collector item and dashboard
*/
CollectorItem dashboardCollectorItem = collectorItemRepository.findOne(pipeline.getCollectorItemId());
Dashboard dashboard = dashboardRepository.findOne(new ObjectId((String)dashboardCollectorItem.getOptions().get("dashboardId")));
if(dashboardCollectorItem.getOptions().get("dashboardId") == null) {
throw new HygieiaException(" Collector Item: " + dashboardCollectorItem.getId() + " is not associated to a dashboard. ", HygieiaException.BAD_DATA);
}
String dashboardId = (String) dashboardCollectorItem.getOptions().get("dashboardId");
Dashboard dashboard = dashboardRepository.findOne(new ObjectId(dashboardId));
if(dashboard == null) {
throw new HygieiaException(" Dashboard " + dashboardId + " is not found for collectorItem: " + dashboardCollectorItem.getId() + " ", HygieiaException.BAD_DATA);
}
PipelineResponse pipelineResponse = new PipelineResponse();
pipelineResponse.setCollectorItemId(dashboardCollectorItem.getId());
pipelineResponse.setProdStage(PipelineUtils.getProdStage(dashboard));
Expand Down Expand Up @@ -126,7 +134,7 @@ private PipelineResponse buildPipelineResponse(Pipeline pipeline, Long beginDate
* @param dashboard
* @return a list of deploy PipelineStages that are not mapped
*/
private List<PipelineStage> findUnmappedStages(Dashboard dashboard,List<PipelineStage> pipelineStageList){
private List<PipelineStage> findUnmappedStages(Dashboard dashboard,List<PipelineStage> pipelineStageList) throws HygieiaException {
List<PipelineStage> unmappedStages = new ArrayList<>();

Map<PipelineStage, String> stageToEnvironmentNameMap = PipelineUtils.getStageToEnvironmentNameMap(dashboard);
Expand All @@ -150,7 +158,7 @@ private List<PipelineStage> findUnmappedStages(Dashboard dashboard,List<Pipeline
* @param dashboard
* @return
*/
private Map<String, PipelineCommit> getCommitsAfterStage(PipelineStage stage, Pipeline pipeline, Dashboard dashboard,List<PipelineStage> pipelineStageList,Map<String,String> orderMap){
private Map<String, PipelineCommit> getCommitsAfterStage(PipelineStage stage, Pipeline pipeline, Dashboard dashboard,List<PipelineStage> pipelineStageList,Map<String,String> orderMap) throws HygieiaException {
Map<String, PipelineCommit> unionOfAllSets = new HashMap<>();
// get key(ordinal) for stage name
List<String> list = getKeysByValue(orderMap,stage.getName());
Expand Down Expand Up @@ -194,7 +202,7 @@ private boolean isBetween(Long commitTimestamp, Long lowerBound, Long upperBound
* @param pipeline
* @return
*/
private PipelineResponseCommit applyStageTimestamps(PipelineResponseCommit commit, Dashboard dashboard, Pipeline pipeline,List<PipelineStage> pipelineStageList){
private PipelineResponseCommit applyStageTimestamps(PipelineResponseCommit commit, Dashboard dashboard, Pipeline pipeline,List<PipelineStage> pipelineStageList) throws HygieiaException {
PipelineResponseCommit returnCommit = new PipelineResponseCommit(commit);

for(PipelineStage systemStage : pipelineStageList) {
Expand All @@ -218,7 +226,7 @@ private PipelineResponseCommit applyStageTimestamps(PipelineResponseCommit commi
* @param stage
* @return
*/
private Map<String, PipelineCommit> findCommitsForStage(Dashboard dashboard, Pipeline pipeline, PipelineStage stage) {
private Map<String, PipelineCommit> findCommitsForStage(Dashboard dashboard, Pipeline pipeline, PipelineStage stage) throws HygieiaException {
Map<String, PipelineCommit> commitMap = new HashMap<>();

String pseudoEnvironmentName =
Expand All @@ -238,7 +246,7 @@ private Map<String, PipelineCommit> findCommitsForStage(Dashboard dashboard, Pip
* @param stage current stage
* @return a list of all commits as pipeline response commits that havent moved past the current stage
*/
public List<PipelineResponseCommit> findNotPropagatedCommits(Dashboard dashboard, Pipeline pipeline, PipelineStage stage,List<PipelineStage> pipelineStageList,Map<String,String> orderMap){
public List<PipelineResponseCommit> findNotPropagatedCommits(Dashboard dashboard, Pipeline pipeline, PipelineStage stage,List<PipelineStage> pipelineStageList,Map<String,String> orderMap) throws HygieiaException {
Map<String, PipelineCommit> startingStage = findCommitsForStage(dashboard, pipeline, stage);
Map<String, PipelineCommit> commitsInLaterStages = getCommitsAfterStage(stage, pipeline, dashboard,pipelineStageList,orderMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Map;
import java.util.Set;

import com.capitalone.dashboard.misc.HygieiaException;
import org.bson.types.ObjectId;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -137,7 +138,7 @@ public void setUp() {
}

@Test
public void testSearch() {
public void testSearch() throws HygieiaException {
CollectorItem pipelineCI = setupPipelineCollectorItem();
CollectorItem scmCI = setupScmCollectorItem();
Component component = setupComponent(scmCI);
Expand Down