jenkinsci/bitbucket-branch-source-plugin

Add support for BitBucket Cloud projects

stevemccann opened this issue · 4 comments

What feature do you want to see added?

The current plugin version 757.vddedc5f2589a_ as of Mar 24, 2022 does not support BitBucket Cloud projects. This has two main issues/lacks of functionality:

  • Causes the functionality of "Organization folder" to not work for BitBucket Cloud projects (it only works for an entire BitBucket Cloud workspace)
  • Can lead to a 504 timeout when attempting to load large amounts of repos in a workspace (>6000 confirmed, perhaps less), when attempting to configure a Multibranch Pipeline and a BitBucket Cloud workspace as a source.

With BitBucket Server, the project functionality works, because there's no need to specify the "workspace", since the entire BitBucket Server is for one organization. For BitBucket Cloud projects to be retrieved, you need the workspace as well as project key.

I’ve reviewed the API call being made by the Jenkins plugin, as well as some of the source code. It appears another parameter to specify the project key needs to be added to The Bitbucket Cloud API call, as well as UI interface.

A BitBucket project can be retrieved from the BitBucket Cloud REST API by adding the parameter q=project.key="<PROJECT_KEY>" to the current call. For example, to a correct REST API call to retrieve repos under the project with key "JENKINS" for a workspace called "some-workspace" would be a GET request to https://api.bitbucket.org/2.0/repositories/some-workspace?role=admin&page=1&pagelen=100&q=project.key="JENKINS"

When inspecting the API call sent by the Jenkins plugin it’s sending requests to https://api.bitbucket.org/2.0/repositories/some-workspace?role=admin&page=1&pagelen=100

There’s an additional parameter that needs to be added for BitBucket Cloud projects to work. BitBucket server only needs a project key, whereas BitBucket Cloud needs the workspace ID (specified in the url path) as well as the project key (specified as a url parameter).

This call appears to be constructed by BitbucketCloudApiClient.java by the method getRepositories. On line 755 you can see the parameters that are set to build the URL:
https://github.com/jenkinsci/bitbucket-branch-source-plugin/blob/master/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/client/BitbucketCloudApiClient.java#L755

My rough assessment is that for BitBucket Cloud projects to work, another input field needs to be added to the plugin interface to collect The Bitbucket Cloud project key. It also needs the q=project.key=“PRKEY” parameter to be added to The Bitbucket Cloud URL called.

For BitBucket Cloud repo retrieval there is two conditions to consider. One is to fetch the entire workspace’s repos (the current functionality). There’s also the additional option of specifying the project key, for further filtering of repos in the workspace. The URL parameter that filters the repos to a certain project key won’t work unless a valid project key is given.

If a new UI element is added to enter the project key, if no project key is entered the URL being built would need to be the same as it is now. However, if a project key is given, it would need to add the extra q=project.key=“PRKEY” parameter.

Here’s an example photo of plugin UI elements I’m referring to:

missing_UI_elements2

Upstream changes

No response

This may be addressed with https://issues.jenkins.io/browse/JENKINS-34071 and the plugin https://plugins.jenkins.io/bitbucket-filter-project-trait/, mentioned in #127

I'll test this out to see if it works correctly in the two contexts I've listed in this issue.

AFAICT, https://plugins.jenkins.io/bitbucket-filter-project-trait/ does not add any parameters to the HTTP request sent to Bitbucket Cloud, but rather filters the results after Bitbucket Cloud has returned them. So if the original HTTP request times out because of the missing parameter, then the plugin won't help there.

I'll add a +1 here. The BitBucket API limits state a limit of 1,000/hour for Access to repository data calls, it's not too hard to imagine scanning an entire team to take more than this.

Plus, the API call that @stevemccann seems to make this relatively straightforward (I looked around for this feature in the API, but missed it.)

This has just been released :)