|
| 1 | +package com.cloudbees.jenkins.plugins.bitbucket; |
| 2 | + |
| 3 | +import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi; |
| 4 | +import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApiFactory; |
| 5 | +import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator; |
| 6 | +import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRequestException; |
| 7 | +import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketCloudEndpoint; |
| 8 | +import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration; |
| 9 | +import com.cloudbees.plugins.credentials.CredentialsProvider; |
| 10 | +import com.cloudbees.plugins.credentials.common.StandardCredentials; |
| 11 | +import hudson.Util; |
| 12 | +import hudson.model.Item; |
| 13 | +import hudson.util.FormFillFailure; |
| 14 | +import hudson.util.ListBoxModel; |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.logging.Level; |
| 17 | +import java.util.logging.Logger; |
| 18 | +import jenkins.authentication.tokens.api.AuthenticationTokens; |
| 19 | +import jenkins.model.Jenkins; |
| 20 | +import jenkins.scm.api.SCMSourceOwner; |
| 21 | +import org.apache.commons.lang.StringUtils; |
| 22 | + |
| 23 | +public class BitbucketApiUtils { |
| 24 | + |
| 25 | + private static final Logger LOGGER = Logger.getLogger(BitbucketApiUtils.class.getName()); |
| 26 | + |
| 27 | + public static ListBoxModel getFromBitbucket(SCMSourceOwner context, |
| 28 | + String serverUrl, |
| 29 | + String credentialsId, |
| 30 | + String repoOwner, |
| 31 | + String repository, |
| 32 | + BitbucketSupplier<ListBoxModel> listBoxModelSupplier) |
| 33 | + throws FormFillFailure { |
| 34 | + repoOwner = Util.fixEmptyAndTrim(repoOwner); |
| 35 | + if (repoOwner == null) { |
| 36 | + return new ListBoxModel(); |
| 37 | + } |
| 38 | + if (context == null && !Jenkins.get().hasPermission(Jenkins.ADMINISTER) || |
| 39 | + context != null && !context.hasPermission(Item.EXTENDED_READ)) { |
| 40 | + return new ListBoxModel(); // not supposed to be seeing this form |
| 41 | + } |
| 42 | + if (context != null && !context.hasPermission(CredentialsProvider.USE_ITEM)) { |
| 43 | + return new ListBoxModel(); // not permitted to try connecting with these credentials |
| 44 | + } |
| 45 | + |
| 46 | + String serverUrlFallback = BitbucketCloudEndpoint.SERVER_URL; |
| 47 | + // if at least one bitbucket server is configured use it instead of bitbucket cloud |
| 48 | + if(BitbucketEndpointConfiguration.get().getEndpointItems().size() > 0){ |
| 49 | + serverUrlFallback = BitbucketEndpointConfiguration.get().getEndpointItems().get(0).value; |
| 50 | + } |
| 51 | + |
| 52 | + serverUrl = StringUtils.defaultIfBlank(serverUrl, serverUrlFallback); |
| 53 | + StandardCredentials credentials = BitbucketCredentials.lookupCredentials( |
| 54 | + serverUrl, |
| 55 | + context, |
| 56 | + credentialsId, |
| 57 | + StandardCredentials.class |
| 58 | + ); |
| 59 | + |
| 60 | + BitbucketAuthenticator authenticator = AuthenticationTokens.convert(BitbucketAuthenticator.authenticationContext(serverUrl), credentials); |
| 61 | + |
| 62 | + try { |
| 63 | + BitbucketApi bitbucket = BitbucketApiFactory.newInstance(serverUrl, authenticator, repoOwner, null, repository); |
| 64 | + return listBoxModelSupplier.get(bitbucket); |
| 65 | + } catch (FormFillFailure | OutOfMemoryError e) { |
| 66 | + throw e; |
| 67 | + } catch (IOException e) { |
| 68 | + if (e instanceof BitbucketRequestException) { |
| 69 | + if (((BitbucketRequestException) e).getHttpCode() == 401) { |
| 70 | + throw FormFillFailure.error(credentials == null |
| 71 | + ? Messages.BitbucketSCMSource_UnauthorizedAnonymous(repoOwner) |
| 72 | + : Messages.BitbucketSCMSource_UnauthorizedOwner(repoOwner)).withSelectionCleared(); |
| 73 | + } |
| 74 | + } else if (e.getCause() instanceof BitbucketRequestException) { |
| 75 | + if (((BitbucketRequestException) e.getCause()).getHttpCode() == 401) { |
| 76 | + throw FormFillFailure.error(credentials == null |
| 77 | + ? Messages.BitbucketSCMSource_UnauthorizedAnonymous(repoOwner) |
| 78 | + : Messages.BitbucketSCMSource_UnauthorizedOwner(repoOwner)).withSelectionCleared(); |
| 79 | + } |
| 80 | + } |
| 81 | + LOGGER.log(Level.SEVERE, e.getMessage(), e); |
| 82 | + throw FormFillFailure.error(e.getMessage()); |
| 83 | + } catch (Throwable e) { |
| 84 | + LOGGER.log(Level.SEVERE, e.getMessage(), e); |
| 85 | + throw FormFillFailure.error(e.getMessage()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public interface BitbucketSupplier<T> { |
| 90 | + T get(BitbucketApi bitbucketApi) throws IOException, InterruptedException; |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments