【发布时间】:2014-09-16 02:14:42
【问题描述】:
我正在尝试使用 Groovy 在 Jenkins 中编写插件配置脚本。一些插件,比如GitHub Pull Request Builder plugin 没有公开任何直接的方式来配置 Jenkins Web 界面之外的插件(它使用一个名为 Stapler 的库将 HTML 表单绑定到对象实例)。
“马厩”
插件实现了一个org.jenkinsci.plugins.ghprb.GhprbTrigger.DescriptorImpl#configure 方法,该方法需要一个StaplerRequest 接口实现。 test cases pass null for the request,所以我想我会在 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
【问题讨论】: