Skip to Content
sonarqube

βš™οΈ Configuration

How host, token, project and branch are resolved from file, environment and options.

resolveConfig() builds a SonarQubeConfig (host, token, project, branch) in layers, each overriding the last:

  1. the config file
  2. the environment
  3. explicit options

Undefined and empty values never overwrite a lower layer.

import { resolveConfig } from "@kud/sonarqube"

const config = resolveConfig({ branch: "main" })

Settings

SettingEnvironment (first set wins)File key
hostSONARQUBE_HOST, SONAR_HOST_URLhost
tokenSONARQUBE_TOKEN, SONAR_TOKENtoken
projectSONARQUBE_PROJECTproject
branchSONARQUBE_BRANCHbranch

The older SONARQUBE_DASH_* names are still read as fallbacks: for host, token, project and branch alike, the names above win over them.

There is no default host. Constructing a client without one throws MissingHostError.

Config file

The file is $XDG_CONFIG_HOME/sonarqube/config.json, defaulting to ~/.config/sonarqube/config.json. The path is resolved on every call, so changes to XDG_CONFIG_HOME are honoured. The legacy ~/.config/sonarqube-dash-cli/config.json is still read when the new file does not exist, and writeConfig() carries its values across on the first write to the new location.

{
  "host": "https://sonarqube.example.com",
  "project": "acme-api"
}
FunctionPurpose
configFilePath(env?)The config file location
readConfigFile(file)Parsed file, or {} when missing or invalid
writeConfig(data, file?)Merges data over the existing file, creates the directory, returns { path, data }

Project-key fallback

With no project from any layer, resolveConfig reads sonar.projectKey from sonar-project.properties. readSonarProjectKey(dir?) does that lookup on its own and skips blank and # or ! comment lines.

Resolve options

resolveConfig accepts the four settings plus three testing hooks:

OptionDefault
configPathThe XDG location
cwdprocess.cwd(), where sonar-project.properties is looked up
envprocess.env