Ejemplo n.º 1
0
  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<>();
    }
  }