<commit_before>package org.jenkinsci.plugins.koji;
import hudson.Launcher;
import hudson.Extension;
import hudson.util.FormValidation;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.AbstractProject;
import hudson.tasks.Builder;
import hudson.tasks.BuildStepDescriptor;
import net.sf.json.JSONObject;
import org.apache.xmlrpc.XmlRpcException;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.QueryParameter;

import javax.servlet.ServletException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;


/**
 * Sample {@link Builder}.
 *
 * <p>
 * When the user configures the project and enables this builder,
 * {@link DescriptorImpl#newInstance(StaplerRequest)} is invoked
 * and a new {@link KojiBuilder} is created. The created
 * instance is persisted to the project configuration XML by using
 * XStream, so this allows you to use instance fields (like {@link #kojiBuild})
 * to remember the configuration.
 *
 * <p>
 * When a build is performed, the {@link #perform(AbstractBuild, Launcher, BuildListener)}
 * method will be invoked.
 *
 * @author <NAME>
 */
public class KojiBuilder extends Builder {

    private final String kojiBuild;
    private BuildListener listener;
    private XmlRpcClient koji;

    // Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
    @DataBoundConstructor
    public KojiBuilder(String kojiBuild) {
        this.kojiBuild = kojiBuild;
    }

    /**
     * We'll use this from the <tt>config.jelly</tt>.
     */
    public String getKojiBuild() {
        return kojiBuild;
    }

    @Override
    public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {
        this.listener = listener;

        printDebugInfo();

        XmlRpcClient koji = connect();
        this.koji = koji;

        sayHello();


        return true;
    }

    private void sayHello() {
        try {
            Object result = koji.execute("hello", new Object[] {"Hello"});
            listener.getLogger().println("Jenkins-Koji Plugin: Hello Koji server running at " + getDescriptor().getKojiInstanceURL());
            listener.getLogger().println("Koji: " + result);
        } catch (XmlRpcException e) {
            e.printStackTrace();
        }
    }

    private XmlRpcClient connect() {
        String kojiInstanceURL = getDescriptor().getKojiInstanceURL();
        XmlRpcClient koji = new XmlRpcClient();
        koji.setTransportFactory(new XmlRpcCommonsTransportFactory(koji));
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();

        try {
          config.setServerURL(new URL(kojiInstanceURL));
        } catch (MalformedURLException e) {
          e.printStackTrace();
        }

        koji.setConfig(config);

        return koji;
    }

    private void printDebugInfo() {
        listener.getLogger().println("This is the selected Koji Instance: " + getDescriptor().getKojiInstanceURL());
        listener.getLogger().println("This is the selected Koji Build: " + kojiBuild);
    }

    // Overridden for better type safety.
    // If your plugin doesn't really define any property on Descriptor,
    // you don't have to do this.
    @Override
    public DescriptorImpl getDescriptor() {
        return (DescriptorImpl)super.getDescriptor();
    }

    /**
     * Descriptor for {@link KojiBuilder}. Used as a singleton.
     * The class is marked as public so that it can be accessed from views.
     *
     * <p><commit_msg>Use fixed TypeFactory correctly handling None XML-RPC data type<commit_after>package org.jenkinsci.plugins.koji;
import hudson.Launcher;
import hudson.Extension;
import hudson.util.FormValidation;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.AbstractProject;
import hudson.tasks.Builder;
import hudson.tasks.BuildStepDescriptor;
import net.sf.json.JSONObject;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.MyTypeFactory;
import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.QueryParameter;

import javax.servlet.ServletException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;


/**
 * Sample {@link Builder}.
 *
 * <p>
 * When the user configures the project and enables this builder,
 * {@link DescriptorImpl#newInstance(StaplerRequest)} is invoked
 * and a new {@link KojiBuilder} is created. The created
 * instance is persisted to the project configuration XML by using
 * XStream, so this allows you to use instance fields (like {@link #kojiBuild})
 * to remember the configuration.
 *
 * <p>
 * When a build is performed, the {@link #perform(AbstractBuild, Launcher, BuildListener)}
 * method will be invoked.
 *
 * @author <NAME>
 */
public class KojiBuilder extends Builder {

    private final String kojiBuild;
    private BuildListener listener;
    private XmlRpcClient koji;

    // Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
    @DataBoundConstructor
    public KojiBuilder(String kojiBuild) {
        this.kojiBuild = kojiBuild;
    }

    /**
     * We'll use this from the <tt>config.jelly</tt>.
     */
    public String getKojiBuild() {
        return kojiBuild;
    }

    @Override
    public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {
        this.listener = listener;

        printDebugInfo();

        XmlRpcClient koji = connect();
        this.koji = koji;

        sayHello();


        return true;
    }

    private void sayHello() {
        try {
            Object result = koji.execute("hello", new Object[] {"Hello"});
            listener.getLogger().println("Jenkins-Koji Plugin: Hello Koji server running at " + getDescriptor().getKojiInstanceURL());
            listener.getLogger().println("Koji: " + result);
        } catch (XmlRpcException e) {
            e.printStackTrace();
        }
    }

    private XmlRpcClient connect() {
        String kojiInstanceURL = getDescriptor().getKojiInstanceURL();
        XmlRpcClient koji = new XmlRpcClient();
        koji.setTransportFactory(new XmlRpcCommonsTransportFactory(koji));
        koji.setTypeFactory(new MyTypeFactory(koji));
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setEnabledForExtensions(true);
        config.setEnabledForExceptions(true);

        try {
          config.setServerURL(new URL(kojiInstanceURL));
        } catch (MalformedURLException e) {
          e.printStackTrace();
        }

        koji.setConfig(config);

        return koji;
    }

    private void printDebugInfo() {
        listener.getLogger().println("This is the selected Koji Instance: " + getDescriptor().getKojiInstanceURL());
        listener.getLogger().println("This is the selected Koji Build: " + kojiBuild);
    }

    // Overridden for better type safety.
    // If your plugin doesn't really define any property on Descriptor,
    // you don't have to do this.
    @Override
    public DescriptorImpl getDescriptor() {
        return (DescriptorImpl)super.getDescriptor();
    }

    /**
     * Descriptor for {@link KojiBuilder}. Used as a singleton.
     * The class is marked as public so that it can be accessed from views.
     *
     * <p>
{'fraction_non_alphanumeric': 0.09669538382409669, 'fraction_numerical': 0.0, 'mean_word_length': 3.2948647156267254, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 9}, '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': '5524379', 'n_tokens_mistral': 2246, 'n_tokens_neox': 2145, 'n_words': 597}