Automating SonarCloud with PowerShell – Part 2 Onboarding a Project

Warning: this post contains unsupported API calls, so use at your own risk

One of the main reasons I started looking at the SonarCloud api was to be able to automate the onboarding of new GitLab projects into SonarCloud. We didn’t want to have to do that manually via the UI, rather via a pipeline job. In part 1 of this series I demonstrated the basics of the API and the PSSonarCloud PowerShell module, in part 2 we’ll look at how to onboard a project into SonarCloud.

False Start

Initially this looked like it would be a very straightforward job, the SonarCloud Web Api has documentation for creating a project:

So as part of the PSSonarCloud module I added a function ```New-SonarCloudProject . You could use it as the following to create a project, all you need is a SonarCloud API key with permission to create a project, then the SonarCloud organisation, GitLab project name and project id:

The project will appear in SonarCloud and all looks good:

However, an issue will arise when you attempt to run some analysis against the project, it will fail and complain that the SonarCloud project is not integrated with your repo.

I discovered via a community forum post and an observation in the UI that the API call above does not include the integration with the code repo - compare that project with a project which was onboarded via the UI and it displays integration with GitLab:

A New Hope

The forum post suggested an unsupported option would be to examine the API call through browser development tools and use that:

So off to Google Chrome development tools we go……

By looking at the API call used when adding a project via the UI, we can see that a URL of /api/alm_integration/provision_projects is used

The payload contained two items:

  • installationKeys - the GitLab project id
  • organization - the SonarCloud organization

Consequently the New-SonarCloudProjectALMIntegrated function was born. So we can now fully onboard a project with the following:

The resultant project has been fully onboarded: