How do I script linking a SonarCloud project to a Bitbucket Cloud repo

I already programmatically create new sonar projects with the REST API: https://sonarcloud.io/api/projects/create

Im setting all these values in the scanner command. everything works except it looks like I still have to link it to the BB repo to get the widget?

// sonar project key
/k:“mykey”

// project name- human readable project name that appears in the UI
/n:“myproject”

// scan version
/v:“myversion”

// sonarcloud token
/d:sonar.login=“mytoken”

// sonarcloud org
/o:“bitbucketTeam”

// coverage report
/d:sonar.cs.opencover.reportsPaths=opencover.xml"

// Bitbucket branch and PR
/d:sonar.pullrequest.branch=“mybranch”
/d:sonar.pullrequest.key=“myPRkey”

// PR decorator. So sonarqube can post build status to the PR in bitbucket
/d:sonar.pullrequest.provider=bitbucketcloud
/d:sonar.pullrequest.bitbucketcloud.repository=repo-UUID
/d:sonar.pullrequest.bitbucketcloud.owner=UUID

When i look at SonarCloud setting it says “To display the quality of your repository, you have to link it with a project analyzed on SonarCloud.”

I dont want to manually do that for every one of my repos especially because ive automated everything up to this point

Hi @red_888,

Welcome to our community!

For now it is not possible to script the link between a BitBucketCloud repo and a SonarCloud project.
We would like to provide the ability to onboard on SonarCloud via an API, but it will not be available in the short term.

Cheers,

Hi. Has there been any development on this front? Is there now a bitbucket/sonarcloud API endpoint for linking bitbucket projects to their corresponding sonarcloud projects?

Thanks.

Hey @Ivan_Kabaivanov

When I create a new project in SonarCloud using the project creation wizard, my project is automatically bound on the Bitbucket side.

Is it working differently for you when a new project is created, bound to a repository on Bitbucket?

For existing projects, we have no public API for doing the link.

Hi, @Colin and thanks for the super quick response.

Does that mean that if I use the SonarCloud API to create a project in SonarCloud, the SonarCloud app/plugin in BitBucket will auto-detect and bind to it?

Also, is there a way to programmatically click that button “Show repository overview widget” in BitBucket?

Thanks!

Unfortunately, there aren’t any Public APIs to accomplish either. Provisioning projects through the UI will bind the project (but not click the widget button).

If you’re looking for something “unofficial” you can probably open your browser’s dev tools and see what API calls are being made when doing either action (provisioning the project, binding an existing project, checking the box). But use that knowledge at their own risk because internal APIs are… just that, and the contract might change at any time.

@Colin

Thanks!

This is possible via the API URL ‘/api/alm_integration/provision_projects’. Example in PowerShell below for a project with GitLab integration:

Invoke-RestMethod -Uri "https://sonarcloud.io/api/alm_integration/provision_projects" `
-Method "POST" `
-Headers @{
  "Accept"="application/json"
  "Authorization"= "Basic tokeninBase64"
} `
-ContentType "application/x-www-form-urlencoded" `
-Body "installationKeys=GitLabProjectID&organization=SonarCloudOrganization"

I’ve also made it available in my PowerShell module for SonarCloud (PSSonarCloud/New-SonarCloudProjectALMIntegrated.ps1 at main · jonathanmedd/PSSonarCloud · GitHub), so you should be able to translate that easily to other languages.

Obviously take the consideration mentioned above that it is not supported.

Would be good to see it added to the supported API though since we require this functionality to automate the onboarding of projects. Doing that manually through the UI with the number of projects we have to onboard is just too painful and time consuming.