SonarQube icon

SonarQube

Interact with SonarQube or SonarCloud API

Actions3

Overview

This node interacts with the SonarQube or SonarCloud API to retrieve code quality metrics and component measures. Specifically, for the Measures > Component operation, it fetches various metric values related to a specific component (e.g., a project or module) in SonarQube. This is useful for monitoring code quality indicators such as bugs, code smells, complexity, coverage, duplications, reliability, security ratings, and more.

Common scenarios include:

  • Automatically retrieving up-to-date code quality metrics during CI/CD pipelines.
  • Integrating SonarQube data into dashboards or reporting tools.
  • Tracking quality gate status or technical debt over time for specific components.
  • Analyzing pull request impact on code quality by specifying PR IDs.

Example: Fetching the number of bugs, code smells, and test coverage percentage for a given project branch to decide if the build should pass quality gates.

Properties

Name Meaning
Component The unique key identifying the component (project/module) in SonarQube for which to retrieve measures. You can find available keys via the SonarQube portal or by using the 'Search Components' operation.
Metrics List of metric keys to retrieve for the component. Defaults include common metrics like bugs, code_smells, complexity, coverage, duplicated_lines_density, ncloc (lines of code), reliability_rating, etc.
Branch Optional branch name within the component to get measures for a specific branch.
Pull Request ID Optional pull request identifier to get measures related to a specific PR.
Additional Fields Optional additional fields to include in the response. Options are:
- periods: Include historical periods data.
- metrics: Include detailed metric definitions alongside measures.

Output

The node outputs a JSON object containing the measures retrieved from the SonarQube API endpoint /api/measures/component. The structure typically includes:

  • component: Information about the component queried.
  • measures: An array of measure objects, each containing:
    • metric: The metric key (e.g., "bugs").
    • value: The current value of the metric.
    • Possibly other metadata depending on additional fields requested.

If binary data were involved (not applicable here), it would be summarized accordingly, but this node only returns JSON data representing metrics.

Dependencies

  • Requires an active connection to a SonarQube or SonarCloud instance.
  • Needs an API authentication token configured in n8n credentials to authorize requests.
  • Uses the SonarQube REST API endpoint /api/measures/component.
  • Optionally depends on other operations like 'Search Components' or 'Search Metrics' to discover valid component keys or metric keys.

Troubleshooting

  • Invalid Component Key: If the specified component key does not exist or the user lacks permission, the API will return an error. Verify the component key via the SonarQube portal or use the 'Search Components' operation.
  • Metric Keys Not Found: Specifying invalid or unsupported metric keys may result in empty or partial results. Use the 'Search Metrics' operation to confirm available metrics.
  • Branch or Pull Request Not Found: If the branch or PR ID does not exist for the component, no measures will be returned. Double-check these parameters.
  • Authentication Errors: Ensure the API token credential is correctly set up and has sufficient permissions.
  • API Rate Limits or Connectivity Issues: Network problems or rate limiting by SonarQube may cause failures; check connectivity and retry policies.

Error messages thrown by the node will include context about the failing item index for easier debugging.

Links and References

Discussion