Skip to content

Commit

Permalink
[FIXED JENKINS-35525] Upgrade to Credentials 2.1.0+ API for populatin…
Browse files Browse the repository at this point in the history
…g credentials drop-down
  • Loading branch information
stephenc committed Jun 10, 2016
1 parent 7a31858 commit ffc9b9f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 47 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>1.25</version>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
63 changes: 38 additions & 25 deletions src/main/java/hudson/plugins/git/UserRemoteConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.Queue;
import hudson.model.TaskListener;
import hudson.model.queue.Tasks;
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import java.io.IOException;
import java.io.Serializable;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.gitclient.GitURIRequirementsBuilder;
Expand All @@ -23,11 +31,8 @@
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

import java.io.IOException;
import java.io.Serializable;
import java.util.regex.Pattern;

import static hudson.Util.*;
import static hudson.Util.fixEmpty;
import static hudson.Util.fixEmptyAndTrim;

@ExportedBean
public class UserRemoteConfig extends AbstractDescribableImpl<UserRemoteConfig> implements Serializable {
Expand Down Expand Up @@ -75,19 +80,22 @@ public String toString() {
public static class DescriptorImpl extends Descriptor<UserRemoteConfig> {

public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item project,
@QueryParameter String url) {
@QueryParameter String url,
@QueryParameter String credentialsId) {
if (project == null || !project.hasPermission(Item.CONFIGURE)) {
return new StandardListBoxModel();
return new StandardListBoxModel().includeCurrentValue(credentialsId);
}
return new StandardListBoxModel()
.withEmptySelection()
.withMatching(
GitClient.CREDENTIALS_MATCHER,
CredentialsProvider.lookupCredentials(StandardCredentials.class,
project,
ACL.SYSTEM,
GitURIRequirementsBuilder.fromUri(url).build())
);
.includeEmptyValue()
.includeMatchingAs(
project instanceof Queue.Task
? Tasks.getAuthenticationOf((Queue.Task) project)
: ACL.SYSTEM,
project,
StandardUsernameCredentials.class,
URIRequirementBuilder.fromUri(url).build(),
GitClient.CREDENTIALS_MATCHER)
.includeCurrentValue(credentialsId);
}

public FormValidation doCheckCredentialsId(@AncestorInPath Item project,
Expand All @@ -114,17 +122,22 @@ public FormValidation doCheckCredentialsId(@AncestorInPath Item project,
{
return FormValidation.ok();
}

StandardCredentials credentials = lookupCredentials(project, value, url);

if (credentials == null) {
// no credentials available, can't check
return FormValidation.warning("Cannot find any credentials with id " + value);
for (ListBoxModel.Option o : CredentialsProvider
.listCredentials(StandardUsernameCredentials.class, project, project instanceof Queue.Task
? Tasks.getAuthenticationOf((Queue.Task) project)
: ACL.SYSTEM,
URIRequirementBuilder.fromUri(url).build(),
GitClient.CREDENTIALS_MATCHER)) {
if (StringUtils.equals(value, o.value)) {
// TODO check if this type of credential is acceptable to the Git client or does it merit warning
// NOTE: we would need to actually lookup the credential to do the check, which may require
// fetching the actual credential instance from a remote credentials store. Perhaps this is
// not required
return FormValidation.ok();
}
}

// TODO check if this type of credential is acceptible to the Git client or does it merit warning the user

return FormValidation.ok();
// no credentials available, can't check
return FormValidation.warning("Cannot find any credentials with id " + value);
}

public FormValidation doCheckUrl(@AncestorInPath Item project,
Expand Down
84 changes: 63 additions & 21 deletions src/main/java/jenkins/plugins/git/GitSCMSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,29 @@
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import hudson.Extension;
import hudson.Util;
import hudson.model.Item;
import hudson.model.ParameterValue;
import hudson.model.Queue;
import hudson.model.queue.Tasks;
import hudson.plugins.git.GitStatus;
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.SCMSourceDescriptor;
import jenkins.scm.api.SCMSourceOwner;
import jenkins.scm.api.SCMSourceOwners;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.URIish;
import org.jenkinsci.plugins.gitclient.GitClient;
Expand All @@ -49,13 +60,6 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

/**
* @author Stephen Connolly
*/
Expand Down Expand Up @@ -123,21 +127,59 @@ public String getDisplayName() {
}

public ListBoxModel doFillCredentialsIdItems(@AncestorInPath SCMSourceOwner context,
@QueryParameter String remote) {
@QueryParameter String remote,
@QueryParameter String credentialsId) {
if (context == null || !context.hasPermission(Item.CONFIGURE)) {
return new ListBoxModel();
return new StandardListBoxModel().includeCurrentValue(credentialsId);
}
StandardListBoxModel result = new StandardListBoxModel();
result.withEmptySelection();
result.withMatching(GitClient.CREDENTIALS_MATCHER,
CredentialsProvider.lookupCredentials(
StandardUsernameCredentials.class,
return new StandardListBoxModel()
.includeEmptyValue()
.includeMatchingAs(
context instanceof Queue.Task ? Tasks.getAuthenticationOf((Queue.Task)context) : ACL.SYSTEM,
context,
ACL.SYSTEM,
URIRequirementBuilder.fromUri(remote).build()
)
);
return result;
StandardUsernameCredentials.class,
URIRequirementBuilder.fromUri(remote).build(),
GitClient.CREDENTIALS_MATCHER)
.includeCurrentValue(credentialsId);
}

public FormValidation doCheckCredentialsId(@AncestorInPath SCMSourceOwner context,
@QueryParameter String url,
@QueryParameter String value) {
if (context == null || !context.hasPermission(Item.CONFIGURE)) {
return FormValidation.ok();
}

value = Util.fixEmptyAndTrim(value);
if (value == null) {
return FormValidation.ok();
}

url = Util.fixEmptyAndTrim(url);
if (url == null)
// not set, can't check
{
return FormValidation.ok();
}

for (ListBoxModel.Option o : CredentialsProvider.listCredentials(
StandardUsernameCredentials.class,
context,
context instanceof Queue.Task
? Tasks.getAuthenticationOf((Queue.Task) context)
: ACL.SYSTEM,
URIRequirementBuilder.fromUri(url).build(),
GitClient.CREDENTIALS_MATCHER)) {
if (StringUtils.equals(value, o.value)) {
// TODO check if this type of credential is acceptable to the Git client or does it merit warning
// NOTE: we would need to actually lookup the credential to do the check, which may require
// fetching the actual credential instance from a remote credentials store. Perhaps this is
// not required
return FormValidation.ok();
}
}
// no credentials available, can't check
return FormValidation.warning("Cannot find any credentials with id " + value);
}


Expand Down

0 comments on commit ffc9b9f

Please sign in to comment.