Search in sources :

Example 1 with MavenModuleSet

use of hudson.maven.MavenModuleSet in project evosuite by EvoSuite.

the class Git method commit.

@Override
public int commit(AbstractMavenProject<?, ?> project, AbstractBuild<?, ?> build, BuildListener listener, String branchName, String ctgBestsDir) {
    try {
        listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Commiting new test cases");
        Set<String> branches = this.getBranches();
        if (!branches.contains(branchName)) {
            // create a new branch called "evosuite-tests" to commit and
            // push the new generated test suites
            listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "There is no branch called " + branchName);
            this.gitClient.branch(branchName);
        }
        this.gitClient.setAuthor("jenkins", "jenkins@localhost.com");
        this.gitClient.setCommitter("jenkins", "jenkins@localhost.com");
        this.gitClient.checkoutBranch(branchName, "HEAD");
        EnvVars env = build.getEnvironment(listener);
        env.overrideAll(build.getBuildVariables());
        int number_of_files_committed = 0;
        try {
            MavenModuleSet prj = (MavenModuleSet) project;
            // parse list of new and modified files per module
            StringBuilder filesToBeCommitted = new StringBuilder();
            for (MavenModule module : prj.getModules()) {
                String status = ((CliGitAPIImpl) this.gitClient).launchCommand("ls-files", "--deleted", "--modified", "--others", (module.getRelativePath().isEmpty() ? "" : module.getRelativePath() + File.separator) + ctgBestsDir);
                listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Status (" + status.length() + "):\n" + status);
                filesToBeCommitted.append(status);
            }
            String s_filesToBeCommitted = filesToBeCommitted.toString();
            if (s_filesToBeCommitted.isEmpty()) {
                listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Nothing to commit");
                return 0;
            }
            for (String toCommit : s_filesToBeCommitted.split("\\R")) {
                String filePath = build.getWorkspace().getRemote() + File.separator + toCommit;
                if (new File(filePath).exists()) {
                    listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "adding: " + filePath);
                    this.gitClient.add(filePath);
                    number_of_files_committed++;
                } else {
                    listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "File '" + filePath + "' reported by git status command does not exist");
                }
            }
        } catch (ClassCastException e) {
            // FIXME when building a project remotely, we just have access to a GitClient of type
            // RemoteGitImpl, which cannot be cast to CliGitAPIImpl. and therefore, we cannot use
            // launchCommand method. as a workaround, we can simple add all files under .evosuite/best-tests
            // and hopefully git will take care of the rest. GitClient already supports the creation
            // of a new branch, checkout some branch, add files to be committed, commmit, push, etc.
            // there must be a way of getting the list of modified / new / deleted files just using
            // GitClient, however we still do not know how to get that.
            listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + e.getMessage() + "\nTrying a different approach!");
            FilePath[] filesToCommit = build.getWorkspace().list(build.getEnvironment(listener).expand("**" + File.separator + ctgBestsDir + File.separator + "**" + File.separator + "*"));
            if (filesToCommit.length == 0) {
                listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Nothing to commit");
                return number_of_files_committed;
            }
            number_of_files_committed = filesToCommit.length;
            for (FilePath fileToCommit : filesToCommit) {
                listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "adding: " + fileToCommit.getRemote());
                this.gitClient.add(fileToCommit.getRemote());
            }
        }
        // commit
        String commit_msg = SCM.COMMIT_MSG_PREFIX + build.getProject().getName().replace(" ", "_") + "-" + build.getNumber();
        listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + commit_msg);
        this.gitClient.commit(commit_msg);
        return number_of_files_committed;
    } catch (InterruptedException | IOException e) {
        listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Commit failed " + e.getMessage());
        e.printStackTrace();
        this.rollback(build, listener);
        return -1;
    }
}
Also used : FilePath(hudson.FilePath) MavenModule(hudson.maven.MavenModule) CliGitAPIImpl(org.jenkinsci.plugins.gitclient.CliGitAPIImpl) IOException(java.io.IOException) EnvVars(hudson.EnvVars) File(java.io.File) MavenModuleSet(hudson.maven.MavenModuleSet)

Example 2 with MavenModuleSet

use of hudson.maven.MavenModuleSet in project evosuite by EvoSuite.

the class Mercurial method commit.

@Override
public int commit(AbstractMavenProject<?, ?> project, AbstractBuild<?, ?> build, BuildListener listener, String branchName, String ctgBestsDir) {
    try {
        listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Commiting new test cases");
        Set<String> branches = this.getBranches(build.getWorkspace(), listener);
        if (!branches.contains(branchName)) {
            // create a new branch called "evosuite-tests" to commit and
            // push the new generated test suites
            listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "There is no branch called " + branchName);
            if (this.hgClient.run("branch", branchName).pwd(build.getWorkspace()).join() != 0) {
                listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Unable to create a new branch called " + branchName);
                return -1;
            }
        }
        // switch to EVOSUITE_BRANCH
        if (this.hgClient.run("update", branchName).pwd(build.getWorkspace()).join() != 0) {
            listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Unable to switch to branch " + branchName);
            return -1;
        }
        // start adding all removed files to commit
        if (this.hgClient.run("remove", "--after").pwd(build.getWorkspace()).join() != 0) {
            this.rollback(build, listener);
            return -1;
        }
        MavenModuleSet prj = (MavenModuleSet) project;
        // parse list of new and modified files
        int number_of_files_committed = 0;
        for (MavenModule module : prj.getModules()) {
            for (String file : this.parseStatus(this.hgClient.popen(build.getWorkspace(), listener, true, new ArgumentListBuilder("status")), (module.getRelativePath().isEmpty() ? "" : module.getRelativePath() + File.separator) + ctgBestsDir)) {
                if (this.hgClient.run("add", file).pwd(build.getWorkspace()).join() != 0) {
                    this.rollback(build, listener);
                    return -1;
                }
                number_of_files_committed++;
            }
        }
        if (number_of_files_committed == 0) {
            listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Nothing to commit");
            return 0;
        }
        // commit
        String commit_msg = SCM.COMMIT_MSG_PREFIX + build.getProject().getName().replace(" ", "_") + "-" + build.getNumber();
        listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + commit_msg);
        if (this.hgClient.run("commit", "--message", commit_msg).pwd(build.getWorkspace()).join() != 0) {
            this.rollback(build, listener);
            return -1;
        }
        return number_of_files_committed++;
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
        return -1;
    }
}
Also used : MavenModule(hudson.maven.MavenModule) ArgumentListBuilder(hudson.util.ArgumentListBuilder) IOException(java.io.IOException) MavenModuleSet(hudson.maven.MavenModuleSet)

Example 3 with MavenModuleSet

use of hudson.maven.MavenModuleSet in project sonar-scanner-jenkins by SonarSource.

the class SonarPublisherBranchSlicerTest method changeJobAdditionalProperties.

@Test
public void changeJobAdditionalProperties() throws IOException {
    final MavenModuleSet project = j.jenkins.createProject(MavenModuleSet.class, "random-name");
    project.getPublishersList().add(new SonarPublisher("MySonar", null, null, null, null, null, null, null, null, null, false));
    final SonarPublisherBranchSlicer.SonarPublisherBranchSlicerSpec branchSpec = new SonarPublisherBranchSlicer.SonarPublisherBranchSlicerSpec();
    final List<String> values = branchSpec.getValues(project);
    assertThat(values.get(0)).isEqualTo("(Empty)");
    final List<String> newValues = new ArrayList<String>();
    newValues.add("branchName");
    branchSpec.setValues(project, newValues);
    final SonarPublisher publisher = project.getPublishersList().get(SonarPublisher.class);
    assertThat(publisher.getBranch()).isEqualTo("branchName");
}
Also used : SonarPublisher(hudson.plugins.sonar.SonarPublisher) ArrayList(java.util.ArrayList) MavenModuleSet(hudson.maven.MavenModuleSet) Test(org.junit.Test)

Example 4 with MavenModuleSet

use of hudson.maven.MavenModuleSet in project sonar-scanner-jenkins by SonarSource.

the class SonarPublisherJdkSlicerTest method changeJobAdditionalProperties.

@Test
public void changeJobAdditionalProperties() throws Exception {
    final MavenModuleSet project = j.jenkins.createProject(MavenModuleSet.class, "random-name");
    final SonarPublisher mySonar = new SonarPublisher("MySonar", null, null, null, null, null, null, "1.7", null, null, false);
    project.getPublishersList().add(mySonar);
    final SonarPublisherJdkSlicer.SonarPublisherJdkSlicerSpec spec = new SonarPublisherJdkSlicer.SonarPublisherJdkSlicerSpec();
    final List<String> values = spec.getValues(project);
    assertThat(values.get(0)).isEqualTo("1.7");
    final List<String> newValues = new ArrayList<String>();
    newValues.add("1.7");
    spec.setValues(project, newValues);
    assertThat(mySonar.getJdkName()).isEqualTo("1.7");
}
Also used : SonarPublisher(hudson.plugins.sonar.SonarPublisher) ArrayList(java.util.ArrayList) MavenModuleSet(hudson.maven.MavenModuleSet) Test(org.junit.Test)

Example 5 with MavenModuleSet

use of hudson.maven.MavenModuleSet in project sonar-scanner-jenkins by SonarSource.

the class SonarPublisher method getPomName.

private String getPomName(AbstractBuild<?, ?> build, BuildListener listener) throws IOException, InterruptedException {
    String pomName;
    MavenModuleSet mavenModuleProject = getMavenProject(build);
    if (mavenModuleProject != null) {
        EnvVars envVars = build.getEnvironment(listener);
        pomName = mavenModuleProject.getRootPOM(envVars);
    } else {
        pomName = getRootPom();
    }
    if (StringUtils.isEmpty(pomName)) {
        pomName = "pom.xml";
    }
    return pomName;
}
Also used : EnvVars(hudson.EnvVars) MavenModuleSet(hudson.maven.MavenModuleSet)

Aggregations

MavenModuleSet (hudson.maven.MavenModuleSet)17 Test (org.junit.Test)10 SonarPublisher (hudson.plugins.sonar.SonarPublisher)8 EnvVars (hudson.EnvVars)4 ArrayList (java.util.ArrayList)4 MavenModule (hudson.maven.MavenModule)3 IOException (java.io.IOException)3 FilePath (hudson.FilePath)2 PerJobLocalRepositoryLocator (hudson.maven.local_repo.PerJobLocalRepositoryLocator)2 ImmutableList (com.google.common.collect.ImmutableList)1 DefaultLocalRepositoryLocator (hudson.maven.local_repo.DefaultLocalRepositoryLocator)1 LocalRepositoryLocator (hudson.maven.local_repo.LocalRepositoryLocator)1 VirtualChannel (hudson.remoting.VirtualChannel)1 ArgumentListBuilder (hudson.util.ArgumentListBuilder)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 List (java.util.List)1 GlobalSettingsProvider (jenkins.mvn.GlobalSettingsProvider)1 SettingsProvider (jenkins.mvn.SettingsProvider)1