com.capitalone.dashboard.util.PipelineUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Core package shared by API layer and Microservices
package com.capitalone.dashboard.util;
import com.capitalone.dashboard.misc.HygieiaException;
import com.capitalone.dashboard.model.Build;
import com.capitalone.dashboard.model.Commit;
import com.capitalone.dashboard.model.Dashboard;
import com.capitalone.dashboard.model.Pipeline;
import com.capitalone.dashboard.model.PipelineCommit;
import com.capitalone.dashboard.model.PipelineStage;
import com.capitalone.dashboard.model.RepoBranch;
import com.capitalone.dashboard.model.SCM;
import com.capitalone.dashboard.model.Widget;
import com.capitalone.dashboard.repository.CommitRepository;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public final class PipelineUtils {
private PipelineUtils(){
}
public static Map commitSetToMap(Set set){
Map returnMap = new HashMap<>();
for(PipelineCommit commit : set){
returnMap.put(commit.getScmRevisionNumber(), commit);
}
return returnMap;
}
public static Map getStageToEnvironmentNameMap(Dashboard dashboard) throws HygieiaException {
Map rt = new LinkedHashMap<>();
for(Widget widget : dashboard.getWidgets()) {
if(widget.getName().equalsIgnoreCase("build")){
rt.put(PipelineStage.valueOf("Build"), "Build");
}if(widget.getName().equalsIgnoreCase("repo")){
rt.put(PipelineStage.valueOf("Commit"), "Commit");
}
if (widget.getName().equalsIgnoreCase("pipeline")) {
Map,?> gh = (Map,?>) widget.getOptions().get("mappings");
if(gh == null) {
throw new HygieiaException("Pipeline " + widget.getId() + " is missing mappings ", HygieiaException.BAD_DATA);
}
for (Map.Entry, ?> entry : gh.entrySet()) {
rt.put(PipelineStage.valueOf((String) entry.getKey()), (String) entry.getValue());
}
}
}
return rt;
}
public static Map getOrderForStages(Dashboard dashboard) {
Map rt = new LinkedHashMap<>();
rt.put("0", "Commit");
rt.put("1", "Build");
for(Widget widget : dashboard.getWidgets()) {
if (widget.getName().equalsIgnoreCase("pipeline")) {
Map,?> gh = (Map,?>) widget.getOptions().get("order");
int count = 2;
if(gh!=null) {
for (Map.Entry, ?> entry : gh.entrySet()) {
rt.put(Integer.parseInt((String) entry.getKey())+count+"", (String) entry.getValue());
}
}
}
}
return rt;
}
public static String getProdStage(Dashboard dashboard) {
String prodStage = "";
for(Widget widget : dashboard.getWidgets()) {
if (widget.getName().equalsIgnoreCase("pipeline")) {
prodStage = (String)widget.getOptions().get("prod");
}
}
return prodStage;
}
public static void setStageToEnvironmentNameMap(Dashboard dashboard, Map map) {
Map mappingsMap = new HashMap<>();
for (Map.Entry e : map.entrySet()) {
if (PipelineStage.BUILD.equals(e.getKey()) || PipelineStage.COMMIT.equals(e.getKey())) {
continue;
}
if (e.getKey() != null) {
mappingsMap.put(e.getKey().getName().toLowerCase(), e.getValue());
}
}
for(Widget widget : dashboard.getWidgets()) {
if (widget.getName().equalsIgnoreCase("pipeline")) {
widget.getOptions().put("mappings", mappingsMap);
}
}
}
/**
* Iterate over failed builds, if the failed build collector item id matches the successful builds collector item id
* take all the commits from the changeset of the failed build and add them to the pipeline.
* Then remove the failed build from the collection after it has been processed.
*
* @param successfulBuild
* @param pipeline
*/
public static void processPreviousFailedBuilds(Build successfulBuild, Pipeline pipeline) {
if (!pipeline.getFailedBuilds().isEmpty()) {
Iterator failedBuilds = pipeline.getFailedBuilds().iterator();
while (failedBuilds.hasNext()) {
Build b = failedBuilds.next();
if (b.getCollectorItemId().equals(successfulBuild.getCollectorItemId())) {
for (SCM scm : b.getSourceChangeSet()) {
PipelineCommit failedBuildCommit = new PipelineCommit(scm, successfulBuild.getStartTime());
pipeline.addCommit(PipelineStage.BUILD.getName(), failedBuildCommit);
}
failedBuilds.remove();
}
}
}
}
public static boolean isMoveCommitToBuild(Build build, SCM scm, CommitRepository commitRepository) {
List commitsFromRepo = getCommitsFromCommitRepo(scm, commitRepository);
List codeReposFromBuild = build.getCodeRepos();
Set codeRepoUrlsFromCommits = new HashSet<>();
for (Commit c : commitsFromRepo) {
codeRepoUrlsFromCommits.add(getRepoNameOnly(c.getScmUrl()));
}
for (RepoBranch rb : codeReposFromBuild) {
if (codeRepoUrlsFromCommits.contains(getRepoNameOnly(rb.getUrl()))) {
return true;
}
}
return false;
}
private static List getCommitsFromCommitRepo(SCM scm, CommitRepository commitRepository) {
return commitRepository.findByScmRevisionNumber(scm.getScmRevisionNumber());
}
private static String getRepoNameOnly(String url) {
try {
URL temp = new URL(url);
return temp.getHost() + temp.getPath();
} catch (MalformedURLException e) {
return url;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy