我正在尝试使用Groovy编写Jenkins中插件的配置脚本。其中一些插件,比如GitHub拉出请求生成器插件,不会公开任何简单的方法来配置Jenkins web接口之外的插件(它使用一个名为Stapler的库将HTML绑定到对象实例)。
‘`Stabl’
插件实现了一个需要StaplerRequest接口实现的StaplerRequest方法。为请求,所以我想我在Groovy中也会这么做。
但是,我无法说服Groovy将null或NullObject作为configure(request, formData)的第一个参数。
Groovy能做到这一点吗?
import org.codehaus.groovy.runtime.NullObject
def descriptor = org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl
net.sf.json.JSONObject jsonObject = new net.sf.json.JSONObject() as net.sf.json.JSONObject;
org.kohsuke.stapler.StaplerRequest stapler = NullObject as org.kohsuke.stapler.StaplerRequest
println ("Is it a stapler request? ${stapler instanceof org.kohsuke.stapler.StaplerRequest}.")
println descriptor.configure(stapler, jsonObject)输出:
Is it a stapler request? true.
groovy.lang.MissingMethodException: No signature of method: static org.jenkinsci.plugins.ghprb.GhprbTrigger$DescriptorImpl.configure() is applicable for argument types: (Class_delegateProxy, net.sf.json.JSONObject) values: [Class_delegateProxy@31433174, [:]]
Possible solutions: configure(org.kohsuke.stapler.StaplerRequest, net.sf.json.JSONObject), configure(org.kohsuke.stapler.StaplerRequest, net.sf.json.JSONObject), configure(org.kohsuke.stapler.StaplerRequest)更新
因此,Jenkins的API可能非常复杂,有时也不太明显。我对静态DescriptorImpl类型的引用是错误的。我仍然想知道为什么上面的这些失败了。
def descriptor = jenkins.model.Jenkins.getInstance().getDescriptorByType(org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl.class)
//def plugin = org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl
net.sf.json.JSONObject jsonObject = new net.sf.json.JSONObject();
org.kohsuke.stapler.StaplerRequest stapler = null
println ("Is it a stapler request? ${stapler instanceof org.kohsuke.stapler.StaplerRequest}.")
jsonObject.put("serverAPIUrl", "https://api.github.com");
jsonObject.put("username", "user");
jsonObject.put("password", "1111");
jsonObject.put("accessToken", "accessToken");
jsonObject.put("adminlist", "user");
jsonObject.put("allowMembersOfWhitelistedOrgsAsAdmin", "false");
jsonObject.put("publishedURL", "");
jsonObject.put("requestForTestingPhrase", "test this");
jsonObject.put("whitelistPhrase", "");
jsonObject.put("okToTestPhrase", "ok to test");
jsonObject.put("retestPhrase", "retest this please");
jsonObject.put("skipBuildPhrase", "[skip ci]");
jsonObject.put("cron", "*/1 * * * *");
jsonObject.put("useComments", "true");
jsonObject.put("logExcerptLines", "0");
jsonObject.put("unstableAs", "");
jsonObject.put("testMode", "true");
jsonObject.put("autoCloseFailedPullRequests", "false");
jsonObject.put("msgSuccess", "Success");
jsonObject.put("msgFailure", "Failure");
println descriptor.configure(stapler, jsonObject)输出
Is it a stapler request? false.
true发布于 2014-09-16 03:01:29
在第一个示例中,您尝试将一个实际的Class实例(而不是null )传递给第一个方法。第二个例子是正确的,只要将null赋值给订书机变量,它就会被称为精细。至于“这是订书机请求”,任何东西的空实例都会返回false。
另一个问题是org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl是一个类实例,但是jenkins.model.Jenkins.getInstance().getDescriptorByType(org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl.class)返回org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl.的实例
https://stackoverflow.com/questions/25859685
复制相似问题