protected PipelineDTO getPipelineForValue(UIContext context, String value) { if (Strings.isNotBlank(value)) { Iterable<PipelineDTO> pipelines = getPipelines(context, false); for (PipelineDTO pipelineDTO : pipelines) { if (pipelineDTO.getValue().equals(value) || pipelineDTO.toString().equals(value)) { return pipelineDTO; } } } return null; }
protected Iterable<PipelineDTO> getPipelines(UIContext context, boolean filterPipelines) { Set<String> builders = null; ProjectOverviewDTO projectOveriew = null; if (filterPipelines) { projectOveriew = getProjectOverview(context); builders = projectOveriew.getBuilders(); } File dir = getJenkinsWorkflowFolder(context); Set<String> buildersFound = new HashSet<>(); if (dir != null) { Filter<File> filter = new Filter<File>() { @Override public boolean matches(File file) { return file.isFile() && Objects.equal(JENKINSFILE, file.getName()); } }; Set<File> files = Files.findRecursive(dir, filter); List<PipelineDTO> pipelines = new ArrayList<>(); for (File file : files) { try { String relativePath = Files.getRelativePath(dir, file); String value = Strings.stripPrefix(relativePath, "/"); String label = value; String postfix = "/" + JENKINSFILE; if (label.endsWith(postfix)) { label = label.substring(0, label.length() - postfix.length()); } if (label.startsWith(jenkinsFilePrefix)) { label = label.substring(jenkinsFilePrefix.length()); } // Lets ignore the fabric8 specific pipelines if (label.startsWith("fabric8-release/")) { continue; } String builder = null; int idx = label.indexOf("/"); if (idx > 0) { builder = label.substring(0, idx); if (filterPipelines && !builders.contains(builder)) { // ignore this builder continue; } else { buildersFound.add(builder); } } String descriptionMarkdown = null; File markdownFile = new File(file.getParentFile(), "ReadMe.md"); if (Files.isFile(markdownFile)) { descriptionMarkdown = IOHelpers.readFully(markdownFile); } PipelineDTO pipeline = new PipelineDTO(value, label, builder, descriptionMarkdown); File yamlFile = new File(file.getParentFile(), "metadata.yml"); if (Files.isFile(yamlFile)) { PipelineMetadata metadata = null; try { metadata = loadYaml(yamlFile, PipelineMetadata.class); } catch (IOException e) { LOG.warn("Failed to parse yaml file " + yamlFile + ". " + e, e); } if (metadata != null) { metadata.configurePipeline(pipeline); } } pipelines.add(pipeline); } catch (IOException e) { LOG.warn( "Failed to find relative path for folder " + dir + " and file " + file + ". " + e, e); } } if (buildersFound.size() == 1) { // lets trim the builder prefix from the labels for (String first : buildersFound) { String prefix = first + "/"; for (PipelineDTO pipeline : pipelines) { String label = pipeline.getLabel(); if (label.startsWith(prefix)) { label = label.substring(prefix.length()); pipeline.setLabel(label); } } break; } } Collections.sort(pipelines); return pipelines; } else { LOG.warn("No jenkinsWorkflowFolder!"); return new ArrayList<>(); } }
@Override public Result execute(UIExecutionContext context) throws Exception { LOG.info("Creating the fabric8.yml file"); String fileName = ProjectConfigs.FILE_NAME; Project project = getSelectedProject(context); File configFile = getProjectConfigFile(context.getUIContext(), getSelectedProject(context)); if (configFile == null) { // lets not fail as we typically want to execute SaveDevOpsStep next... return Results.success(); } ProjectConfig config = null; boolean hasFile = false; if (configFile.exists()) { config = ProjectConfigs.parseProjectConfig(configFile); hasFile = true; } if (config == null) { config = new ProjectConfig(); } CommandHelpers.putComponentValuesInAttributeMap(context, inputComponents); updateConfiguration(context, config); LOG.info("Result: " + config); String message; if (config.isEmpty() && !hasFile) { message = "No " + fileName + " need be generated as there is no configuration"; return Results.success(message); } else { String operation = "Updated"; if (!configFile.exists()) { operation = "Created"; } ProjectConfigs.saveConfig(config, configFile); message = operation + " " + fileName; } // now lets update the devops stuff UIContext uiContext = context.getUIContext(); Map<Object, Object> attributeMap = uiContext.getAttributeMap(); String gitUrl = getStringAttribute(attributeMap, "gitUrl"); if (Strings.isNullOrBlank(gitUrl)) { gitUrl = getStringAttribute(attributeMap, "gitAddress"); } Object object = attributeMap.get(Project.class); String user = getStringAttribute(attributeMap, "gitUser"); String named = getStringAttribute(attributeMap, "projectName"); ; File basedir = CommandHelpers.getBaseDir(project); if (basedir == null && configFile != null) { basedir = configFile.getParentFile(); } if (object instanceof Project) { Project newProject = (Project) object; MetadataFacet facet = newProject.getFacet(MetadataFacet.class); if (facet != null) { if (Strings.isNullOrBlank(named)) { named = facet.getProjectName(); } if (Strings.isNullOrBlank(gitUrl)) { String address = getStringAttribute(attributeMap, "gitAddress"); gitUrl = address + user + "/" + named + ".git"; } } else { LOG.error("No MetadataFacet for newly created project " + newProject); } } else { // updating an existing project - so lets try find the git url from the current source code if (Strings.isNullOrBlank(gitUrl)) { gitUrl = GitHelpers.extractGitUrl(basedir); } if (basedir != null) { if (Strings.isNullOrBlank(named)) { named = basedir.getName(); } } } // lets default the environments from the pipeline PipelineDTO pipelineValue = pipeline.getValue(); LOG.info("Using pipeline " + pipelineValue); String buildName = config.getBuildName(); if (Strings.isNotBlank(buildName)) { if (pipelineValue != null) { List<String> environments = pipelineValue.getEnvironments(); if (environments == null) { environments = new ArrayList<>(); } LinkedHashMap<String, String> environmentMap = new LinkedHashMap<>(); if (environments.isEmpty()) { environmentMap.put("Current", namespace); } else { for (String environment : environments) { String envNamespace = namespace + "-" + environment.toLowerCase(); environmentMap.put(environment, envNamespace); } } config.setEnvironments(environmentMap); } } LOG.info("Configured project " + buildName + " environments: " + config.getEnvironments()); ProjectConfigs.defaultEnvironments(config, namespace); String projectName = config.getBuildName(); if (Strings.isNullOrBlank(projectName)) { projectName = named; config.setBuildName(projectName); } LOG.info("Project name is: " + projectName); if (Strings.isNotBlank(projectName) && project != null) { MavenFacet maven = project.getFacet(MavenFacet.class); Model pom = maven.getModel(); if (pom != null && !isFunktionParentPom(project) && !isFabric8MavenPlugin3OrGreater(project)) { Properties properties = pom.getProperties(); boolean updated = false; updated = MavenHelpers.updatePomProperty( properties, "fabric8.label.project", projectName, updated); updated = MavenHelpers.updatePomProperty( properties, "fabric8.label.version", "${project.version}", updated); if (updated) { LOG.info("Updating pom.xml properties!"); maven.setModel(pom); } else { LOG.warn("Did not update pom.xml properties!"); } } else { LOG.warn("No pom.xml found!"); } } Boolean copyFlowToProjectValue = copyPipelineToProject.getValue(); if (copyFlowToProjectValue != null && copyFlowToProjectValue.booleanValue()) { if (basedir == null || !basedir.isDirectory()) { LOG.warn("Cannot copy the pipeline to the project as no basedir!"); } else { String flow = null; PipelineDTO pipelineDTO = pipelineValue; if (pipelineDTO != null) { flow = pipelineDTO.getValue(); } if (Strings.isNullOrBlank(flow)) { LOG.warn("Cannot copy the pipeline to the project as no pipeline selected!"); } else { String flowText = getFlowContent(flow, uiContext); if (Strings.isNullOrBlank(flowText)) { LOG.warn( "Cannot copy the pipeline to the project as no pipeline text could be loaded!"); } else { flowText = Strings.replaceAllWithoutRegex(flowText, "GIT_URL", "'" + gitUrl + "'"); File newFile = new File(basedir, ProjectConfigs.LOCAL_FLOW_FILE_NAME); Files.writeToFile(newFile, flowText.getBytes()); LOG.info("Written pipeline to " + newFile); if (config != null) { config.setPipeline(null); config.setUseLocalFlow(true); } } } } } final DevOpsConnector connector = new DevOpsConnector(); connector.setProjectConfig(config); connector.setTryLoadConfigFileFromRemoteGit(false); connector.setUsername(user); connector.setPassword(getStringAttribute(attributeMap, "gitPassword")); connector.setBranch(getStringAttribute(attributeMap, "gitBranch", "master")); connector.setBasedir(basedir); connector.setGitUrl(gitUrl); connector.setRepoName(named); connector.setRegisterWebHooks(true); // lets not trigger the jenkins webhook yet as the git push should trigger the build connector.setTriggerJenkinsJob(false); LOG.info("Using connector: " + connector); /* attributeMap.put("registerWebHooks", new Runnable() { @Override public void run() { LOG.info("Now registering webhooks!"); connector.registerWebHooks(); } }); */ try { connector.execute(); } catch (Exception e) { LOG.error("Failed to update DevOps resources: " + e, e); } return Results.success(message); }