diff options
Diffstat (limited to 'src/main/java')
2 files changed, 45 insertions, 2 deletions
diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java index 247bd67..664c6d8 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,6 +31,7 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { private static final Logger logger = Logger.getLogger(BitbucketBuildTrigger.class.getName()); private final String projectPath; private final String cron; + private final String credentialsId; private final String username; private final String password; private final String repositoryOwner; @@ -43,6 +51,7 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { public BitbucketBuildTrigger( String projectPath, String cron, + String credentialsId, String username, String password, String repositoryOwner, @@ -56,6 +65,7 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { super(cron); this.projectPath = projectPath; this.cron = cron; + this.credentialsId = credentialsId; this.username = username; this.password = password; this.repositoryOwner = repositoryOwner; @@ -75,6 +85,10 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { return this.cron; } + public String getCredentialsId() { + return credentialsId; + } + public String getUsername() { return username; } @@ -194,5 +208,12 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { 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 dd0f5e5..309ceec 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java @@ -9,8 +9,15 @@ import java.util.logging.Logger; import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.ApiClient; import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.BuildState; 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 jenkins.model.Jenkins; +import static com.cloudbees.plugins.credentials.CredentialsMatchers.instanceOf; + /** * Created by nishio */ @@ -31,9 +38,16 @@ 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( - trigger.getUsername(), - trigger.getPassword(), + username, + password, trigger.getRepositoryOwner(), trigger.getRepositoryName(), trigger.getCiKey(), @@ -153,4 +167,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))); + } } |