Skip to content

SonarQube issues plugin #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Nov 20, 2024
Merged

SonarQube issues plugin #29

merged 27 commits into from
Nov 20, 2024

Conversation

martindstone
Copy link
Contributor

Background

This is a simple plugin to show SonarQube issues in Cortex

This PR

This PR adds the SonarQube issues plugin

Checklists

Security

  • Security impact of change has been considered
  • Code follows company security practices and guidelines

@jreock jreock self-requested a review November 5, 2024 20:46
@martindstone martindstone changed the title SonarQube plugin SonarQube issues plugin Nov 8, 2024
`${context.apiBaseUrl}/catalog/sonarqube-plugin-config/openapi`
);
const data = await response.json();
newBaseUrl = data.info["x-cortex-definition"]["sonarqube-url"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is slightly bugged, we should be pulling newBaseUrl as an optional value before attempting the fetch. I am running SonarQube locally and exposing a URL with ngrok, and I noticed that when I use a custom URL (check out my config in the justinreock-sandbox tenant), the service never actually gets hit. That's because if the endpoint isn't sonarcloud.io and since you are using await, the exception will trigger on the event loop before newBaseUrl has a chance to populate. I tested by adding some console logging:

newBaseUrl = data.info["x-cortex-definition"]["sonarqube-url"]; console.log("Switching to updated URL" + newBaseUrl); } catch (e) {} setBaseUrl(newBaseUrl); console.error("Trying new base URL" + newBaseUrl);

And you can see in the console that the variable is undefined:

image

Despite the plugin configured as:

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the key in your entity is named wrong; it should be sonarqube-url not sonarqube-api-url so in this case your service should not get hit and the plugin should try to use https://sonarcloud.io.

That said, we should not be trying to set baseUrl to undefined ever... I'll fix that, let me know if there is another bug here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am thinking to do like this

  useEffect(() => {
    if (!context?.apiBaseUrl) {
      return;
    }
    const fetchPluginConfig = async (): Promise<void> => {
      console.log("Fetching plugin config");
      let newBaseUrl = "https://sonarcloud.io";
      try {
        const response = await fetch(
          `${context.apiBaseUrl}/catalog/sonarqube-plugin-config/openapi`
        );
        const data = await response.json();
        const baseUrlFromEntity = data.info["x-cortex-definition"]["sonarqube-url"];
        console.log("baseUrlFromEntity is ", baseUrlFromEntity);
        if (baseUrlFromEntity) {
          newBaseUrl = baseUrlFromEntity;
        }
      } catch (e) {
        console.log("Failed to fetch plugin config", e);
      }
      console.log("Setting base url to ", newBaseUrl);
      setBaseUrl(newBaseUrl);
    };
    void fetchPluginConfig();
  }, [context?.apiBaseUrl]);

so that if the plugin-config can't be fetched, I get
image

if the plugin-config exists but doesn't have the key (your situation) I get
image

and if the plugin-config exists and has the key, I get
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Testing this now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, and the reason I was missing a key is because the docs say to use sonarqube-api-url, but the code is looking for sonarqube-url:

image

Will comment in the readme!

x-cortex-tag: sonarqube-plugin-config
x-cortex-type: plugin-configuration
x-cortex-definition:
sonarqube-api-url: https://sonarqube.martindstone.com
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs ask for the sonarqube-api-url key, but the code is checking for sonarqube-key:

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more tweak and then I'm good to sign off! At the beginning, the README specifies:

If the cortex.yaml has a SonarQube Project defined in its x-cortex-static-analysis configuration, it will query for issues pertaining to that project. For example:

And the example shows:

openapi: 3.0.1 info: title: Funrepo description: it is a fun repo x-cortex-git: github: alias: cortex repository: martindstone-org/funrepo x-cortex-tag: funrepo x-cortex-type: service x-cortex-static-analysis: sonarqube: **project: martindstone-org_funrepo**

I think we need to call out specifically that the value in x-cortex-static-analysis.sonarqube.project needs to specifically match the project key in SonarQube and not the project name. That tripped me up once I got the authentication working, I started using the project name and it just wasn't retrieving issues, switched to project key and it works like a charm!!

image image

Copy link
Contributor

@jreock jreock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the quick turnaround on all the changes @martindstone !!

@jreock jreock merged commit bce1fb3 into master Nov 20, 2024
2 checks passed
@jreock jreock deleted the sonarqube-issues branch November 20, 2024 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants