<commit_before>package hudson.plugins.starteam;

import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.scm.ChangeLogParser;
import hudson.scm.SCM;
import hudson.scm.SCMDescriptor;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import net.sf.json.JSONObject;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;


/**
 * StarTeam SCM plugin for Hudson.
 * Add support for change log and synchronization between starteam repository and hudson's workspace.
 * Add support for change log creation.
 * Refactoring to use Extension annotation and to remove use of deprecated API.
 * 
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
public class StarTeamSCM extends SCM {

	/**
	 * Singleton descriptor.
	 */
	@Extension
	public static final StarTeamSCMDescriptorImpl DESCRIPTOR = new StarTeamSCMDescriptorImpl();

	private final String user;
	private final String passwd;
	private final String projectname;
	private final String viewname;
	private final String foldername;
	private final String hostname;
	private final int port;

	/**
	 * 
	 * default stapler constructor.
	 * 
	 * @param hostname
	 *            starteam host name.
	 * @param port
	 *            starteam port name
	 * @param projectname
	 *            name of the project
	 * @param viewname
	 *            name of the view
	 * @param foldername
	 *            parent folder name.
	 * @param username
	 *            the user name required to connect to starteam's server
	 * @param password
	 *            password required to connect to starteam's server
	 *
	 */
	@DataBoundConstructor
	public StarTeamSCM(String hostname, int port, String projectname,
			String viewname, String foldername, String username, String password) {
		this.hostname = hostname;
		this.port = port;
		this.projectname = projectname;
		this.viewname = viewname;
		this.foldername = foldername;
		this.user = username;
		this.passwd = password;
	}

	/*
	 * @see hudson.scm.SCM#checkout(hudson.model.AbstractBuild, hudson.Launcher,
	 *      hudson.FilePath, hudson.model.BuildListener, java.io.File)
	 */
	@Override
	public boolean checkout(AbstractBuild build, Launcher launcher,
			FilePath workspace, BuildListener listener, File changelogFile)
			throws IOException, InterruptedException {
		boolean status = false;

		Date previousBuildDate = null;
		if ( build.getPreviousBuild() != null)	{
		    previousBuildDate = build.getPreviousBuild().getTimestamp().getTime();
		}
		Date currentBuildDate = build.getTimestamp().getTime();
		
		//create a FilePath to be able to create changelog file on a remote computer.
		FilePath changeLogFilePath = new FilePath( changelogFile ) ;
		
		// Create an actor to do the checkout, possibly on a remote machine
		StarTeamCheckoutActor co_actor = new StarTeamCheckoutActor(hostname,
				port, user, passwd, projectname, viewname, foldername, previousBuildDate, currentBuildDate, changeLogFilePath, listener);
		if (workspace.act(co_actor)) {
			// change log is written during checkout (only one pass for
			// comparison)
			status = true;
		} else {
			listener.getLogger().println("StarTeam checkout failed");
			status = false;
		}
		return status;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see hudson.scm.SCM#createChangeLogParser()
	 */
	@Override
	public ChangeLogParser createChangeLogParser() {
		return new StarTeamChangeLogParser();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see hudson.scm.SCM#getDescriptor()
	 */
	@Override
	public StarTeamSCMDescriptorImpl getDescriptor() {
		return DESCRIPTOR;
//		return (StarTeamSCMDescriptorImpl)super.getDescriptor();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see hudson.scm.SCM#pollChanges(hudson.model.AbstractProject,
	 *      hudson.Launcher, hudson.FilePath, hudson.model.TaskListener)
	 */
	@Override
	public boolean pollChanges(final AbstractProject proj,
			final Launcher launcher, final FilePath workspace,
			final TaskListener listener) throws IOException,
			InterruptedException {
		boolean status = false;
		Run run = proj.getLastBuild();
		Date sinceDate = null;
		if ( run != null) {
		    sinceDate= run.getTimestamp().getTime();
		}
		Date currentServerDate = new Date();
		// Create an actor to do the polling, possibly on a remote machine
		StarTeamPollingActor p_actor = new StarTeamPollingActor(hostname, port,
				user, passwd, projectname, viewname, foldername,
				sinceDate, currentServerDate,
				listener);
		if (workspace.act(p_actor)) {
			status = true;
		} else {
			listener.getLogger().println("StarTeam polling failed");
		}
		return status;
	}

	/**
	 * Descriptor class for the SCM class.
	 * 
	 * @author <NAME> <<EMAIL>>
	 * 
	 */
	@Extension
	public static final class StarTeamSCMDescriptorImpl extends SCMDescriptor<StarTeamSCM> {

		private final Collection<StarTeamSCM> scms = new ArrayList<StarTeamSCM>();

		public StarTeamSCMDescriptorImpl() {
			super(StarTeamSCM.class, null);<commit_msg>cleanup<commit_after>package hudson.plugins.starteam;

import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.scm.ChangeLogParser;
import hudson.scm.SCM;
import hudson.scm.SCMDescriptor;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import net.sf.json.JSONObject;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;


/**
 * StarTeam SCM plugin for Hudson.
 * Add support for change log and synchronization between starteam repository and hudson's workspace.
 * Add support for change log creation.
 * Refactoring to use Extension annotation and to remove use of deprecated API.
 * 
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
public class StarTeamSCM extends SCM {

	/**
	 * Singleton descriptor.
	 */
	@Extension
	public static final StarTeamSCMDescriptorImpl DESCRIPTOR = new StarTeamSCMDescriptorImpl();

	private final String user;
	private final String passwd;
	private final String projectname;
	private final String viewname;
	private final String foldername;
	private final String hostname;
	private final int port;

	/**
	 * 
	 * default stapler constructor.
	 * 
	 * @param hostname
	 *            starteam host name.
	 * @param port
	 *            starteam port name
	 * @param projectname
	 *            name of the project
	 * @param viewname
	 *            name of the view
	 * @param foldername
	 *            parent folder name.
	 * @param username
	 *            the user name required to connect to starteam's server
	 * @param password
	 *            password required to connect to starteam's server
	 *
	 */
	@DataBoundConstructor
	public StarTeamSCM(String hostname, int port, String projectname,
			String viewname, String foldername, String username, String password) {
		this.hostname = hostname;
		this.port = port;
		this.projectname = projectname;
		this.viewname = viewname;
		this.foldername = foldername;
		this.user = username;
		this.passwd = password;
	}

	/*
	 * @see hudson.scm.SCM#checkout(hudson.model.AbstractBuild, hudson.Launcher,
	 *      hudson.FilePath, hudson.model.BuildListener, java.io.File)
	 */
	@Override
	public boolean checkout(AbstractBuild build, Launcher launcher,
			FilePath workspace, BuildListener listener, File changelogFile)
			throws IOException, InterruptedException {
		boolean status = false;

		Date previousBuildDate = null;
		if ( build.getPreviousBuild() != null)	{
		    previousBuildDate = build.getPreviousBuild().getTimestamp().getTime();
		}
		Date currentBuildDate = build.getTimestamp().getTime();
		
		//create a FilePath to be able to create changelog file on a remote computer.
		FilePath changeLogFilePath = new FilePath( changelogFile ) ;
		
		// Create an actor to do the checkout, possibly on a remote machine
		StarTeamCheckoutActor co_actor = new StarTeamCheckoutActor(hostname,
				port, user, passwd, projectname, viewname, foldername, previousBuildDate, currentBuildDate, changeLogFilePath, listener);
		if (workspace.act(co_actor)) {
			// change log is written during checkout (only one pass for
			// comparison)
			status = true;
		} else {
			listener.getLogger().println("StarTeam checkout failed");
			status = false;
		}
		return status;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see hudson.scm.SCM#createChangeLogParser()
	 */
	@Override
	public ChangeLogParser createChangeLogParser() {
		return new StarTeamChangeLogParser();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see hudson.scm.SCM#getDescriptor()
	 */
	@Override
	public StarTeamSCMDescriptorImpl getDescriptor() {
		return DESCRIPTOR;
//		return (StarTeamSCMDescriptorImpl)super.getDescriptor();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see hudson.scm.SCM#pollChanges(hudson.model.AbstractProject,
	 *      hudson.Launcher, hudson.FilePath, hudson.model.TaskListener)
	 */
	@Override
	public boolean pollChanges(final AbstractProject proj,
			final Launcher launcher, final FilePath workspace,
			final TaskListener listener) throws IOException,
			InterruptedException {
		boolean status = false;
		Run run = proj.getLastBuild();
		Date sinceDate = null;
		if ( run != null) {
		    sinceDate= run.getTimestamp().getTime();
		}
		Date currentServerDate = new Date();
		// Create an actor to do the polling, possibly on a remote machine
		StarTeamPollingActor p_actor = new StarTeamPollingActor(hostname, port,
				user, passwd, projectname, viewname, foldername,
				sinceDate, currentServerDate,
				listener);
		if (workspace.act(p_actor)) {
			status = true;
		} else {
			listener.getLogger().println("StarTeam polling failed");
		}
		return status;
	}

	/**
	 * Descriptor class for the SCM class.
	 * 
	 * @author <NAME> <<EMAIL>>
	 * 
	 */
	public static final class StarTeamSCMDescriptorImpl extends SCMDescriptor<StarTeamSCM> {

		private final Collection<StarTeamSCM> scms = new ArrayList<StarTeamSCM>();

		public StarTeamSCMDescriptorImpl() {
			super(StarTeamSCM.class, null);
{'fraction_non_alphanumeric': 0.09447983014861996, 'fraction_numerical': 0.0, 'mean_word_length': 3.8767058823529412, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5901057', 'n_tokens_mistral': 3405, 'n_tokens_neox': 3024, 'n_words': 866}