创建一个挂钩到全局构建状态的Jenkins插件并具有全局可重复配置?

时间:2012-02-20 16:57:10

标签: java jenkins jenkins-plugins

我正在尝试创建一个简单的Jenkins插件,该插件将与IP电源板设备进行通信,但我遇到了一些问题。我已经对文档进行了挖掘,但它看起来似乎很少。

我的理论是创建一个带有关联描述符的发布者。描述符将通过bean对象(params)以可重复的形式呈现,然后使其可用于记录器。

实际上似乎发生了:

Caused by: net.sf.json.JSONException: Error while setting property=data type interface java.util.List
at net.sf.json.JSONObject.toBean(JSONObject.java:577)
at net.sf.json.JSONObject.toBean(JSONObject.java:383)
at net.sf.json.JSONObject.toBean(JSONObject.java:250)
at hudson.tools.JDKInstaller$JDKList.toList(JDKInstaller.java:612)
at hudson.tools.JDKInstaller$DescriptorImpl.getInstallableJDKs(JDKInstaller.java:588)
... 237 more
Caused by: java.lang.NullPointerException
at net.sf.json.JSONObject$MethodProperty.isWritable(JSONObject.java:311)
at net.sf.json.JSONObject.toBean(JSONObject.java:429)
... 241 more

这是params类

package com.mb;


import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

import java.io.Serializable;

@ExportedBean
public class PlugParamters extends AbstractDescribableImpl<PlugParamters> implements Serializable {

private final String buildState;
private final String plugId;
private final int plugAction;

@DataBoundConstructor
public PlugParamters(String buildState, String plugId, int plugAction) {
this.buildState = buildState;
this.plugId = plugId;
this.plugAction = plugAction;
}

@Exported
public String getBuildState() {
return buildState;
}

@Exported
public String getPlugId() {
return plugId;
}

@Exported
public int getPlugAction() {
return plugAction;
}

@Extension
public static class DescriptorImpl extends Descriptor<PlugParamters> {

public DescriptorImpl() {
    super(PlugParamters.class);
}

@Override
public String getDisplayName() {
    return "";
}
}

}

最后:

package com.mb;


import hudson.Extension;
import hudson.Launcher;
import hudson.model.*;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import hudson.util.FormValidation;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class IPPowerPublisher extends Recorder {

public IPPowerPublisher() {
}

@Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {

return true;
}

@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}


public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}

private boolean oneOrMoreBuildsFailing(List<Project> projects) {

boolean oneOrMoreBuildsFailing = false;
for (Project project : projects) {
    Result projectLatestBuildResult = project.getLastBuild().getResult();

    if (!(projectLatestBuildResult.isBetterOrEqualTo(Result.SUCCESS) || !projectLatestBuildResult.equals(Result.NOT_BUILT))) {
        oneOrMoreBuildsFailing = true;
        break;
    }
}

return oneOrMoreBuildsFailing;
}



@Extension
public final static class DescriptorImpl extends BuildStepDescriptor<Publisher> {

public List<PlugParamters> getPlugParamters() {
    return plugParamters;
}

public void setPlugParamters(List<PlugParamters> plugParamters) {
    this.plugParamters = plugParamters;
}

private List<PlugParamters> plugParamters = new ArrayList<PlugParamters>();
private String host = "";

public FormValidation doCheckHost(@QueryParameter String value) throws IOException, ServletException {

    if (value != null && value.length() == 0)
        return FormValidation.error("Please set a host name or IP address");

    return FormValidation.ok();
}

@Override
public String getDisplayName() {
    return "Continuous Integration Feedback";
}

@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws hudson.model.Descriptor.FormException {
    host = formData.getString("host");

    //plugParamters = req.bindParametersToList(PlugParamters.class, "");

    save();
    return super.configure(req, formData);
}

public String getHost() {
    return host;
}

public void setHost(String host) {
    this.host = host;
}


@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
    return true;
}
}

}

0 个答案:

没有答案