Exemple #1
0
  @Override
  public boolean checkout(
      @SuppressWarnings("rawtypes") final AbstractBuild build,
      final Launcher launcher,
      final FilePath workspace,
      final BuildListener listener,
      final File changelogFile)
      throws IOException, InterruptedException {

    FilePath repoDir;
    if (destinationDir != null) {
      repoDir = workspace.child(destinationDir);
      if (!repoDir.isDirectory()) {
        repoDir.mkdirs();
      }
    } else {
      repoDir = workspace;
    }

    if (!checkoutCode(launcher, repoDir, listener.getLogger())) {
      return false;
    }
    final String manifest = getStaticManifest(launcher, repoDir, listener.getLogger());
    final RevisionState currentState =
        new RevisionState(manifest, manifestBranch, listener.getLogger());
    build.addAction(currentState);
    final RevisionState previousState = getLastState(build.getPreviousBuild());

    ChangeLog.saveChangeLog(currentState, previousState, changelogFile, launcher, repoDir);
    build.addAction(new TagAction(build));
    return true;
  }
Exemple #2
0
  @Override
  protected PollingResult compareRemoteRevisionWith(
      final AbstractProject<?, ?> project,
      final Launcher launcher,
      final FilePath workspace,
      final TaskListener listener,
      final SCMRevisionState baseline)
      throws IOException, InterruptedException {
    SCMRevisionState myBaseline = baseline;
    if (myBaseline == null) {
      // Probably the first build, or possibly an aborted build.
      myBaseline = getLastState(project.getLastBuild());
      if (myBaseline == null) {
        return PollingResult.BUILD_NOW;
      }
    }

    FilePath repoDir;
    if (destinationDir != null) {
      repoDir = workspace.child(destinationDir);
      if (!repoDir.isDirectory()) {
        repoDir.mkdirs();
      }
    } else {
      repoDir = workspace;
    }

    if (!checkoutCode(launcher, repoDir, listener.getLogger())) {
      // Some error occurred, try a build now so it gets logged.
      return new PollingResult(myBaseline, myBaseline, Change.INCOMPARABLE);
    }

    final RevisionState currentState =
        new RevisionState(
            getStaticManifest(launcher, repoDir, listener.getLogger()),
            manifestBranch,
            listener.getLogger());
    final Change change;
    if (currentState.equals(myBaseline)) {
      change = Change.NONE;
    } else {
      change = Change.SIGNIFICANT;
    }
    return new PollingResult(myBaseline, currentState, change);
  }
Exemple #3
0
  @Test
  public void globShouldIgnoreDefaultExcludesByRequest() throws Exception {
    FilePath tmp = new FilePath(tmpRule.getRoot());
    try {
      tmp.child(".gitignore").touch(0);
      FilePath git = tmp.child(".git");
      git.mkdirs();
      git.child("HEAD").touch(0);

      DirScanner glob = new DirScanner.Glob("**/*", null, false);
      MatchingFileVisitor gitdir = new MatchingFileVisitor("HEAD");
      MatchingFileVisitor gitignore = new MatchingFileVisitor(".gitignore");

      glob.scan(new File(tmp.getRemote()), gitdir);
      glob.scan(new File(tmp.getRemote()), gitignore);

      assertTrue(gitdir.found);
      assertTrue(gitignore.found);
    } finally {
      tmp.deleteRecursive();
    }
  }
 public void saveSourcesFrom(FilePath dir) throws IOException, InterruptedException {
   FilePath d = new FilePath(getSourcesDir());
   d.mkdirs();
   dir.copyRecursiveTo(d);
 }
  public Map<String, String> generateContents(AbstractProject<?, ?> project, GitSCM git)
      throws IOException, InterruptedException {

    Map<String, String> paramList = new LinkedHashMap<String, String>();
    // for (AbstractProject<?,?> project :
    // Hudson.getInstance().getItems(AbstractProject.class)) {
    if (project.getSomeWorkspace() == null) {
      this.errorMessage = "noWorkspace";
    }

    EnvVars environment = null;

    try {
      environment = project.getSomeBuildWithWorkspace().getEnvironment(TaskListener.NULL);
    } catch (Exception e) {
    }

    for (RemoteConfig repository : git.getRepositories()) {
      LOGGER.log(
          Level.INFO,
          "generateContents contenttype " + type + " RemoteConfig " + repository.getURIs());
      for (URIish remoteURL : repository.getURIs()) {
        GitClient newgit =
            git.createClient(
                TaskListener.NULL, environment, new Run(project) {}, project.getSomeWorkspace());
        FilePath wsDir = null;
        if (project.getSomeBuildWithWorkspace() != null) {
          wsDir = project.getSomeBuildWithWorkspace().getWorkspace();
          if (wsDir == null || !wsDir.exists()) {
            LOGGER.log(
                Level.WARNING, "generateContents create wsDir " + wsDir + " for " + remoteURL);
            wsDir.mkdirs();
            if (!wsDir.exists()) {
              LOGGER.log(Level.SEVERE, "generateContents wsDir.mkdirs() failed.");
              String errMsg = "!Failed To Create Workspace";
              return Collections.singletonMap(errMsg, errMsg);
            }
            newgit.init();
            newgit.clone(remoteURL.toASCIIString(), "origin", false, null);
            LOGGER.log(Level.INFO, "generateContents clone done");
          }
        } else {
          // probably our first build. We cannot yet fill in any
          // values.
          LOGGER.log(Level.INFO, "getSomeBuildWithWorkspace is null");
          String errMsg = "!No workspace. Please build the project at least once";
          return Collections.singletonMap(errMsg, errMsg);
        }

        long time = -System.currentTimeMillis();
        FetchCommand fetch = newgit.fetch_().from(remoteURL, repository.getFetchRefSpecs());
        fetch.execute();
        LOGGER.finest("Took " + (time + System.currentTimeMillis()) + "ms to fetch");
        if (type.equalsIgnoreCase(PARAMETER_TYPE_REVISION)) {
          List<ObjectId> oid;

          if (this.branch != null && !this.branch.isEmpty()) {
            oid = newgit.revList(this.branch);
          } else {
            oid = newgit.revListAll();
          }

          for (ObjectId noid : oid) {
            Revision r = new Revision(noid);
            paramList.put(r.getSha1String(), prettyRevisionInfo(newgit, r));
          }
        }
        if (type.equalsIgnoreCase(PARAMETER_TYPE_TAG)
            || type.equalsIgnoreCase(PARAMETER_TYPE_TAG_BRANCH)) {

          Set<String> tagSet = newgit.getTagNames(tagFilter);
          ArrayList<String> orderedTagNames;

          if (this.getSortMode().getIsSorting()) {
            orderedTagNames = sortByName(tagSet);
            if (this.getSortMode().getIsDescending()) Collections.reverse(orderedTagNames);
          } else {
            orderedTagNames = new ArrayList<String>(tagSet);
          }

          for (String tagName : orderedTagNames) {
            paramList.put(tagName, tagName);
          }
        }
        if (type.equalsIgnoreCase(PARAMETER_TYPE_BRANCH)
            || type.equalsIgnoreCase(PARAMETER_TYPE_TAG_BRANCH)) {
          time = -System.currentTimeMillis();
          Set<String> branchSet = new HashSet<String>();
          final boolean wildcard = "*".equals(branchfilter);
          for (Branch branch : newgit.getRemoteBranches()) {
            // It'd be nice if we could filter on remote branches via the GitClient,
            // but that's not an option.
            final String branchName = branch.getName();
            if (wildcard || branchName.matches(branchfilter)) {
              branchSet.add(branchName);
            }
          }
          LOGGER.finest("Took " + (time + System.currentTimeMillis()) + "ms to fetch branches");

          time = -System.currentTimeMillis();
          List<String> orderedBranchNames;
          if (this.getSortMode().getIsSorting()) {
            orderedBranchNames = sortByName(branchSet);
            if (this.getSortMode().getIsDescending()) Collections.reverse(orderedBranchNames);
          } else {
            orderedBranchNames = new ArrayList<String>(branchSet);
          }

          for (String branchName : orderedBranchNames) {
            paramList.put(branchName, branchName);
          }
          LOGGER.finest(
              "Took " + (time + System.currentTimeMillis()) + "ms to sort and add to param list.");
        }
      }
      break;
    }
    return paramList;
  }
  private int archiveArtifacts(Collection<ArtifactDTO> artifacts) {
    int count = 0;

    FilePath workspace = build.getWorkspace();
    // Check if workspace exists.
    if (null == workspace) {
      muxlog.error("Missing workspace to archive artifacts from.");
      // TODO: consider this a failure?
      // build.setResult(Result.FAILURE);
      return count;
    }

    try {
      FilePath archiveDirectory = new FilePath(build.getArtifactsDir());
      archiveDirectory.mkdirs();
      log.debug("Archiving to {}", archiveDirectory);

      VirtualChannel channel = workspace.getChannel();
      for (ArtifactDTO artifact : artifacts) {
        if (null != artifact.getCreatedProject() && maybeIncludePom(artifact)) {
          MavenCoordinatesDTO gav = artifact.getCoordinates();

          // File information for the artifact.
          File file = ArtifactDTOHelper.getFile(artifact);
          // TODO: filter out nulls when generating the artifact list; i.e. at ArtifactRecorder
          if (file != null) {
            // Executing on the master; do not try to canonicalize the path since it could have been
            // defined
            // from a remote file system that is not the same as the masters.  E.g. master being
            // linux,
            // remote being windows.
            // TODO: consider making a FilePath constructor using channel and file and extract the
            // path
            // so that users don't have to think about differences between canonical, absolute, and
            // path.
            FilePath source = new FilePath(channel, file.getPath());

            // Store in archive using a similar format as a Maven repository.
            FilePath target =
                archiveDirectory
                    .child(gav.getGroupId())
                    .child(gav.getArtifactId())
                    .child(gav.getVersion())
                    // Resolve source filename using the same node that created the file.  This
                    // should
                    // ensure that the name portion is properly extracted since the file system is
                    // the same.
                    .child(source.getName());

            if (log.isTraceEnabled()) {
              // Separate calls to get more info when there are failures.
              log.trace("Created path to archive: {} on channel {}", target, target.getChannel());
              log.trace(
                  "Copying FROM {} on channel {} TO {} on channel {}",
                  $(source, source.getChannel(), target, target.getChannel()));
            }

            source.copyTo(target);
            // TODO: catch IOException and continue with rest of artifacts?
            artifact.withArchivedFile(target.getRemote());
            count++;
          } else {
            muxlog.error("Failed to archive Maven 3 artifact {} -" + " unresolved.", gav);
          }
        }
      }
    }
    // No need to stop the build due to these exceptions.
    catch (IOException e) {
      Util.displayIOException(e, listener);
      muxlog.error("Failed to archive Maven 3 artifacts", e);
    } catch (InterruptedException e) {
      muxlog.error("Failed to archive Maven 3 artifacts", e);
    }

    return count;
  }
  /** {@inheritDoc} */
  @Override
  public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
      throws InterruptedException, IOException {
    Result threshold = onlyStable ? Result.SUCCESS : Result.UNSTABLE;
    if (build.getResult().isWorseThan(threshold)) {
      listener
          .getLogger()
          .println(
              "Skipping Cobertura coverage report as build was not "
                  + threshold.toString()
                  + " or better ...");
      return true;
    }

    listener.getLogger().println("[Cobertura] Publishing Cobertura coverage report...");
    final FilePath[] moduleRoots = build.getModuleRoots();
    final boolean multipleModuleRoots = moduleRoots != null && moduleRoots.length > 1;
    final FilePath moduleRoot = multipleModuleRoots ? build.getWorkspace() : build.getModuleRoot();
    final File buildCoberturaDir = build.getRootDir();
    FilePath buildTarget = new FilePath(buildCoberturaDir);

    FilePath[] reports = new FilePath[0];
    try {
      reports = moduleRoot.act(new ParseReportCallable(coberturaReportFile));

      // if the build has failed, then there's not
      // much point in reporting an error
      if (build.getResult().isWorseOrEqualTo(Result.FAILURE) && reports.length == 0) {
        return true;
      }

    } catch (IOException e) {
      Util.displayIOException(e, listener);
      e.printStackTrace(listener.fatalError("Unable to find coverage results"));
      build.setResult(Result.FAILURE);
    }

    if (reports.length == 0) {
      String msg =
          "[Cobertura] No coverage results were found using the pattern '"
              + coberturaReportFile
              + "' relative to '"
              + moduleRoot.getRemote()
              + "'."
              + "  Did you enter a pattern relative to the correct directory?"
              + "  Did you generate the XML report(s) for Cobertura?";
      listener.getLogger().println(msg);
      if (failNoReports) {
        build.setResult(Result.FAILURE);
      } else {
        listener.getLogger().println("[Cobertura] Skipped cobertura reports.");
      }
      return true;
    }

    for (int i = 0; i < reports.length; i++) {
      final FilePath targetPath =
          new FilePath(buildTarget, "coverage" + (i == 0 ? "" : i) + ".xml");
      try {
        reports[i].copyTo(targetPath);
      } catch (IOException e) {
        Util.displayIOException(e, listener);
        e.printStackTrace(
            listener.fatalError(
                "Unable to copy coverage from " + reports[i] + " to " + buildTarget));
        build.setResult(Result.FAILURE);
      }
    }

    listener.getLogger().println("Publishing Cobertura coverage results...");
    Set<String> sourcePaths = new HashSet<String>();
    CoverageResult result = null;
    for (File coberturaXmlReport : getCoberturaReports(build)) {
      try {
        result = CoberturaCoverageParser.parse(coberturaXmlReport, result, sourcePaths);
      } catch (IOException e) {
        Util.displayIOException(e, listener);
        e.printStackTrace(listener.fatalError("Unable to parse " + coberturaXmlReport));
        build.setResult(Result.FAILURE);
      }
    }
    if (result != null) {
      listener.getLogger().println("Cobertura coverage report found.");
      result.setOwner(build);
      final FilePath paintedSourcesPath =
          new FilePath(new File(build.getProject().getRootDir(), "cobertura"));
      paintedSourcesPath.mkdirs();

      if (sourcePaths.contains(".")) {
        sourcePaths.remove(".");
        for (FilePath f : reports) {
          FilePath p = f.getParent();
          if (p != null && p.isDirectory()) {
            sourcePaths.add(p.getRemote());
          }
        }
      }

      SourceCodePainter painter =
          new SourceCodePainter(
              paintedSourcesPath,
              sourcePaths,
              result.getPaintedSources(),
              listener,
              getSourceEncoding());

      moduleRoot.act(painter);

      final CoberturaBuildAction action =
          CoberturaBuildAction.load(
              build,
              result,
              healthyTarget,
              unhealthyTarget,
              getOnlyStable(),
              getFailUnhealthy(),
              getFailUnstable(),
              getAutoUpdateHealth(),
              getAutoUpdateStability());

      build.getActions().add(action);
      Set<CoverageMetric> failingMetrics = failingTarget.getFailingMetrics(result);
      if (!failingMetrics.isEmpty()) {
        listener.getLogger().println("Code coverage enforcement failed for the following metrics:");
        float oldStabilityPercent;
        float setStabilityPercent;
        for (CoverageMetric metric : failingMetrics) {
          oldStabilityPercent = failingTarget.getObservedPercent(result, metric);
          setStabilityPercent = failingTarget.getSetPercent(result, metric);
          listener
              .getLogger()
              .println(
                  "    "
                      + metric.getName()
                      + "'s stability is "
                      + roundDecimalFloat(oldStabilityPercent * 100f)
                      + " and set mininum stability is "
                      + roundDecimalFloat(setStabilityPercent * 100f)
                      + ".");
        }
        if (!getFailUnstable()) {
          listener.getLogger().println("Setting Build to unstable.");
          build.setResult(Result.UNSTABLE);
        } else {
          listener.getLogger().println("Failing build due to unstability.");
          build.setResult(Result.FAILURE);
        }
      }
      if (getFailUnhealthy()) {
        Set<CoverageMetric> unhealthyMetrics = unhealthyTarget.getFailingMetrics(result);
        if (!unhealthyMetrics.isEmpty()) {
          listener.getLogger().println("Unhealthy for the following metrics:");
          float oldHealthyPercent;
          float setHealthyPercent;
          for (CoverageMetric metric : unhealthyMetrics) {
            oldHealthyPercent = unhealthyTarget.getObservedPercent(result, metric);
            setHealthyPercent = unhealthyTarget.getSetPercent(result, metric);
            listener
                .getLogger()
                .println(
                    "    "
                        + metric.getName()
                        + "'s health is "
                        + roundDecimalFloat(oldHealthyPercent * 100f)
                        + " and set minimum health is "
                        + roundDecimalFloat(setHealthyPercent * 100f)
                        + ".");
          }
          listener.getLogger().println("Failing build because it is unhealthy.");
          build.setResult(Result.FAILURE);
        }
      }
      if (build.getResult() == Result.SUCCESS) {
        if (getAutoUpdateHealth()) {
          setNewPercentages(result, true, listener);
        }

        if (getAutoUpdateStability()) {
          setNewPercentages(result, false, listener);
        }
      }
    } else {
      listener
          .getLogger()
          .println(
              "No coverage results were successfully parsed.  Did you generate "
                  + "the XML report(s) for Cobertura?");
      build.setResult(Result.FAILURE);
    }

    return true;
  }