CloudBees 核心:2.249.1.2-滚动
此处的代码位于 Jenkins 共享库中,而该共享库又被其他 Jenkins 共享库调用。
下面是标准 Builder 模式的实现,用于创建自定义 HttpRequest 类的实例。
class HttpRequest {
//Mandatory
private final String url
private final String httpMethod
private final String requestContentType
private final String acceptContentType
//Optional
private final String requestBody
private final List<Integer> validResponseCodes
private HttpRequest(HttpRequestBuilder httpRequestBuilder) {
//Mandatory
this.url = httpRequestBuilder.url
this.httpMethod = httpRequestBuilder.httpMethod
this.requestContentType = httpRequestBuilder.requestContentType
this.acceptContentType = httpRequestBuilder.acceptContentType
//Optional
this.requestBody = httpRequestBuilder.requestBody
this.validResponseCodes = httpRequestBuilder.validResponseCodes
}
static class HttpRequestBuilder {
//Mandatory
private final String url
private final String httpMethod
private final String requestContentType
private final String acceptContentType
//Optional
private String requestBody
private List<Integer> validResponseCodes
HttpRequestBuilder(String url, String httpMethod, String requestContentType, String acceptContentType) {
this.url = url
this.httpMethod = httpMethod
this.requestContentType = requestContentType
this.acceptContentType = acceptContentType
}
HttpRequest build() {
return new HttpRequest(this)
}
/**
*
* @param requestBody A string in a format valid according to the Content-Type.
* @return
*/
HttpRequestBuilder requestBody(String requestBody) {
this.requestBody = requestBody
return this
}
HttpRequestBuilder validResponseCodes(List<Integer> validResponseCodes) {
this.validResponseCodes = validResponseCodes
return this
}
}
String getUrl() {
return url
}
String getHttpMethod() {
return httpMethod
}
String getRequestContentType() {
return requestContentType
}
String getAcceptContentType() {
return acceptContentType
}
String getRequestBody() {
return requestBody
}
List<Integer> getValidResponseCodes() {
return validResponseCodes
}
}
类中的调用,即。Change.groovy 如下:
HttpRequest httpRequest = new HttpRequest.HttpRequestBuilder(fullURL,
HttpConstants.HTTP_METHODS.PUT.name(),
HttpConstants.REQUEST_CONTENT_TYPE_JSON,
HttpConstants.ACCEPT_TYPE_JSON
).requestBody(JsonOutput.toJson(authorizePayloadMap))
.validResponseCodes(validResponseCodes)
.build()
错误:
org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed:
file:/var/jenkins_home/jobs/Cores%20Services/jobs/pseudo-id/branches/CORES-1961-retrieve-print-error/builds/5/libs/jenkins-shared-utilities/src/com/Change.groovy: 127: unable to resolve class HttpRequest.HttpRequestBuilder
@ line 127, column 35.
HttpRequest httpRequest = new HttpRequest.HttpRequestBuilder(fullURL,
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:958)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:554)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:254)
at groovy.lang.GroovyClassLoader.recompile(GroovyClassLoader.java:761)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:718)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:787)
at java.lang.ClassLoader.loadClass(ClassLoader.java:405)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell$TimingLoader.loadClass(CpsGroovyShell.java:170)
at java.lang.ClassLoader.loadClass(ClassLoader.java:405)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:677)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:787)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:775)
at org.jenkinsci.plugins.workflow.cps.global.UserDefinedGlobalVariable.getValue(UserDefinedGlobalVariable.java:57)
at org.jenkinsci.plugins.workflow.cps.CpsScript.getProperty(CpsScript.java:135)
at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174)
at groovy.lang.Closure.getPropertyTryThese(Closure.java:312)
at groovy.lang.Closure.getPropertyOwnerFirst(Closure.java:306)
at groovy.lang.Closure.getProperty(Closure.java:295)
at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174)
at groovy.lang.Closure.getPropertyTryThese(Closure.java:312)
at groovy.lang.Closure.getPropertyOwnerFirst(Closure.java:306)
at groovy.lang.Closure.getProperty(Closure.java:295)
at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:456)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:39)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at openSnapChange.call(openSnapChange.groovy:140)
at com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.use(CpsDefaultGroovyMethods:407)
at openSnapChange.call(openSnapChange.groovy:59)
at withStageNotification.call(withStageNotification.groovy:55)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
at sun.reflect.GeneratedMethodAccessor333.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:400)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:96)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:312)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:276)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:136)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
我在看什么?