@Override
  public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
      throws IOException, InterruptedException {

    listener.getLogger().println("[CucumberReportPublisher] Compiling Cucumber Html Reports ...");

    File workspaceJsonReportDirectory = new File(build.getWorkspace().toURI().getPath());
    if (!jsonReportDirectory.isEmpty()) {
      workspaceJsonReportDirectory =
          new File(build.getWorkspace().toURI().getPath(), jsonReportDirectory);
    }
    File targetBuildDirectory = new File(build.getRootDir(), "cucumber-html-reports");

    String buildNumber = Integer.toString(build.getNumber());
    String buildProject = build.getProject().getName();

    if (!targetBuildDirectory.exists()) {
      targetBuildDirectory.mkdirs();
    }

    boolean buildResult = true;

    // if we are on a slave
    if (Computer.currentComputer() instanceof SlaveComputer) {
      listener
          .getLogger()
          .println("[CucumberReportPublisher] detected this build is running on a slave ");
      FilePath projectWorkspaceOnSlave = build.getProject().getSomeWorkspace();
      FilePath masterJsonReportDirectory = new FilePath(targetBuildDirectory);
      listener
          .getLogger()
          .println(
              "[CucumberReportPublisher] copying json from: "
                  + projectWorkspaceOnSlave.toURI()
                  + "to reports directory: "
                  + masterJsonReportDirectory.toURI());
      projectWorkspaceOnSlave.copyRecursiveTo("**/*.json", "", masterJsonReportDirectory);
    } else {
      // if we are on the master
      listener
          .getLogger()
          .println("[CucumberReportPublisher] detected this build is running on the master ");
      String[] files = findJsonFiles(workspaceJsonReportDirectory);

      if (files.length != 0) {
        listener
            .getLogger()
            .println(
                "[CucumberReportPublisher] copying json to reports directory: "
                    + targetBuildDirectory);
        for (String file : files) {
          FileUtils.copyFile(
              new File(workspaceJsonReportDirectory.getPath() + "/" + file),
              new File(targetBuildDirectory, file));
        }
      } else {
        listener
            .getLogger()
            .println(
                "[CucumberReportPublisher] there were no json results found in: "
                    + workspaceJsonReportDirectory);
      }
    }

    // generate the reports from the targetBuildDirectory
    String[] jsonReportFiles = findJsonFiles(targetBuildDirectory);
    if (jsonReportFiles.length != 0) {

      listener
          .getLogger()
          .println(
              "[CucumberReportPublisher] Found the following number of json files: "
                  + jsonReportFiles.length);
      int jsonIndex = 0;
      for (String jsonReportFile : jsonReportFiles) {
        listener
            .getLogger()
            .println(
                "[CucumberReportPublisher] "
                    + jsonIndex
                    + ". Found a json file: "
                    + jsonReportFile);
        jsonIndex++;
      }
      listener.getLogger().println("[CucumberReportPublisher] Generating HTML reports");

      try {
        ReportBuilder reportBuilder =
            new ReportBuilder(
                fullPathToJsonFiles(jsonReportFiles, targetBuildDirectory),
                targetBuildDirectory,
                pluginUrlPath,
                buildNumber,
                buildProject,
                skippedFails,
                undefinedFails,
                !noFlashCharts,
                true,
                false,
                "",
                false);
        reportBuilder.generateReports();
        buildResult = reportBuilder.getBuildStatus();
      } catch (Exception e) {
        e.printStackTrace();
        listener
            .getLogger()
            .println("[CucumberReportPublisher] there was an error generating the reports: " + e);
        for (StackTraceElement error : e.getStackTrace()) {
          listener.getLogger().println(error);
        }
      }
    } else {
      listener
          .getLogger()
          .println(
              "[CucumberReportPublisher] there were no json results found in: "
                  + targetBuildDirectory);
    }

    build.addAction(new CucumberReportBuildAction(build));
    return buildResult;
  }
コード例 #2
0
  @Override
  public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
      throws IOException, InterruptedException {

    listener.getLogger().println("[CucumberReportPublisher] Compiling Cucumber Html Reports ...");

    // source directory (possibly on slave)
    FilePath workspaceJsonReportDirectory;
    if (jsonReportDirectory.isEmpty()) {
      workspaceJsonReportDirectory = build.getWorkspace();
    } else {
      workspaceJsonReportDirectory = new FilePath(build.getWorkspace(), jsonReportDirectory);
    }

    // target directory (always on master)
    File targetBuildDirectory = new File(build.getRootDir(), "cucumber-html-reports");
    if (!targetBuildDirectory.exists()) {
      targetBuildDirectory.mkdirs();
    }

    String buildNumber = Integer.toString(build.getNumber());
    String buildProject = build.getProject().getName();

    if (Computer.currentComputer() instanceof SlaveComputer) {
      listener
          .getLogger()
          .println(
              "[CucumberReportPublisher] Copying all json files from slave: "
                  + workspaceJsonReportDirectory.getRemote()
                  + " to master reports directory: "
                  + targetBuildDirectory);
    } else {
      listener
          .getLogger()
          .println(
              "[CucumberReportPublisher] Copying all json files from: "
                  + workspaceJsonReportDirectory.getRemote()
                  + " to reports directory: "
                  + targetBuildDirectory);
    }
    workspaceJsonReportDirectory.copyRecursiveTo(
        DEFAULT_FILE_INCLUDE_PATTERN, new FilePath(targetBuildDirectory));

    // generate the reports from the targetBuildDirectory
    Result result = Result.NOT_BUILT;
    String[] jsonReportFiles =
        findJsonFiles(targetBuildDirectory, fileIncludePattern, fileExcludePattern);
    if (jsonReportFiles.length > 0) {
      listener
          .getLogger()
          .println(
              String.format(
                  "[CucumberReportPublisher] Found %d json files.", jsonReportFiles.length));
      int jsonIndex = 0;
      for (String jsonReportFile : jsonReportFiles) {
        listener
            .getLogger()
            .println(
                "[CucumberReportPublisher] "
                    + jsonIndex
                    + ". Found a json file: "
                    + jsonReportFile);
        jsonIndex++;
      }
      listener.getLogger().println("[CucumberReportPublisher] Generating HTML reports");

      try {
        ReportBuilder reportBuilder =
            new ReportBuilder(
                fullPathToJsonFiles(jsonReportFiles, targetBuildDirectory),
                targetBuildDirectory,
                pluginUrlPath,
                buildNumber,
                buildProject,
                skippedFails,
                pendingFails,
                undefinedFails,
                missingFails,
                !noFlashCharts,
                true,
                false,
                parallelTesting);
        reportBuilder.generateReports();

        boolean buildSuccess = reportBuilder.getBuildStatus();

        if (buildSuccess) {
          result = Result.SUCCESS;
        } else {
          result = ignoreFailedTests ? Result.UNSTABLE : Result.FAILURE;
        }

      } catch (Exception e) {
        e.printStackTrace();
        result = Result.FAILURE;
        listener
            .getLogger()
            .println("[CucumberReportPublisher] there was an error generating the reports: " + e);
        for (StackTraceElement error : e.getStackTrace()) {
          listener.getLogger().println(error);
        }
      }
    } else {
      result = Result.SUCCESS;
      listener
          .getLogger()
          .println(
              "[CucumberReportPublisher] there were no json results found in: "
                  + targetBuildDirectory);
    }

    build.addAction(new CucumberReportBuildAction(build));
    build.setResult(result);

    return true;
  }