community-plugins icon indicating copy to clipboard operation
community-plugins copied to clipboard

Backstage sonarqube integration - "No project with key exist"

Open somyabhatia opened this issue 1 year ago • 5 comments

📜 Description

I have been trying to integrate backstage with sonarqube . I have tried the instruction given on https://roadie.io/backstage/plugins/sonarqube/ and also tried https://github.com/backstage/community-plugins/blob/main/workspaces/sonarqube/plugins/sonarqube-backend/README.md . But nothing is working. I am getting "no project with key exists. In token/api I am sending token generated from administration tab. I have tried project token and user token (squ..., and sqp...). Can anyone please tell me exact config , suposing that my sonarqube instance is https://test.sonar.net/sonarqube.

To debug the issue, in Postman I hit API [https://test.sonar.net/sonarqube/api/components/search?qualifiers=TRK&componentKeys=test:projectKey"] . With project token it gives me correct result but with usertoken it gives me 401 unauthorized, (though I have created user token using admin account). Also, in Postman when I hit the same api via backstage host like https://<backstage_host>/api/sonarqube/components/search?qualifiers=TRK&componentKeys=test:projectKey , then it return html which is loging page of my backstage host.

👍 Expected behavior

image

👎 Actual Behavior with Screenshots

image

👟 Reproduction steps

Added config in app-config and added annotation with project key in the catalog-info

📃 Provide the context for the Bug.

No response

🖥️ Your Environment

No response

👀 Have you spent some time to check if this bug has been raised before?

  • [X] I checked and didn't find similar issue

🏢 Have you read the Code of Conduct?

Are you willing to submit PR?

None

somyabhatia avatar Oct 01 '24 05:10 somyabhatia

then it return html which is loging page of my backstage host

Can I ask if other parts of the API are returning correct responses? Are you using a proxy or the sonarqube backend. If you are using the proxy, you may need to set the following setting to the internal url of the backend api.

sonarQube:
  baseUrl: https://your.sonarqube.instance.com

punkle avatar Oct 07 '24 08:10 punkle

can you also share the annotation you have set in the entity?

Please double check that the annotation is in one of the following format:

      // Examples:
      //   instanceA/projectA  -> projectInstance = "instanceA" & projectKey = "projectA"
      //   instanceA/tenantA:projectA -> projectInstance = "instanceA" & projectKey = "tenantA:projectA"
      //   instanceA/tenantA/projectA -> projectInstance = "instanceA" & projectKey = "tenantA/projectA"

vinzscam avatar Oct 08 '24 11:10 vinzscam

@vinzscam

The annotation I am using is: sonarqube.org/project-key: "" as suggested in the document.

Can you please tell me with example what exactly should come in tenantA or projectA or what is instanceA in instanceA/projectA -> projectInstance = "instanceA" & projectKey = "projectA". I have defined instanceA in app-config as given below but it not work for me:

sonarqube: instances: - name: default baseUrl: apiKey: squ_......

somyabhatia avatar Oct 09 '24 08:10 somyabhatia

@punkle

Can I ask if other parts of the API are returning correct responses? : Can you please explain what are you referring to "other parts of API" . If you are asking for other integrations are working, then yes jfrog artifactory plugin and jira plugin are working fine. Facing issue with sonarqube only.

Also, as I mentioned earlier in postman if I hit sonarqube api "api/components/search" , it works fine with project token, but if I hit same api with backstage host, it return backstage login page html

Are you using a proxy or the sonarqube backend. I tried with both but none of them worked.

you may need to set the following setting to the internal url of the backend api. What do you mean by that. I did add this in app-config. Is this the same you are talking about else please guide me.

somyabhatia avatar Oct 09 '24 08:10 somyabhatia

@vinzscam

The annotation I am using is: sonarqube.org/project-key: "" as suggested in the document.

Can you please tell me with example what exactly should come in tenantA or projectA or what is instanceA in instanceA/projectA -> projectInstance = "instanceA" & projectKey = "projectA". I have defined instanceA in app-config as given below but it not work for me:

sonarqube: instances: - name: default baseUrl: apiKey: squ_......

I have no idea, I'm not familiar with the plugin. But looking at the documentation it suggests the following: https://github.com/backstage/community-plugins/tree/main/workspaces/sonarqube/plugins/sonarqube-backend#example---single-global-instance

so the value of sonarqube.org/project-key is supposed to be defined by two parts (instance name, project key) separated by /

vinzscam avatar Oct 11 '24 12:10 vinzscam

I've found a way to make it works.

In fact, there is a key/value missing in app-config.yaml which is instanceKey.

From the code we can see in router.test.ts

it('query default instance when instanceKey not provided', async () => {
      getBaseUrlMock.mockReturnValue({ baseUrl: DUMMY_INSTANCE_URL });
      const response = await request(app).get('/instanceUrl').send();
      expect(getBaseUrlMock).toHaveBeenCalledTimes(1);
      expect(getBaseUrlMock).toHaveBeenCalledWith({
        instanceName: undefined,
      });
      expect(response.status).toEqual(200);
      expect(response.body).toEqual({ instanceUrl: DUMMY_INSTANCE_URL });
    });

That helped me to see there is a fallback on default instance which is localhost if not provided, which is of course not the case because I use sonarcloud.io.

By updating my config like bellow it works : 

sonarqube:
  name: myName
  instanceKey: myInstance
  baseUrl: https://sonarcloud.io
  apiKey: *************************

I've made a PR to update README here : https://github.com/backstage/community-plugins/pull/2026

LeBaronDeCharlus avatar Nov 20 '24 10:11 LeBaronDeCharlus

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Jan 19 '25 12:01 github-actions[bot]

Closed by #2026

vinzscam avatar Jan 22 '25 22:01 vinzscam

Hi , i have been using community edition of sonarcloud.io , and there will be no instance name i guess @LeBaronDeCharlus , any insight regarding this ?

Surya8361 avatar Jan 30 '25 11:01 Surya8361

Hello @Surya8361 :)

You need to use the instanceKey key in your app-config file. Your instance name would probably be the ID you have in your URL : https://sonarcloud.io/summary/new_code?id=MY_INSTANCE_KEY

LeBaronDeCharlus avatar Jan 30 '25 15:01 LeBaronDeCharlus