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<>();
    }
  }
Ejemplo n.º 2
0
  @Override
  public void initializeUI(UIBuilder builder) throws Exception {
    final UIContext context = builder.getUIContext();
    copyPipelineToProject.setValue(Boolean.TRUE);
    pipeline.setCompleter(
        new UICompleter<PipelineDTO>() {
          @Override
          public Iterable<PipelineDTO> getCompletionProposals(
              UIContext context, InputComponent<?, PipelineDTO> input, String value) {
            return getPipelines(context, true);
          }
        });
    pipeline.setValueConverter(
        new Converter<String, PipelineDTO>() {
          @Override
          public PipelineDTO convert(String text) {
            return getPipelineForValue(context, text);
          }
        });
    pipeline.addValueChangeListener(
        new ValueChangeListener() {
          @Override
          public void valueChanged(ValueChangeEvent event) {
            String value = event.getNewValue() != null ? event.getNewValue().toString() : null;
            if (value != null) {
              String description = getDescriptionForFlow(value);
              pipeline.setNote(description != null ? description : "");
            } else {
              pipeline.setNote("");
            }
            boolean canCopy = Strings.isNotBlank(value);
            copyPipelineToProject.setEnabled(canCopy);
          }
        });
    if (getCurrentSelectedProject(context) != null) {
      PipelineDTO defaultValue = getPipelineForValue(context, DEFAULT_MAVEN_FLOW);
      if (defaultValue != null) {
        pipeline.setDefaultValue(defaultValue);
        pipeline.setValue(defaultValue);
      }
    }
    chatRoom.setCompleter(
        new UICompleter<String>() {
          @Override
          public Iterable<String> getCompletionProposals(
              UIContext context, InputComponent<?, String> input, String value) {
            return filterCompletions(getChatRoomNames(), value);
          }
        });
    chatRoom.addValueChangeListener(
        new ValueChangeListener() {
          @Override
          public void valueChanged(ValueChangeEvent event) {
            String value = event.getNewValue() != null ? event.getNewValue().toString() : null;
            if (value != null) {
              String description = getDescriptionForChatRoom(value);
              chatRoom.setNote(description != null ? description : "");
            } else {
              chatRoom.setNote("");
            }
          }
        });
    issueProjectName.setCompleter(
        new UICompleter<String>() {
          @Override
          public Iterable<String> getCompletionProposals(
              UIContext context, InputComponent<?, String> input, String value) {
            return filterCompletions(getIssueProjectNames(), value);
          }
        });
    issueProjectName.addValueChangeListener(
        new ValueChangeListener() {
          @Override
          public void valueChanged(ValueChangeEvent event) {
            String value = event.getNewValue() != null ? event.getNewValue().toString() : null;
            if (value != null) {
              String description = getDescriptionForIssueProject(value);
              issueProjectName.setNote(description != null ? description : "");
            } else {
              issueProjectName.setNote("");
            }
          }
        });

    // lets initialise the data from the current config if it exists
    ProjectConfig config = null;
    Project project = getCurrentSelectedProject(context);
    File configFile = getProjectConfigFile(context, getSelectedProject(context));
    if (configFile != null && configFile.exists()) {
      config = ProjectConfigs.parseProjectConfig(configFile);
    }
    if (config != null) {
      PipelineDTO flow = getPipelineForValue(context, config.getPipeline());
      if (flow != null) {
        CommandHelpers.setInitialComponentValue(this.pipeline, flow);
      }
      CommandHelpers.setInitialComponentValue(chatRoom, config.getChatRoom());
      CommandHelpers.setInitialComponentValue(issueProjectName, config.getIssueProjectName());
      CommandHelpers.setInitialComponentValue(codeReview, config.getCodeReview());
    }
    inputComponents = new ArrayList<>();
    File jenkinsFile = CommandHelpers.getProjectContextFile(context, project, "Jenkinsfile");
    boolean hasJenkinsFile = Files.isFile(jenkinsFile);
    LOG.debug("Has Jenkinsfile " + hasJenkinsFile + " with file: " + jenkinsFile);
    if (!hasJenkinsFile) {
      inputComponents.addAll(
          CommandHelpers.addInputComponents(builder, pipeline, copyPipelineToProject));
    }
    inputComponents.addAll(
        CommandHelpers.addInputComponents(builder, chatRoom, issueProjectName, codeReview));
  }