@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;
  }
Esempio n. 2
0
  protected void buildOpenMergeRequests(
      GitLabPushTrigger trigger, Integer projectId, String projectRef) {
    try {
      GitLab api = new GitLab();
      // TODO Replace this with a call to GitlabAPI.getOpenMergeRequests, once timols has deployed
      // version 1.1.7
      String tailUrl =
          GitlabProject.URL
              + "/"
              + projectId
              + GitlabMergeRequest.URL
              + "?state=opened&per_page=100";
      List<GitlabMergeRequest> mergeRequests =
          api.instance().retrieve().getAll(tailUrl, GitlabMergeRequest[].class);

      for (org.gitlab.api.models.GitlabMergeRequest mr : mergeRequests) {
        if (projectRef.endsWith(mr.getSourceBranch())
            || (trigger.getTriggerOpenMergeRequestOnPush().equals("both")
                && projectRef.endsWith(mr.getTargetBranch()))) {

          if (trigger.getCiSkip() && mr.getDescription().contains("[ci-skip]")) {
            LOGGER.log(Level.INFO, "Skipping MR " + mr.getTitle() + " due to ci-skip.");
            continue;
          }
          GitlabBranch branch =
              api.instance().getBranch(api.instance().getProject(projectId), mr.getSourceBranch());
          LastCommit lastCommit = new LastCommit();
          lastCommit.setId(branch.getCommit().getId());
          lastCommit.setMessage(branch.getCommit().getMessage());
          lastCommit.setUrl(
              GitlabProject.URL
                  + "/"
                  + projectId
                  + "/repository"
                  + GitlabCommit.URL
                  + "/"
                  + branch.getCommit().getId());

          LOGGER.log(
              Level.FINE,
              "Generating new merge trigger from "
                  + mr.toString()
                  + "\n source: "
                  + mr.getSourceBranch()
                  + "\n target: "
                  + mr.getTargetBranch()
                  + "\n state: "
                  + mr.getState()
                  + "\n assign: "
                  + mr.getAssignee()
                  + "\n author: "
                  + mr.getAuthor()
                  + "\n id: "
                  + mr.getId()
                  + "\n iid: "
                  + mr.getIid()
                  + "\n last commit: "
                  + lastCommit.getId()
                  + "\n\n");
          GitLabMergeRequest newReq = new GitLabMergeRequest();
          newReq.setObject_kind("merge_request");
          newReq.setObjectAttribute(new GitLabMergeRequest.ObjectAttributes());
          if (mr.getAssignee() != null)
            newReq.getObjectAttribute().setAssigneeId(mr.getAssignee().getId());
          if (mr.getAuthor() != null)
            newReq.getObjectAttribute().setAuthorId(mr.getAuthor().getId());
          newReq.getObjectAttribute().setDescription(mr.getDescription());
          newReq.getObjectAttribute().setId(mr.getId());
          newReq.getObjectAttribute().setIid(mr.getIid());
          newReq.getObjectAttribute().setMergeStatus(mr.getState());
          newReq.getObjectAttribute().setSourceBranch(mr.getSourceBranch());
          newReq.getObjectAttribute().setSourceProjectId(mr.getSourceProjectId());
          newReq.getObjectAttribute().setTargetBranch(mr.getTargetBranch());
          newReq.getObjectAttribute().setTargetProjectId(projectId);
          newReq.getObjectAttribute().setTitle(mr.getTitle());
          newReq.getObjectAttribute().setLastCommit(lastCommit);

          Authentication old = SecurityContextHolder.getContext().getAuthentication();
          SecurityContextHolder.getContext().setAuthentication(ACL.SYSTEM);
          try {
            trigger.onPost(newReq);
          } finally {
            SecurityContextHolder.getContext().setAuthentication(old);
          }
        }
      }
    } catch (Exception e) {
      LOGGER.warning(
          "failed to communicate with gitlab server to determine is this is an update for a merge request: "
              + e.getMessage());
      e.printStackTrace();
    }
  }