Search in sources :

Example 1 with CliGitAPIImpl

use of org.jenkinsci.plugins.gitclient.CliGitAPIImpl 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)

Aggregations

EnvVars (hudson.EnvVars)1 FilePath (hudson.FilePath)1 MavenModule (hudson.maven.MavenModule)1 MavenModuleSet (hudson.maven.MavenModuleSet)1 File (java.io.File)1 IOException (java.io.IOException)1 CliGitAPIImpl (org.jenkinsci.plugins.gitclient.CliGitAPIImpl)1