【发布时间】:2020-03-05 00:23:19
【问题描述】:
我正在关注这里的文档https://github.com/jenkinsci/credentials-plugin/blob/master/docs/consumer.adoc
具体是这个例子:
public ListBoxModel doFillCredentialsIdItems(
@AncestorInPath Item item,
@QueryParameter String credentialsId,
... (1)
) {
StandardListBoxModel result = new StandardListBoxModel();
if (item == null) {
if (!Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER)) {
return result.includeCurrentValue(credentialsId); (2)
}
} else {
if (!item.hasPermission(Item.EXTENDED_READ)
&& !item.hasPermission(CredentialsProvider.USE_ITEM)) {
return result.includeCurrentValue(credentialsId); (2)
}
}
return result
.includeEmptySelection() (3)
.includeMatchingAs(...) (4)
.includeCurrentValue(credentialsId); (5)
}
包括您需要的任何其他上下文参数以优化凭据列表。例如,如果凭据将用于连接到远程服务器,您可以将服务器 URL 表单元素包含为 @QueryParameter,以便可以从该 URL 构建域要求。
我们防止完全填充无法实际进行选择的用户的下拉列表。这对于防止向外部凭据存储发出不需要的请求也很有用。
如果用户选择无凭据有效,则包括空选择。
我们需要包含匹配的凭据。在某些情况下,您可能有不相交的凭据联合,在这种情况下,您可以多次调用此方法,为任何给定 ID 添加的第一个凭据获胜。
如果包含当前值,则在删除支持凭据的情况下,表单配置将保持不变。另一种方法是让表单“神奇地”选择一个新的凭证,但通常这将是错误的凭证。建议只添加“不存在”的凭据并让表单验证报告错误
但是,我在示例中的第 4 步卡住了,我应该在这里做什么?我尝试查看其他插件以了解它们是如何实现的,但我很快就迷路了。是否有一个简单的演示来展示它是如何工作的?
这是我的代码,它基于 Jenkins “Hello World Example”。
package io.jenkins.plugins.sample;
import com.cloudbees.plugins.credentials.common.*;
import hudson.Launcher;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.*;
import hudson.util.FormValidation;
import hudson.tasks.Builder;
import hudson.tasks.BuildStepDescriptor;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import org.apache.commons.httpclient.auth.CredentialsProvider;
import org.kohsuke.stapler.*;
import javax.servlet.ServletException;
import java.io.IOException;
import jenkins.tasks.SimpleBuildStep;
import org.jenkinsci.Symbol;
public class HelloWorldBuilder extends Builder implements SimpleBuildStep {
private final String name;
private final String credentials;
private boolean useFrench;
private final String tppurl;
@DataBoundConstructor
public HelloWorldBuilder(String name, String tppurl, String credentials) {
this.name = name;
this.tppurl = tppurl;
this.credentials = credentials;
}
@DataBoundSetter
public void setUseFrench(boolean useFrench) {
this.useFrench = useFrench;
}
public String getName() {
return name;
}
public String getTppurl() {
return tppurl;
}
public String getCredentials() {
return credentials;
}
public boolean isUseFrench() {
return useFrench;
}
@Override
public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException {
if (useFrench) {
listener.getLogger().println("Bonjour, " + name + "!");
} else {
listener.getLogger().println("Hello, " + name + tppurl + "! " );
}
}
@Symbol("greet")
@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
public FormValidation doCheckName(@QueryParameter String value, @QueryParameter boolean useFrench)
throws IOException, ServletException {
if (value.length() == 0)
return FormValidation.error(Messages.HelloWorldBuilder_DescriptorImpl_errors_missingName());
if (value.length() < 4)
return FormValidation.warning(Messages.HelloWorldBuilder_DescriptorImpl_warnings_tooShort());
if (!useFrench && value.matches(".*[éáàç].*")) {
return FormValidation.warning(Messages.HelloWorldBuilder_DescriptorImpl_warnings_reallyFrench());
}
return FormValidation.ok();
}
@Override
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
return true;
}
@Override
public String getDisplayName() {
return Messages.HelloWorldBuilder_DescriptorImpl_DisplayName();
}
}
//This is the credentials bit
public ListBoxModel doFillCredentialsIdItems(
@AncestorInPath Item item,
@QueryParameter String credentialsId,
) {
StandardListBoxModel result = new StandardListBoxModel();
if (item == null) {
if (!Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER)) {
return result.includeCurrentValue(credentialsId);
}
} else {
if (!item.hasPermission(Item.EXTENDED_READ)
&& !item.hasPermission(CredentialsProvider.USE_ITEM)) {
return result.includeCurrentValue(credentialsId);
}
}
return result
.includeEmptySelection()
.includeMatchingAs(STUCK HERE!!!!!)
.includeCurrentValue(credentialsId);
}
// end credentials
}
【问题讨论】: