Class BitbucketApi

  • Direct Known Subclasses:
    BitbucketCloudApi, BitbucketServerApi

    @Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class)
    public abstract class BitbucketApi
    extends Object
    Bitbucket APIs needed to perform BlueOcean pipeline creation flow.
    Author:
    Vivek Pandey
    • Field Detail

      • apiUrl

        protected final String apiUrl
      • userName

        protected final String userName
    • Constructor Detail

      • BitbucketApi

        protected BitbucketApi​(@NonNull
                               String apiUrl,
                               @NonNull
                               com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials credentials)
    • Method Detail

      • getUser

        @NonNull
        public BbUser getUser()
        Returns:
        BbUser
      • getUser

        @NonNull
        public abstract BbUser getUser​(@NonNull
                                       String userSlug)
        Gives user for given userName.
        Parameters:
        userSlug - name of user, BbUser.getSlug()
        Returns:
        BbUser
      • getOrgs

        @NonNull
        public abstract BbPage<BbOrg> getOrgs​(int pageNumber,
                                              int pageSize)
        Gives collection of Bitbucket organizations (Project/Team).
        Parameters:
        pageNumber - page number
        pageSize - number of items in a page
        Returns:
        Collection of BbOrgs
      • getOrg

        @NonNull
        public abstract BbOrg getOrg​(@NonNull
                                     String orgName)
        Gives BbOrg for given project/team name.
        Parameters:
        orgName - Bitbucket project/team key BbOrg.getKey()
        Returns:
        BbOrg instance
      • getRepos

        @NonNull
        public abstract BbPage<BbRepo> getRepos​(@NonNull
                                                String orgId,
                                                int pageNumber,
                                                int pageSize)
        Gives collection of BbRepos.
        Parameters:
        orgId - Bitbucket project/team key BbOrg.getKey()
        pageNumber - page number
        pageSize - page size
        Returns:
      • getContent

        @NonNull
        public abstract String getContent​(@NonNull
                                          String orgId,
                                          @NonNull
                                          String repoSlug,
                                          @NonNull
                                          String path,
                                          @NonNull
                                          String commitId)
        Gives content of files in Bitbucket. If given path is not available then ServiceException.NotFoundException is thrown.
        Parameters:
        orgId - Bitbucket project/team key BbOrg.getKey()
        repoSlug - Bitbucket repo slig BbRepo.getSlug()
        path - path to file in bitbucket repo, e.g. "Jenkinsfile"
        commitId - commitId of branch, path will be served off it.
        Returns:
        content
      • saveContent

        @NonNull
        public abstract BbSaveContentResponse saveContent​(@NonNull
                                                          String orgId,
                                                          @NonNull
                                                          String repoSlug,
                                                          @NonNull
                                                          String path,
                                                          @NonNull
                                                          String content,
                                                          @NonNull
                                                          String commitMessage,
                                                          @Nullable
                                                          String branch,
                                                          @Nullable
                                                          String sourceBranch,
                                                          @Nullable
                                                          String commitId)
        Saves file to Bitbucket.
        Parameters:
        orgId - Bitbucket project/team key BbOrg.getKey()
        repoSlug - Repo slug BbRepo.getSlug()
        path - destination path, e.g. "Jenkinsfile"
        content - file content to save
        commitMessage - commit message
        branch - branch name. If null then implementation should save on default branch
        commitId - if not provided, then file should be saved on tip of branch.
        Returns:
        BbSaveContentResponse on successful save.
        Throws:
        io.jenkins.blueocean.commons.ServiceException.ConflictException - in case of conflict during save
      • fileExists

        public abstract boolean fileExists​(@NonNull
                                           String orgId,
                                           @NonNull
                                           String repoSlug,
                                           @NonNull
                                           String path,
                                           @NonNull
                                           String branch)
        Checks if a file exists in Bitbucket repo.
        Parameters:
        orgId - Bitbucket project/team key BbOrg.getKey()
        repoSlug - repo slug BbRepo.getSlug()
        path - path of file, e.g. "Jenkinsfile"
        branch - Bitbucket branch BbBranch.getDisplayId()
        Returns:
        true if file exists
      • getDefaultBranch

        @CheckForNull
        public abstract BbBranch getDefaultBranch​(@NonNull
                                                  String orgId,
                                                  @NonNull
                                                  String repoSlug)
        Get default branch of a repo.
        Parameters:
        orgId - Bitbucket project/team key BbOrg.getKey()
        repoSlug - Repo slug BbRepo.getSlug()
        Returns:
        Default branch. null if it's empty repo or if the scm doesn't support default branch concept.
      • isEmptyRepo

        public abstract boolean isEmptyRepo​(@NonNull
                                            String orgId,
                                            @NonNull
                                            String repoSlug)
        Checks if its empty/un-initialized
        Parameters:
        orgId - Bitbucket project/team key BbOrg.getKey()
        repoSlug - Repo slug BbRepo.getSlug()
        Returns:
        true if this is empty or un-initialized repo
      • handleException

        protected io.jenkins.blueocean.commons.ServiceException handleException​(Exception e)
        Converts thrown exception during BB HTTP call in to JSON serializable ServiceException
        Parameters:
        e - exception
        Returns:
        ServiceException instance
      • ensureTrailingSlash

        protected static String ensureTrailingSlash​(String url)