我正在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
}
答案 0 :(得分:0)
页面上有更新的信息,其中提供了有关应如何使用的更多详细信息
在com.cloudbees.plugins.credentials.common.AbstractIdCredentialsListBoxModel类中,该方法声明为:
/**
* Adds the ids of the specified credential type that are available to the specified context as the specified
* authentication with the specified domain requirements and match the specified filter.
*
* @param authentication the authentication to search with
* @param context the context to add credentials from.
* @param type the base class of the credentials to add.
* @param domainRequirements the domain requirements.
* @param matcher the filter to apply to the credentials.
* @return {@code this} for method chaining.
* @see CredentialsProvider#listCredentials(Class, Item, Authentication, List, CredentialsMatcher)
* @since 2.1.0
*/
public AbstractIdCredentialsListBoxModel<T, C> includeMatchingAs(@NonNull Authentication authentication,
@NonNull Item context,
@NonNull Class<? extends C> type,
@NonNull
List<DomainRequirement> domainRequirements,
@NonNull CredentialsMatcher matcher) {
addMissing(CredentialsProvider.listCredentials(type, context, authentication, domainRequirements, matcher));
return this;
}
/**
* Adds the ids of the specified credential type that are available to the specified context as the specified
* authentication with the specified domain requirements and match the specified filter.
*
* @param authentication the authentication to search with
* @param context the context to add credentials from.
* @param type the base class of the credentials to add.
* @param domainRequirements the domain requirements.
* @param matcher the filter to apply to the credentials.
* @return {@code this} for method chaining.
* @see CredentialsProvider#listCredentials(Class, ItemGroup, Authentication, List, CredentialsMatcher)
* @since 2.1.0
*/
public AbstractIdCredentialsListBoxModel<T, C> includeMatchingAs(@NonNull Authentication authentication,
@NonNull ItemGroup context,
@NonNull Class<? extends C> type,
@NonNull
List<DomainRequirement> domainRequirements,
@NonNull CredentialsMatcher matcher) {
addMissing(CredentialsProvider.listCredentials(type, context, authentication, domainRequirements, matcher));
return this;
}