From 8101c2fc7b6ca2b2fa1d835f2c5b2a5f159539a0 Mon Sep 17 00:00:00 2001 From: Joel Baranick Date: Wed, 21 Oct 2015 14:21:34 -0700 Subject: Support credentials by allowing UsernamePasswordCredentials to be picked when configuring BitbucketBuildTrigger and change BitbucketRepository to use the credentials when setting up the ApiClient. --- .../BitbucketBuildTrigger.java | 31 +++++++++++++--------- .../BitbucketRepository.java | 19 +++++++++++-- .../BitbucketBuildTrigger/config.jelly | 9 +++---- 3 files changed, 39 insertions(+), 20 deletions(-) (limited to 'src/main') diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java index 879facb..974d3eb 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java @@ -1,12 +1,17 @@ package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder; import antlr.ANTLRException; +import com.cloudbees.plugins.credentials.CredentialsProvider; +import com.cloudbees.plugins.credentials.common.StandardListBoxModel; +import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; +import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials; import hudson.Extension; import hudson.model.*; import hudson.model.queue.QueueTaskFuture; import hudson.plugins.git.RevisionParameterAction; import hudson.triggers.Trigger; import hudson.triggers.TriggerDescriptor; +import hudson.util.ListBoxModel; import net.sf.json.JSONObject; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.StaplerRequest; @@ -17,6 +22,8 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; +import static com.cloudbees.plugins.credentials.CredentialsMatchers.instanceOf; + /** * Created by nishio */ @@ -24,8 +31,7 @@ public class BitbucketBuildTrigger extends Trigger> { private static final Logger logger = Logger.getLogger(BitbucketBuildTrigger.class.getName()); private final String projectPath; private final String cron; - private final String username; - private final String password; + private final String credentialsId; private final String repositoryOwner; private final String repositoryName; private final String ciSkipPhrases; @@ -41,8 +47,7 @@ public class BitbucketBuildTrigger extends Trigger> { public BitbucketBuildTrigger( String projectPath, String cron, - String username, - String password, + String credentialsId, String repositoryOwner, String repositoryName, String ciSkipPhrases, @@ -52,8 +57,7 @@ public class BitbucketBuildTrigger extends Trigger> { super(cron); this.projectPath = projectPath; this.cron = cron; - this.username = username; - this.password = password; + this.credentialsId = credentialsId; this.repositoryOwner = repositoryOwner; this.repositoryName = repositoryName; this.ciSkipPhrases = ciSkipPhrases; @@ -69,12 +73,8 @@ public class BitbucketBuildTrigger extends Trigger> { return this.cron; } - public String getUsername() { - return username; - } - - public String getPassword() { - return password; + public String getCredentialsId() { + return credentialsId; } public String getRepositoryOwner() { @@ -180,5 +180,12 @@ public class BitbucketBuildTrigger extends Trigger> { save(); return super.configure(req, json); } + + public ListBoxModel doFillCredentialsIdItems() { + return new StandardListBoxModel() + .withEmptySelection() + .withMatching(instanceOf(UsernamePasswordCredentials.class), + CredentialsProvider.lookupCredentials(StandardUsernamePasswordCredentials.class)); + } } } diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java index bdf5d7c..8606c45 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java @@ -10,6 +10,12 @@ import java.util.regex.Pattern; import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.ApiClient; import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.Pullrequest; +import com.cloudbees.plugins.credentials.CredentialsMatchers; +import com.cloudbees.plugins.credentials.CredentialsProvider; +import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; +import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials; + +import static com.cloudbees.plugins.credentials.CredentialsMatchers.instanceOf; /** * Created by nishio @@ -39,9 +45,10 @@ public class BitbucketRepository { public void init() { trigger = this.builder.getTrigger(); + StandardUsernamePasswordCredentials credentials = getCredentials(trigger.getCredentialsId()); client = new ApiClient( - trigger.getUsername(), - trigger.getPassword(), + credentials.getUsername(), + credentials.getPassword().getPlainText(), trigger.getRepositoryOwner(), trigger.getRepositoryName()); } @@ -192,4 +199,12 @@ public class BitbucketRepository { } return false; } + + private StandardUsernamePasswordCredentials getCredentials(String credentialsId) { + return CredentialsMatchers + .firstOrNull( + CredentialsProvider.lookupCredentials(StandardUsernamePasswordCredentials.class), + CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialsId), + instanceOf(UsernamePasswordCredentials.class))); + } } diff --git a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly index 0eee781..df43e5a 100644 --- a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly +++ b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly @@ -1,12 +1,9 @@ - + - - - - - + + -- cgit v1.2.3 From f2294ad4c37592d4046fd02e35439340278b09a0 Mon Sep 17 00:00:00 2001 From: Joel Baranick Date: Wed, 28 Oct 2015 12:54:10 -0700 Subject: Add support for plaintext username/password unless credentials are specified. --- .gitignore | 4 ++++ .../bitbucketpullrequestbuilder/BitbucketBuildTrigger.java | 14 ++++++++++++++ .../bitbucketpullrequestbuilder/BitbucketRepository.java | 11 +++++++++-- .../BitbucketBuildTrigger/config.jelly | 6 ++++++ 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 .gitignore (limited to 'src/main') diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c7b603 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +target +*.iml +.idea +dependency-reduced-pom.xml diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java index 974d3eb..09802af 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java @@ -32,6 +32,8 @@ public class BitbucketBuildTrigger extends Trigger> { private final String projectPath; private final String cron; private final String credentialsId; + private final String username; + private final String password; private final String repositoryOwner; private final String repositoryName; private final String ciSkipPhrases; @@ -48,6 +50,8 @@ public class BitbucketBuildTrigger extends Trigger> { String projectPath, String cron, String credentialsId, + String username, + String password, String repositoryOwner, String repositoryName, String ciSkipPhrases, @@ -58,6 +62,8 @@ public class BitbucketBuildTrigger extends Trigger> { this.projectPath = projectPath; this.cron = cron; this.credentialsId = credentialsId; + this.username = username; + this.password = password; this.repositoryOwner = repositoryOwner; this.repositoryName = repositoryName; this.ciSkipPhrases = ciSkipPhrases; @@ -77,6 +83,14 @@ public class BitbucketBuildTrigger extends Trigger> { return credentialsId; } + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + public String getRepositoryOwner() { return repositoryOwner; } diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java index 8606c45..fb43f12 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java @@ -45,10 +45,17 @@ public class BitbucketRepository { public void init() { trigger = this.builder.getTrigger(); + String username = trigger.getUsername(); + String password = trigger.getPassword(); StandardUsernamePasswordCredentials credentials = getCredentials(trigger.getCredentialsId()); + if (credentials != null) { + username = credentials.getUsername(); + password = credentials.getPassword().getPlainText(); + } + client = new ApiClient( - credentials.getUsername(), - credentials.getPassword().getPlainText(), + username, + password, trigger.getRepositoryOwner(), trigger.getRepositoryName()); } diff --git a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly index df43e5a..db5ca0f 100644 --- a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly +++ b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly @@ -5,6 +5,12 @@ + + + + + + -- cgit v1.2.3